137 |
137 |
138 static EngineID AiChooseTrainToBuild(RailType railtype, Money money, byte flag, TileIndex tile) |
138 static EngineID AiChooseTrainToBuild(RailType railtype, Money money, byte flag, TileIndex tile) |
139 { |
139 { |
140 EngineID best_veh_index = INVALID_ENGINE; |
140 EngineID best_veh_index = INVALID_ENGINE; |
141 byte best_veh_score = 0; |
141 byte best_veh_score = 0; |
142 CommandCost ret; |
|
143 EngineID i; |
142 EngineID i; |
144 |
143 |
145 for (i = 0; i < NUM_TRAIN_ENGINES; i++) { |
144 FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_TRAIN) { |
146 const RailVehicleInfo *rvi = RailVehInfo(i); |
145 const RailVehicleInfo *rvi = RailVehInfo(i); |
147 const Engine* e = GetEngine(i); |
146 const Engine* e = GetEngine(i); |
148 |
147 |
149 if (!IsCompatibleRail(rvi->railtype, railtype) || |
148 if (!IsCompatibleRail(rvi->railtype, railtype) || |
150 rvi->railveh_type == RAILVEH_WAGON || |
149 rvi->railveh_type == RAILVEH_WAGON || |
152 !HasBit(e->player_avail, _current_player) || |
151 !HasBit(e->player_avail, _current_player) || |
153 e->reliability < 0x8A3D) { |
152 e->reliability < 0x8A3D) { |
154 continue; |
153 continue; |
155 } |
154 } |
156 |
155 |
157 ret = DoCommand(tile, i, 0, 0, CMD_BUILD_RAIL_VEHICLE); |
156 CommandCost ret = DoCommand(tile, i, 0, 0, CMD_BUILD_RAIL_VEHICLE); |
158 if (CmdSucceeded(ret) && ret.GetCost() <= money && rvi->ai_rank >= best_veh_score) { |
157 if (CmdSucceeded(ret) && ret.GetCost() <= money && rvi->ai_rank >= best_veh_score) { |
159 best_veh_score = rvi->ai_rank; |
158 best_veh_score = rvi->ai_rank; |
160 best_veh_index = i; |
159 best_veh_index = i; |
161 } |
160 } |
162 } |
161 } |
166 |
165 |
167 static EngineID AiChooseRoadVehToBuild(CargoID cargo, Money money, TileIndex tile) |
166 static EngineID AiChooseRoadVehToBuild(CargoID cargo, Money money, TileIndex tile) |
168 { |
167 { |
169 EngineID best_veh_index = INVALID_ENGINE; |
168 EngineID best_veh_index = INVALID_ENGINE; |
170 int32 best_veh_rating = 0; |
169 int32 best_veh_rating = 0; |
171 EngineID i = ROAD_ENGINES_INDEX; |
170 EngineID i; |
172 EngineID end = i + NUM_ROAD_ENGINES; |
171 |
173 |
172 FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_ROAD) { |
174 for (; i != end; i++) { |
|
175 const RoadVehicleInfo *rvi = RoadVehInfo(i); |
173 const RoadVehicleInfo *rvi = RoadVehInfo(i); |
176 const Engine* e = GetEngine(i); |
174 const Engine* e = GetEngine(i); |
177 int32 rating; |
|
178 CommandCost ret; |
|
179 |
175 |
180 if (!HasBit(e->player_avail, _current_player) || e->reliability < 0x8A3D) { |
176 if (!HasBit(e->player_avail, _current_player) || e->reliability < 0x8A3D) { |
181 continue; |
177 continue; |
182 } |
178 } |
183 |
179 |
184 /* Skip vehicles which can't take our cargo type */ |
180 /* Skip vehicles which can't take our cargo type */ |
185 if (rvi->cargo_type != cargo && !CanRefitTo(i, cargo)) continue; |
181 if (rvi->cargo_type != cargo && !CanRefitTo(i, cargo)) continue; |
186 |
182 |
187 /* Rate and compare the engine by speed & capacity */ |
183 /* Rate and compare the engine by speed & capacity */ |
188 rating = rvi->max_speed * rvi->capacity; |
184 int rating = rvi->max_speed * rvi->capacity; |
189 if (rating <= best_veh_rating) continue; |
185 if (rating <= best_veh_rating) continue; |
190 |
186 |
191 ret = DoCommand(tile, i, 0, 0, CMD_BUILD_ROAD_VEH); |
187 CommandCost ret = DoCommand(tile, i, 0, 0, CMD_BUILD_ROAD_VEH); |
192 if (CmdFailed(ret)) continue; |
188 if (CmdFailed(ret)) continue; |
193 |
189 |
194 /* Add the cost of refitting */ |
190 /* Add the cost of refitting */ |
195 if (rvi->cargo_type != cargo) ret.AddCost(GetRefitCost(i)); |
191 if (rvi->cargo_type != cargo) ret.AddCost(GetRefitCost(i)); |
196 if (ret.GetCost() > money) continue; |
192 if (ret.GetCost() > money) continue; |
206 { |
202 { |
207 EngineID best_veh_index = INVALID_ENGINE; |
203 EngineID best_veh_index = INVALID_ENGINE; |
208 Money best_veh_cost = 0; |
204 Money best_veh_cost = 0; |
209 EngineID i; |
205 EngineID i; |
210 |
206 |
211 for (i = AIRCRAFT_ENGINES_INDEX; i != AIRCRAFT_ENGINES_INDEX + NUM_AIRCRAFT_ENGINES; i++) { |
207 FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_AIRCRAFT) { |
212 const Engine* e = GetEngine(i); |
208 const Engine* e = GetEngine(i); |
213 CommandCost ret; |
|
214 |
209 |
215 if (!HasBit(e->player_avail, _current_player) || e->reliability < 0x8A3D) { |
210 if (!HasBit(e->player_avail, _current_player) || e->reliability < 0x8A3D) { |
216 continue; |
211 continue; |
217 } |
212 } |
218 |
213 |
219 if ((AircraftVehInfo(i)->subtype & AIR_CTOL) != flag) continue; |
214 if ((AircraftVehInfo(i)->subtype & AIR_CTOL) != flag) continue; |
220 |
215 |
221 ret = DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_AIRCRAFT); |
216 CommandCost ret = DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_AIRCRAFT); |
222 if (CmdSucceeded(ret) && ret.GetCost() <= money && ret.GetCost() >= best_veh_cost) { |
217 if (CmdSucceeded(ret) && ret.GetCost() <= money && ret.GetCost() >= best_veh_cost) { |
223 best_veh_cost = ret.GetCost(); |
218 best_veh_cost = ret.GetCost(); |
224 best_veh_index = i; |
219 best_veh_index = i; |
225 } |
220 } |
226 } |
221 } |