📄 cartest01.java
字号:
//package
/**
*车辆agent模型
*2006.03.31-04.17
*/
//import java.awt.*;
//import java.applet.*;
import java.awt.Graphics;
import java.util.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.discovery.DiscoveryService;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.exception.PeerGroupException;
import net.jxta.socket.JxtaServerSocket;
import net.jxta.socket.JxtaSocket;
public class CarTest01{
//规定最高车速
int MaxV=5;
//标示车辆运行状态,0-正常直行;1-左换道;2-右换道;3-左转;4-右转;5-路口直行;6-停车让行
int RunState=0;
final int Drive=0;
final int TurnLeft=1;
final int TurnRight=2;
final int ChangeLeft=3;
final int ChangeRight=4;
final int GoAhead=5;
final int StopWait=6;
//判断前方车速用
int peold=680;
int penew=680;
int psold=480;
int psnew=480;
int pwold=20;
int pwnew=20;
int pnold=20;
int pnnew=20;
int v=0;
//减速等待计数,用于判断是否换道超车
int SlowCount=0;
//to do:获得路口对象的引用,需要与路口agent对应
SimpleJxtaApp Road;
int xCenter=350;
int yCenter=250;
//标示车辆位置,0-道路区;1-路口外围区;2-路口核心区
int Area=0;
//定义转向速度标尺
final int VT[]={1,2,3,4,5};
//final int VT[]={0,1,2,3};
//定义车辆行驶坐标、方向
int x,y;
int direction; //预设为从东驶入,以后再根据道路间的连接,相应修改
//定义车辆驶入方向,用于switch语句判断
final int FromEast=1;
final int FromSouth=2;
final int FromWest=3;
final int FromNorth=4;
//判断左-右车道
int LeftLane=0;
//判断是否左-右转弯
boolean IsTurnLeft=false;
boolean IsTurnRight=true;
boolean IsGoStrait=false;
boolean Dece;
boolean Dece1;
boolean ChangeWait;
boolean RightWait;
Graphics g;
Random random=new Random(2006); //随机函数产生是否转弯的一个随机数;
//Socket通讯
//connect2E mysocket = new connect2E();
// private PipeAdvertisement pipeAdv;
// private transient JxtaSocket socket;
// Put agent initializations here
CarTest01(SimpleJxtaApp a,int y,int x,int maxv,int direct)
{
this.Road=a;
this.y=y;
this.x=x;
this.MaxV=maxv;
this.direction=direct;
}
CarTest01(SimpleJxtaApp a,int maxv,int LeftLane)
{
this.Road=a;
this.MaxV=maxv;
this.LeftLane=LeftLane;
}
CarTest01(SimpleJxtaApp a)
{
this.Road=a;
}
CarTest01()
{
}
/*protected void setupSocket()
{
//构架JxtaSocket通讯,东部边界
int bufsize = 2;
try {
System.out.println("reading in socket.adv");
FileInputStream is = new FileInputStream("socket.adv");
pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);
is.close();
// run it once
System.out.println("Connecting to the server");
socket = new JxtaSocket(Road.netPeerGroup,
//no specific peerid
null,
pipeAdv,
//general TO: 30 seconds
30000,
// reliable connection
true);
// Set buffer size to payload size
socket.setOutputStreamBufferSize(32);
// The server initiates communication by sending a small data packet
// and then awaits data from the client
System.out.println("Reading in data");
InputStream in = socket.getInputStream();
byte[] inbuf = new byte[bufsize];
int read = in.read(inbuf, 0, bufsize);
System.out.println("received " + read + " bytes");
// Server is awaiting this data
// Send data and time it.
//System.out.println("Sending back " + payloadSize + " * " + ITERATIONS + " bytes");
OutputStream out = socket.getOutputStream();
byte[] payload = new byte[2];
out.write(payload);
out.flush();
// include close in timing since it may need to flush the
// tail end of the stream.
socket.close();
} catch (Throwable e) {
System.out.println("failed : " + e);
e.printStackTrace();
System.exit(-1);
}
//direction=FromEast;
}
public void Trans(){
int bufsize = 2;
try {
/*System.out.println("reading in socket.adv");
FileInputStream is = new FileInputStream("socket.adv");
pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);
is.close();
// run it once
System.out.println("Connecting to the server");
socket = new JxtaSocket(Road.netPeerGroup,
//no specific peerid
null,
pipeAdv,
//general TO: 30 seconds
30000,
// reliable connection
true);
// Set buffer size to payload size
socket.setOutputStreamBufferSize(32);
// The server initiates communication by sending a small data packet
// and then awaits data from the client
System.out.println("Reading in data");
InputStream in = socket.getInputStream();
byte[] inbuf = new byte[bufsize];
int read = in.read(inbuf, 0, bufsize);
System.out.println("received " + read + " bytes");
// Server is awaiting this data
// Send data and time it.
//System.out.println("Sending back " + payloadSize + " * " + ITERATIONS + " bytes");
OutputStream out = socket.getOutputStream();
byte[] payload = new byte[2];
out.write(payload);
out.flush();
// include close in timing since it may need to flush the
// tail end of the stream.
socket.close();
} catch (Throwable e) {
System.out.println("failed : " + e);
e.printStackTrace();
System.exit(-1);
}
}
*/
/*
**********正常行驶函数***********
*/
protected void runsel()
{
switch(RunState){ //顶层分支语句,判断车辆行驶状态
case 0: Running();break;
case 1: Turn2Left();break;
case 2: Turn2Right();break;
case 3: Change2Left();break;
case 4: Change2Right();break;
case 5: Hidden();break;
//case 6: Wait(x,y);break;
}//switch RunState 结束
}
public void Running(){
//connect2E mysocket=new connect2E();
Road.s[x][y]=0;
switch(direction){
case FromEast:
{
if((x>xCenter+40+MaxV)&&(x<=xCenter+140)) //判断车辆到达区域
Area=1;
else if((x>=xCenter+40)&&(x<=xCenter+40+MaxV)) //外围区
Area=2;
else if((x>xCenter-40)&&(x<xCenter+40)) //核心区
Area=3;
else if(x>=625){ //加入组间信息传递,
//mysocket.Show();
Area=4;
}
else if(x<=45)
Area=5;
else
Area=0;
peold=penew;
if (y==215) //上面车道
{
LeftLane=1;// right lane
switch(Area){
case 4:
{
Dece=false;
v=MaxV;
for(int pe=x-20;pe<x;pe++) //判断前方是否有车
{
if(Road.s[pe][y]==1) //这种判断方法是否有效,还需要详细考虑??
{
//System.out.println("pe"+pe);
Dece=true;
}
}
if (!Dece)
{
Road.s[x][y]=0;
x=x-MaxV;
Road.s[x][y]=1;
}
//添加Socket通讯功能
//mysocket.Show();
break;
}
case 5:
{
v=MaxV;
Road.s[x][y]=0; //向西行驶
if (x>=v)
{
x=x-v;
//System.out.println("x"+x);
Road.s[x][y]=1;
}
else
{ //加入换道函数
x=690;
Road.s[x][y]=1;
}
break;
}
case 0:{
Dece=false;
Dece1=false;
boolean flag1=false;
boolean flag2=true;
boolean flag3=true;
boolean flag4=true;
peold=x;
penew=x;
for(int pe=x-40;pe<x;pe++) //判断前方是否有车
{
if(Road.s[pe][y]==1) //这种判断方法是否有效,还需要详细考虑??
{
Dece=true;
penew=pe;
//System.out.println("There is a car1, x = "+x+"y ="+y+"pe ="+pe);
}
}
for(int pe=x-40;pe<=(x-15);pe++) //判断前方15-40是否有车
{
if(Road.s[pe][y]==1) //这种判断方法是否有效,还需要详细考虑??
{
flag1=true;
//System.out.println("There is a car1, x = "+x+"y ="+y+"pe ="+pe);
}
}
for(int pe=x-40;pe<=(x+75);pe++) //判断下面车道是否有车
{
if(Road.s[pe][235]==1) //这种判断方法是否有效,还需要详细考虑??
{
flag2=false;
}
}
for (int pe=x-24;pe<x;pe++)
{
for (int m=216;m<236;m++)
{
if (Road.s[pe][m]==1)
flag3=false;
}
}
for (int pe=x+1;pe<x+45;pe++)
{
for (int m=216;m<236;m++)
{
if (Road.s[pe][m]==1)
flag4=false;
}
}
if (flag1&&flag2&&flag3&&flag4)
Dece1=true;
if(Dece1)
{
RunState=ChangeLeft;
//System.out.println("ChangeLeft"+RunState);
}
else if (Dece)
{
if(peold-penew<=15)
v=0;
else if((peold-penew>15)&&(peold-penew<=20))
v=1;
else if((peold-penew>20)&&(peold-penew<=30))
v=3;
else if((peold-penew>30)&&(peold-penew<=35))
v=MaxV-2;
else if(peold-penew>35)
v=MaxV-1;
Road.s[x][y]=0;
x=x-v; //跟驰行驶逻辑结束
Road.s[x][y]=1;
}
else
{
v=MaxV;
Road.s[x][y]=0;
x=x-v; //向西行驶
Road.s[x][y]=1;
}
break;
}
case 1:{ //进入路口外围区
Dece=false;
peold=x;
penew=x;
for(int pe=x-40;pe<x;pe++){ //判断前方是否有车
if(Road.s[pe][y]==1){ //这种判断方法是否有效,还需要详细考虑??
Dece=true;
penew=pe;
}
}
if(Dece){
if(peold-penew<=15){
v=0;
System.out.println("There is a car x="+x);
}
else if((peold-penew>15)&&(peold-penew<=20))
v=1;
else if((peold-penew>20)&&(peold-penew<=30))
v=3;
else if((peold-penew>30)&&(peold-penew<=35))
v=MaxV-2;
else if(peold-penew>35)
v=MaxV-1;
}
else if(!Dece)
v=MaxV;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -