src/ai/api/ai_object.cpp
branchnoai
changeset 9844 738b8f69675f
parent 9841 f931a10242c9
child 9847 b89e6adc18d4
equal deleted inserted replaced
9843:29f34a179daf 9844:738b8f69675f
     1 /* $Id$ */
     1 /* $Id$ */
     2 
     2 
     3 /** @file ai_object.cpp Implementation of AIObject. */
     3 /** @file ai_object.cpp Implementation of AIObject. */
     4 
     4 
     5 #include "ai_object.hpp"
     5 #include "ai_object.hpp"
       
     6 #include "table/strings.h"
     6 #include "../../command_func.h"
     7 #include "../../command_func.h"
     7 #include "../../network/network.h"
     8 #include "../../network/network.h"
     8 #include "../../player_func.h"
     9 #include "../../player_func.h"
     9 #include "../../signs_func.h"
    10 #include "../../signs_func.h"
    10 #include "../../vehicle_func.h"
    11 #include "../../vehicle_func.h"
    49 }
    50 }
    50 
    51 
    51 Money AIObject::GetDoCommandCosts()
    52 Money AIObject::GetDoCommandCosts()
    52 {
    53 {
    53 	return AIObject::GetDoCommandStruct(_current_player)->costs.GetCost();
    54 	return AIObject::GetDoCommandStruct(_current_player)->costs.GetCost();
       
    55 }
       
    56 
       
    57 void AIObject::SetLastError(const StringID last_error)
       
    58 {
       
    59 	AIObject::GetDoCommandStruct(_current_player)->last_error = last_error;
       
    60 }
       
    61 
       
    62 const StringID AIObject::GetLastError()
       
    63 {
       
    64 	return AIObject::GetDoCommandStruct(_current_player)->last_error;
    54 }
    65 }
    55 
    66 
    56 void AIObject::SetLastCommandRes(bool res)
    67 void AIObject::SetLastCommandRes(bool res)
    57 {
    68 {
    58 	AIObject::GetDoCommandStruct(_current_player)->last_command_res = res;
    69 	AIObject::GetDoCommandStruct(_current_player)->last_command_res = res;
   118 	uint32 flags = 0;
   129 	uint32 flags = 0;
   119 	PlayerID old_lp;
   130 	PlayerID old_lp;
   120 	CommandCost res;
   131 	CommandCost res;
   121 	const char *tmp_cmdtext;
   132 	const char *tmp_cmdtext;
   122 
   133 
       
   134 	/* Make sure the last error is reset, so we don't give faulty warnings */
       
   135 	SetLastError(STR_NULL);
       
   136 
   123 	if (procc != CMD_LANDSCAPE_CLEAR) flags |= DC_AUTO;
   137 	if (procc != CMD_LANDSCAPE_CLEAR) flags |= DC_AUTO;
   124 	if (water_protection) flags |= DC_NO_WATER;
   138 	if (water_protection) flags |= DC_NO_WATER;
   125 
   139 
   126 	/* The test already resets _cmd_text, so backup the pointer */
   140 	/* The test already resets _cmd_text, so backup the pointer */
   127 	tmp_cmdtext = _cmd_text;
   141 	tmp_cmdtext = _cmd_text;
   128 
   142 
   129 	/* First, do a test-run to see if we can do this */
   143 	/* First, do a test-run to see if we can do this */
   130 	res = ::DoCommand(tile, p1, p2, flags, procc);
   144 	res = ::DoCommand(tile, p1, p2, flags, procc);
   131 	/* The command failed, so return */
   145 	/* The command failed, so return */
   132 	if (::CmdFailed(res)) return false;
   146 	if (::CmdFailed(res)) {
       
   147 		SetLastError(_error_message);
       
   148 		return false;
       
   149 	}
   133 
   150 
   134 	/* Restore _cmd_text */
   151 	/* Restore _cmd_text */
   135 	_cmd_text = tmp_cmdtext;
   152 	_cmd_text = tmp_cmdtext;
   136 
   153 
   137 	/* Check what the callback wants us to do */
   154 	/* Check what the callback wants us to do */
   170 
   187 
   171 		/* Suspend the AI player for 1 tick, so it simulates MultiPlayer */
   188 		/* Suspend the AI player for 1 tick, so it simulates MultiPlayer */
   172 		AI_SuspendPlayer(_current_player, AIObject::GetDoCommandDelay());
   189 		AI_SuspendPlayer(_current_player, AIObject::GetDoCommandDelay());
   173 	}
   190 	}
   174 
   191 
   175 	if (::CmdFailed(res)) return false;
   192 	if (::CmdFailed(res)) {
       
   193 		SetLastError(_error_message);
       
   194 		return false;
       
   195 	}
   176 
   196 
   177 	AIObject::IncreaseDoCommandCosts(res.GetCost());
   197 	AIObject::IncreaseDoCommandCosts(res.GetCost());
   178 	return true;
   198 	return true;
   179 }
   199 }