2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/** @file ai.hpp Base functions for all AIs. */
|
|
|
|
|
|
|
|
#ifndef AI_HPP
|
|
|
|
#define AI_HPP
|
|
|
|
|
2010-02-10 16:24:05 +00:00
|
|
|
#ifdef ENABLE_AI
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "api/ai_event_types.hpp"
|
2009-01-13 22:58:03 +00:00
|
|
|
#include "../date_type.h"
|
2009-02-03 18:08:07 +00:00
|
|
|
#include "../core/string_compare_type.hpp"
|
2009-04-25 23:51:15 +00:00
|
|
|
#include <map>
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-07-31 22:16:34 +00:00
|
|
|
/** A list that maps AI names to their AIInfo object. */
|
2009-02-03 18:08:07 +00:00
|
|
|
typedef std::map<const char *, class AIInfo *, StringCompare> AIInfoList;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-07-31 22:16:34 +00:00
|
|
|
/**
|
|
|
|
* Main AI class. Contains all functions needed to start, stop, save and load AIs.
|
|
|
|
*/
|
2009-01-12 17:11:45 +00:00
|
|
|
class AI {
|
|
|
|
public:
|
2009-01-13 14:00:26 +00:00
|
|
|
/**
|
|
|
|
* The default months AIs start after eachother.
|
|
|
|
*/
|
|
|
|
enum StartNext {
|
2009-01-14 21:32:06 +00:00
|
|
|
START_NEXT_EASY = DAYS_IN_YEAR * 2,
|
|
|
|
START_NEXT_MEDIUM = DAYS_IN_YEAR,
|
|
|
|
START_NEXT_HARD = DAYS_IN_YEAR / 2,
|
2009-01-13 22:36:44 +00:00
|
|
|
START_NEXT_MIN = 1,
|
2009-01-13 18:04:12 +00:00
|
|
|
START_NEXT_MAX = 3600,
|
|
|
|
START_NEXT_DEVIATION = 60,
|
2009-01-13 14:00:26 +00:00
|
|
|
};
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* Is it possible to start a new AI company?
|
|
|
|
* @return True if a new AI company can be started.
|
|
|
|
*/
|
|
|
|
static bool CanStartNew();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a new AI company.
|
|
|
|
* @param company At which slot the AI company should start.
|
2010-01-09 14:41:22 +00:00
|
|
|
* @param rerandomise_ai Whether to rerandomise the configured AI.
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
2010-01-09 14:41:22 +00:00
|
|
|
static void StartNew(CompanyID company, bool rerandomise_ai = true);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called every game-tick to let AIs do something.
|
|
|
|
*/
|
|
|
|
static void GameLoop();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current AI tick.
|
|
|
|
*/
|
|
|
|
static uint GetTick();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop a company to be controlled by an AI.
|
|
|
|
* @param company The company from which the AI needs to detach.
|
2009-06-10 22:05:01 +00:00
|
|
|
* @pre Company::IsValidAiID(company)
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
|
|
|
static void Stop(CompanyID company);
|
|
|
|
|
2010-04-02 17:35:20 +00:00
|
|
|
/**
|
|
|
|
* Suspend an AI for the reminder of the current tick. If the AI is
|
|
|
|
* in a state when it cannot be suspended, it will continue to run
|
|
|
|
* until it can be suspended.
|
|
|
|
* @param company The company for which the AI should be suspended.
|
|
|
|
* @pre Company::IsValidAiID(company)
|
|
|
|
*/
|
|
|
|
static void Suspend(CompanyID company);
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* Kill any and all AIs we manage.
|
|
|
|
*/
|
|
|
|
static void KillAll();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the AI system.
|
|
|
|
*/
|
|
|
|
static void Initialize();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uninitialize the AI system
|
|
|
|
* @param keepConfig Should we keep AIConfigs, or can we free that memory?
|
|
|
|
*/
|
|
|
|
static void Uninitialize(bool keepConfig);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset all AIConfigs, and make them reload their AIInfo.
|
|
|
|
* If the AIInfo could no longer be found, an error is reported to the user.
|
|
|
|
*/
|
|
|
|
static void ResetConfig();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue a new event for an AI.
|
|
|
|
*/
|
|
|
|
static void NewEvent(CompanyID company, AIEvent *event);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Broadcast a new event to all active AIs.
|
|
|
|
*/
|
|
|
|
static void BroadcastNewEvent(AIEvent *event, CompanyID skip_company = MAX_COMPANIES);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save data from an AI to a savegame.
|
|
|
|
*/
|
|
|
|
static void Save(CompanyID company);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load data for an AI from a savegame.
|
|
|
|
*/
|
2009-01-13 01:46:46 +00:00
|
|
|
static void Load(CompanyID company, int version);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-01-13 14:00:26 +00:00
|
|
|
/**
|
2009-01-13 18:04:12 +00:00
|
|
|
* Get the number of days before the next AI should start.
|
2009-01-13 14:00:26 +00:00
|
|
|
*/
|
|
|
|
static int GetStartNextTime();
|
|
|
|
|
2010-07-31 22:16:34 +00:00
|
|
|
/** Wrapper function for AIScanner::GetAIConsoleList */
|
2011-02-07 09:51:16 +00:00
|
|
|
static char *GetConsoleList(char *p, const char *last, bool newest_only = false);
|
2011-01-03 14:52:30 +00:00
|
|
|
/** Wrapper function for AIScanner::GetAIConsoleLibraryList */
|
|
|
|
static char *GetConsoleLibraryList(char *p, const char *last);
|
2010-07-31 22:16:34 +00:00
|
|
|
/** Wrapper function for AIScanner::GetAIInfoList */
|
2009-01-12 17:11:45 +00:00
|
|
|
static const AIInfoList *GetInfoList();
|
2010-07-31 22:16:34 +00:00
|
|
|
/** Wrapper function for AIScanner::GetUniqueAIInfoList */
|
2009-01-20 16:49:10 +00:00
|
|
|
static const AIInfoList *GetUniqueInfoList();
|
2010-07-31 22:16:34 +00:00
|
|
|
/** Wrapper function for AIScanner::FindInfo */
|
2010-01-29 00:03:31 +00:00
|
|
|
static AIInfo *FindInfo(const char *name, int version, bool force_exact_match);
|
2010-07-31 22:16:34 +00:00
|
|
|
/** Wrapper function for AIScanner::ImportLibrary */
|
2009-01-12 17:11:45 +00:00
|
|
|
static bool ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm);
|
2010-07-31 22:16:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rescans all searchpaths for available AIs. If a used AI is no longer
|
|
|
|
* found it is removed from the config.
|
|
|
|
*/
|
2009-01-12 17:11:45 +00:00
|
|
|
static void Rescan();
|
2009-01-17 16:53:32 +00:00
|
|
|
#if defined(ENABLE_NETWORK)
|
2010-07-31 22:16:34 +00:00
|
|
|
/** Wrapper function for AIScanner::HasAI */
|
2009-01-17 16:53:32 +00:00
|
|
|
static bool HasAI(const struct ContentInfo *ci, bool md5sum);
|
|
|
|
#endif
|
2009-01-12 17:11:45 +00:00
|
|
|
private:
|
2010-08-01 19:36:56 +00:00
|
|
|
static uint frame_counter; ///< Tick counter for the AI code
|
|
|
|
static class AIScanner *ai_scanner; ///< AIScanner instance that is used to find AIs
|
2009-01-12 17:11:45 +00:00
|
|
|
};
|
|
|
|
|
2010-02-10 16:24:05 +00:00
|
|
|
#else /* ENABLE_AI */
|
|
|
|
|
|
|
|
#include "../company_type.h"
|
|
|
|
|
|
|
|
#define NewEvent(cid, event) nop()
|
|
|
|
#define BroadcastNewEvent(...) nop()
|
|
|
|
|
|
|
|
class AI {
|
|
|
|
public:
|
|
|
|
static void StartNew(CompanyID company, bool rerandomise_ai = true) {}
|
|
|
|
static void Stop(CompanyID company) {}
|
|
|
|
static void Initialize() {}
|
|
|
|
static void Uninitialize(bool keepConfig) {}
|
|
|
|
static void KillAll() {}
|
|
|
|
static void GameLoop() {}
|
|
|
|
static bool HasAI(const struct ContentInfo *ci, bool md5sum) { return false; }
|
|
|
|
static void Rescan() {}
|
2011-03-07 18:44:36 +00:00
|
|
|
static char *GetConsoleList(char *p, const char *last, bool newest_only = false) { return p; }
|
2010-02-10 16:24:05 +00:00
|
|
|
static void nop() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* ENABLE_AI */
|
2009-01-12 17:11:45 +00:00
|
|
|
#endif /* AI_HPP */
|