ai.c
changeset 2049 ad0d49c916d4
parent 1981 de031d2aed47
equal deleted inserted replaced
2048:430522aa954c 2049:ad0d49c916d4
  2180 	TileIndex tile = p->ai.cur_tile_a;
  2180 	TileIndex tile = p->ai.cur_tile_a;
  2181 	int offs;
  2181 	int offs;
  2182 	TileIndex tilenew;
  2182 	TileIndex tilenew;
  2183 
  2183 
  2184 	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
  2184 	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
  2185 		if (!(_map5[tile] & 0x80)) {
  2185 		if (!(_m[tile].m5 & 0x80)) {
  2186 			// Clear the tunnel and continue at the other side of it.
  2186 			// Clear the tunnel and continue at the other side of it.
  2187 			if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)) )
  2187 			if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)) )
  2188 				return false;
  2188 				return false;
  2189 			p->ai.cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDir(p->ai.cur_dir_a));
  2189 			p->ai.cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDir(p->ai.cur_dir_a));
  2190 			return true;
  2190 			return true;
  2191 		}
  2191 		}
  2192 
  2192 
  2193 		if (!(_map5[tile] & 0x40)) {
  2193 		if (!(_m[tile].m5 & 0x40)) {
  2194 
  2194 
  2195 			// Check if the bridge points in the right direction.
  2195 			// Check if the bridge points in the right direction.
  2196 			// This is not really needed the first place AiRemoveTileAndGoForward is called.
  2196 			// This is not really needed the first place AiRemoveTileAndGoForward is called.
  2197 			if ((_map5[tile]&1) != (p->ai.cur_dir_a&1))
  2197 			if ((_m[tile].m5&1) != (p->ai.cur_dir_a&1))
  2198 				return false;
  2198 				return false;
  2199 
  2199 
  2200 			// Find other side of bridge.
  2200 			// Find other side of bridge.
  2201 			offs = TileOffsByDir(p->ai.cur_dir_a);
  2201 			offs = TileOffsByDir(p->ai.cur_dir_a);
  2202 			do {
  2202 			do {
  2203 				tile = TILE_MASK(tile - offs);
  2203 				tile = TILE_MASK(tile - offs);
  2204 			} while (_map5[tile] & 0x40);
  2204 			} while (_m[tile].m5 & 0x40);
  2205 
  2205 
  2206 			tilenew = TILE_MASK(tile - offs);
  2206 			tilenew = TILE_MASK(tile - offs);
  2207 			// And clear the bridge.
  2207 			// And clear the bridge.
  2208 			if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)) )
  2208 			if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)) )
  2209 				return false;
  2209 				return false;
  2220 	// Convert into a bit position that CMD_REMOVE_SINGLE_RAIL expects.
  2220 	// Convert into a bit position that CMD_REMOVE_SINGLE_RAIL expects.
  2221 	bit = FindFirstBit(b);
  2221 	bit = FindFirstBit(b);
  2222 
  2222 
  2223 	// Then remove and signals if there are any.
  2223 	// Then remove and signals if there are any.
  2224 	if (IsTileType(tile, MP_RAILWAY) &&
  2224 	if (IsTileType(tile, MP_RAILWAY) &&
  2225 			(_map5[tile]&0xC0) == 0x40) {
  2225 			(_m[tile].m5&0xC0) == 0x40) {
  2226 		DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_REMOVE_SIGNALS);
  2226 		DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_REMOVE_SIGNALS);
  2227 	}
  2227 	}
  2228 
  2228 
  2229 	// And also remove the rail.
  2229 	// And also remove the rail.
  2230 	if (CmdFailed(DoCommandByTile(tile, 0, bit, DC_EXEC, CMD_REMOVE_SINGLE_RAIL)) )
  2230 	if (CmdFailed(DoCommandByTile(tile, 0, bit, DC_EXEC, CMD_REMOVE_SINGLE_RAIL)) )
  2358 
  2358 
  2359 static int AiGetStationIdByDef(TileIndex tile, int id)
  2359 static int AiGetStationIdByDef(TileIndex tile, int id)
  2360 {
  2360 {
  2361 	const AiDefaultBlockData *p = _default_rail_track_data[id]->data;
  2361 	const AiDefaultBlockData *p = _default_rail_track_data[id]->data;
  2362 	while (p->mode != 1) p++;
  2362 	while (p->mode != 1) p++;
  2363 	return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))];
  2363 	return _m[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))].m2;
  2364 }
  2364 }
  2365 
  2365 
  2366 static void AiStateBuildRailVeh(Player *p)
  2366 static void AiStateBuildRailVeh(Player *p)
  2367 {
  2367 {
  2368 	const AiDefaultBlockData *ptr;
  2368 	const AiDefaultBlockData *ptr;
  2544 
  2544 
  2545 		_cleared_town = NULL;
  2545 		_cleared_town = NULL;
  2546 
  2546 
  2547 		if (p->mode == 2) {
  2547 		if (p->mode == 2) {
  2548 			if (IsTileType(c, MP_STREET) &&
  2548 			if (IsTileType(c, MP_STREET) &&
  2549 					(_map5[c]&0xF0)==0 &&
  2549 					(_m[c].m5&0xF0)==0 &&
  2550 					(_map5[c]&p->attr)!=0) {
  2550 					(_m[c].m5&p->attr)!=0) {
  2551 				roadflag |= 2;
  2551 				roadflag |= 2;
  2552 
  2552 
  2553 				// all bits are already built?
  2553 				// all bits are already built?
  2554 				if ((_map5[c]&p->attr)==p->attr)
  2554 				if ((_m[c].m5&p->attr)==p->attr)
  2555 					continue;
  2555 					continue;
  2556 			}
  2556 			}
  2557 
  2557 
  2558 			ret = DoCommandByTile(c, p->attr, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
  2558 			ret = DoCommandByTile(c, p->attr, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
  2559 			if (CmdFailed(ret)) return CMD_ERROR;
  2559 			if (CmdFailed(ret)) return CMD_ERROR;
  2590 				continue;
  2590 				continue;
  2591 
  2591 
  2592 			if (GetTileSlope(c, NULL) != 0)
  2592 			if (GetTileSlope(c, NULL) != 0)
  2593 				return CMD_ERROR;
  2593 				return CMD_ERROR;
  2594 
  2594 
  2595 			if (!(IsTileType(c, MP_STREET) && (_map5[c] & 0xF0) == 0)) {
  2595 			if (!(IsTileType(c, MP_STREET) && (_m[c].m5 & 0xF0) == 0)) {
  2596 				ret = DoCommandByTile(c, 0, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_LANDSCAPE_CLEAR);
  2596 				ret = DoCommandByTile(c, 0, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_LANDSCAPE_CLEAR);
  2597 				if (CmdFailed(ret)) return CMD_ERROR;
  2597 				if (CmdFailed(ret)) return CMD_ERROR;
  2598 			}
  2598 			}
  2599 
  2599 
  2600 		}
  2600 		}
  2774 
  2774 
  2775 	if (dist <= a->best_dist) {
  2775 	if (dist <= a->best_dist) {
  2776 		TileIndex tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track]));
  2776 		TileIndex tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track]));
  2777 
  2777 
  2778 		if (IsTileType(tile2, MP_STREET) &&
  2778 		if (IsTileType(tile2, MP_STREET) &&
  2779 				(_map5[tile2]&0xF0) == 0) {
  2779 				(_m[tile2].m5&0xF0) == 0) {
  2780 			a->best_dist = dist;
  2780 			a->best_dist = dist;
  2781 			a->best_tile = tile;
  2781 			a->best_tile = tile;
  2782 			a->best_track = track;
  2782 			a->best_track = track;
  2783 		}
  2783 		}
  2784 	}
  2784 	}
  3159 
  3159 
  3160 static int AiGetStationIdFromRoadBlock(TileIndex tile, int id)
  3160 static int AiGetStationIdFromRoadBlock(TileIndex tile, int id)
  3161 {
  3161 {
  3162 	const AiDefaultBlockData *p = _road_default_block_data[id]->data;
  3162 	const AiDefaultBlockData *p = _road_default_block_data[id]->data;
  3163 	while (p->mode != 1) p++;
  3163 	while (p->mode != 1) p++;
  3164 	return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))];
  3164 	return _m[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))].m2;
  3165 }
  3165 }
  3166 
  3166 
  3167 static void AiStateBuildRoadVehicles(Player *p)
  3167 static void AiStateBuildRoadVehicles(Player *p)
  3168 {
  3168 {
  3169 	const AiDefaultBlockData *ptr;
  3169 	const AiDefaultBlockData *ptr;
  3243 	TileIndex tile = st->airport_tile;
  3243 	TileIndex tile = st->airport_tile;
  3244 
  3244 
  3245 	// HANGAR of airports
  3245 	// HANGAR of airports
  3246 	// 0x20 - hangar large airport (32)
  3246 	// 0x20 - hangar large airport (32)
  3247 	// 0x41 - hangar small airport (65)
  3247 	// 0x41 - hangar small airport (65)
  3248 	return (_map5[tile] == 32 || _map5[tile] == 65);
  3248 	return (_m[tile].m5 == 32 || _m[tile].m5 == 65);
  3249 }
  3249 }
  3250 
  3250 
  3251 static void AiStateAirportStuff(Player *p)
  3251 static void AiStateAirportStuff(Player *p)
  3252 {
  3252 {
  3253 	Station *st;
  3253 	Station *st;
  3429 			if (!IsTileType(aib->use_tile, MP_STREET) &&
  3429 			if (!IsTileType(aib->use_tile, MP_STREET) &&
  3430 					!IsTileType(aib->use_tile, MP_RAILWAY) &&
  3430 					!IsTileType(aib->use_tile, MP_RAILWAY) &&
  3431 					!IsTileType(aib->use_tile, MP_STATION)
  3431 					!IsTileType(aib->use_tile, MP_STATION)
  3432 					) {
  3432 					) {
  3433 
  3433 
  3434 				_map_type_and_height[aib->use_tile] = 0xa1;
  3434 				_m[aib->use_tile].type_height = 0xa1;
  3435 				_map5[aib->use_tile] = 0x80;
  3435 				_m[aib->use_tile].m5 = 0x80;
  3436 				MarkTileDirtyByTile(aib->use_tile);
  3436 				MarkTileDirtyByTile(aib->use_tile);
  3437 			}
  3437 			}
  3438 #endif
  3438 #endif
  3439 //			SetRedErrorSquare(aib->use_tile);
  3439 //			SetRedErrorSquare(aib->use_tile);
  3440 
  3440 
  3478 
  3478 
  3479 static int AiGetStationIdFromAircraftBlock(TileIndex tile, int id)
  3479 static int AiGetStationIdFromAircraftBlock(TileIndex tile, int id)
  3480 {
  3480 {
  3481 	const AiDefaultBlockData *p = _airport_default_block_data[id];
  3481 	const AiDefaultBlockData *p = _airport_default_block_data[id];
  3482 	while (p->mode != 1) p++;
  3482 	while (p->mode != 1) p++;
  3483 	return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))];
  3483 	return _m[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))].m2;
  3484 }
  3484 }
  3485 
  3485 
  3486 static void AiStateBuildAircraftVehicles(Player *p)
  3486 static void AiStateBuildAircraftVehicles(Player *p)
  3487 {
  3487 {
  3488 	const AiDefaultBlockData *ptr;
  3488 	const AiDefaultBlockData *ptr;
  3644 	byte m5;
  3644 	byte m5;
  3645 
  3645 
  3646 	if (IsTileType(tile, MP_RAILWAY)) {
  3646 	if (IsTileType(tile, MP_RAILWAY)) {
  3647 		if (!IsTileOwner(tile, _current_player)) return;
  3647 		if (!IsTileOwner(tile, _current_player)) return;
  3648 
  3648 
  3649 		m5 = _map5[tile];
  3649 		m5 = _m[tile].m5;
  3650 		if ((m5&~0x3) != 0xC0) {
  3650 		if ((m5&~0x3) != 0xC0) {
  3651 is_rail_crossing:;
  3651 is_rail_crossing:;
  3652 			m5 = GetRailTrackStatus(tile);
  3652 			m5 = GetRailTrackStatus(tile);
  3653 
  3653 
  3654 			if (m5 == 0xC || m5 == 0x30)
  3654 			if (m5 == 0xC || m5 == 0x30)
  3706 		if (!IsTileOwner(tile, _current_player)) return;
  3706 		if (!IsTileOwner(tile, _current_player)) return;
  3707 
  3707 
  3708 		if (IsLevelCrossing(tile))
  3708 		if (IsLevelCrossing(tile))
  3709 			goto is_rail_crossing;
  3709 			goto is_rail_crossing;
  3710 
  3710 
  3711 		if ( (_map5[tile]&0xF0) == 0x20) {
  3711 		if ( (_m[tile].m5&0xF0) == 0x20) {
  3712 			int dir;
  3712 			int dir;
  3713 
  3713 
  3714 			// Check if there are any stations around.
  3714 			// Check if there are any stations around.
  3715 			if (IsTileType(tile + TileDiffXY(-1, 0), MP_STATION) &&
  3715 			if (IsTileType(tile + TileDiffXY(-1, 0), MP_STATION) &&
  3716 					IsTileOwner(tile + TileDiffXY(-1, 0), _current_player))
  3716 					IsTileOwner(tile + TileDiffXY(-1, 0), _current_player))
  3726 
  3726 
  3727 			if (IsTileType(tile + TileDiffXY(0, 1), MP_STATION) &&
  3727 			if (IsTileType(tile + TileDiffXY(0, 1), MP_STATION) &&
  3728 					IsTileOwner(tile + TileDiffXY(0, 1), _current_player))
  3728 					IsTileOwner(tile + TileDiffXY(0, 1), _current_player))
  3729 						return;
  3729 						return;
  3730 
  3730 
  3731 			dir = _map5[tile] & 3;
  3731 			dir = _m[tile].m5 & 3;
  3732 
  3732 
  3733 			DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
  3733 			DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
  3734 			DoCommandByTile(
  3734 			DoCommandByTile(
  3735 				TILE_MASK(tile + TileOffsByDir(dir)),
  3735 				TILE_MASK(tile + TileOffsByDir(dir)),
  3736 				8 >> (dir ^ 2),
  3736 				8 >> (dir ^ 2),
  3739 				CMD_REMOVE_ROAD);
  3739 				CMD_REMOVE_ROAD);
  3740 		}
  3740 		}
  3741 	} else if (IsTileType(tile, MP_TUNNELBRIDGE)) {
  3741 	} else if (IsTileType(tile, MP_TUNNELBRIDGE)) {
  3742 		byte b;
  3742 		byte b;
  3743 
  3743 
  3744 		if (!IsTileOwner(tile, _current_player) || (_map5[tile] & 0xC6) != 0x80)
  3744 		if (!IsTileOwner(tile, _current_player) || (_m[tile].m5 & 0xC6) != 0x80)
  3745 			return;
  3745 			return;
  3746 
  3746 
  3747 		m5 = 0;
  3747 		m5 = 0;
  3748 
  3748 
  3749 		b = _map5[tile] & 0x21;
  3749 		b = _m[tile].m5 & 0x21;
  3750 		if (b == 0) goto pos_0;
  3750 		if (b == 0) goto pos_0;
  3751 		if (b == 1) goto pos_3;
  3751 		if (b == 1) goto pos_3;
  3752 		if (b == 0x20) goto pos_2;
  3752 		if (b == 0x20) goto pos_2;
  3753 		goto pos_1;
  3753 		goto pos_1;
  3754 	}
  3754 	}