truelight@2395: #ifndef AI_H truelight@2395: #define AI_H truelight@2395: truelight@2395: #include "../functions.h" rubidium@5720: #include "../network/network.h" bjarni@2727: #include "../player.h" Darkvater@4828: #include "../command.h" truelight@2395: truelight@2395: /* How DoCommands look like for an AI */ truelight@2395: typedef struct AICommand { truelight@2395: uint32 tile; truelight@2395: uint32 p1; truelight@2395: uint32 p2; truelight@2395: uint32 procc; tron@3946: CommandCallback* callback; truelight@2395: truelight@2738: char *text; truelight@2738: uint uid; truelight@2395: truelight@2395: struct AICommand *next; truelight@2395: } AICommand; truelight@2395: truelight@2395: /* The struct for an AIScript Player */ truelight@2395: typedef 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 truelight@2395: } AIPlayer; truelight@2395: truelight@2395: /* The struct to keep some data about the AI in general */ truelight@2395: typedef 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) truelight@2395: } AIStruct; truelight@2395: truelight@2395: VARDEF AIStruct _ai; truelight@2395: VARDEF 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); truelight@2395: void AI_RunGameLoop(void); truelight@2395: void AI_Initialize(void); truelight@2395: void AI_Uninitialize(void); tron@3887: int32 AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc); tron@3946: int32 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: */ truelight@2682: static inline bool AI_AllowNewAI(void) 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? */ truelight@2682: if (!_patches.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... */ tron@2767: if (!_patches.ainew_active) truelight@2682: return false; truelight@2682: } truelight@2682: truelight@2682: return true; truelight@2682: } truelight@2682: truelight@2395: #define AI_CHANCE16(a,b) ((uint16) AI_Random() <= (uint16)((65536 * a) / b)) truelight@2395: #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: */ truelight@2395: static inline uint32 AI_Random(void) 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 */