truelight@2395: /* $Id$ */ truelight@2395: truelight@2395: #include "../stdafx.h" truelight@2395: #include "../openttd.h" truelight@2395: #include "../variables.h" truelight@2395: #include "../command.h" rubidium@5720: #include "../network/network.h" rubidium@5838: #include "../helpers.hpp" truelight@2395: #include "ai.h" tron@2447: #include "default/default.h" truelight@2715: truelight@2395: /** truelight@2395: * Dequeues commands put in the queue via AI_PutCommandInQueue. truelight@2395: */ Darkvater@3692: static void AI_DequeueCommands(PlayerID player) truelight@2395: { truelight@2395: AICommand *com, *entry_com; truelight@2395: truelight@2395: entry_com = _ai_player[player].queue; truelight@2395: truelight@2395: /* It happens that DoCommandP issues a new DoCommandAI which adds a new command truelight@2395: * to this very same queue (don't argue about this, if it currently doesn't truelight@2395: * happen I can tell you it will happen with AIScript -- TrueLight). If we truelight@2395: * do not make the queue NULL, that commands will be dequeued immediatly. truelight@2395: * Therefor we safe the entry-point to entry_com, and make the queue NULL, so truelight@2395: * the new queue can be safely built up. */ truelight@2395: _ai_player[player].queue = NULL; truelight@2395: _ai_player[player].queue_tail = NULL; truelight@2395: truelight@2395: /* Dequeue all commands */ truelight@2395: while ((com = entry_com) != NULL) { truelight@2395: _current_player = player; truelight@2395: truelight@2738: _cmd_text = com->text; tron@3946: DoCommandP(com->tile, com->p1, com->p2, com->callback, com->procc); truelight@2395: truelight@2395: /* Free item */ truelight@2395: entry_com = com->next; tron@3886: free(com->text); truelight@2395: free(com); truelight@2395: } truelight@2395: } truelight@2395: truelight@2395: /** truelight@2395: * Needed for SP; we need to delay DoCommand with 1 tick, because else events truelight@2395: * will make infinite loops (AIScript). truelight@2395: */ tron@3946: static void AI_PutCommandInQueue(PlayerID player, TileIndex tile, uint32 p1, uint32 p2, uint procc, CommandCallback* callback) truelight@2395: { truelight@2395: AICommand *com; truelight@2395: truelight@2395: if (_ai_player[player].queue_tail == NULL) { truelight@2395: /* There is no item in the queue yet, create the queue */ KUDr@5860: _ai_player[player].queue = MallocT(1); truelight@2395: _ai_player[player].queue_tail = _ai_player[player].queue; truelight@2395: } else { truelight@2395: /* Add an item at the end */ KUDr@5860: _ai_player[player].queue_tail->next = MallocT(1); truelight@2395: _ai_player[player].queue_tail = _ai_player[player].queue_tail->next; truelight@2395: } truelight@2395: truelight@2395: /* This is our new item */ truelight@2395: com = _ai_player[player].queue_tail; truelight@2395: truelight@2395: /* Assign the info */ truelight@2395: com->tile = tile; truelight@2395: com->p1 = p1; truelight@2395: com->p2 = p2; truelight@2395: com->procc = procc; tron@3946: com->callback = callback; truelight@2395: com->next = NULL; truelight@2738: com->text = NULL; truelight@2395: truelight@2738: /* Copy the cmd_text, if needed */ truelight@2738: if (_cmd_text != NULL) { truelight@2738: com->text = strdup(_cmd_text); truelight@2738: _cmd_text = NULL; truelight@2738: } truelight@2395: } truelight@2395: truelight@2395: /** truelight@2395: * Executes a raw DoCommand for the AI. truelight@2395: */ tron@3946: int32 AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc, CommandCallback* callback) truelight@2395: { tron@2551: PlayerID old_lp; truelight@2395: int32 res = 0; tron@3950: const char* tmp_cmdtext; truelight@2395: truelight@2395: /* If you enable DC_EXEC with DC_QUERY_COST you are a really strange truelight@2395: * person.. should we check for those funny jokes? truelight@2395: */ truelight@2395: tron@3950: /* The test already resets _cmd_text, so backup the pointer */ tron@3950: tmp_cmdtext = _cmd_text; truelight@2738: truelight@2395: /* First, do a test-run to see if we can do this */ tron@3491: res = DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc); truelight@2395: /* The command failed, or you didn't want to execute, or you are quering, return */ tron@4000: if (CmdFailed(res) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST)) { truelight@2395: return res; truelight@2738: } truelight@2738: tron@3950: /* Restore _cmd_text */ tron@3950: _cmd_text = tmp_cmdtext; truelight@2395: truelight@2395: /* If we did a DC_EXEC, and the command did not return an error, execute it tron@4000: * over the network */ tron@4000: if (flags & DC_AUTO) procc |= CMD_AUTO; tron@4000: if (flags & DC_NO_WATER) procc |= CMD_NO_WATER; truelight@2395: truelight@2395: /* NetworkSend_Command needs _local_player to be set correctly, so tron@4000: * adjust it, and put it back right after the function */ truelight@2395: old_lp = _local_player; truelight@2395: _local_player = _current_player; truelight@2395: bjarni@2727: #ifdef ENABLE_NETWORK truelight@2395: /* Send the command */ tron@4000: if (_networking) { truelight@2395: /* Network is easy, send it to his handler */ tron@3946: NetworkSend_Command(tile, p1, p2, procc, callback); tron@4000: } else { tron@4000: #else tron@4000: { bjarni@2727: #endif truelight@2395: /* If we execute BuildCommands directly in SP, we have a big problem with events truelight@2395: * so we need to delay is for 1 tick */ tron@3946: AI_PutCommandInQueue(_current_player, tile, p1, p2, procc, callback); tron@4000: } truelight@2395: truelight@2395: /* Set _local_player back */ truelight@2395: _local_player = old_lp; truelight@2395: truelight@2395: return res; truelight@2395: } truelight@2395: tron@3946: tron@3946: int32 AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc) tron@3946: { tron@3946: return AI_DoCommandCc(tile, p1, p2, flags, procc, NULL); tron@3946: } tron@3946: tron@3946: truelight@2759: /** truelight@2395: * Run 1 tick of the AI. Don't overdo it, keep it realistic. truelight@2395: */ tron@2551: static void AI_RunTick(PlayerID player) truelight@2395: { tron@2767: extern void AiNewDoGameLoop(Player *p); tron@2767: tron@2767: Player *p = GetPlayer(player); truelight@2395: _current_player = player; truelight@2395: tron@2767: if (_patches.ainew_active) { tron@2767: AiNewDoGameLoop(p); tron@2767: } else { tron@2767: /* Enable all kind of cheats the old AI needs in order to operate correctly... */ tron@2767: _is_old_ai_player = true; tron@2767: AiDoGameLoop(p); tron@2767: _is_old_ai_player = false; truelight@2422: } truelight@2395: } truelight@2395: truelight@2395: truelight@2395: /** truelight@2395: * The gameloop for AIs. truelight@2395: * Handles one tick for all the AIs. truelight@2395: */ truelight@2395: void AI_RunGameLoop(void) truelight@2395: { truelight@2395: /* Don't do anything if ai is disabled */ tron@2639: if (!_ai.enabled) return; truelight@2395: Darkvater@5332: /* Don't do anything if we are a network-client, or the AI has been disabled */ Darkvater@5332: if (_networking && (!_network_server || !_patches.ai_in_multiplayer)) return; truelight@2682: truelight@2395: /* New tick */ truelight@2395: _ai.tick++; truelight@2395: truelight@2395: /* Make sure the AI follows the difficulty rule.. */ tron@2446: assert(_opt.diff.competitor_speed <= 4); tron@4000: if ((_ai.tick & ((1 << (4 - _opt.diff.competitor_speed)) - 1)) != 0) return; truelight@2395: truelight@2395: /* Check for AI-client (so joining a network with an AI) */ Darkvater@4854: if (!_networking || _network_server) { truelight@2395: /* Check if we want to run AIs (server or SP only) */ tron@4000: const Player* p; truelight@2395: truelight@2395: FOR_ALL_PLAYERS(p) { tron@2767: if (p->is_active && p->is_ai) { tron@2767: /* This should always be true, else something went wrong... */ tron@2767: assert(_ai_player[p->index].active); tron@2767: truelight@2395: /* Run the script */ truelight@2395: AI_DequeueCommands(p->index); truelight@2395: AI_RunTick(p->index); truelight@2395: } truelight@2395: } truelight@2395: } truelight@2395: truelight@2395: _current_player = OWNER_NONE; truelight@2395: } truelight@2395: truelight@2395: /** truelight@2395: * A new AI sees the day of light. You can do here what ever you think is needed. truelight@2395: */ tron@2551: void AI_StartNewAI(PlayerID player) truelight@2395: { Darkvater@4850: assert(IsValidPlayer(player)); truelight@2702: truelight@2395: /* Called if a new AI is booted */ truelight@2395: _ai_player[player].active = true; truelight@2395: } truelight@2395: truelight@2395: /** truelight@2395: * This AI player died. Give it some chance to make a final puf. truelight@2395: */ tron@2551: void AI_PlayerDied(PlayerID player) truelight@2395: { truelight@2395: /* Called if this AI died */ truelight@2395: _ai_player[player].active = false; truelight@2395: } truelight@2395: truelight@2395: /** truelight@2395: * Initialize some AI-related stuff. truelight@2395: */ truelight@2395: void AI_Initialize(void) truelight@2395: { truelight@2706: /* First, make sure all AIs are DEAD! */ truelight@2706: AI_Uninitialize(); truelight@2706: truelight@2395: memset(&_ai, 0, sizeof(_ai)); tron@2767: memset(&_ai_player, 0, sizeof(_ai_player)); truelight@2395: truelight@2395: _ai.enabled = true; truelight@2395: } truelight@2395: truelight@2395: /** truelight@2395: * Deinitializer for AI-related stuff. truelight@2395: */ truelight@2395: void AI_Uninitialize(void) truelight@2395: { tron@4000: const Player* p; truelight@2395: truelight@2395: FOR_ALL_PLAYERS(p) { tron@2767: if (p->is_active && p->is_ai) AI_PlayerDied(p->index); truelight@2395: } truelight@2395: }