ai.c
changeset 1977 37bbebf94434
parent 1927 1181e7652e2f
child 1981 3c9c682f1212
equal deleted inserted replaced
1976:2eb82bb3cb2d 1977:37bbebf94434
   279 static void AiHandleReplaceTrain(Player *p)
   279 static void AiHandleReplaceTrain(Player *p)
   280 {
   280 {
   281 	Vehicle *v = p->ai.cur_veh;
   281 	Vehicle *v = p->ai.cur_veh;
   282 	BackuppedOrders orderbak[1];
   282 	BackuppedOrders orderbak[1];
   283 	int veh;
   283 	int veh;
   284 	uint tile;
       
   285 
   284 
   286 	// wait until the vehicle reaches the depot.
   285 	// wait until the vehicle reaches the depot.
   287 	if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
   286 	if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
   288 		AiHandleGotoDepot(p, CMD_TRAIN_GOTO_DEPOT);
   287 		AiHandleGotoDepot(p, CMD_TRAIN_GOTO_DEPOT);
   289 		return;
   288 		return;
   290 	}
   289 	}
   291 
   290 
   292 	veh = AiChooseTrainToReplaceWith(p, v);
   291 	veh = AiChooseTrainToReplaceWith(p, v);
   293 	if (veh != -1) {
   292 	if (veh != -1) {
       
   293 		TileIndex tile;
       
   294 
   294 		BackupVehicleOrders(v, orderbak);
   295 		BackupVehicleOrders(v, orderbak);
   295 		tile = v->tile;
   296 		tile = v->tile;
   296 
   297 
   297 		if (!CmdFailed(DoCommandByTile(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON)) &&
   298 		if (!CmdFailed(DoCommandByTile(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON)) &&
   298 			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE)) ) {
   299 			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE)) ) {
   308 static void AiHandleReplaceRoadVeh(Player *p)
   309 static void AiHandleReplaceRoadVeh(Player *p)
   309 {
   310 {
   310 	Vehicle *v = p->ai.cur_veh;
   311 	Vehicle *v = p->ai.cur_veh;
   311 	BackuppedOrders orderbak[1];
   312 	BackuppedOrders orderbak[1];
   312 	int veh;
   313 	int veh;
   313 	uint tile;
       
   314 
   314 
   315 	if (!IsTileDepotType(v->tile, TRANSPORT_ROAD) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) {
   315 	if (!IsTileDepotType(v->tile, TRANSPORT_ROAD) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) {
   316 		AiHandleGotoDepot(p, CMD_SEND_ROADVEH_TO_DEPOT);
   316 		AiHandleGotoDepot(p, CMD_SEND_ROADVEH_TO_DEPOT);
   317 		return;
   317 		return;
   318 	}
   318 	}
   319 
   319 
   320 	veh = AiChooseRoadVehToReplaceWith(p, v);
   320 	veh = AiChooseRoadVehToReplaceWith(p, v);
   321 	if (veh != -1) {
   321 	if (veh != -1) {
       
   322 		TileIndex tile;
       
   323 
   322 		BackupVehicleOrders(v, orderbak);
   324 		BackupVehicleOrders(v, orderbak);
   323 		tile = v->tile;
   325 		tile = v->tile;
   324 
   326 
   325 		if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH)) &&
   327 		if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH)) &&
   326 			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH)) ) {
   328 			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH)) ) {
   336 static void AiHandleReplaceAircraft(Player *p)
   338 static void AiHandleReplaceAircraft(Player *p)
   337 {
   339 {
   338 	Vehicle *v = p->ai.cur_veh;
   340 	Vehicle *v = p->ai.cur_veh;
   339 	int veh;
   341 	int veh;
   340 	BackuppedOrders orderbak[1];
   342 	BackuppedOrders orderbak[1];
   341 	uint tile;
       
   342 
   343 
   343 	if (!IsAircraftHangarTile(v->tile) && !(v->vehstatus&VS_STOPPED)) {
   344 	if (!IsAircraftHangarTile(v->tile) && !(v->vehstatus&VS_STOPPED)) {
   344 		AiHandleGotoDepot(p, CMD_SEND_AIRCRAFT_TO_HANGAR);
   345 		AiHandleGotoDepot(p, CMD_SEND_AIRCRAFT_TO_HANGAR);
   345 		return;
   346 		return;
   346 	}
   347 	}
   347 
   348 
   348 	veh = AiChooseAircraftToReplaceWith(p, v);
   349 	veh = AiChooseAircraftToReplaceWith(p, v);
   349 	if (veh != -1) {
   350 	if (veh != -1) {
       
   351 		TileIndex tile;
       
   352 
   350 		BackupVehicleOrders(v, orderbak);
   353 		BackupVehicleOrders(v, orderbak);
   351 		tile = v->tile;
   354 		tile = v->tile;
   352 
   355 
   353 		if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT)) &&
   356 		if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT)) &&
   354 			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT)) ) {
   357 			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT)) ) {
  1530 
  1533 
  1531 static bool AiCheckTrackResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
  1534 static bool AiCheckTrackResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
  1532 {
  1535 {
  1533 	uint values[NUM_CARGO];
  1536 	uint values[NUM_CARGO];
  1534 	int w,h;
  1537 	int w,h;
  1535 	uint tile2;
       
  1536 	int rad;
  1538 	int rad;
  1537 
  1539 
  1538 	for(;p->mode != 4;p++) if (p->mode == 1) {
  1540 	for(;p->mode != 4;p++) if (p->mode == 1) {
  1539 		tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
  1541 		TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
  1540 
  1542 
  1541 		w = ((p->attr>>1) & 7);
  1543 		w = ((p->attr>>1) & 7);
  1542 		h = ((p->attr>>4) & 7);
  1544 		h = ((p->attr>>4) & 7);
  1543 		if (p->attr&1) intswap(w, h);
  1545 		if (p->attr&1) intswap(w, h);
  1544 
  1546 
  1831 	TileIndex tile2;
  1833 	TileIndex tile2;
  1832 	int count;
  1834 	int count;
  1833 	bool flag;
  1835 	bool flag;
  1834 } AiRailPathFindData;
  1836 } AiRailPathFindData;
  1835 
  1837 
  1836 static bool AiEnumFollowTrack(uint tile, AiRailPathFindData *a, int track, uint length, byte *state)
  1838 static bool AiEnumFollowTrack(TileIndex tile, AiRailPathFindData *a, int track, uint length, byte *state)
  1837 {
  1839 {
  1838 	if (a->flag)
  1840 	if (a->flag)
  1839 		return true;
  1841 		return true;
  1840 
  1842 
  1841 	if (length > 20 || tile == a->tile) {
  1843 	if (length > 20 || tile == a->tile) {
  2173 static bool AiRemoveTileAndGoForward(Player *p)
  2175 static bool AiRemoveTileAndGoForward(Player *p)
  2174 {
  2176 {
  2175 	byte b;
  2177 	byte b;
  2176 	int bit;
  2178 	int bit;
  2177 	const byte *ptr;
  2179 	const byte *ptr;
  2178 	uint tile = p->ai.cur_tile_a;
  2180 	TileIndex tile = p->ai.cur_tile_a;
  2179 	int offs;
  2181 	int offs;
  2180 	TileIndex tilenew;
  2182 	TileIndex tilenew;
  2181 
  2183 
  2182 	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
  2184 	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
  2183 		if (!(_map5[tile] & 0x80)) {
  2185 		if (!(_map5[tile] & 0x80)) {
  2492 
  2494 
  2493 	for(;;p++) {
  2495 	for(;;p++) {
  2494 		if (p->mode == 4) {
  2496 		if (p->mode == 4) {
  2495 			return true;
  2497 			return true;
  2496 		} else if (p->mode == 1) {
  2498 		} else if (p->mode == 1) {
  2497 			uint tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
  2499 			TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
       
  2500 
  2498 			if (cargo & 0x80) {
  2501 			if (cargo & 0x80) {
  2499 				GetProductionAroundTiles(values, tile2, 1, 1, rad);
  2502 				GetProductionAroundTiles(values, tile2, 1, 1, rad);
  2500 				return values[cargo & 0x7F] != 0;
  2503 				return values[cargo & 0x7F] != 0;
  2501 			} else {
  2504 			} else {
  2502 				GetAcceptanceAroundTiles(values, tile2, 1, 1, rad);
  2505 				GetAcceptanceAroundTiles(values, tile2, 1, 1, rad);
  2763 
  2766 
  2764 	return better;
  2767 	return better;
  2765 }
  2768 }
  2766 
  2769 
  2767 
  2770 
  2768 static bool AiEnumFollowRoad(uint tile, AiRoadEnum *a, int track, uint length, byte *state)
  2771 static bool AiEnumFollowRoad(TileIndex tile, AiRoadEnum *a, int track, uint length, byte *state)
  2769 {
  2772 {
  2770 	uint dist = DistanceManhattan(tile, a->dest);
  2773 	uint dist = DistanceManhattan(tile, a->dest);
  2771 	uint tile2;
       
  2772 
  2774 
  2773 	if (dist <= a->best_dist) {
  2775 	if (dist <= a->best_dist) {
  2774 		tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track]));
  2776 		TileIndex tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track]));
       
  2777 
  2775 		if (IsTileType(tile2, MP_STREET) &&
  2778 		if (IsTileType(tile2, MP_STREET) &&
  2776 				(_map5[tile2]&0xF0) == 0) {
  2779 				(_map5[tile2]&0xF0) == 0) {
  2777 			a->best_dist = dist;
  2780 			a->best_dist = dist;
  2778 			a->best_tile = tile;
  2781 			a->best_tile = tile;
  2779 			a->best_track = track;
  2782 			a->best_track = track;
  2791 };
  2794 };
  2792 
  2795 
  2793 static bool AiCheckRoadFinished(Player *p)
  2796 static bool AiCheckRoadFinished(Player *p)
  2794 {
  2797 {
  2795 	AiRoadEnum are;
  2798 	AiRoadEnum are;
  2796 	uint tile;
  2799 	TileIndex tile;
  2797 	int dir = p->ai.cur_dir_a;
  2800 	int dir = p->ai.cur_dir_a;
  2798 	uint32 bits;
  2801 	uint32 bits;
  2799 	int i;
  2802 	int i;
  2800 
  2803 
  2801 	are.dest = p->ai.cur_tile_b;
  2804 	are.dest = p->ai.cur_tile_b;
  2822 	p->ai.cur_dir_a = _dir_by_track[are.best_track];
  2825 	p->ai.cur_dir_a = _dir_by_track[are.best_track];
  2823 	return false;
  2826 	return false;
  2824 }
  2827 }
  2825 
  2828 
  2826 
  2829 
  2827 static bool AiBuildRoadHelper(uint tile, int flags, int type)
  2830 static bool AiBuildRoadHelper(TileIndex tile, int flags, int type)
  2828 {
  2831 {
  2829 	static const byte _road_bits[] = {
  2832 	static const byte _road_bits[] = {
  2830 		8+2,
  2833 		8+2,
  2831 		1+4,
  2834 		1+4,
  2832 		1+8,
  2835 		1+8,
  2960 
  2963 
  2961 static void AiBuildRoadConstruct(Player *p)
  2964 static void AiBuildRoadConstruct(Player *p)
  2962 {
  2965 {
  2963 	AiRoadFinder arf;
  2966 	AiRoadFinder arf;
  2964 	int i;
  2967 	int i;
  2965 	uint tile;
  2968 	TileIndex tile;
  2966 
  2969 
  2967 	// Reached destination?
  2970 	// Reached destination?
  2968 	if (AiCheckRoadFinished(p)) {
  2971 	if (AiCheckRoadFinished(p)) {
  2969 		p->ai.state_mode = 255;
  2972 		p->ai.state_mode = 255;
  2970 		return;
  2973 		return;
  3162 }
  3165 }
  3163 
  3166 
  3164 static void AiStateBuildRoadVehicles(Player *p)
  3167 static void AiStateBuildRoadVehicles(Player *p)
  3165 {
  3168 {
  3166 	const AiDefaultBlockData *ptr;
  3169 	const AiDefaultBlockData *ptr;
  3167 	uint tile,loco_id;
  3170 	TileIndex tile;
       
  3171 	uint loco_id;
  3168 	int veh, i;
  3172 	int veh, i;
  3169 
  3173 
  3170 	ptr = _road_default_block_data[p->ai.src.cur_building_rule]->data;
  3174 	ptr = _road_default_block_data[p->ai.src.cur_building_rule]->data;
  3171 	for(;ptr->mode != 0;ptr++) {}
  3175 	for(;ptr->mode != 0;ptr++) {}
  3172 	tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
  3176 	tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
  3234 	p->ai.state = AIS_0;
  3238 	p->ai.state = AIS_0;
  3235 }
  3239 }
  3236 
  3240 
  3237 static bool AiCheckIfHangar(Station *st)
  3241 static bool AiCheckIfHangar(Station *st)
  3238 {
  3242 {
  3239 	uint tile = st->airport_tile;
  3243 	TileIndex tile = st->airport_tile;
       
  3244 
  3240 	// HANGAR of airports
  3245 	// HANGAR of airports
  3241 	// 0x20 - hangar large airport (32)
  3246 	// 0x20 - hangar large airport (32)
  3242 	// 0x41 - hangar small airport (65)
  3247 	// 0x41 - hangar small airport (65)
  3243 	return (_map5[tile] == 32 || _map5[tile] == 65);
  3248 	return (_map5[tile] == 32 || _map5[tile] == 65);
  3244 }
  3249 }
  3346 
  3351 
  3347 static bool AiCheckAirportResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
  3352 static bool AiCheckAirportResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
  3348 {
  3353 {
  3349 	uint values[NUM_CARGO];
  3354 	uint values[NUM_CARGO];
  3350 	int w,h;
  3355 	int w,h;
  3351 	uint tile2;
       
  3352 	int rad;
  3356 	int rad;
  3353 
  3357 
  3354 	if (_patches.modified_catchment) {
  3358 	if (_patches.modified_catchment) {
  3355 		rad = CA_AIR_LARGE;		//I Have NFI what airport the
  3359 		rad = CA_AIR_LARGE;		//I Have NFI what airport the
  3356 	} else {				//AI is going to build here
  3360 	} else {				//AI is going to build here
  3357 		rad = 4;
  3361 		rad = 4;
  3358 	}
  3362 	}
  3359 
  3363 
  3360 	for(;p->mode==0;p++) {
  3364 	for(;p->mode==0;p++) {
  3361 		tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
  3365 		TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
       
  3366 
  3362 		w = _airport_size_x[p->attr];
  3367 		w = _airport_size_x[p->attr];
  3363 		h = _airport_size_y[p->attr];
  3368 		h = _airport_size_y[p->attr];
  3364 		if (cargo & 0x80) {
  3369 		if (cargo & 0x80) {
  3365 			GetProductionAroundTiles(values, tile2, w, h, rad);
  3370 			GetProductionAroundTiles(values, tile2, w, h, rad);
  3366 			return values[cargo & 0x7F] != 0;
  3371 			return values[cargo & 0x7F] != 0;
  3479 }
  3484 }
  3480 
  3485 
  3481 static void AiStateBuildAircraftVehicles(Player *p)
  3486 static void AiStateBuildAircraftVehicles(Player *p)
  3482 {
  3487 {
  3483 	const AiDefaultBlockData *ptr;
  3488 	const AiDefaultBlockData *ptr;
  3484 	uint tile;
  3489 	TileIndex tile;
  3485 	int veh;
  3490 	int veh;
  3486 	int i;
  3491 	int i;
  3487 	uint loco_id;
  3492 	uint loco_id;
  3488 
  3493 
  3489 	ptr = _airport_default_block_data[p->ai.src.cur_building_rule];
  3494 	ptr = _airport_default_block_data[p->ai.src.cur_building_rule];