Hello,
I need to ask someone for a big favor. What you see in the code below are my calculations for train movement for a train game I am writing. There is a problem with the calculations, but I just can't seem to find it. The problem seems to be located in this code:
if($trainRow['direction'] == '-') {
$trackUnitCount = $routeData['track_unit_count'] - $trackUnitCount;
}
I will explain the exact problem in more depth later, but for now, I'd like to explain the train movement logic (the game may be found at http://apps.facebook.com/rails%5Facross%5Feurope):
The game board consists of a map of Europe containing cities. Cities are connected by routes. Routes have a starting city and an ending city. Routes are connected by track which is divided into track units. Each route has a given number of track units. A train may travel a route in either a positive (+) or negative (-) direction. If the city the train started at is the route's starting city, then the train is traveling in a positive direction. If the train's starting city is the route's ending city, then the direction is negative.
A train starts in a city and the user chooses a destination city to travel to. The train has a given number of track units it may travel in a single turn. If the train does not have enough track units to reach the destination city in a single turn, then the train's status is set to 'ENROUTE' and the train's current track unit is set to the track unit the train stopped on. The train's remaining track units is zero (since the train used all it's track units). If the train has more than enough track units to reach the destination, then the train stops at the destination and the train's status is set to 'ARRIVED'. The train's remaining track units is set to the trains starting track units minus the number of track units used to reach it's destination.
The player's turn will not end until his train's remaining track units reach zero, so once the player has finished his business at the current city, he will choose another city destination and the train will move it's remaining track units toward that city. If the train has more than enough track units remaining to reach that city, then it stops at the city and the track units traveled are subtracted from it's remaining track units. This goes on until the remaining track units reach zero.
The problem is that this code sets $trackUnitCount to zero, which then in turn sets the train's current track unit to zero, which indicates that the train has arrived at its destination city (which is incorrect). This results in the train hopping from one city to another without running out of track units remaining which should end the player's turn (before he arrived at the destination city)
If you can help me out with this, you are truly a cyber-saint :)
public function moveTrain($destCityId) {
require_once 'Train.php';
$trainModel = new Train();
require_once 'Route.php';
$routeModel = new Route();
$userNamespace = new Zend_Session_Namespace('User');
$gamePlayerId = $userNamespace->gamePlayerId;
$trainData = $trainModel->getTrain($gamePlayerId);
if($destCityId == $trainData['dest_city_id']) {
$originCityId = $trainData['origin_city_id'];
} else {
$originCityId = $trainData['dest_city_id'];
}
$routeId = $routeModel->getRouteIdByCityIds($originCityId, $destCityId);
$trainRow = array();
if($routeId !== null) {
$routeData = $routeModel->getRouteByCityIds($originCityId, $destCityId);
$trainRow['direction'] = $routeModel->getRouteTravelDirection($originCityId, $destCityId); //+
//$routeData['track_unit_count'] are the total number of track units in this route
//$trainData['track_unit'] is the track unit the train is currently stopped on
$trackUnitCount = $routeData['track_unit_count'] - $trainData['track_unit']; //6-3=3
//$trainData['track_units_remaining'] are the number of track units the train has left to move
$trackUnitsRemaining = $trainData['track_units_remaining'] - $trackUnitCount; //5-3=2
if($trackUnitsRemaining > 0) {
$trackUnitsTraveled = $trackUnitCount; //2
} else {
$trackUnitsTraveled = $trainData['track_units_remaining'];
$trackUnitsRemaining = 0;
}
//$trainRow = $trainData;
$trainRow['route_id'] = $routeId;
$trainRow['origin_city_id'] = $originCityId;
$trainRow['dest_city_id'] = $destCityId;
if($trainRow['direction'] == '+') {
$trainRowTrackUnit = $trackUnitsTraveled + $trainData['track_unit']; //2+3=5
} else {
$trainRowTrackUnit = $routeData['track_unit_count'] - $trainData['track_units_remaining'];
if($trainRowTrackUnit < 0) $trainRowTrackUnit = 0;
}
$trainRow['track_unit'] = $trainRowTrackUnit; //5
$trainRow['track_units_remaining'] = $trackUnitsRemaining; //2
$trainArrived = ($trainRowTrackUnit == 0 || $trainRowTrackUnit == $routeData['track_unit_count']);
$trainRow['status'] = ($trainArrived == true) ? 'ARRIVED' : 'ENROUTE';
$trainId = $trainModel->getTrainId($gamePlayerId);
$where = $trainModel->getAdapter()->quoteInto('id = ?', $trainId);
$trainModel->update($trainRow, $where);
}
return $trainRow;
}