# ai.h

    00001 /* $Id: ai.h 11832 2008-01-13 13:36:01Z rubidium $ */
    00002
    00003 #ifndef AI_H
    00004 #define AI_H
    00005
    00006 #include "../network/network.h"
    00007 #include "../command_type.h"
    00008 #include "../core/random_func.hpp"
    00009 #include "../settings_type.h"
    00010
    00011 /* How DoCommands look like for an AI */
    00012 struct AICommand {
    00013   uint32 tile;
    00014   uint32 p1;
    00015   uint32 p2;
    00016   uint32 procc;
    00017   CommandCallback *callback;
    00018
    00019   char *text;
    00020   uint uid;
    00021
    00022   AICommand *next;
    00023 };
    00024
    00025 /* The struct for an AIScript Player */
    00026 struct AIPlayer {
    00027   bool active;
    00028   AICommand *queue;
    00029   AICommand *queue_tail;
    00030 };
    00031
    00032 /* The struct to keep some data about the AI in general */
    00033 struct AIStruct {
    00034   /* General */
    00035   bool enabled;
    00036   uint tick;
    00037 };
    00038
    00039 extern AIStruct _ai;
    00040 extern AIPlayer _ai_player[MAX_PLAYERS];
    00041
    00042 // ai.c
    00043 void AI_StartNewAI(PlayerID player);
    00044 void AI_PlayerDied(PlayerID player);
    00045 void AI_RunGameLoop();
    00046 void AI_Initialize();
    00047 void AI_Uninitialize();
    00048 CommandCost AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
    00049 CommandCost AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc, CommandCallback* callback);
    00050
    00055 static inline bool AI_AllowNewAI()
    00056 {
    00057   /* If disabled, no AI */
    00058   if (!_ai.enabled)
    00059     return false;
    00060
    00061   /* If in network, but no server, no AI */
    00062   if (_networking && !_network_server)
    00063     return false;
    00064
    00065   /* If in network, and server, possible AI */
    00066   if (_networking && _network_server) {
    00067     /* Do we want AIs in multiplayer? */
    00068     if (!_patches.ai_in_multiplayer)
    00069       return false;
    00070
    00071     /* Only the NewAI is allowed... sadly enough the old AI just doesn't support this
    00072      *  system, because all commands are delayed by at least 1 tick, which causes
    00073      *  a big problem, because it uses variables that are only set AFTER the command
    00074      *  is really executed... */
    00075     if (!_patches.ainew_active)
    00076       return false;
    00077   }
    00078
    00079   return true;
    00080 }
    00081
    00082 #define AI_CHANCE16(a, b)    ((uint16)     AI_Random()  <= (uint16)((65536 * a) / b))
    00083 #define AI_CHANCE16R(a, b, r) ((uint16)(r = AI_Random()) <= (uint16)((65536 * a) / b))
    00084
    00088 static inline uint AI_RandomRange(uint max)
    00089 {
    00090   /* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
    00091    *   but we pick InteractiveRandomRange if we are a network_server or network-client.
    00092    */
    00093   if (_networking)
    00094     return InteractiveRandomRange(max);
    00095   else
    00096     return RandomRange(max);
    00097 }
    00098
    00102 static inline uint32 AI_Random()
    00103 {
    00104 /* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
    00105    *   but we pick InteractiveRandomRange if we are a network_server or network-client.
    00106    */
    00107   if (_networking)
    00108     return InteractiveRandom();
    00109   else
    00110     return Random();
    00111 }
    00112
    00113 #endif /* AI_H */