ai.h

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

Generated on Fri Nov 21 19:01:31 2008 for openttd by  doxygen 1.5.6