mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
-Fix: removed the ability for the oldAI to cheat (this will criple him somewhat) -Add: base-code for many improvements to come in the AI-system -Add: added base-code for multiplayer AIs (DOES NOT WORK YET!)
This commit is contained in:
parent
e39bf0dc34
commit
991d5c6234
2
Makefile
2
Makefile
|
@ -681,6 +681,8 @@ C_SOURCES += sound/null_s.c
|
|||
C_SOURCES += video/dedicated_v.c
|
||||
C_SOURCES += video/null_v.c
|
||||
|
||||
# AI related files
|
||||
C_SOURCES += ai/ai.c
|
||||
C_SOURCES += ai/default/default.c
|
||||
C_SOURCES += ai/trolly/trolly.c
|
||||
C_SOURCES += ai/trolly/build.c
|
||||
|
|
|
@ -0,0 +1,235 @@
|
|||
/* $Id$ */
|
||||
|
||||
#include "../stdafx.h"
|
||||
#include "../openttd.h"
|
||||
#include "../variables.h"
|
||||
#include "../command.h"
|
||||
#include "../network.h"
|
||||
#include "ai.h"
|
||||
|
||||
/**
|
||||
* Dequeues commands put in the queue via AI_PutCommandInQueue.
|
||||
*/
|
||||
void AI_DequeueCommands(byte player)
|
||||
{
|
||||
AICommand *com, *entry_com;
|
||||
|
||||
entry_com = _ai_player[player].queue;
|
||||
|
||||
/* It happens that DoCommandP issues a new DoCommandAI which adds a new command
|
||||
* to this very same queue (don't argue about this, if it currently doesn't
|
||||
* happen I can tell you it will happen with AIScript -- TrueLight). If we
|
||||
* do not make the queue NULL, that commands will be dequeued immediatly.
|
||||
* Therefor we safe the entry-point to entry_com, and make the queue NULL, so
|
||||
* the new queue can be safely built up. */
|
||||
_ai_player[player].queue = NULL;
|
||||
_ai_player[player].queue_tail = NULL;
|
||||
|
||||
/* Dequeue all commands */
|
||||
while ((com = entry_com) != NULL) {
|
||||
_current_player = player;
|
||||
|
||||
/* Copy the DP back in place */
|
||||
memcpy(_decode_parameters, com->dp, sizeof(com->dp));
|
||||
DoCommandP(com->tile, com->p1, com->p2, NULL, com->procc);
|
||||
|
||||
/* Free item */
|
||||
entry_com = com->next;
|
||||
free(com);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Needed for SP; we need to delay DoCommand with 1 tick, because else events
|
||||
* will make infinite loops (AIScript).
|
||||
*/
|
||||
void AI_PutCommandInQueue(byte player, uint tile, uint32 p1, uint32 p2, uint procc)
|
||||
{
|
||||
AICommand *com;
|
||||
|
||||
if (_ai_player[player].queue_tail == NULL) {
|
||||
/* There is no item in the queue yet, create the queue */
|
||||
_ai_player[player].queue = malloc(sizeof(AICommand));
|
||||
_ai_player[player].queue_tail = _ai_player[player].queue;
|
||||
} else {
|
||||
/* Add an item at the end */
|
||||
_ai_player[player].queue_tail->next = malloc(sizeof(AICommand));
|
||||
_ai_player[player].queue_tail = _ai_player[player].queue_tail->next;
|
||||
}
|
||||
|
||||
/* This is our new item */
|
||||
com = _ai_player[player].queue_tail;
|
||||
|
||||
/* Assign the info */
|
||||
com->tile = tile;
|
||||
com->p1 = p1;
|
||||
com->p2 = p2;
|
||||
com->procc = procc;
|
||||
com->next = NULL;
|
||||
|
||||
/* Copy the decode_parameters */
|
||||
memcpy(com->dp, _decode_parameters, sizeof(com->dp));
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a raw DoCommand for the AI.
|
||||
*/
|
||||
int32 AI_DoCommand(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
|
||||
{
|
||||
byte old_lp;
|
||||
int32 res = 0;
|
||||
|
||||
/* If you enable DC_EXEC with DC_QUERY_COST you are a really strange
|
||||
* person.. should we check for those funny jokes?
|
||||
*/
|
||||
|
||||
/* First, do a test-run to see if we can do this */
|
||||
res = DoCommandByTile(tile, p1, p2, flags & ~DC_EXEC, procc);
|
||||
/* The command failed, or you didn't want to execute, or you are quering, return */
|
||||
if ((res & CMD_ERROR) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST))
|
||||
return res;
|
||||
|
||||
/* If we did a DC_EXEC, and the command did not return an error, execute it
|
||||
over the network */
|
||||
if (flags & DC_AUTO) procc |= CMD_AUTO;
|
||||
if (flags & DC_NO_WATER) procc |= CMD_NO_WATER;
|
||||
|
||||
/* NetworkSend_Command needs _local_player to be set correctly, so
|
||||
adjust it, and put it back right after the function */
|
||||
old_lp = _local_player;
|
||||
_local_player = _current_player;
|
||||
|
||||
/* Send the command */
|
||||
if (_networking)
|
||||
/* Network is easy, send it to his handler */
|
||||
NetworkSend_Command(tile, p1, p2, procc, NULL);
|
||||
else
|
||||
/* If we execute BuildCommands directly in SP, we have a big problem with events
|
||||
* so we need to delay is for 1 tick */
|
||||
AI_PutCommandInQueue(_current_player, tile, p1, p2, procc);
|
||||
|
||||
/* Set _local_player back */
|
||||
_local_player = old_lp;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run 1 tick of the AI. Don't overdo it, keep it realistic.
|
||||
*/
|
||||
void AI_RunTick(byte player)
|
||||
{
|
||||
extern void AiNewDoGameLoop(Player *p);
|
||||
|
||||
Player *p = GetPlayer(player);
|
||||
_current_player = player;
|
||||
|
||||
if (_patches.ainew_active)
|
||||
AiNewDoGameLoop(p);
|
||||
else
|
||||
AiDoGameLoop(p);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The gameloop for AIs.
|
||||
* Handles one tick for all the AIs.
|
||||
*/
|
||||
void AI_RunGameLoop(void)
|
||||
{
|
||||
/* Don't do anything if ai is disabled */
|
||||
if (!_ai.enabled)
|
||||
return;
|
||||
|
||||
/* New tick */
|
||||
_ai.tick++;
|
||||
|
||||
/* Make sure the AI follows the difficulty rule.. */
|
||||
switch (_opt.diff.competitor_speed) {
|
||||
case 0: // Very slow
|
||||
if (!(_ai.tick & 8)) return;
|
||||
break;
|
||||
|
||||
case 1: // Slow
|
||||
if (!(_ai.tick & 4)) return;
|
||||
break;
|
||||
|
||||
case 2: // Medium
|
||||
if (!(_ai.tick & 2)) return;
|
||||
break;
|
||||
|
||||
case 3: // Fast
|
||||
if (!(_ai.tick & 1)) return;
|
||||
break;
|
||||
|
||||
case 4: // Very fast
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
/* Check for AI-client (so joining a network with an AI) */
|
||||
if (_ai.network_client) {
|
||||
/* Run the script */
|
||||
AI_DequeueCommands(_ai.network_player);
|
||||
AI_RunTick(_ai.network_player);
|
||||
} else if (!_networking || _network_server) {
|
||||
/* Check if we want to run AIs (server or SP only) */
|
||||
Player *p;
|
||||
|
||||
FOR_ALL_PLAYERS(p) {
|
||||
if (p->is_active && p->is_ai) {
|
||||
/* This should always be true, else something went wrong... */
|
||||
assert(_ai_player[p->index].active);
|
||||
|
||||
/* Run the script */
|
||||
AI_DequeueCommands(p->index);
|
||||
AI_RunTick(p->index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_current_player = OWNER_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* A new AI sees the day of light. You can do here what ever you think is needed.
|
||||
*/
|
||||
void AI_StartNewAI(byte player)
|
||||
{
|
||||
/* Called if a new AI is booted */
|
||||
_ai_player[player].active = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* This AI player died. Give it some chance to make a final puf.
|
||||
*/
|
||||
void AI_PlayerDied(byte player)
|
||||
{
|
||||
/* Called if this AI died */
|
||||
_ai_player[player].active = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize some AI-related stuff.
|
||||
*/
|
||||
void AI_Initialize(void)
|
||||
{
|
||||
memset(&_ai, 0, sizeof(_ai));
|
||||
memset(&_ai_player, 0, sizeof(_ai_player));
|
||||
|
||||
_ai.enabled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deinitializer for AI-related stuff.
|
||||
*/
|
||||
void AI_Uninitialize(void)
|
||||
{
|
||||
Player *p;
|
||||
|
||||
FOR_ALL_PLAYERS(p) {
|
||||
if (p->is_active && p->is_ai) {
|
||||
AI_PlayerDied(p->index);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
#ifndef AI_H
|
||||
#define AI_H
|
||||
|
||||
#include "../functions.h"
|
||||
|
||||
/* How DoCommands look like for an AI */
|
||||
typedef struct AICommand {
|
||||
uint32 tile;
|
||||
uint32 p1;
|
||||
uint32 p2;
|
||||
uint32 procc;
|
||||
|
||||
uint32 dp[20];
|
||||
|
||||
struct AICommand *next;
|
||||
} AICommand;
|
||||
|
||||
/* The struct for an AIScript Player */
|
||||
typedef struct AIPlayer {
|
||||
bool active; //! Is this AI active?
|
||||
AICommand *queue; //! The commands that he has in his queue
|
||||
AICommand *queue_tail; //! The tail of this queue
|
||||
} AIPlayer;
|
||||
|
||||
/* The struct to keep some data about the AI in general */
|
||||
typedef struct AIStruct {
|
||||
/* General */
|
||||
bool enabled; //! Is AI enabled?
|
||||
uint tick; //! The current tick (something like _frame_counter, only for AIs)
|
||||
|
||||
/* For network-clients (a OpenTTD client who acts as an AI connected to a server) */
|
||||
bool network_client; //! Are we a network_client?
|
||||
uint8 network_player; //! The current network player we are connected as
|
||||
} AIStruct;
|
||||
|
||||
VARDEF AIStruct _ai;
|
||||
VARDEF AIPlayer _ai_player[MAX_PLAYERS];
|
||||
|
||||
// ai.c
|
||||
void AI_StartNewAI(byte player);
|
||||
void AI_PlayerDied(byte player);
|
||||
void AI_RunGameLoop(void);
|
||||
void AI_Initialize(void);
|
||||
void AI_Uninitialize(void);
|
||||
int32 AI_DoCommand(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
|
||||
|
||||
#define AI_CHANCE16(a,b) ((uint16) AI_Random() <= (uint16)((65536 * a) / b))
|
||||
#define AI_CHANCE16R(a,b,r) ((uint16)(r = AI_Random()) <= (uint16)((65536 * a) / b))
|
||||
|
||||
/**
|
||||
* The random-function that should be used by ALL AIs.
|
||||
*/
|
||||
static inline uint AI_RandomRange(uint max)
|
||||
{
|
||||
/* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
|
||||
* but we pick InteractiveRandomRange if we are a network_server or network-client.
|
||||
*/
|
||||
if (_networking)
|
||||
return InteractiveRandomRange(max);
|
||||
else
|
||||
return RandomRange(max);
|
||||
}
|
||||
|
||||
/**
|
||||
* The random-function that should be used by ALL AIs.
|
||||
*/
|
||||
static inline uint32 AI_Random(void)
|
||||
{
|
||||
/* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
|
||||
* but we pick InteractiveRandomRange if we are a network_server or network-client.
|
||||
*/
|
||||
if (_networking)
|
||||
return InteractiveRandom();
|
||||
else
|
||||
return Random();
|
||||
}
|
||||
|
||||
#endif /* AI_H */
|
|
@ -25,6 +25,7 @@
|
|||
#include "network_data.h"
|
||||
#include "variables.h"
|
||||
#include "vehicle_gui.h"
|
||||
#include "ai/ai.h"
|
||||
|
||||
// Score info
|
||||
const ScoreInfo _score_info[] = {
|
||||
|
@ -473,6 +474,9 @@ static void PlayersCheckBankrupt(Player *p)
|
|||
ChangeOwnershipOfPlayerItems(owner, 0xFF); // 255 is no owner
|
||||
// Register the player as not-active
|
||||
p->is_active = false;
|
||||
|
||||
if (!IS_HUMAN_PLAYER(owner) && (!_networking || _network_server) && _ai.enabled)
|
||||
AI_PlayerDied(owner);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1249,10 +1253,6 @@ static int32 DeliverGoods(int num_pieces, byte cargo_type, uint16 source, uint16
|
|||
}
|
||||
}
|
||||
|
||||
// Computers get 25% extra profit if they're intelligent.
|
||||
if (_opt.diff.competitor_intelligence>=1 && !IS_HUMAN_PLAYER(_current_player))
|
||||
profit += profit >> 2;
|
||||
|
||||
return profit;
|
||||
}
|
||||
|
||||
|
|
13
openttd.c
13
openttd.c
|
@ -39,6 +39,7 @@
|
|||
#include "signs.h"
|
||||
#include "depot.h"
|
||||
#include "waypoint.h"
|
||||
#include "ai/ai.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
|
@ -50,7 +51,6 @@
|
|||
void GenerateWorld(int mode, uint size_x, uint size_y);
|
||||
void CallLandscapeTick(void);
|
||||
void IncreaseDate(void);
|
||||
void RunOtherPlayersLoop(void);
|
||||
void DoPaletteAnimations(void);
|
||||
void MusicLoop(void);
|
||||
void ResetMusic(void);
|
||||
|
@ -452,6 +452,9 @@ int ttd_main(int argc, char* argv[])
|
|||
/* initialize all variables that are allocated dynamically */
|
||||
InitializeDynamicVariables();
|
||||
|
||||
/* start the AI */
|
||||
AI_Initialize();
|
||||
|
||||
// Sample catalogue
|
||||
DEBUG(misc, 1) ("Loading sound effects...");
|
||||
MxInitialize(11025);
|
||||
|
@ -535,6 +538,9 @@ int ttd_main(int argc, char* argv[])
|
|||
/* uninitialize variables that are allocated dynamic */
|
||||
UnInitializeDynamicVariables();
|
||||
|
||||
/* stop the AI */
|
||||
AI_Uninitialize();
|
||||
|
||||
/* Close all and any open filehandles */
|
||||
FioCloseAll();
|
||||
UnInitializeGame();
|
||||
|
@ -870,10 +876,7 @@ void StateGameLoop(void)
|
|||
CallVehicleTicks();
|
||||
CallLandscapeTick();
|
||||
|
||||
// To bad the AI does not work in multiplayer, because states are not saved
|
||||
// perfectly
|
||||
if (!_networking)
|
||||
RunOtherPlayersLoop();
|
||||
AI_RunGameLoop();
|
||||
|
||||
CallWindowTickEvent();
|
||||
NewsLoop();
|
||||
|
|
|
@ -215,7 +215,7 @@ typedef struct GameDifficulty {
|
|||
int initial_interest;
|
||||
int vehicle_costs;
|
||||
int competitor_speed;
|
||||
int competitor_intelligence;
|
||||
int competitor_intelligence; // no longer in use
|
||||
int vehicle_breakdowns;
|
||||
int subsidy_multiplier;
|
||||
int construction_cost;
|
||||
|
|
50
players.c
50
players.c
|
@ -23,6 +23,7 @@
|
|||
#include "sound.h"
|
||||
#include "network.h"
|
||||
#include "variables.h"
|
||||
#include "ai/ai.h"
|
||||
|
||||
PlayerID _current_player;
|
||||
|
||||
|
@ -473,16 +474,15 @@ static Player *AllocatePlayer(void)
|
|||
Player *DoStartupNewPlayer(bool is_ai)
|
||||
{
|
||||
Player *p;
|
||||
int index, i;
|
||||
int i;
|
||||
|
||||
p = AllocatePlayer();
|
||||
if (p == NULL) return NULL;
|
||||
|
||||
index = p->index;
|
||||
if (p == NULL)
|
||||
return NULL;
|
||||
|
||||
// Make a color
|
||||
p->player_color = GeneratePlayerColor();
|
||||
_player_colors[index] = p->player_color;
|
||||
_player_colors[p->index] = p->player_color;
|
||||
p->name_1 = STR_SV_UNNAMED;
|
||||
p->is_active = true;
|
||||
|
||||
|
@ -492,7 +492,7 @@ Player *DoStartupNewPlayer(bool is_ai)
|
|||
p->ai.state = 5; /* AIS_WANT_NEW_ROUTE */
|
||||
p->share_owners[0] = p->share_owners[1] = p->share_owners[2] = p->share_owners[3] = 0xFF;
|
||||
|
||||
p->avail_railtypes = GetPlayerRailtypes(index);
|
||||
p->avail_railtypes = GetPlayerRailtypes(p->index);
|
||||
p->inaugurated_year = _cur_year;
|
||||
p->face = Random();
|
||||
|
||||
|
@ -510,6 +510,9 @@ Player *DoStartupNewPlayer(bool is_ai)
|
|||
InvalidateWindow(WC_TOOLBAR_MENU, 0);
|
||||
InvalidateWindow(WC_CLIENT_LIST, 0);
|
||||
|
||||
if (is_ai && (!_networking || _network_server) && _ai.enabled)
|
||||
AI_StartNewAI(p->index);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@ -526,9 +529,10 @@ static void MaybeStartNewPlayer(void)
|
|||
|
||||
// count number of competitors
|
||||
n = 0;
|
||||
for(p=_players; p!=endof(_players); p++)
|
||||
FOR_ALL_PLAYERS(p) {
|
||||
if (p->is_active && p->is_ai)
|
||||
n++;
|
||||
}
|
||||
|
||||
// when there's a lot of computers in game, the probability that a new one starts is lower
|
||||
if (n < (uint)_opt.diff.max_no_competitors)
|
||||
|
@ -560,31 +564,10 @@ void OnTick_Players(void)
|
|||
_cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS;
|
||||
if (p->name_1 != 0) GenerateCompanyName(p);
|
||||
|
||||
if (!_networking && _game_mode != GM_MENU && !--_next_competitor_start) {
|
||||
/* XXX -- For now, multiplayer AIs still aren't working, WIP! */
|
||||
//if (_ai.enabled && (!_networking || _network_server) && _game_mode != GM_MENU && !--_next_competitor_start)
|
||||
if (_ai.enabled && !_networking && _game_mode != GM_MENU && !--_next_competitor_start)
|
||||
MaybeStartNewPlayer();
|
||||
}
|
||||
}
|
||||
|
||||
extern void AiNewDoGameLoop(Player *p);
|
||||
|
||||
void RunOtherPlayersLoop(void)
|
||||
{
|
||||
Player *p;
|
||||
|
||||
_is_ai_player = true;
|
||||
|
||||
FOR_ALL_PLAYERS(p) {
|
||||
if (p->is_active && p->is_ai) {
|
||||
_current_player = p->index;
|
||||
if (_patches.ainew_active)
|
||||
AiNewDoGameLoop(p);
|
||||
else
|
||||
AiDoGameLoop(p);
|
||||
}
|
||||
}
|
||||
|
||||
_is_ai_player = false;
|
||||
_current_player = OWNER_NONE;
|
||||
}
|
||||
|
||||
// index is the next parameter in _decode_parameters to set up
|
||||
|
@ -1272,10 +1255,13 @@ static void Load_PLYR(void)
|
|||
int index;
|
||||
while ((index = SlIterateArray()) != -1) {
|
||||
Player *p = GetPlayer(index);
|
||||
p->is_ai = (index != 0);
|
||||
SaveLoad_PLYR(p);
|
||||
_player_colors[index] = p->player_color;
|
||||
UpdatePlayerMoney32(p);
|
||||
|
||||
/* This is needed so an AI is attached to a loaded AI */
|
||||
if (p->is_ai && (!_networking || _network_server) && _ai.enabled)
|
||||
AI_StartNewAI(p->index);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2546,8 +2546,6 @@ static void StationHandleBigTick(Station *st)
|
|||
|
||||
static inline void byte_inc_sat(byte *p) { byte b = *p + 1; if (b != 0) *p = b; }
|
||||
|
||||
static byte _rating_boost[3] = { 0, 31, 63};
|
||||
|
||||
static void UpdateStationRating(Station *st)
|
||||
{
|
||||
GoodsEntry *ge;
|
||||
|
@ -2581,11 +2579,6 @@ static void UpdateStationRating(Station *st)
|
|||
(rating += 13, true);
|
||||
}
|
||||
|
||||
{
|
||||
if (st->owner != OWNER_NONE && !IS_HUMAN_PLAYER(st->owner))
|
||||
rating += _rating_boost[_opt.diff.competitor_intelligence];
|
||||
}
|
||||
|
||||
if (st->owner < MAX_PLAYERS && HASBIT(st->town->statues, st->owner))
|
||||
rating += 26;
|
||||
|
||||
|
|
Loading…
Reference in New Issue