src/ai/api/ai_object.cpp
branchnoai
changeset 9452 4c5eedbc3ba9
parent 9450 d675836e865c
child 9454 ee6a65b37b82
equal deleted inserted replaced
9451:7629656423cc 9452:4c5eedbc3ba9
    27 AIModeProc *AIObject::GetDoCommandMode()
    27 AIModeProc *AIObject::GetDoCommandMode()
    28 {
    28 {
    29 	return AIObject::GetDoCommandStruct(_current_player)->mode;
    29 	return AIObject::GetDoCommandStruct(_current_player)->mode;
    30 }
    30 }
    31 
    31 
    32 bool AIObject::CmdFailed(int32 res)
    32 void AIObject::SetDoCommandCosts(int32 value)
    33 {
    33 {
    34 	return ::CmdFailed(res);
    34 	AIObject::GetDoCommandStruct(_current_player)->costs = value;
    35 }
    35 }
    36 
    36 
    37 bool AIObject::CmdSucceeded(int32 res)
    37 void AIObject::IncreaseDoCommandCosts(int32 value)
    38 {
    38 {
    39 	return !::CmdFailed(res);
    39 	AIObject::GetDoCommandStruct(_current_player)->costs += value;
       
    40 }
       
    41 
       
    42 int32 AIObject::GetDoCommandCosts()
       
    43 {
       
    44 	return AIObject::GetDoCommandStruct(_current_player)->costs;
    40 }
    45 }
    41 
    46 
    42 AIObject::AIDoCommandStruct *AIObject::GetDoCommandStruct(PlayerID player)
    47 AIObject::AIDoCommandStruct *AIObject::GetDoCommandStruct(PlayerID player)
    43 {
    48 {
    44 	static bool initialized = false;
    49 	static bool initialized = false;
    49 		initialized = true;
    54 		initialized = true;
    50 		/* Set default values */
    55 		/* Set default values */
    51 		for (int i = 0; i < MAX_PLAYERS; i++) {
    56 		for (int i = 0; i < MAX_PLAYERS; i++) {
    52 			command_struct[i].mode = NULL;
    57 			command_struct[i].mode = NULL;
    53 			command_struct[i].delay = 1;
    58 			command_struct[i].delay = 1;
       
    59 			command_struct[i].costs = 0;
    54 		}
    60 		}
    55 	}
    61 	}
    56 
    62 
    57 	return &command_struct[player];
    63 	return &command_struct[player];
    58 }
    64 }
    59 
    65 
    60 int32 AIObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
    66 bool AIObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
    61 {
    67 {
    62 	PlayerID old_lp;
    68 	PlayerID old_lp;
    63 	int32 res = 0;
    69 	int32 res = 0;
    64 	const char* tmp_cmdtext;
    70 	const char* tmp_cmdtext;
    65 
    71 
    67 	tmp_cmdtext = _cmd_text;
    73 	tmp_cmdtext = _cmd_text;
    68 
    74 
    69 	/* First, do a test-run to see if we can do this */
    75 	/* First, do a test-run to see if we can do this */
    70 	res = ::DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc);
    76 	res = ::DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc);
    71 	/* The command failed, so return */
    77 	/* The command failed, so return */
    72 	if (this->CmdFailed(res)) return res;
    78 	if (::CmdFailed(res)) return false;
    73 
    79 
    74 	/* Check what the callback wants us to do */
    80 	/* Check what the callback wants us to do */
    75 	if (this->GetDoCommandMode() != NULL && !this->GetDoCommandMode()(tile, p1, p2, flags, procc)) return res;
    81 	if (this->GetDoCommandMode() != NULL && !this->GetDoCommandMode()(tile, p1, p2, flags, procc)) {
       
    82 		this->IncreaseDoCommandCosts(res);
       
    83 		return true;
       
    84 	}
    76 
    85 
    77 	/* Restore _cmd_text */
    86 	/* Restore _cmd_text */
    78 	_cmd_text = tmp_cmdtext;
    87 	_cmd_text = tmp_cmdtext;
    79 
    88 
    80 	/* If we did a DC_EXEC, and the command did not return an error, execute it
    89 	/* If we did a DC_EXEC, and the command did not return an error, execute it
   104 		::DoCommandP(tile, p1, p2, NULL, procc);
   113 		::DoCommandP(tile, p1, p2, NULL, procc);
   105 		/* Suspend the AI player for 1 tick, so it simulates MultiPlayer */
   114 		/* Suspend the AI player for 1 tick, so it simulates MultiPlayer */
   106 		AI_SuspendPlayer(_current_player, this->GetDoCommandDelay());
   115 		AI_SuspendPlayer(_current_player, this->GetDoCommandDelay());
   107 	}
   116 	}
   108 
   117 
   109 	return res;
   118 	if (::CmdFailed(res)) return false;
       
   119 
       
   120 	this->IncreaseDoCommandCosts(res);
       
   121 	return true;
   110 }
   122 }