ai.c
changeset 555 02df8a1b7f33
parent 539 7e16a61c51ce
child 558 1ae2a752501e
equal deleted inserted replaced
554:a4ba0fbbf018 555:02df8a1b7f33
   246 	return 0;
   246 	return 0;
   247 }
   247 }
   248 
   248 
   249 static void AiHandleGotoDepot(Player *p, int cmd)
   249 static void AiHandleGotoDepot(Player *p, int cmd)
   250 {
   250 {
   251 	if ((p->ai.cur_veh->next_order & OT_MASK) != OT_GOTO_DEPOT)
   251 	if (p->ai.cur_veh->current_order.type != OT_GOTO_DEPOT)
   252 		DoCommandByTile(0, p->ai.cur_veh->index, 0, DC_EXEC, cmd);
   252 		DoCommandByTile(0, p->ai.cur_veh->index, 0, DC_EXEC, cmd);
   253 
   253 
   254 	if (++p->ai.state_counter <= 1387) {
   254 	if (++p->ai.state_counter <= 1387) {
   255 		p->ai.state = AIS_VEH_DO_REPLACE_VEHICLE;
   255 		p->ai.state = AIS_VEH_DO_REPLACE_VEHICLE;
   256 		return;
   256 		return;
   257 	}
   257 	}
   258 
   258 
   259 	if ((p->ai.cur_veh->next_order&OT_MASK) == OT_GOTO_DEPOT) {
   259 	if (p->ai.cur_veh->current_order.type == OT_GOTO_DEPOT) {
   260 		p->ai.cur_veh->next_order = OT_DUMMY;
   260 		p->ai.cur_veh->current_order.type = OT_DUMMY;
       
   261 		p->ai.cur_veh->current_order.flags = 0;
   261 		InvalidateWindow(WC_VEHICLE_VIEW, p->ai.cur_veh->index);
   262 		InvalidateWindow(WC_VEHICLE_VIEW, p->ai.cur_veh->index);
   262 	}
   263 	}
   263 }
   264 }
   264 
   265 
   265 static void AiRestoreVehicleOrders(Vehicle *v, BackuppedOrders *bak)
   266 static void AiRestoreVehicleOrders(Vehicle *v, BackuppedOrders *bak)
   266 {
   267 {
   267 	uint16 *os = bak->order, ord;
   268 	const Order *os = bak->order;
   268 	int ind = 0;
   269 	int ind = 0;
   269 	while ((ord = *os++) != 0) {
   270 
   270 		if (DoCommandByTile(0, v->index + (ind << 16), ord, DC_EXEC, CMD_INSERT_ORDER) == CMD_ERROR)
   271 	while (os++->type != OT_NOTHING) {
       
   272 		if (DoCommandByTile(0, v->index + (ind << 16), PackOrder(os), DC_EXEC, CMD_INSERT_ORDER) == CMD_ERROR)
   271 			break;
   273 			break;
   272 		ind++;
   274 		ind++;
   273 	}
   275 	}
   274 }
   276 }
   275 
   277 
  3517 
  3519 
  3518 	if (v->owner == _current_player) {
  3520 	if (v->owner == _current_player) {
  3519 		if (v->type == VEH_Train) {
  3521 		if (v->type == VEH_Train) {
  3520 
  3522 
  3521 			if (!IsTrainDepotTile(v->tile) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
  3523 			if (!IsTrainDepotTile(v->tile) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
  3522 				if ((v->next_order & OT_MASK) != OT_GOTO_DEPOT)
  3524 				if (v->current_order.type != OT_GOTO_DEPOT)
  3523 					DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_TRAIN_GOTO_DEPOT);
  3525 					DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_TRAIN_GOTO_DEPOT);
  3524 				goto going_to_depot;
  3526 				goto going_to_depot;
  3525 			}
  3527 			}
  3526 
  3528 
  3527 			// Sell whole train
  3529 			// Sell whole train
  3528 			DoCommandByTile(v->tile, v->index, 1, DC_EXEC, CMD_SELL_RAIL_WAGON);
  3530 			DoCommandByTile(v->tile, v->index, 1, DC_EXEC, CMD_SELL_RAIL_WAGON);
  3529 
  3531 
  3530 		} else if (v->type == VEH_Road) {
  3532 		} else if (v->type == VEH_Road) {
  3531 			if (!IsRoadDepotTile(v->tile) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) {
  3533 			if (!IsRoadDepotTile(v->tile) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) {
  3532 				if ((v->next_order & OT_MASK) != OT_GOTO_DEPOT)
  3534 				if (v->current_order.type != OT_GOTO_DEPOT)
  3533 					DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
  3535 					DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
  3534 				goto going_to_depot;
  3536 				goto going_to_depot;
  3535 			}
  3537 			}
  3536 
  3538 
  3537 			DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
  3539 			DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
  3538 		} else if (v->type == VEH_Aircraft) {
  3540 		} else if (v->type == VEH_Aircraft) {
  3539 			if (!IsAircraftHangarTile(v->tile) && !(v->vehstatus&VS_STOPPED)) {
  3541 			if (!IsAircraftHangarTile(v->tile) && !(v->vehstatus&VS_STOPPED)) {
  3540 				if ((v->next_order & OT_MASK) != OT_GOTO_DEPOT)
  3542 				if (v->current_order.type != OT_GOTO_DEPOT)
  3541 					DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SEND_AIRCRAFT_TO_HANGAR);
  3543 					DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SEND_AIRCRAFT_TO_HANGAR);
  3542 				goto going_to_depot;
  3544 				goto going_to_depot;
  3543 			}
  3545 			}
  3544 
  3546 
  3545 			DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT);
  3547 			DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT);
  3552 	goto return_to_loop;
  3554 	goto return_to_loop;
  3553 going_to_depot:;
  3555 going_to_depot:;
  3554 	if (++p->ai.state_counter <= 832)
  3556 	if (++p->ai.state_counter <= 832)
  3555 		return;
  3557 		return;
  3556 
  3558 
  3557 	if ((v->next_order&OT_MASK) == OT_GOTO_DEPOT) {
  3559 	if (v->current_order.type == OT_GOTO_DEPOT) {
  3558 		v->next_order = OT_DUMMY;
  3560 		v->current_order.type = OT_DUMMY;
       
  3561 		v->current_order.flags = 0;
  3559 		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
  3562 		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
  3560 	}
  3563 	}
  3561 return_to_loop:;
  3564 return_to_loop:;
  3562 	p->ai.state = AIS_VEH_LOOP;
  3565 	p->ai.state = AIS_VEH_LOOP;
  3563 }
  3566 }
  3564 
  3567 
  3565 static void AiStateRemoveStation(Player *p)
  3568 static void AiStateRemoveStation(Player *p)
  3566 {
  3569 {
  3567 	// Remove stations that aren't in use by any vehicle
  3570 	// Remove stations that aren't in use by any vehicle
  3568 	byte in_use[256], *used;
  3571 	byte in_use[256], *used;
  3569 	uint16 *ord;
  3572 	Order *ord;
  3570 	Station *st;
  3573 	Station *st;
  3571 	uint tile;
  3574 	uint tile;
  3572 
  3575 
  3573 	// Go to this state when we're done.
  3576 	// Go to this state when we're done.
  3574 	p->ai.state = AIS_1;
  3577 	p->ai.state = AIS_1;
  3575 
  3578 
  3576 	// Get a list of all stations that are in use by a vehicle
  3579 	// Get a list of all stations that are in use by a vehicle
  3577 	memset(in_use, 0, sizeof(in_use));
  3580 	memset(in_use, 0, sizeof(in_use));
  3578 	for(ord=_order_array; ord != _ptr_to_next_order; ord++) {
  3581 	for (ord = _order_array; ord != _ptr_to_next_order; ++ord) {
  3579 		if ((*ord & OT_MASK) == OT_GOTO_STATION)
  3582 		if (ord->type == OT_GOTO_STATION)
  3580 			in_use[*ord >> 8] = 1;
  3583 			in_use[ord->station] = 1;
  3581 	}
  3584 	}
  3582 
  3585 
  3583 	// Go through all stations and delete those that aren't in use
  3586 	// Go through all stations and delete those that aren't in use
  3584 	used=in_use;
  3587 	used=in_use;
  3585 	FOR_ALL_STATIONS(st) {
  3588 	FOR_ALL_STATIONS(st) {