src/ai/default/default.cpp
changeset 8617 338141724037
parent 8616 fd862a55c47f
child 8755 c7afa0b1df35
equal deleted inserted replaced
8616:fd862a55c47f 8617:338141724037
  2195 	}
  2195 	}
  2196 
  2196 
  2197 	_players_ai[p->index].cur_tile_a += TileOffsByDiagDir(_players_ai[p->index].cur_dir_a);
  2197 	_players_ai[p->index].cur_tile_a += TileOffsByDiagDir(_players_ai[p->index].cur_dir_a);
  2198 
  2198 
  2199 	if (arf.best_ptr[0] & 0x80) {
  2199 	if (arf.best_ptr[0] & 0x80) {
  2200 		int i;
  2200 		TileIndex t1 = _players_ai[p->index].cur_tile_a;
  2201 		int32 bridge_len = GetTunnelBridgeLength(arf.bridge_end_tile, _players_ai[p->index].cur_tile_a);
  2201 		TileIndex t2 = arf.bridge_end_tile;
  2202 
  2202 
  2203 		/* Figure out which (rail)bridge type to build
  2203 		int32 bridge_len = GetTunnelBridgeLength(t1, t2);
  2204 		 * start with best bridge, then go down to worse and worse bridges
  2204 
  2205 		 * unnecessary to check for worst bridge (i=0), since AI will always build
  2205 		DiagDirection dir = (TileX(t1) == TileX(t2) ? DIAGDIR_SE : DIAGDIR_SW);
  2206 		 * that.
  2206 		Track track = AxisToTrack(DiagDirToAxis(dir));
  2207 		 */
  2207 
  2208 		for (i = MAX_BRIDGES - 1; i != 0; i--) {
  2208 		if (t2 < t1) dir = ReverseDiagDir(dir);
  2209 			if (CheckBridge_Stuff(i, bridge_len)) {
  2209 
  2210 				CommandCost cost = DoCommand(arf.bridge_end_tile, _players_ai[p->index].cur_tile_a, i | (_players_ai[p->index].railtype_to_use << 8), DC_AUTO, CMD_BUILD_BRIDGE);
  2210 		/* try to build a long rail instead of bridge... */
  2211 				if (CmdSucceeded(cost) && cost.GetCost() < (p->player_money >> 1) && cost.GetCost() < ((p->player_money + _economy.max_loan - p->current_loan) >> 5)) break;
  2211 		bool fail = false;
       
  2212 		CommandCost cost;
       
  2213 		TileIndex t = t1;
       
  2214 
       
  2215 		/* try to build one rail on each tile - can't use CMD_BUILD_RAILROAD_TRACK now, it can build one part of track without failing */
       
  2216 		do {
       
  2217 			cost = DoCommand(t, _players_ai[p->index].railtype_to_use, track, DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
       
  2218 			/* do not allow building over existing track */
       
  2219 			if (CmdFailed(cost) || IsTileType(t, MP_RAILWAY)) {
       
  2220 				fail = true;
       
  2221 				break;
  2212 			}
  2222 			}
  2213 		}
  2223 			t += TileOffsByDiagDir(dir);
  2214 
  2224 		} while (t != t2);
  2215 		// Build it
  2225 
  2216 		DoCommand(arf.bridge_end_tile, _players_ai[p->index].cur_tile_a, i | (_players_ai[p->index].railtype_to_use << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
  2226 		/* can we build long track? */
  2217 
  2227 		if (!fail) cost = DoCommand(t1, t2, _players_ai[p->index].railtype_to_use | (track << 4), DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_TRACK);
  2218 		_players_ai[p->index].cur_tile_a = arf.bridge_end_tile;
  2228 
       
  2229 		if (!fail && CmdSucceeded(cost) && cost.GetCost() <= p->player_money) {
       
  2230 			DoCommand(t1, t2, _players_ai[p->index].railtype_to_use | (track << 4), DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_BUILD_RAILROAD_TRACK);
       
  2231 		} else {
       
  2232 
       
  2233 			/* Figure out which (rail)bridge type to build
       
  2234 			 * start with best bridge, then go down to worse and worse bridges
       
  2235 			 * unnecessary to check for worst bridge (i=0), since AI will always build that. */
       
  2236 			int i;
       
  2237 			for (i = MAX_BRIDGES - 1; i != 0; i--) {
       
  2238 				if (CheckBridge_Stuff(i, bridge_len)) {
       
  2239 					CommandCost cost = DoCommand(t1, t2, i | (_players_ai[p->index].railtype_to_use << 8), DC_AUTO, CMD_BUILD_BRIDGE);
       
  2240 					if (CmdSucceeded(cost) && cost.GetCost() < (p->player_money >> 1) && cost.GetCost() < ((p->player_money + _economy.max_loan - p->current_loan) >> 5)) break;
       
  2241 				}
       
  2242 			}
       
  2243 
       
  2244 			/* Build it */
       
  2245 			DoCommand(t1, t2, i | (_players_ai[p->index].railtype_to_use << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
       
  2246 		}
       
  2247 
       
  2248 		_players_ai[p->index].cur_tile_a = t2;
  2219 		_players_ai[p->index].state_counter = 0;
  2249 		_players_ai[p->index].state_counter = 0;
  2220 	} else if (arf.best_ptr[0] & 0x40) {
  2250 	} else if (arf.best_ptr[0] & 0x40) {
  2221 		// tunnel
  2251 		// tunnel
  2222 		DoCommand(_players_ai[p->index].cur_tile_a, _players_ai[p->index].railtype_to_use, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL);
  2252 		DoCommand(_players_ai[p->index].cur_tile_a, _players_ai[p->index].railtype_to_use, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL);
  2223 		_players_ai[p->index].cur_tile_a = _build_tunnel_endtile;
  2253 		_players_ai[p->index].cur_tile_a = _build_tunnel_endtile;
  3083 	}
  3113 	}
  3084 
  3114 
  3085 	tile = TILE_MASK(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(_players_ai[p->index].cur_dir_a));
  3115 	tile = TILE_MASK(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(_players_ai[p->index].cur_dir_a));
  3086 
  3116 
  3087 	if (arf.best_ptr[0] & 0x80) {
  3117 	if (arf.best_ptr[0] & 0x80) {
  3088 		int i;
  3118 		TileIndex t1 = tile;
  3089 		int32 bridge_len;
  3119 		TileIndex t2 = arf.bridge_end_tile;
  3090 		_players_ai[p->index].cur_tile_a = arf.bridge_end_tile;
  3120 
  3091 		bridge_len = GetTunnelBridgeLength(tile, _players_ai[p->index].cur_tile_a); // tile
  3121 		int32 bridge_len = GetTunnelBridgeLength(t1, t2);
  3092 
  3122 
  3093 		/* Figure out what (road)bridge type to build
  3123 		Axis axis = (TileX(t1) == TileX(t2) ? AXIS_Y : AXIS_X);
  3094 		 * start with best bridge, then go down to worse and worse bridges
  3124 
  3095 		 * unnecessary to check for worse bridge (i=0), since AI will always build that.
  3125 		/* try to build a long road instead of bridge - CMD_BUILD_LONG_ROAD has to fail if it couldn't build at least one piece! */
  3096 		 */
  3126  		CommandCost cost = DoCommand(t2, t1, (t2 < t1 ? 1 : 2) | (axis << 2) | (ROADTYPE_ROAD << 3), DC_AUTO | DC_NO_WATER, CMD_BUILD_LONG_ROAD);
  3097 		for (i = MAX_BRIDGES - 1; i != 0; i--) {
  3127 
  3098 			if (CheckBridge_Stuff(i, bridge_len)) {
  3128 		if (CmdSucceeded(cost) && cost.GetCost() <= p->player_money) {
  3099 				CommandCost cost = DoCommand(tile, _players_ai[p->index].cur_tile_a, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE);
  3129 			DoCommand(t2, t1, (t2 < t1 ? 1 : 2) | (axis << 2) | (ROADTYPE_ROAD << 3), DC_AUTO | DC_EXEC | DC_NO_WATER, CMD_BUILD_LONG_ROAD);
  3100 				if (CmdSucceeded(cost) && cost.GetCost() < (p->player_money >> 1) && cost.GetCost() < ((p->player_money + _economy.max_loan - p->current_loan) >> 5)) break;
  3130 		} else {
       
  3131 			int i;
       
  3132 
       
  3133 			/* Figure out what (road)bridge type to build
       
  3134 			 * start with best bridge, then go down to worse and worse bridges
       
  3135 			 * unnecessary to check for worse bridge (i=0), since AI will always build that */
       
  3136 			for (i = MAX_BRIDGES - 1; i != 0; i--) {
       
  3137 				if (CheckBridge_Stuff(i, bridge_len)) {
       
  3138 					CommandCost cost = DoCommand(t1, t2, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE);
       
  3139 					if (CmdSucceeded(cost) && cost.GetCost() < (p->player_money >> 1) && cost.GetCost() < ((p->player_money + _economy.max_loan - p->current_loan) >> 5)) break;
       
  3140 				}
  3101 			}
  3141 			}
  3102 		}
  3142 
  3103 
  3143 			/* Build it */
  3104 		// Build it
  3144 			DoCommand(t1, t2, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
  3105 		DoCommand(tile, _players_ai[p->index].cur_tile_a, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO | DC_EXEC, CMD_BUILD_BRIDGE);
  3145 		}
  3106 
  3146 
       
  3147 		_players_ai[p->index].cur_tile_a = t2;
  3107 		_players_ai[p->index].state_counter = 0;
  3148 		_players_ai[p->index].state_counter = 0;
  3108 	} else if (arf.best_ptr[0] & 0x40) {
  3149 	} else if (arf.best_ptr[0] & 0x40) {
  3109 		// tunnel
  3150 		// tunnel
  3110 		DoCommand(tile, 0x200, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL);
  3151 		DoCommand(tile, 0x200, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL);
  3111 		_players_ai[p->index].cur_tile_a = _build_tunnel_endtile;
  3152 		_players_ai[p->index].cur_tile_a = _build_tunnel_endtile;