mirror of https://github.com/OpenTTD/OpenTTD
(svn r5096) Avoid unnecessary copying of _cmd_text
parent
3e0902ff14
commit
9b89d3b8cd
13
ai/ai.c
13
ai/ai.c
|
@ -85,26 +85,23 @@ int32 AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint pr
|
||||||
{
|
{
|
||||||
PlayerID old_lp;
|
PlayerID old_lp;
|
||||||
int32 res = 0;
|
int32 res = 0;
|
||||||
char *tmp_cmdtext = NULL;
|
const char* tmp_cmdtext;
|
||||||
|
|
||||||
/* If you enable DC_EXEC with DC_QUERY_COST you are a really strange
|
/* If you enable DC_EXEC with DC_QUERY_COST you are a really strange
|
||||||
* person.. should we check for those funny jokes?
|
* person.. should we check for those funny jokes?
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* The test already free _cmd_text in most cases, so let's backup the string, else we have a problem ;) */
|
/* The test already resets _cmd_text, so backup the pointer */
|
||||||
if (_cmd_text != NULL)
|
tmp_cmdtext = _cmd_text;
|
||||||
tmp_cmdtext = strdup(_cmd_text);
|
|
||||||
|
|
||||||
/* First, do a test-run to see if we can do this */
|
/* First, do a test-run to see if we can do this */
|
||||||
res = DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc);
|
res = DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc);
|
||||||
/* The command failed, or you didn't want to execute, or you are quering, return */
|
/* The command failed, or you didn't want to execute, or you are quering, return */
|
||||||
if ((CmdFailed(res)) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST)) {
|
if ((CmdFailed(res)) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST)) {
|
||||||
free(tmp_cmdtext);
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Recover _cmd_text */
|
/* Restore _cmd_text */
|
||||||
if (tmp_cmdtext != NULL)
|
|
||||||
_cmd_text = tmp_cmdtext;
|
_cmd_text = tmp_cmdtext;
|
||||||
|
|
||||||
/* If we did a DC_EXEC, and the command did not return an error, execute it
|
/* If we did a DC_EXEC, and the command did not return an error, execute it
|
||||||
|
@ -131,8 +128,6 @@ int32 AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint pr
|
||||||
/* Set _local_player back */
|
/* Set _local_player back */
|
||||||
_local_player = old_lp;
|
_local_player = old_lp;
|
||||||
|
|
||||||
free(tmp_cmdtext);
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue