smatz@8587: /* $Id$ */ smatz@8587: rubidium@10429: /** @file ai.h Base functions for all AIs. */ rubidium@10429: truelight@2395: #ifndef AI_H truelight@2395: #define AI_H truelight@2395: rubidium@5720: #include "../network/network.h" rubidium@8612: #include "../command_type.h" rubidium@8627: #include "../core/random_func.hpp" rubidium@8707: #include "../settings_type.h" truelight@2395: truelight@2395: /* How DoCommands look like for an AI */ rubidium@6574: struct AICommand { truelight@2395: uint32 tile; truelight@2395: uint32 p1; truelight@2395: uint32 p2; truelight@2395: uint32 procc; rubidium@8612: CommandCallback *callback; truelight@2395: truelight@2738: char *text; truelight@2738: uint uid; truelight@2395: rubidium@6574: AICommand *next; rubidium@6574: }; truelight@2395: truelight@2395: /* The struct for an AIScript Player */ rubidium@6574: struct AIPlayer { peter1138@3173: bool active; ///< Is this AI active? peter1138@3173: AICommand *queue; ///< The commands that he has in his queue peter1138@3173: AICommand *queue_tail; ///< The tail of this queue rubidium@6574: }; truelight@2395: truelight@2395: /* The struct to keep some data about the AI in general */ rubidium@6574: struct AIStruct { truelight@2395: /* General */ peter1138@3173: bool enabled; ///< Is AI enabled? peter1138@3173: uint tick; ///< The current tick (something like _frame_counter, only for AIs) rubidium@6574: }; truelight@2395: rubidium@8764: extern AIStruct _ai; rubidium@8764: extern AIPlayer _ai_player[MAX_PLAYERS]; truelight@2395: truelight@2395: // ai.c tron@2551: void AI_StartNewAI(PlayerID player); tron@2551: void AI_PlayerDied(PlayerID player); rubidium@6573: void AI_RunGameLoop(); rubidium@6573: void AI_Initialize(); rubidium@6573: void AI_Uninitialize(); rubidium@7439: CommandCost AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc); rubidium@7439: CommandCost AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc, CommandCallback* callback); truelight@2395: truelight@2682: /** Is it allowed to start a new AI. truelight@2682: * This function checks some boundries to see if we should launch a new AI. truelight@2682: * @return True if we can start a new AI. truelight@2682: */ rubidium@6573: static inline bool AI_AllowNewAI() truelight@2682: { truelight@2682: /* If disabled, no AI */ truelight@2682: if (!_ai.enabled) truelight@2682: return false; truelight@2682: truelight@2682: /* If in network, but no server, no AI */ truelight@2682: if (_networking && !_network_server) truelight@2682: return false; truelight@2682: truelight@2682: /* If in network, and server, possible AI */ truelight@2682: if (_networking && _network_server) { truelight@2682: /* Do we want AIs in multiplayer? */ rubidium@10775: if (!_settings_game.ai.ai_in_multiplayer) truelight@2682: return false; truelight@2682: truelight@2682: /* Only the NewAI is allowed... sadly enough the old AI just doesn't support this truelight@2682: * system, because all commands are delayed by at least 1 tick, which causes truelight@2682: * a big problem, because it uses variables that are only set AFTER the command truelight@2682: * is really executed... */ rubidium@10775: if (!_settings_game.ai.ainew_active) truelight@2682: return false; truelight@2682: } truelight@2682: truelight@2682: return true; truelight@2682: } truelight@2682: rubidium@6987: #define AI_CHANCE16(a, b) ((uint16) AI_Random() <= (uint16)((65536 * a) / b)) rubidium@6987: #define AI_CHANCE16R(a, b, r) ((uint16)(r = AI_Random()) <= (uint16)((65536 * a) / b)) truelight@2395: truelight@2395: /** truelight@2395: * The random-function that should be used by ALL AIs. truelight@2395: */ truelight@2395: static inline uint AI_RandomRange(uint max) truelight@2395: { truelight@2395: /* We pick RandomRange if we are in SP (so when saved, we do the same over and over) truelight@2395: * but we pick InteractiveRandomRange if we are a network_server or network-client. truelight@2395: */ truelight@2395: if (_networking) truelight@2395: return InteractiveRandomRange(max); truelight@2395: else truelight@2395: return RandomRange(max); truelight@2395: } truelight@2395: truelight@2395: /** truelight@2395: * The random-function that should be used by ALL AIs. truelight@2395: */ rubidium@6573: static inline uint32 AI_Random() truelight@2395: { truelight@2395: /* We pick RandomRange if we are in SP (so when saved, we do the same over and over) truelight@2395: * but we pick InteractiveRandomRange if we are a network_server or network-client. truelight@2395: */ truelight@2395: if (_networking) truelight@2395: return InteractiveRandom(); truelight@2395: else truelight@2395: return Random(); truelight@2395: } truelight@2395: truelight@2395: #endif /* AI_H */