src/ai/ai.cpp
branchNewGRF_ports
changeset 6872 1c4a4a609f85
parent 6870 ca3fd1fbe311
child 10724 68a692eacf22
equal deleted inserted replaced
6871:5a9dc001e1ad 6872:1c4a4a609f85
     1 /* $Id$ */
     1 /* $Id$ */
     2 
     2 
     3 #include "../stdafx.h"
     3 #include "../stdafx.h"
     4 #include "../openttd.h"
     4 #include "../openttd.h"
     5 #include "../variables.h"
     5 #include "../variables.h"
     6 #include "../command.h"
     6 #include "../command_func.h"
     7 #include "../network/network.h"
     7 #include "../network/network.h"
     8 #include "../helpers.hpp"
     8 #include "../core/alloc_func.hpp"
       
     9 #include "../player_func.h"
       
    10 #include "../player_base.h"
     9 #include "ai.h"
    11 #include "ai.h"
    10 #include "default/default.h"
    12 #include "default/default.h"
       
    13 #include "../signal_func.h"
       
    14 
       
    15 AIStruct _ai;
       
    16 AIPlayer _ai_player[MAX_PLAYERS];
    11 
    17 
    12 /**
    18 /**
    13  * Dequeues commands put in the queue via AI_PutCommandInQueue.
    19  * Dequeues commands put in the queue via AI_PutCommandInQueue.
    14  */
    20  */
    15 static void AI_DequeueCommands(PlayerID player)
    21 static void AI_DequeueCommands(PlayerID player)
    43 
    49 
    44 /**
    50 /**
    45  * Needed for SP; we need to delay DoCommand with 1 tick, because else events
    51  * Needed for SP; we need to delay DoCommand with 1 tick, because else events
    46  *  will make infinite loops (AIScript).
    52  *  will make infinite loops (AIScript).
    47  */
    53  */
    48 static void AI_PutCommandInQueue(PlayerID player, TileIndex tile, uint32 p1, uint32 p2, uint procc, CommandCallback* callback)
    54 static void AI_PutCommandInQueue(PlayerID player, TileIndex tile, uint32 p1, uint32 p2, uint32 procc, CommandCallback* callback)
    49 {
    55 {
    50 	AICommand *com;
    56 	AICommand *com;
    51 
    57 
    52 	if (_ai_player[player].queue_tail == NULL) {
    58 	if (_ai_player[player].queue_tail == NULL) {
    53 		/* There is no item in the queue yet, create the queue */
    59 		/* There is no item in the queue yet, create the queue */
    79 }
    85 }
    80 
    86 
    81 /**
    87 /**
    82  * Executes a raw DoCommand for the AI.
    88  * Executes a raw DoCommand for the AI.
    83  */
    89  */
    84 CommandCost AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc, CommandCallback* callback)
    90 CommandCost AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint32 procc, CommandCallback* callback)
    85 {
    91 {
    86 	PlayerID old_lp;
    92 	PlayerID old_lp;
    87 	CommandCost res;
    93 	CommandCost res;
    88 	const char* tmp_cmdtext;
    94 	const char* tmp_cmdtext;
    89 
    95 
   101 		return res;
   107 		return res;
   102 	}
   108 	}
   103 
   109 
   104 	/* Restore _cmd_text */
   110 	/* Restore _cmd_text */
   105 	_cmd_text = tmp_cmdtext;
   111 	_cmd_text = tmp_cmdtext;
   106 
       
   107 	/* If we did a DC_EXEC, and the command did not return an error, execute it
       
   108 	 * over the network */
       
   109 	if (flags & DC_NO_WATER) procc |= CMD_NO_WATER;
       
   110 
   112 
   111 	/* NetworkSend_Command needs _local_player to be set correctly, so
   113 	/* NetworkSend_Command needs _local_player to be set correctly, so
   112 	 * adjust it, and put it back right after the function */
   114 	 * adjust it, and put it back right after the function */
   113 	old_lp = _local_player;
   115 	old_lp = _local_player;
   114 	_local_player = _current_player;
   116 	_local_player = _current_player;
   132 
   134 
   133 	return res;
   135 	return res;
   134 }
   136 }
   135 
   137 
   136 
   138 
   137 CommandCost AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
   139 CommandCost AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint32 procc)
   138 {
   140 {
   139 	return AI_DoCommandCc(tile, p1, p2, flags, procc, NULL);
   141 	return AI_DoCommandCc(tile, p1, p2, flags, procc, NULL);
   140 }
   142 }
   141 
   143 
   142 
   144 
   156 		/* Enable all kind of cheats the old AI needs in order to operate correctly... */
   158 		/* Enable all kind of cheats the old AI needs in order to operate correctly... */
   157 		_is_old_ai_player = true;
   159 		_is_old_ai_player = true;
   158 		AiDoGameLoop(p);
   160 		AiDoGameLoop(p);
   159 		_is_old_ai_player = false;
   161 		_is_old_ai_player = false;
   160 	}
   162 	}
       
   163 
       
   164 	/* AI could change some track, so update signals */
       
   165 	UpdateSignalsInBuffer();
   161 }
   166 }
   162 
   167 
   163 
   168 
   164 /**
   169 /**
   165  * The gameloop for AIs.
   170  * The gameloop for AIs.