src/ai/default/default.cpp
branchgamebalance
changeset 9895 7bd07f43b0e3
parent 6432 8fb778a7f2d7
child 9903 dc85aaa556ae
equal deleted inserted replaced
9894:70d78ac95d6c 9895:7bd07f43b0e3
    91 	index = (p->ai.cur_veh == NULL) ? 0 : p->ai.cur_veh->index + 1;
    91 	index = (p->ai.cur_veh == NULL) ? 0 : p->ai.cur_veh->index + 1;
    92 
    92 
    93 	FOR_ALL_VEHICLES_FROM(v, index) {
    93 	FOR_ALL_VEHICLES_FROM(v, index) {
    94 		if (v->owner != _current_player) continue;
    94 		if (v->owner != _current_player) continue;
    95 
    95 
    96 		if ((v->type == VEH_Train && v->subtype == 0) ||
    96 		if ((v->type == VEH_TRAIN && v->subtype == 0) ||
    97 				v->type == VEH_Road ||
    97 				v->type == VEH_ROAD ||
    98 				(v->type == VEH_Aircraft && IsNormalAircraft(v)) ||
    98 				(v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) ||
    99 				v->type == VEH_Ship) {
    99 				v->type == VEH_SHIP) {
   100 			/* replace engine? */
   100 			/* replace engine? */
   101 			if (v->type == VEH_Train && v->engine_type < 3 &&
   101 			if (v->type == VEH_TRAIN && v->engine_type < 3 &&
   102 					(_price.build_railvehicle >> 3) < p->player_money) {
   102 					(_price.build_railvehicle >> 3) < p->player_money) {
   103 				p->ai.state = AIS_VEH_CHECK_REPLACE_VEHICLE;
   103 				p->ai.state = AIS_VEH_CHECK_REPLACE_VEHICLE;
   104 				p->ai.cur_veh = v;
   104 				p->ai.cur_veh = v;
   105 				return;
   105 				return;
   106 			}
   106 			}
   425 {
   425 {
   426 	const Vehicle* v = p->ai.cur_veh;
   426 	const Vehicle* v = p->ai.cur_veh;
   427 
   427 
   428 	if (!IsValidVehicle(v) ||
   428 	if (!IsValidVehicle(v) ||
   429 			v->owner != _current_player ||
   429 			v->owner != _current_player ||
   430 			v->type > VEH_Ship ||
   430 			v->type > VEH_SHIP ||
   431 			_veh_check_replace_proc[v->type - VEH_Train](p, v) == INVALID_ENGINE) {
   431 			_veh_check_replace_proc[v->type - VEH_TRAIN](p, v) == INVALID_ENGINE) {
   432 		p->ai.state = AIS_VEH_LOOP;
   432 		p->ai.state = AIS_VEH_LOOP;
   433 	} else {
   433 	} else {
   434 		p->ai.state_counter = 0;
   434 		p->ai.state_counter = 0;
   435 		p->ai.state = AIS_VEH_DO_REPLACE_VEHICLE;
   435 		p->ai.state = AIS_VEH_DO_REPLACE_VEHICLE;
   436 	}
   436 	}
   441 	const Vehicle* v = p->ai.cur_veh;
   441 	const Vehicle* v = p->ai.cur_veh;
   442 
   442 
   443 	p->ai.state = AIS_VEH_LOOP;
   443 	p->ai.state = AIS_VEH_LOOP;
   444 	// vehicle is not owned by the player anymore, something went very wrong.
   444 	// vehicle is not owned by the player anymore, something went very wrong.
   445 	if (!IsValidVehicle(v) || v->owner != _current_player) return;
   445 	if (!IsValidVehicle(v) || v->owner != _current_player) return;
   446 	_veh_do_replace_proc[v->type - VEH_Train](p);
   446 	_veh_do_replace_proc[v->type - VEH_TRAIN](p);
   447 }
   447 }
   448 
   448 
   449 typedef struct FoundRoute {
   449 struct FoundRoute {
   450 	int distance;
   450 	int distance;
   451 	CargoID cargo;
   451 	CargoID cargo;
   452 	void *from;
   452 	void *from;
   453 	void *to;
   453 	void *to;
   454 } FoundRoute;
   454 };
   455 
   455 
   456 static Town *AiFindRandomTown(void)
   456 static Town *AiFindRandomTown()
   457 {
   457 {
   458 	return GetRandomTown();
   458 	return GetRandomTown();
   459 }
   459 }
   460 
   460 
   461 static Industry *AiFindRandomIndustry(void)
   461 static Industry *AiFindRandomIndustry()
   462 {
   462 {
   463 	return GetRandomIndustry();
   463 	int num = RandomRange(GetMaxIndustryIndex());
       
   464 	if (IsValidIndustry(GetIndustry(num))) return GetIndustry(num);
       
   465 
       
   466 	return NULL;
   464 }
   467 }
   465 
   468 
   466 static void AiFindSubsidyIndustryRoute(FoundRoute *fr)
   469 static void AiFindSubsidyIndustryRoute(FoundRoute *fr)
   467 {
   470 {
   468 	uint i;
   471 	uint i;
  1822 	while (p->mode != 3 || !((--cmd) & 0x80)) p++;
  1825 	while (p->mode != 3 || !((--cmd) & 0x80)) p++;
  1823 
  1826 
  1824 	return tile + ToTileIndexDiff(p->tileoffs) - TileOffsByDiagDir(*dir = p->attr);
  1827 	return tile + ToTileIndexDiff(p->tileoffs) - TileOffsByDiagDir(*dir = p->attr);
  1825 }
  1828 }
  1826 
  1829 
  1827 typedef struct AiRailPathFindData {
  1830 struct AiRailPathFindData {
  1828 	TileIndex tile;
  1831 	TileIndex tile;
  1829 	TileIndex tile2;
  1832 	TileIndex tile2;
  1830 	int count;
  1833 	int count;
  1831 	bool flag;
  1834 	bool flag;
  1832 } AiRailPathFindData;
  1835 };
  1833 
  1836 
  1834 static bool AiEnumFollowTrack(TileIndex tile, AiRailPathFindData *a, int track, uint length, byte *state)
  1837 static bool AiEnumFollowTrack(TileIndex tile, AiRailPathFindData *a, int track, uint length, byte *state)
  1835 {
  1838 {
  1836 	if (a->flag) return true;
  1839 	if (a->flag) return true;
  1837 
  1840 
  1856 	FollowTrack(p->ai.cur_tile_a + TileOffsByDiagDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, (DiagDirection)(p->ai.cur_dir_a^2),
  1859 	FollowTrack(p->ai.cur_tile_a + TileOffsByDiagDir(p->ai.cur_dir_a), 0x2000 | TRANSPORT_RAIL, (DiagDirection)(p->ai.cur_dir_a^2),
  1857 		(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
  1860 		(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
  1858 	return arpfd.count > 8;
  1861 	return arpfd.count > 8;
  1859 }
  1862 }
  1860 
  1863 
  1861 typedef struct AiRailFinder {
  1864 struct AiRailFinder {
  1862 	TileIndex final_tile;
  1865 	TileIndex final_tile;
  1863 	byte final_dir;
  1866 	byte final_dir;
  1864 	byte depth;
  1867 	byte depth;
  1865 	byte recursive_mode;
  1868 	byte recursive_mode;
  1866 	byte cur_best_dir;
  1869 	byte cur_best_dir;
  1871 	const byte *best_ptr;
  1874 	const byte *best_ptr;
  1872 	uint best_dist;
  1875 	uint best_dist;
  1873 	TileIndex cur_best_tile, best_tile;
  1876 	TileIndex cur_best_tile, best_tile;
  1874 	TileIndex bridge_end_tile;
  1877 	TileIndex bridge_end_tile;
  1875 	Player *player;
  1878 	Player *player;
  1876 } AiRailFinder;
  1879 };
  1877 
  1880 
  1878 static const byte _ai_table_15[4][8] = {
  1881 static const byte _ai_table_15[4][8] = {
  1879 	{0, 0, 4, 3, 3, 1, 128 + 0, 64},
  1882 	{0, 0, 4, 3, 3, 1, 128 + 0, 64},
  1880 	{1, 1, 2, 0, 4, 2, 128 + 1, 65},
  1883 	{1, 1, 2, 0, 4, 2, 128 + 1, 65},
  1881 	{0, 2, 2, 3, 5, 1, 128 + 2, 66},
  1884 	{0, 2, 2, 3, 5, 1, 128 + 2, 66},
  2711 	// yep, all are done. switch state to the rail building state.
  2714 	// yep, all are done. switch state to the rail building state.
  2712 	p->ai.state = AIS_BUILD_ROAD;
  2715 	p->ai.state = AIS_BUILD_ROAD;
  2713 	p->ai.state_mode = 255;
  2716 	p->ai.state_mode = 255;
  2714 }
  2717 }
  2715 
  2718 
  2716 typedef struct {
  2719 struct AiRoadFinder {
  2717 	TileIndex final_tile;
  2720 	TileIndex final_tile;
  2718 	byte final_dir;
  2721 	byte final_dir;
  2719 	byte depth;
  2722 	byte depth;
  2720 	byte recursive_mode;
  2723 	byte recursive_mode;
  2721 	byte cur_best_dir;
  2724 	byte cur_best_dir;
  2726 	const byte *best_ptr;
  2729 	const byte *best_ptr;
  2727 	uint best_dist;
  2730 	uint best_dist;
  2728 	TileIndex cur_best_tile, best_tile;
  2731 	TileIndex cur_best_tile, best_tile;
  2729 	TileIndex bridge_end_tile;
  2732 	TileIndex bridge_end_tile;
  2730 	Player *player;
  2733 	Player *player;
  2731 } AiRoadFinder;
  2734 };
  2732 
  2735 
  2733 typedef struct AiRoadEnum {
  2736 struct AiRoadEnum {
  2734 	TileIndex dest;
  2737 	TileIndex dest;
  2735 	TileIndex best_tile;
  2738 	TileIndex best_tile;
  2736 	int best_track;
  2739 	int best_track;
  2737 	uint best_dist;
  2740 	uint best_dist;
  2738 } AiRoadEnum;
  2741 };
  2739 
  2742 
  2740 static const byte _dir_by_track[] = {
  2743 static const byte _dir_by_track[] = {
  2741 	0, 1, 0, 1, 2, 1,
  2744 	0, 1, 0, 1, 2, 1,
  2742 	0, 0,
  2745 	0, 0,
  2743 	2, 3, 3, 2, 3, 0,
  2746 	2, 3, 3, 2, 3, 0,
  3513 static void AiStateSellVeh(Player *p)
  3516 static void AiStateSellVeh(Player *p)
  3514 {
  3517 {
  3515 	Vehicle *v = p->ai.cur_veh;
  3518 	Vehicle *v = p->ai.cur_veh;
  3516 
  3519 
  3517 	if (v->owner == _current_player) {
  3520 	if (v->owner == _current_player) {
  3518 		if (v->type == VEH_Train) {
  3521 		if (v->type == VEH_TRAIN) {
  3519 
  3522 
  3520 			if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
  3523 			if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
  3521 				if (v->current_order.type != OT_GOTO_DEPOT)
  3524 				if (v->current_order.type != OT_GOTO_DEPOT)
  3522 					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_TRAIN_TO_DEPOT);
  3525 					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_TRAIN_TO_DEPOT);
  3523 				goto going_to_depot;
  3526 				goto going_to_depot;
  3524 			}
  3527 			}
  3525 
  3528 
  3526 			// Sell whole train
  3529 			// Sell whole train
  3527 			DoCommand(v->tile, v->index, 1, DC_EXEC, CMD_SELL_RAIL_WAGON);
  3530 			DoCommand(v->tile, v->index, 1, DC_EXEC, CMD_SELL_RAIL_WAGON);
  3528 
  3531 
  3529 		} else if (v->type == VEH_Road) {
  3532 		} else if (v->type == VEH_ROAD) {
  3530 			if (!IsRoadVehInDepotStopped(v)) {
  3533 			if (!IsRoadVehInDepotStopped(v)) {
  3531 				if (v->current_order.type != OT_GOTO_DEPOT)
  3534 				if (v->current_order.type != OT_GOTO_DEPOT)
  3532 					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
  3535 					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
  3533 				goto going_to_depot;
  3536 				goto going_to_depot;
  3534 			}
  3537 			}
  3535 
  3538 
  3536 			DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
  3539 			DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
  3537 		} else if (v->type == VEH_Aircraft) {
  3540 		} else if (v->type == VEH_AIRCRAFT) {
  3538 			if (!IsAircraftInHangarStopped(v)) {
  3541 			if (!IsAircraftInHangarStopped(v)) {
  3539 				if (v->current_order.type != OT_GOTO_DEPOT)
  3542 				if (v->current_order.type != OT_GOTO_DEPOT)
  3540 					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_AIRCRAFT_TO_HANGAR);
  3543 					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_AIRCRAFT_TO_HANGAR);
  3541 				goto going_to_depot;
  3544 				goto going_to_depot;
  3542 			}
  3545 			}
  3543 
  3546 
  3544 			DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT);
  3547 			DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT);
  3545 			} else if (v->type == VEH_Ship) {
  3548 			} else if (v->type == VEH_SHIP) {
  3546 			// XXX: not implemented
  3549 			// XXX: not implemented
  3547 			error("!v->type == VEH_Ship");
  3550 			error("!v->type == VEH_SHIP");
  3548 		}
  3551 		}
  3549 	}
  3552 	}
  3550 
  3553 
  3551 	goto return_to_loop;
  3554 	goto return_to_loop;
  3552 going_to_depot:;
  3555 going_to_depot:;