src/ai/trolly/trolly.cpp
changeset 9413 7042a8ec3fa8
parent 9358 2e1e4d2f71dd
child 9912 ee5cc92fde16
equal deleted inserted replaced
9412:163c465bf250 9413:7042a8ec3fa8
   128 			_players_ainew[p->index].action = AI_ACTION_REPAY_LOAN;
   128 			_players_ainew[p->index].action = AI_ACTION_REPAY_LOAN;
   129 		} else if (_players_ainew[p->index].last_vehiclecheck_date + AI_DAYS_BETWEEN_VEHICLE_CHECKS < _date) {
   129 		} else if (_players_ainew[p->index].last_vehiclecheck_date + AI_DAYS_BETWEEN_VEHICLE_CHECKS < _date) {
   130 			// Check all vehicles once in a while
   130 			// Check all vehicles once in a while
   131 			_players_ainew[p->index].action = AI_ACTION_CHECK_ALL_VEHICLES;
   131 			_players_ainew[p->index].action = AI_ACTION_CHECK_ALL_VEHICLES;
   132 			_players_ainew[p->index].last_vehiclecheck_date = _date;
   132 			_players_ainew[p->index].last_vehiclecheck_date = _date;
   133 		} else if (c < 100 && !_settings.ai.ai_disable_veh_roadveh) {
   133 		} else if (c < 100 && !_settings_game.ai.ai_disable_veh_roadveh) {
   134 			// Do we have any spots for road-vehicles left open?
   134 			// Do we have any spots for road-vehicles left open?
   135 			if (GetFreeUnitNumber(VEH_ROAD) <= _settings.vehicle.max_roadveh) {
   135 			if (GetFreeUnitNumber(VEH_ROAD) <= _settings_game.vehicle.max_roadveh) {
   136 				if (c < 85) {
   136 				if (c < 85) {
   137 					_players_ainew[p->index].action = AI_ACTION_TRUCK_ROUTE;
   137 					_players_ainew[p->index].action = AI_ACTION_TRUCK_ROUTE;
   138 				} else {
   138 				} else {
   139 					_players_ainew[p->index].action = AI_ACTION_BUS_ROUTE;
   139 					_players_ainew[p->index].action = AI_ACTION_BUS_ROUTE;
   140 				}
   140 				}
   141 			}
   141 			}
   142 #if 0
   142 #if 0
   143 		} else if (c < 200 && !_settings.ai.ai_disable_veh_train) {
   143 		} else if (c < 200 && !_settings_game.ai.ai_disable_veh_train) {
   144 			if (GetFreeUnitNumber(VEH_TRAIN) <= _settings.vehicle.max_trains) {
   144 			if (GetFreeUnitNumber(VEH_TRAIN) <= _settings_game.vehicle.max_trains) {
   145 				_players_ainew[p->index].action = AI_ACTION_TRAIN_ROUTE;
   145 				_players_ainew[p->index].action = AI_ACTION_TRAIN_ROUTE;
   146 			}
   146 			}
   147 #endif
   147 #endif
   148 		}
   148 		}
   149 
   149 
   153 	if (_players_ainew[p->index].counter++ > AI_MAX_TRIES_FOR_SAME_ROUTE) {
   153 	if (_players_ainew[p->index].counter++ > AI_MAX_TRIES_FOR_SAME_ROUTE) {
   154 		_players_ainew[p->index].action = AI_ACTION_NONE;
   154 		_players_ainew[p->index].action = AI_ACTION_NONE;
   155 		return;
   155 		return;
   156 	}
   156 	}
   157 
   157 
   158 	if (_settings.ai.ai_disable_veh_roadveh && (
   158 	if (_settings_game.ai.ai_disable_veh_roadveh && (
   159 				_players_ainew[p->index].action == AI_ACTION_BUS_ROUTE ||
   159 				_players_ainew[p->index].action == AI_ACTION_BUS_ROUTE ||
   160 				_players_ainew[p->index].action == AI_ACTION_TRUCK_ROUTE
   160 				_players_ainew[p->index].action == AI_ACTION_TRUCK_ROUTE
   161 			)) {
   161 			)) {
   162 		_players_ainew[p->index].action = AI_ACTION_NONE;
   162 		_players_ainew[p->index].action = AI_ACTION_NONE;
   163 		return;
   163 		return;
   177 
   177 
   178 	// It is useless to start finding a route if we don't have enough money
   178 	// It is useless to start finding a route if we don't have enough money
   179 	//  to build the route anyway..
   179 	//  to build the route anyway..
   180 	if (_players_ainew[p->index].action == AI_ACTION_BUS_ROUTE &&
   180 	if (_players_ainew[p->index].action == AI_ACTION_BUS_ROUTE &&
   181 			money > AI_MINIMUM_BUS_ROUTE_MONEY) {
   181 			money > AI_MINIMUM_BUS_ROUTE_MONEY) {
   182 		if (GetFreeUnitNumber(VEH_ROAD) > _settings.vehicle.max_roadveh) {
   182 		if (GetFreeUnitNumber(VEH_ROAD) > _settings_game.vehicle.max_roadveh) {
   183 			_players_ainew[p->index].action = AI_ACTION_NONE;
   183 			_players_ainew[p->index].action = AI_ACTION_NONE;
   184 			return;
   184 			return;
   185 		}
   185 		}
   186 		_players_ainew[p->index].cargo = AI_NEED_CARGO;
   186 		_players_ainew[p->index].cargo = AI_NEED_CARGO;
   187 		_players_ainew[p->index].state = AI_STATE_LOCATE_ROUTE;
   187 		_players_ainew[p->index].state = AI_STATE_LOCATE_ROUTE;
   188 		_players_ainew[p->index].tbt = AI_BUS; // Bus-route
   188 		_players_ainew[p->index].tbt = AI_BUS; // Bus-route
   189 		return;
   189 		return;
   190 	}
   190 	}
   191 	if (_players_ainew[p->index].action == AI_ACTION_TRUCK_ROUTE &&
   191 	if (_players_ainew[p->index].action == AI_ACTION_TRUCK_ROUTE &&
   192 			money > AI_MINIMUM_TRUCK_ROUTE_MONEY) {
   192 			money > AI_MINIMUM_TRUCK_ROUTE_MONEY) {
   193 		if (GetFreeUnitNumber(VEH_ROAD) > _settings.vehicle.max_roadveh) {
   193 		if (GetFreeUnitNumber(VEH_ROAD) > _settings_game.vehicle.max_roadveh) {
   194 			_players_ainew[p->index].action = AI_ACTION_NONE;
   194 			_players_ainew[p->index].action = AI_ACTION_NONE;
   195 			return;
   195 			return;
   196 		}
   196 		}
   197 		_players_ainew[p->index].cargo = AI_NEED_CARGO;
   197 		_players_ainew[p->index].cargo = AI_NEED_CARGO;
   198 		_players_ainew[p->index].last_id = 0;
   198 		_players_ainew[p->index].last_id = 0;
  1015 	assert(_players_ainew[p->index].state == AI_STATE_BUILD_PATH);
  1015 	assert(_players_ainew[p->index].state == AI_STATE_BUILD_PATH);
  1016 	// _players_ainew[p->index].temp is set to -1 when this function is called for the first time
  1016 	// _players_ainew[p->index].temp is set to -1 when this function is called for the first time
  1017 	if (_players_ainew[p->index].temp == -1) {
  1017 	if (_players_ainew[p->index].temp == -1) {
  1018 		DEBUG(ai, 1, "Starting to build new path");
  1018 		DEBUG(ai, 1, "Starting to build new path");
  1019 		// Init the counter
  1019 		// Init the counter
  1020 		_players_ainew[p->index].counter = (4 - _settings.difficulty.competitor_speed) * AI_BUILDPATH_PAUSE + 1;
  1020 		_players_ainew[p->index].counter = (4 - _settings_game.difficulty.competitor_speed) * AI_BUILDPATH_PAUSE + 1;
  1021 		// Set the position to the startingplace (-1 because in a minute we do ++)
  1021 		// Set the position to the startingplace (-1 because in a minute we do ++)
  1022 		_players_ainew[p->index].path_info.position = -1;
  1022 		_players_ainew[p->index].path_info.position = -1;
  1023 		// And don't do this again
  1023 		// And don't do this again
  1024 		_players_ainew[p->index].temp = 0;
  1024 		_players_ainew[p->index].temp = 0;
  1025 	}
  1025 	}
  1026 	// Building goes very fast on normal rate, so we are going to slow it down..
  1026 	// Building goes very fast on normal rate, so we are going to slow it down..
  1027 	//  By let the counter count from AI_BUILDPATH_PAUSE to 0, we have a nice way :)
  1027 	//  By let the counter count from AI_BUILDPATH_PAUSE to 0, we have a nice way :)
  1028 	if (--_players_ainew[p->index].counter != 0) return;
  1028 	if (--_players_ainew[p->index].counter != 0) return;
  1029 	_players_ainew[p->index].counter = (4 - _settings.difficulty.competitor_speed) * AI_BUILDPATH_PAUSE + 1;
  1029 	_players_ainew[p->index].counter = (4 - _settings_game.difficulty.competitor_speed) * AI_BUILDPATH_PAUSE + 1;
  1030 
  1030 
  1031 	// Increase the building position
  1031 	// Increase the building position
  1032 	_players_ainew[p->index].path_info.position++;
  1032 	_players_ainew[p->index].path_info.position++;
  1033 	// Build route
  1033 	// Build route
  1034 	AiNew_Build_RoutePart(p, &_players_ainew[p->index].path_info, DC_EXEC);
  1034 	AiNew_Build_RoutePart(p, &_players_ainew[p->index].path_info, DC_EXEC);
  1035 	if (_players_ainew[p->index].path_info.position == -2) {
  1035 	if (_players_ainew[p->index].path_info.position == -2) {
  1036 		// This means we are done building!
  1036 		// This means we are done building!
  1037 
  1037 
  1038 		if (_players_ainew[p->index].tbt == AI_TRUCK && !_settings.pf.roadveh_queue) {
  1038 		if (_players_ainew[p->index].tbt == AI_TRUCK && !_settings_game.pf.roadveh_queue) {
  1039 			// If they not queue, they have to go up and down to try again at a station...
  1039 			// If they not queue, they have to go up and down to try again at a station...
  1040 			// We don't want that, so try building some road left or right of the station
  1040 			// We don't want that, so try building some road left or right of the station
  1041 			DiagDirection dir1, dir2, dir3;
  1041 			DiagDirection dir1, dir2, dir3;
  1042 			TileIndex tile;
  1042 			TileIndex tile;
  1043 			CommandCost ret;
  1043 			CommandCost ret;
  1184 	} else {
  1184 	} else {
  1185 		_players_ainew[p->index].veh_main_id = _players_ainew[p->index].veh_id;
  1185 		_players_ainew[p->index].veh_main_id = _players_ainew[p->index].veh_id;
  1186 	}
  1186 	}
  1187 
  1187 
  1188 	// Very handy for AI, goto depot.. but yeah, it needs to be activated ;)
  1188 	// Very handy for AI, goto depot.. but yeah, it needs to be activated ;)
  1189 	if (_settings.order.gotodepot) {
  1189 	if (_settings_game.order.gotodepot) {
  1190 		idx = 0;
  1190 		idx = 0;
  1191 		order.MakeGoToDepot(GetDepotByTile(_players_ainew[p->index].depot_tile)->index, ODTFB_PART_OF_ORDERS);
  1191 		order.MakeGoToDepot(GetDepotByTile(_players_ainew[p->index].depot_tile)->index, ODTFB_PART_OF_ORDERS);
  1192 		AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
  1192 		AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
  1193 	}
  1193 	}
  1194 
  1194