⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pilotagent.java.svn-base

📁 這是一個JAVA語言寫的多代理人程式用來模擬飛機起飛或是降落的程式
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
		else if(pilotState == PilotState.READY_TO_DEPART){
			DoDeparture();
			return true;
		}
		else if(pilotState==PilotState.GOT_DEPARTURE_TRACON){
			DoEchoDepartureTracon();
			return true;
		}
		// System.out.println("returning false");
		
		return false;
	}

//ACTIONS
//ARRIVALS
private void DoSendArrivalChecklist(){
	pilotState = PilotState.SENT_CHECKLIST;
	////print("Sending local controller msgNewArrivingFlight");
	//localController.msgNewArrivingFlight(plane, flightNumber, landing, destinationGate);
	stateChanged();
}
private void DoAskToLand(){
    pilotState = PilotState.ASKED_TO_LAND;
				    approach = "visual";
    //print("Sending local controller msgRequestToLand");
	localController.msgRequestToLand(flightNumber, approach, landing);
    stateChanged();
}

private void DoEchoClearToLand(){
    pilotState = PilotState.ECHO_LANDING;
    //print("Sending local controller msgConfirmClearanceToLand");
    localController.msgConfirmClearanceToLand(flightNumber, landing);
    stateChanged();
}

private void DoWaitToLand(){
	//hover in sky for certain time
	try {
		Thread.sleep(15000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	pilotState = PilotState.NEED_TO_ASK_TO_LAND;
	stateChanged();
}

private void DoEchoLandingPath(){
	pilotState = PilotState.ECHO_LANDING_PATH;
	//print("Sending local controller msgConfirmLandingPath");
	localController.msgConfirmLandingPath(flightNumber, taxiLandingPath, groundController);
	stateChanged();
}


private void DoLanding(){
	pilotState = PilotState.LANDING;
	//Landing procedure
	ActionLanding();
	//print("Sending ground controller msgLeavingRunway");
	print(destinationGate.toString());
	print(flightNumber);
	groundController.msgLeavingRunway(flightNumber, landing, destinationGate);
	stateChanged();
}
private void DoEchoLandTaxiPath() {
	//print("Sending ground controller msgConfirmTaxiLandingPath");
	pilotState = PilotState.ECHO_TAXI_LAND_PATH;
	groundController.msgConfirmTaxiLandingPath(flightNumber, taxiLandingPath);
	stateChanged();
}
private void DoTaxiToGate(){
	pilotState = PilotState.TAXIING;
	//Taxiing procedure
	DoFollowTaxiways(taxiLandingPath);
	MoveToGate();
	pilotState = PilotState.ARRIVED;
	System.out.println("arrived state");
	stateChanged();
}

//DEPARTURES


private void DoSendDepartureChecklist(){
	pilotState = PilotState.SENT_DEPARTURE_CHECKLIST;
	//print("Sending clearance delivery msgNewDepartingFlight");

	clearanceDelivery.msgNewDepartingFlight(plane, flightNumber, departing, destinationGate, destination, this);
	stateChanged();
}
private void DoRequestClearanceForDeparture(){
	pilotState = PilotState.REQUESTED_DEPARTURE_CLEARANCE;
	//print("Sending clearance delivery msgRequestingClearance");
	clearanceDelivery.msgRequestingClearance(flightNumber, destinationGate);
	stateChanged();
} 
private void DoEchoDepartureClearance(){
	pilotState = PilotState.ECHO_CLEARED_FOR_DEPARTURE;
	//print("Sending clearance delivery msgConfirmClearance");
	clearanceDelivery.msgConfirmClearance(flightNumber, groundController);
	stateChanged();
}

private void DoEchoWeather(){
	pilotState = PilotState.ECHO_WEATHER;
	//print("Sending clearance delivery msgConfirmWeather");
	clearanceDelivery.msgConfirmWeather(flightNumber);
	//print("Sending ground control msgRequestToPushBack");
	groundController.msgRequestPushBack(flightNumber, destinationGate);
	stateChanged();
}
private void DoRequestPushBack(){
	pilotState = PilotState.ASKED_FOR_PUSH_BACK;
	//print("Sending ground controller msgRequestPushBack");
	groundController.msgRequestPushBack(flightNumber, destinationGate);
	stateChanged();
}
private void DoEchoPushBack(){
	pilotState = PilotState.ECHO_PUSH_BACK;
	//print("Sending ground controller msgConfirmPushBack");
	groundController.msgConfirmPushBack(flightNumber, departing, destinationGate);
	stateChanged();
}
private void DoWaitForPushBack(){
	pilotState = PilotState.PUSH_BACK;
	//print("Sending ground controller msgReadyForTaxi");
	groundController.msgReadyForTaxi(flightNumber, destinationGate.getTaxiway());
	pilotState = PilotState.READY_FOR_TAXI;
	stateChanged();
}
private void DoEchoTaxiPath(){
	pilotState = PilotState.ECHO_TAXI_PATH;
	//print("Sending ground controller msgConfirmTaxiTakeOffPath");
	groundController.msgConfirmTaxiTakeoffPath(flightNumber, taxiDepartingPath, localController);
	stateChanged();
}
private void DoDepartureTaxiing(){
	pilotState = PilotState.DEPARTURE_TAXIING;
	//COMPLETE TAXIING PROCEDURE
	DoFollowTaxiways(taxiDepartingPath);
	pilotState = PilotState.READY_FOR_TAKEOFF;
	//print("Sending local controller msgReadyForTakeoff");
	localController.msgReadyForTakeoff(flightNumber, departing);
	stateChanged();
}
private void DoEchoHoldForDeparture(){
	pilotState = PilotState.ECHO_HOLD_FOR_DEPARTURE;
	//print("Sending local controller msgConfirmationPositionAndHold");
	localController.msgConfirmPositionAndHold(flightNumber);
	stateChanged();
}
private void DoEchoClearedForLCDeparture(){
	pilotState = PilotState.READY_TO_DEPART;
	//print("Sending local controller msgConfirmClearedForTakeoff");
	localController.msgConfirmClearedForTakeoff(flightNumber, departing);
	stateChanged();
}
private void DoEchoDepartureTracon(){
	pilotState = PilotState.ECHO_DEPARTURE_TRACON;
	//print("Sending local controller msgConfirmDepartureControl");
	localController.msgConfirmDepartureControl(flightNumber);
	stateChanged();
}

private void DoDeparture(){
	pilotState = PilotState.TAKING_OFF;
	//DoFollowTaxiways(taxiDepartingPath);
	ActionDeparture();
	////print("Sending local controller msgConfirmDepartureControl");
	//localController.msgConfirmDepartureControl(flightNumber);
	stateChanged();
}



void DoFollowTaxiways(List<Taxiway> path){
    WorldVector nextPos;
    //WorldVector otherPlanepos;				
    boolean okaytogo = false;
    /*
    if(pilotState == PilotState.DEPARTURE_TAXIING) {
		for (WorldObject world_object : World.world_objects) {
			if ((world_object instanceof Plane) && (this.plane != world_object)) {
				// if(world_object.objectState.position.x == this.plane.objectState.position.x) { // They are on the same taxiway 
				// System.out.println(world_object.getName() + " " + world_object.objectState.position.y);
				// System.out.println(this.plane.objectState.position.y);
			    if((world_object.objectState.position.y == this.departing.objectState.position.y))  {
			    	okaytogo = false;
			    	// moveTo = new WorldVector(this.plane.objectState.position.x,this.plane.objectState.position.y,this.plane.objectState.position.z);
			    	pilotState = PilotState.HOLD_FOR_DEPARTURE;
			    	stateChanged();
			    	break;
			    } else {
			    	okaytogo = true;
			    }
			}
		}    
		if(okaytogo) {
		    for(int x = 0; x < path.size()-1; x++){
		        System.out.print("Path: " + path.size()+" " + path.get(x+1));
		        ////print("Next position: "+nextPos.toString());
		
		    	nextPos = GetSimilarPoint(path.get(x), path.get(x+1));
		        if((nextPos!=null))
		        	MoveTo(nextPos);
		    }
		}
    } else {
    */
	    for(int x = 0; x < path.size()-1; x++){
	        System.out.print("Path: " + path.size()+" " + path.get(x+1));
	        ////print("Next position: "+nextPos.toString());

	    	nextPos = GetSimilarPoint(path.get(x), path.get(x+1));
	        if((nextPos!=null))
	        	MoveTo(nextPos);
	    }

    //print("Taxiway path list size: "+path.size());

}

void MoveTo(WorldVector moveTo){
	//print("\nMove to next taxiway: "+moveTo.x+", "+moveTo.y);
	System.out.println("Current plane pos y at " + plane.get_world_object_state().position.y);
    WorldVector newVelocity = calculateHeading(moveTo, taxiVelocity);
    ////print("Velocity is: "+newVelocity.toString());
    boolean okaytogo = false;

    //System.out.println("calling rotate to");
    //rotate_plane(new Double(calculateAngle(newVelocity.x, newVelocity.y)));
    //System.out.println("out of rotate");
    plane.get_world_object_state().angularPosition = new WorldVector(0,0,calculateAngle(newVelocity.x, newVelocity.y));
    
    plane.get_world_object_state().set_velocity(newVelocity);
    ////print("Angular Position is: "+plane.get_world_object_state().angularPosition.z);
    while(getDistance(plane.get_world_object_state().get_position(), moveTo) >= ((taxiVelocity/2)+1)){
    	// System.out.print("Hello");
    	for (WorldObject world_object : World.world_objects) {
    		if ((world_object instanceof Plane) && (this.plane != world_object)) {
    			// if(world_object.objectState.position.x == this.plane.objectState.position.x) { // They are on the same taxiway 
				// System.out.println(world_object.getName() + " " + world_object.objectState.position.y);
				// System.out.println(this.plane.objectState.position.y);
			    if((world_object.objectState.position.y > this.plane.objectState.position.y - 90) 
			    		&& (world_object.objectState.position.x == this.plane.objectState.position.x))  {
			    	okaytogo = false;
			    	moveTo = new WorldVector(this.plane.objectState.position.x,this.plane.objectState.position.y,this.plane.objectState.position.z);
			    	break;
			    } else {
			    	okaytogo = true;
			    }
    			// }
    		}	
    	}
    	if(okaytogo == false) {
    		break;
    	}
        try {
			inc_velocity.acquire();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    }
    
    plane.get_world_object_state().set_position(moveTo);
    plane.get_world_object_state().set_velocity(new WorldVector(0,0,0));///////////////////////////////////////dont always do this?
}

void MoveToGate(){
	WorldVector moveTo = destinationGate.get_world_object_state().get_position();
	MoveTo(moveTo);

    System.out.println("done moving to gate");
}

double calculateAngle(double x, double y){
	if(x==0 && y>=0)
		return 3*Math.PI/2;
	else if(x==0 && y<0)
		return Math.PI/2;
	else if(x>0 && y>=0)
		return 2*Math.PI - Math.atan(y/x);
	else if(x<0 && y>=0)
		return Math.PI-Math.atan(y/x);
	else if(x<0 && y<0) // correct
		return Math.PI-Math.atan(y/x);
	else if(x>0 && y<0) // correct
		return 2*Math.PI - Math.atan(y/x);
		
	else
		return 0;
	
}

double getDistance(WorldVector currentPos, WorldVector nextPos){
    return Math.sqrt(Math.pow(currentPos.x-nextPos.x,2)+Math.pow(currentPos.y-nextPos.y,2));
}

//override
WorldVector calculateHeading(WorldVector moveTo, double speed){
    WorldVector planeVector = plane.get_world_object_state().get_position();
	WorldVector velocity = new WorldVector(moveTo.x-planeVector.x, moveTo.y-planeVector.y, moveTo.z-planeVector.z);
	velocity = setMagnitudeTo(velocity, speed);
	return velocity;
}

WorldVector GetSimilarPoint(WorldObject wo1, WorldObject wo2){
	WorldObjectState wos1 = wo1.get_world_object_state();
	WorldObjectState wos2 = wo2.get_world_object_state();
	
	List<WorldVector> connectionList1 = wos1.getConnections();
	List<WorldVector> connectionList2 = wos2.getConnections();
	
	WorldVector real_conn1, real_conn2;
	
	for(int x=0;x<connectionList1.size();x++)
	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -