Skip to content

Commit

Permalink
Make unregister an action in driving skill
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc0711 committed Aug 11, 2021
1 parent 3804124 commit 43eb9aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
Expand Up @@ -1394,7 +1394,7 @@ public void primDriveRandom(final IScope scope) throws GamaRuntimeException {
int newLane = laneAndAccPair.getKey();

setCurrentTarget(vehicle, newTarget);
RoadSkill.unregister(scope, vehicle);
unregister(scope, vehicle);
RoadSkill.register(scope, vehicle, newRoad, newLane);
// Choose the next road in advance
IAgent nextRoad = chooseNextRoadRandomly(scope, graph, newTarget, roadProba);
Expand Down Expand Up @@ -1551,7 +1551,7 @@ public void primDrive(final IScope scope) throws GamaRuntimeException {

setCurrentIndex(vehicle, newEdgeIdx);
setCurrentTarget(vehicle, newTarget);
RoadSkill.unregister(scope, vehicle);
unregister(scope, vehicle);
RoadSkill.register(scope, vehicle, newRoad, newLane);

setViolatingOneway(vehicle, violatingOneway);
Expand Down Expand Up @@ -1835,6 +1835,32 @@ private void clearDrivingStates(final IScope scope) {
setCurrentPath(vehicle, null);
}

@action(
name = "unregister",
doc = @doc(
value = "remove the vehicle from its current roads",
examples = { @example("do unregister") }
)
)
public void primUnregister(final IScope scope) throws GamaRuntimeException {
IAgent vehicle = getCurrentAgent(scope);
unregister(scope, vehicle);
}

public static void unregister(final IScope scope, final IAgent driver)
throws GamaRuntimeException {
IAgent currentRoad = getCurrentRoad(driver);
if (currentRoad == null) return;

Integer lowestLane = (Integer) driver.getAttribute(LOWEST_LANE);
int numLanesOccupied = (int) driver.getAttribute(NUM_LANES_OCCUPIED);
for (int i = 0; i < numLanesOccupied; i += 1) {
int lane = lowestLane + i;
RoadSkill.getVehiclesOnLaneSegment(scope, currentRoad, lane).remove(driver);
}
setCurrentRoad(driver, null);
}

// TODO: this action is not overridden
@action(
name = "die",
Expand All @@ -1846,7 +1872,7 @@ private void clearDrivingStates(final IScope scope) {
public void primDieWrapper(final IScope scope) throws GamaRuntimeException {
AbstractAgent vehicle = (AbstractAgent) getCurrentAgent(scope);
if (!vehicle.dead() && getCurrentRoad(vehicle) != null) {
RoadSkill.unregister(scope, vehicle);
unregister(scope, vehicle);
}
vehicle.primDie(scope);
}
Expand Down
Expand Up @@ -391,33 +391,13 @@ public static void register(IScope scope, IAgent vehicle, IAgent road, int lowes
},
doc = @doc(
value = "unregister the agent on the road",
examples = { @example ("do unregister agent: the_driver") }
examples = { @example ("do unregister agent: the_driver") },
deprecated = "use the `unregister` action in advanced_driving skill instead"
)
)
@Deprecated
public void primUnregister(final IScope scope) throws GamaRuntimeException {
IAgent driver = (IAgent) scope.getArg("agent", IType.AGENT);
unregister(scope, driver);
}

/**
* Unregisters the driver from all the roads that it's currently on.
*
* @param scope
* @param driver the agent that we want to unregister.
*
* @throws GamaRuntimeException
*/
public static void unregister(IScope scope, final IAgent driver)
throws GamaRuntimeException {
IAgent currentRoad = (IAgent) driver.getAttribute(DrivingSkill.CURRENT_ROAD);
if (currentRoad == null) return;

Integer lowestLane = (Integer) driver.getAttribute(DrivingSkill.LOWEST_LANE);
int numLanesOccupied = (int) driver.getAttribute(DrivingSkill.NUM_LANES_OCCUPIED);
for (int i = 0; i < numLanesOccupied; i += 1) {
int lane = lowestLane + i;
getVehiclesOnLaneSegment(scope, currentRoad, lane).remove(driver);
}
getAgents(currentRoad).remove(driver);
DrivingSkill.unregister(scope, driver);
}
}

0 comments on commit 43eb9aa

Please sign in to comment.