From 63e97754fbf907cfefd277087bfbac5e0d4434e8 Mon Sep 17 00:00:00 2001 From: truelight Date: Thu, 6 Jan 2005 22:31:58 +0000 Subject: (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries (in prepare of dynamic arrays): - DEREF_XXX is changed into GetXXX - All direct call are directed via GetXXX - struct Industry has now an index-field - ENUM'd some stuff - Replaced home built loops with FOR_ALL_XXX - Added _stations_size, _vehicles_size, ... which gives the length of the array (which will be dynamic in the near future) - Changed lengtof(XXX) to _XXX_size (e.g. _stations_size) - Removed all endof(XXX) (because mostly it was part of a FOR_ALL_XXX) - Made the sort-functions of all 4 dynamic - Made all 4 Initialize functions more of the same - Some minor tab-fixing and stuff (tnx to Tron for proof-reading my 100kb patch ;)) Note for all: please do NOT directly call _stations, _vehicles, _towns and _industries, but use the right wrapper to access them. Thank you. Ps: please also do not use 'v++', where v is of type Vehicle *. --- ai.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'ai.c') diff --git a/ai.c b/ai.c index 0c647cf37..9ca11cc70 100644 --- a/ai.c +++ b/ai.c @@ -69,10 +69,11 @@ static void AiCase1(Player *p) static void AiStateVehLoop(Player *p) { Vehicle *v; + uint index; - v = p->ai.cur_veh == NULL ? _vehicles : p->ai.cur_veh+1; + index = (p->ai.cur_veh == NULL) ? 0 : p->ai.cur_veh->index + 1; - for (;v != endof(_vehicles); v++) { + FOR_ALL_VEHICLES_FROM(v, index) { if (v->type == 0 || v->owner != _current_player) continue; @@ -297,7 +298,7 @@ static void AiHandleReplaceTrain(Player *p) if (DoCommandByTile(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON) != CMD_ERROR && DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE) != CMD_ERROR) { veh = _new_train_id; - AiRestoreVehicleOrders(&_vehicles[veh], orderbak); + AiRestoreVehicleOrders(GetVehicle(veh), orderbak); DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_TRAIN); DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT); @@ -325,7 +326,7 @@ static void AiHandleReplaceRoadVeh(Player *p) if (DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH) != CMD_ERROR && DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH) != CMD_ERROR) { veh = _new_roadveh_id; - AiRestoreVehicleOrders(&_vehicles[veh], orderbak); + AiRestoreVehicleOrders(GetVehicle(veh), orderbak); DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_ROADVEH); DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT); @@ -353,7 +354,7 @@ static void AiHandleReplaceAircraft(Player *p) if (DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT) != CMD_ERROR && DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT) != CMD_ERROR) { veh = _new_aircraft_id; - AiRestoreVehicleOrders(&_vehicles[veh], orderbak); + AiRestoreVehicleOrders(GetVehicle(veh), orderbak); DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT); DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT); @@ -413,12 +414,12 @@ typedef struct FoundRoute { } FoundRoute; static Town *AiFindRandomTown() { - Town *t = DEREF_TOWN(RandomRange(_total_towns)); + Town *t = GetTown(RandomRange(_total_towns)); return (t->xy != 0) ? t : NULL; } static Industry *AiFindRandomIndustry() { - Industry *i = DEREF_INDUSTRY(RandomRange(_total_industries)); + Industry *i = GetIndustry(RandomRange(_total_industries)); return (i->xy != 0) ? i : NULL; } @@ -447,16 +448,16 @@ static void AiFindSubsidyIndustryRoute(FoundRoute *fr) return; fr->cargo = cargo; - fr->from = from = DEREF_INDUSTRY(s->from); + fr->from = from = GetIndustry(s->from); if (cargo == CT_GOODS || cargo == CT_FOOD) { - to_tow = DEREF_TOWN(s->to); + to_tow = GetTown(s->to); if (to_tow->population < (uint32)(cargo == CT_FOOD ? 200 : 900)) return; // error fr->to = to_tow; to_xy = to_tow->xy; } else { - to_ind = DEREF_INDUSTRY(s->to); + to_ind = GetIndustry(s->to); fr->to = to_ind; to_xy = to_ind->xy; } @@ -485,8 +486,8 @@ static void AiFindSubsidyPassengerRoute(FoundRoute *fr) return; fr->cargo = s->cargo_type; - fr->from = from = DEREF_TOWN(s->from); - fr->to = to = DEREF_TOWN(s->to); + fr->from = from = GetTown(s->from); + fr->to = to = GetTown(s->to); // They must be big enough if (from->population < 400 || to->population < 400) @@ -1416,7 +1417,7 @@ static void AiWantOilRigAircraftRoute(Player *p) t = AiFindRandomTown(); if (t != NULL) { // Find a random oil rig industry - in = DEREF_INDUSTRY(RandomRange(_total_industries)); + in = GetIndustry(RandomRange(_total_industries)); if (in != NULL && in->type == IT_OIL_RIG) { if (GetTileDist(t->xy, in->xy) < 60) break; @@ -2412,7 +2413,7 @@ handle_nocash: loco_id = _new_train_id; // Sell a vehicle if the train is double headed. - v = &_vehicles[loco_id]; + v = GetVehicle(loco_id); if (v->next != NULL) { i = p->ai.wagon_list[p->ai.num_wagons*2-2]; p->ai.wagon_list[p->ai.num_wagons*2-2] = INVALID_VEHICLE; -- cgit v1.2.3-54-g00ecf