src/players.cpp
changeset 10278 f0fbbccb931d
parent 10276 e28934bc929a
child 10284 4f4086cd9585
equal deleted inserted replaced
10277:a01c1acdd701 10278:f0fbbccb931d
   650  * if p1 = 5, then
   650  * if p1 = 5, then
   651  * - p2 = enable renew_keep_length
   651  * - p2 = enable renew_keep_length
   652  */
   652  */
   653 CommandCost CmdSetAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   653 CommandCost CmdSetAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   654 {
   654 {
   655 	Player *p;
       
   656 	if (!IsValidPlayer(_current_player)) return CMD_ERROR;
   655 	if (!IsValidPlayer(_current_player)) return CMD_ERROR;
   657 
   656 
   658 	p = GetPlayer(_current_player);
   657 	Player *p = GetPlayer(_current_player);
   659 	switch (GB(p1, 0, 3)) {
   658 	switch (GB(p1, 0, 3)) {
   660 		case 0:
   659 		case 0:
   661 			if (p->engine_renew == HasBit(p2, 0))
   660 			if (p->engine_renew == HasBit(p2, 0)) return CMD_ERROR;
   662 				return CMD_ERROR;
       
   663 
   661 
   664 			if (flags & DC_EXEC) {
   662 			if (flags & DC_EXEC) {
   665 				p->engine_renew = HasBit(p2, 0);
   663 				p->engine_renew = HasBit(p2, 0);
   666 				if (IsLocalPlayer()) {
   664 				if (IsLocalPlayer()) {
   667 					_patches.autorenew = p->engine_renew;
   665 					_patches.autorenew = p->engine_renew;
   668 					InvalidateWindow(WC_GAME_OPTIONS, 0);
   666 					InvalidateWindow(WC_GAME_OPTIONS, 0);
   669 				}
   667 				}
   670 			}
   668 			}
   671 			break;
   669 			break;
       
   670 
   672 		case 1:
   671 		case 1:
   673 			if (Clamp((int16)p2, -12, 12) != (int16)p2) return CMD_ERROR;
   672 			if (Clamp((int16)p2, -12, 12) != (int16)p2) return CMD_ERROR;
   674 			if (p->engine_renew_months == (int16)p2)
   673 			if (p->engine_renew_months == (int16)p2) return CMD_ERROR;
   675 				return CMD_ERROR;
       
   676 
   674 
   677 			if (flags & DC_EXEC) {
   675 			if (flags & DC_EXEC) {
   678 				p->engine_renew_months = (int16)p2;
   676 				p->engine_renew_months = (int16)p2;
   679 				if (IsLocalPlayer()) {
   677 				if (IsLocalPlayer()) {
   680 					_patches.autorenew_months = p->engine_renew_months;
   678 					_patches.autorenew_months = p->engine_renew_months;
   681 					InvalidateWindow(WC_GAME_OPTIONS, 0);
   679 					InvalidateWindow(WC_GAME_OPTIONS, 0);
   682 				}
   680 				}
   683 			}
   681 			}
   684 			break;
   682 			break;
       
   683 
   685 		case 2:
   684 		case 2:
   686 			if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
   685 			if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
   687 			if (p->engine_renew_money == (uint32)p2)
   686 			if (p->engine_renew_money == p2) return CMD_ERROR;
   688 				return CMD_ERROR;
       
   689 
   687 
   690 			if (flags & DC_EXEC) {
   688 			if (flags & DC_EXEC) {
   691 				p->engine_renew_money = (uint32)p2;
   689 				p->engine_renew_money = p2;
   692 				if (IsLocalPlayer()) {
   690 				if (IsLocalPlayer()) {
   693 					_patches.autorenew_money = p->engine_renew_money;
   691 					_patches.autorenew_money = p->engine_renew_money;
   694 					InvalidateWindow(WC_GAME_OPTIONS, 0);
   692 					InvalidateWindow(WC_GAME_OPTIONS, 0);
   695 				}
   693 				}
   696 			}
   694 			}
   697 			break;
   695 			break;
       
   696 
   698 		case 3: {
   697 		case 3: {
   699 			EngineID old_engine_type = GB(p2, 0, 16);
   698 			EngineID old_engine_type = GB(p2, 0, 16);
   700 			EngineID new_engine_type = GB(p2, 16, 16);
   699 			EngineID new_engine_type = GB(p2, 16, 16);
   701 			GroupID id_g = GB(p1, 16, 16);
   700 			GroupID id_g = GB(p1, 16, 16);
   702 			CommandCost cost;
   701 			CommandCost cost;
   703 
   702 
   704 			if (!IsValidGroupID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
   703 			if (!IsValidGroupID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
   705 			if (new_engine_type != INVALID_ENGINE) {
   704 			if (new_engine_type != INVALID_ENGINE) {
   706 				/* First we make sure that it's a valid type the user requested
   705 				/* First we make sure that it's a valid type the user requested
   707 				 * check that it's an engine that is in the engine array */
   706 				 * check that it's an engine that is in the engine array */
   708 				if (!IsEngineIndex(new_engine_type))
   707 				if (!IsEngineIndex(new_engine_type)) return CMD_ERROR;
   709 					return CMD_ERROR;
       
   710 
   708 
   711 				/* check that the new vehicle type is the same as the original one */
   709 				/* check that the new vehicle type is the same as the original one */
   712 				if (GetEngine(old_engine_type)->type != GetEngine(new_engine_type)->type)
   710 				if (GetEngine(old_engine_type)->type != GetEngine(new_engine_type)->type) return CMD_ERROR;
   713 					return CMD_ERROR;
       
   714 
   711 
   715 				/* make sure that we do not replace a plane with a helicopter or vise versa */
   712 				/* make sure that we do not replace a plane with a helicopter or vise versa */
   716 				if (GetEngine(new_engine_type)->type == VEH_AIRCRAFT &&
   713 				if (GetEngine(new_engine_type)->type == VEH_AIRCRAFT &&
   717 						(AircraftVehInfo(old_engine_type)->subtype & AIR_CTOL) != (AircraftVehInfo(new_engine_type)->subtype & AIR_CTOL))
   714 						(AircraftVehInfo(old_engine_type)->subtype & AIR_CTOL) != (AircraftVehInfo(new_engine_type)->subtype & AIR_CTOL)) {
   718 					return CMD_ERROR;
   715 					return CMD_ERROR;
       
   716 				}
   719 
   717 
   720 				/* make sure that the player can actually buy the new engine */
   718 				/* make sure that the player can actually buy the new engine */
   721 				if (!HasBit(GetEngine(new_engine_type)->player_avail, _current_player))
   719 				if (!HasBit(GetEngine(new_engine_type)->player_avail, _current_player)) return CMD_ERROR;
   722 					return CMD_ERROR;
       
   723 
   720 
   724 				cost = AddEngineReplacementForPlayer(p, old_engine_type, new_engine_type, id_g, flags);
   721 				cost = AddEngineReplacementForPlayer(p, old_engine_type, new_engine_type, id_g, flags);
   725 			} else {
   722 			} else {
   726 				cost = RemoveEngineReplacementForPlayer(p, old_engine_type, id_g, flags);
   723 				cost = RemoveEngineReplacementForPlayer(p, old_engine_type, id_g, flags);
   727 			}
   724 			}
   732 		}
   729 		}
   733 
   730 
   734 		case 4:
   731 		case 4:
   735 			if (Clamp((int16)GB(p1, 16, 16), -12, 12) != (int16)GB(p1, 16, 16)) return CMD_ERROR;
   732 			if (Clamp((int16)GB(p1, 16, 16), -12, 12) != (int16)GB(p1, 16, 16)) return CMD_ERROR;
   736 			if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
   733 			if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
       
   734 
   737 			if (flags & DC_EXEC) {
   735 			if (flags & DC_EXEC) {
   738 				p->engine_renew = HasBit(p1, 15);
   736 				p->engine_renew = HasBit(p1, 15);
   739 				p->engine_renew_months = (int16)GB(p1, 16, 16);
   737 				p->engine_renew_months = (int16)GB(p1, 16, 16);
   740 				p->engine_renew_money = (uint32)p2;
   738 				p->engine_renew_money = p2;
   741 
   739 
   742 				if (IsLocalPlayer()) {
   740 				if (IsLocalPlayer()) {
   743 					_patches.autorenew = p->engine_renew;
   741 					_patches.autorenew = p->engine_renew;
   744 					_patches.autorenew_months = p->engine_renew_months;
   742 					_patches.autorenew_months = p->engine_renew_months;
   745 					_patches.autorenew_money = p->engine_renew_money;
   743 					_patches.autorenew_money = p->engine_renew_money;
   746 					InvalidateWindow(WC_GAME_OPTIONS, 0);
   744 					InvalidateWindow(WC_GAME_OPTIONS, 0);
   747 				}
   745 				}
   748 			}
   746 			}
   749 			break;
   747 			break;
       
   748 
   750 		case 5:
   749 		case 5:
   751 			if (p->renew_keep_length == HasBit(p2, 0))
   750 			if (p->renew_keep_length == HasBit(p2, 0)) return CMD_ERROR;
   752 				return CMD_ERROR;
       
   753 
   751 
   754 			if (flags & DC_EXEC) {
   752 			if (flags & DC_EXEC) {
   755 				p->renew_keep_length = HasBit(p2, 0);
   753 				p->renew_keep_length = HasBit(p2, 0);
   756 				if (IsLocalPlayer()) {
   754 				if (IsLocalPlayer()) {
   757 					InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
   755 					InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
   758 				}
   756 				}
   759 			}
   757 			}
   760 		break;
   758 		break;
   761 
   759 	}
   762 	}
   760 
   763 	return CommandCost();
   761 	return CommandCost();
   764 }
   762 }
   765 
   763 
   766 /** Control the players: add, delete, etc.
   764 /** Control the players: add, delete, etc.
   767  * @param tile unused
   765  * @param tile unused