(svn r4941) Replace some ints and magic numbers by proper types and enums

This commit is contained in:
tron 2006-05-21 15:19:20 +00:00
parent 3282efda88
commit 5783d57ec0
3 changed files with 28 additions and 20 deletions

View File

@ -220,28 +220,30 @@ int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte fla
// This functions tries to find the best vehicle for this type of cargo
// It returns vehicle_id or -1 if not found
int AiNew_PickVehicle(Player *p)
// It returns INVALID_ENGINE if not suitable engine is found
EngineID AiNew_PickVehicle(Player *p)
{
if (p->ainew.tbt == AI_TRAIN) {
// Not supported yet
return -1;
return INVALID_ENGINE;
} else {
int start, count, i, ret = CMD_ERROR;
start = _cargoc.ai_roadveh_start[p->ainew.cargo];
count = _cargoc.ai_roadveh_count[p->ainew.cargo];
EngineID start = _cargoc.ai_roadveh_start[p->ainew.cargo];
EngineID end = start + _cargoc.ai_roadveh_count[p->ainew.cargo];
EngineID i;
// Let's check it backwards.. we simply want to best engine available..
for (i = start + count - 1; i >= start; i--) {
for (i = end - 1; i >= start; i--) {
int32 ret;
// Is it availiable?
// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
if (!HASBIT(GetEngine(i)->player_avail, _current_player) || GetEngine(i)->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
// Can we build it?
ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
if (!CmdFailed(ret)) break;
if (!CmdFailed(ret)) return i;
}
// We did not find a vehicle :(
return CmdFailed(ret) ? -1 : i;
return INVALID_ENGINE;
}
}
@ -249,9 +251,9 @@ int AiNew_PickVehicle(Player *p)
// Builds the best vehicle possible
int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
{
int i = AiNew_PickVehicle(p);
if (i == -1) return CMD_ERROR;
EngineID i = AiNew_PickVehicle(p);
if (i == INVALID_ENGINE) return CMD_ERROR;
if (p->ainew.tbt == AI_TRAIN) return CMD_ERROR;
return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);

View File

@ -567,7 +567,8 @@ static void AiNew_State_FindStation(Player *p)
{
TileIndex tile;
Station *st;
int i, count = 0;
int count = 0;
EngineID i;
TileIndex new_tile = 0;
byte direction = 0;
Town *town = NULL;
@ -606,7 +607,10 @@ static void AiNew_State_FindStation(Player *p)
i = AiNew_PickVehicle(p);
// Euhmz, this should not happen _EVER_
// Quit finding a route...
if (i == -1) { p->ainew.state = AI_STATE_NOTHING; return; }
if (i == INVALID_ENGINE) {
p->ainew.state = AI_STATE_NOTHING;
return;
}
FOR_ALL_STATIONS(st) {
if (st->xy != 0) {
@ -867,10 +871,11 @@ static int AiNew_HowManyVehicles(Player *p)
{
if (p->ainew.tbt == AI_BUS) {
// For bus-routes we look at the time before we are back in the station
int i, length, tiles_a_day;
EngineID i;
int length, tiles_a_day;
int amount;
i = AiNew_PickVehicle(p);
if (i == -1) return 0;
if (i == INVALID_ENGINE) return 0;
// Passenger run.. how long is the route?
length = p->ainew.path_info.route_length;
// Calculating tiles a day a vehicle moves is not easy.. this is how it must be done!
@ -882,10 +887,11 @@ static int AiNew_HowManyVehicles(Player *p)
return amount;
} else if (p->ainew.tbt == AI_TRUCK) {
// For truck-routes we look at the cargo
int i, length, amount, tiles_a_day;
EngineID i;
int length, amount, tiles_a_day;
int max_cargo;
i = AiNew_PickVehicle(p);
if (i == -1) return 0;
if (i == INVALID_ENGINE) return 0;
// Passenger run.. how long is the route?
length = p->ainew.path_info.route_length;
// Calculating tiles a day a vehicle moves is not easy.. this is how it must be done!
@ -1122,7 +1128,7 @@ static void AiNew_State_BuildDepot(Player *p)
p->ainew.state = AI_STATE_BUILD_VEHICLE;
p->ainew.idle = 10;
p->ainew.veh_main_id = (VehicleID)-1;
p->ainew.veh_main_id = INVALID_VEHICLE;
}
@ -1181,7 +1187,7 @@ static void AiNew_State_GiveOrders(Player *p)
p->ainew.veh_id = _new_roadveh_id;
}
if (p->ainew.veh_main_id != (VehicleID)-1) {
if (p->ainew.veh_main_id != INVALID_VEHICLE) {
AI_DoCommand(0, p->ainew.veh_id + (p->ainew.veh_main_id << 16), 0, DC_EXEC, CMD_CLONE_ORDER);
p->ainew.state = AI_STATE_START_VEHICLE;

View File

@ -254,7 +254,7 @@ bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile);
int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag);
int AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag);
int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag);
int AiNew_PickVehicle(Player *p);
EngineID AiNew_PickVehicle(Player *p);
int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag);
int AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag);