Changeset - r17616:3ed8a6b01283
[Not reviewed]
src/ai/ai_gui.cpp
Show inline comments
 
@@ -42,18 +42,23 @@ enum AIListWindowWidgets {
 
};
 

	
 
/**
 
 * Window that let you choose an available AI.
 
 */
 
struct AIListWindow : public Window {
 
	const AIInfoList *ai_info_list;
 
	int selected;
 
	CompanyID slot;
 
	int line_height; // Height of a row in the matrix widget.
 
	Scrollbar *vscroll;
 
	const AIInfoList *ai_info_list; ///< The list of AIs.
 
	int selected;                   ///< The currently selected AI.
 
	CompanyID slot;                 ///< The company we're selecting a new AI for.
 
	int line_height;                ///< Height of a row in the matrix widget.
 
	Scrollbar *vscroll;             ///< Cache of the vertical scrollbar.
 

	
 
	/**
 
	 * Constructor for the window.
 
	 * @param desc The description of the window.
 
	 * @param slot The company we're changing the AI for.
 
	 */
 
	AIListWindow(const WindowDesc *desc, CompanyID slot) : Window(),
 
		slot(slot)
 
	{
 
		this->ai_info_list = AI::GetUniqueInfoList();
 

	
 
		this->CreateNestedTree(desc);
 
@@ -132,12 +137,15 @@ struct AIListWindow : public Window {
 
				}
 
				break;
 
			}
 
		}
 
	}
 

	
 
	/**
 
	 * Changes the AI of the current slot.
 
	 */
 
	void ChangeAI()
 
	{
 
		if (this->selected == -1) {
 
			AIConfig::GetConfig(slot)->ChangeAI(NULL);
 
		} else {
 
			AIInfoList::const_iterator it = this->ai_info_list->begin();
 
@@ -251,21 +259,26 @@ enum AISettingsWindowWidgest {
 
};
 

	
 
/**
 
 * Window for settings the parameters of an AI.
 
 */
 
struct AISettingsWindow : public Window {
 
	CompanyID slot;
 
	AIConfig *ai_config;
 
	int clicked_button;
 
	bool clicked_increase;
 
	int timeout;
 
	int clicked_row;
 
	int line_height; // Height of a row in the matrix widget.
 
	Scrollbar *vscroll;
 
	CompanyID slot;        ///< The currently show company's setting.
 
	AIConfig *ai_config;   ///< The configuration we're modifying.
 
	int clicked_button;    ///< The button we clicked.
 
	bool clicked_increase; ///< Whether we clicked the increase or decrease button.
 
	int timeout;           ///< Timeout for unclicking the button.
 
	int clicked_row;       ///< The clicked row of settings.
 
	int line_height;       ///< Height of a row in the matrix widget.
 
	Scrollbar *vscroll;    ///< Cache of the vertical scrollbar.
 

	
 
	/**
 
	 * Constructor for the window.
 
	 * @param desc The description of the window.
 
	 * @param slot The company we're changing the settings for.
 
	 */
 
	AISettingsWindow(const WindowDesc *desc, CompanyID slot) : Window(),
 
		slot(slot),
 
		clicked_button(-1),
 
		timeout(0)
 
	{
 
		this->ai_config = AIConfig::GetConfig(slot);
 
@@ -338,12 +351,15 @@ struct AISettingsWindow : public Window 
 

	
 
			DrawString(text_left, text_right, y + WD_MATRIX_TOP, str, colour);
 
			y += this->line_height;
 
		}
 
	}
 

	
 
	/**
 
	 * Check whether we modified the difficulty level or not.
 
	 */
 
	void CheckDifficultyLevel()
 
	{
 
		if (_game_mode == GM_MENU) {
 
			if (_settings_newgame.difficulty.diff_level != 3) {
 
				_settings_newgame.difficulty.diff_level = 3;
 
				ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
 
@@ -553,13 +569,13 @@ static const WindowDesc _ai_config_desc(
 
/**
 
 * Window to configure which AIs will start.
 
 */
 
struct AIConfigWindow : public Window {
 
	CompanyID selected_slot; ///< The currently selected AI slot or \c INVALID_COMPANY.
 
	int line_height;         ///< Height of a single AI-name line.
 
	Scrollbar *vscroll;
 
	Scrollbar *vscroll;      ///< Cache of the vertical scrollbar.
 

	
 
	AIConfigWindow() : Window()
 
	{
 
		this->InitNested(&_ai_config_desc); // Initializes 'this->line_height' as a side effect.
 
		this->vscroll = this->GetScrollbar(AIC_WIDGET_SCROLLBAR);
 
		this->selected_slot = INVALID_COMPANY;
 
@@ -751,28 +767,33 @@ enum AIDebugWindowWidgets {
 
};
 

	
 
/**
 
 * Window with everything an AI prints via AILog.
 
 */
 
struct AIDebugWindow : public QueryStringBaseWindow {
 
	static const int top_offset;    ///< Offset of the text at the top of the ::AID_WIDGET_LOG_PANEL.
 
	static const int bottom_offset; ///< Offset of the text at the bottom of the ::AID_WIDGET_LOG_PANEL.
 
	static const int top_offset;    ///< Offset of the text at the top of the AID_WIDGET_LOG_PANEL.
 
	static const int bottom_offset; ///< Offset of the text at the bottom of the AID_WIDGET_LOG_PANEL.
 

	
 
	static const unsigned int MAX_BREAK_STR_STRING_LENGTH = 256;
 
	static const unsigned int MAX_BREAK_STR_STRING_LENGTH = 256; ///< Maximum length of the break string.
 

	
 
	static CompanyID ai_debug_company;                     ///< The AI that is (was last) being debugged.
 
	int redraw_timer;
 
	int last_vscroll_pos;
 
	bool autoscroll;
 
	bool show_break_box;
 
	int redraw_timer;                                      ///< Timer for redrawing the window, otherwise it'll happen every tick.
 
	int last_vscroll_pos;                                  ///< Last position of the scrolling.
 
	bool autoscroll;                                       ///< Whether automatically scrolling should be enabled or not.
 
	bool show_break_box;                                   ///< Whether the break/debug box is visible.
 
	static bool break_check_enabled;                       ///< Stop an AI when it prints a matching string
 
	static char break_string[MAX_BREAK_STR_STRING_LENGTH]; ///< The string to match to the AI output
 
	static bool case_sensitive_break_check;                ///< Is the matching done case-sensitive
 
	int highlight_row;                                     ///< The output row that matches the given string, or -1
 
	Scrollbar *vscroll;
 
	Scrollbar *vscroll;                                    ///< Cache of the vertical scrollbar.
 

	
 
	/**
 
	 * Constructor for the window.
 
	 * @param desc The description of the window.
 
	 * @param number The window number (actually unused).
 
	 */
 
	AIDebugWindow(const WindowDesc *desc, WindowNumber number) : QueryStringBaseWindow(MAX_BREAK_STR_STRING_LENGTH)
 
	{
 
		this->CreateNestedTree(desc);
 
		this->vscroll = this->GetScrollbar(AID_WIDGET_SCROLLBAR);
 
		this->show_break_box = _settings_client.gui.ai_developer_tools;
 
		this->GetWidget<NWidgetStacked>(AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
 
@@ -966,12 +987,16 @@ struct AIDebugWindow : public QueryStrin
 
				}
 
				break;
 
			}
 
		}
 
	}
 

	
 
	/**
 
	 * Change all settings to select another AI.
 
	 * @param show_ai The new AI to show.
 
	 */
 
	void ChangeToAI(CompanyID show_ai)
 
	{
 
		this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
 
		ai_debug_company = show_ai;
 

	
 
		Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
src/ai/ai_info.hpp
Show inline comments
 
@@ -60,12 +60,16 @@ protected:
 
	class AIScanner *base; ///< AIScanner object that was used to scan this AI (library) info.
 
};
 

	
 
/** All static information from an AI like name, version, etc. */
 
class AIInfo : public AIFileInfo {
 
public:
 
	/**
 
	 * Get the class name, so the script code can create the right code.
 
	 * @return The class name.
 
	 */
 
	static const char *GetClassName() { return "AIInfo"; }
 

	
 
	AIInfo();
 
	~AIInfo();
 

	
 
	/**
src/ai/ai_instance.hpp
Show inline comments
 
@@ -21,12 +21,17 @@ typedef void (AISuspendCallbackProc)(cla
 

	
 
/**
 
 * A throw-class that is given when the VM wants to suspend.
 
 */
 
class AI_VMSuspend {
 
public:
 
	/**
 
	 * Create the suspend exception.
 
	 * @param time The amount of ticks to suspend.
 
	 * @param callback The callback to call when the AI may resume again.
 
	 */
 
	AI_VMSuspend(int time, AISuspendCallbackProc *callback) :
 
		time(time),
 
		callback(callback)
 
	{}
 

	
 
	/**
 
@@ -48,26 +53,39 @@ private:
 

	
 
/**
 
 * A throw-class that is given when the AI made a fatal error.
 
 */
 
class AI_FatalError {
 
public:
 
	/**
 
	 * Creates a "fatal error" exception.
 
	 * @param msg The message describing the cause of the fatal error.
 
	 */
 
	AI_FatalError(const char *msg) :
 
		msg(msg)
 
	{}
 

	
 
	/**
 
	 * The error message associated with the fatal error.
 
	 * @return The error message.
 
	 */
 
	const char *GetErrorMessage() { return msg; }
 

	
 
private:
 
	const char *msg;
 
	const char *msg; ///< The error message.
 
};
 

	
 
/** Runtime information about an AI like a pointer to the squirrel vm and the current state. */
 
class AIInstance {
 
public:
 
	friend class AIObject;
 

	
 
	/**
 
	 * Create a new AI.
 
	 * @param info The AI to create the instance of.
 
	 */
 
	AIInstance(class AIInfo *info);
 
	~AIInstance();
 

	
 
	/**
 
	 * An AI in multiplayer waits for the server to handle his DoCommand.
 
	 *  It keeps waiting for this until this function is called.
src/ai/ai_scanner.hpp
Show inline comments
 
@@ -35,12 +35,16 @@ public:
 

	
 
	/**
 
	 * Register an AI to be put in the available list.
 
	 */
 
	void RegisterAI(class AIInfo *info);
 

	
 
	/**
 
	 * Register the dummy AI.
 
	 * @param info The dummy AI that.
 
	 */
 
	void SetDummyAI(class AIInfo *info) { this->info_dummy = info; }
 

	
 
	/**
 
	 * Select a Random AI.
 
	 */
 
	class AIInfo *SelectRandomAI() const;
 
@@ -76,25 +80,25 @@ public:
 
	void RescanAIDir();
 

	
 
#if defined(ENABLE_NETWORK)
 
	bool HasAI(const struct ContentInfo *ci, bool md5sum);
 
#endif
 
private:
 
	typedef std::map<const char *, class AILibrary *, StringCompare> AILibraryList;
 
	typedef std::map<const char *, class AILibrary *, StringCompare> AILibraryList; ///< Type for the list of libraries.
 

	
 
	/**
 
	 * Scan the AI dir for scripts.
 
	 */
 
	void ScanAIDir();
 

	
 
	/**
 
	 * Reset all allocated lists.
 
	 */
 
	void Reset();
 

	
 
	AIInfo *info_dummy;
 
	AIInfoList info_list;
 
	AIInfoList info_single_list;
 
	AILibraryList library_list;
 
	AIInfo *info_dummy;          ///< The dummy AI.
 
	AIInfoList info_list;        ///< The list of all AIs.
 
	AIInfoList info_single_list; ///< The list of all unique AIs, based on shortname. The best AI (highest version) is shown.
 
	AILibraryList library_list;  ///< The list of libraries.
 
};
 

	
 
#endif /* AI_SCANNER_HPP */
src/ai/api/ai_accounting.hpp
Show inline comments
 
@@ -59,10 +59,10 @@ public:
 
	 * @note when nesting AIAccounting instances all instances' ResetCosts
 
	 *   will always effect on the 'top' instance.
 
	 */
 
	void ResetCosts();
 

	
 
private:
 
	Money last_costs;
 
	Money last_costs; ///< The last cost we did return.
 
};
 

	
 
#endif /* AI_ACCOUNTING_HPP */
src/ai/api/ai_bridge.cpp
Show inline comments
 
@@ -32,24 +32,32 @@
 
/* static */ BridgeID AIBridge::GetBridgeID(TileIndex tile)
 
{
 
	if (!IsBridgeTile(tile)) return (BridgeID)-1;
 
	return (BridgeID)::GetBridgeType(tile);
 
}
 

	
 
/**
 
 * Helper function to connect a just built bridge to nearby roads.
 
 * @param instance The AI we have to built the road for.
 
 */
 
static void _DoCommandReturnBuildBridge2(class AIInstance *instance)
 
{
 
	if (!AIBridge::_BuildBridgeRoad2()) {
 
		AIInstance::DoCommandReturn(instance);
 
		return;
 
	}
 

	
 
	/* This can never happen, as in test-mode this callback is never executed,
 
	 *  and in execute-mode, the other callback is called. */
 
	NOT_REACHED();
 
}
 

	
 
/**
 
 * Helper function to connect a just built bridge to nearby roads.
 
 * @param instance The AI we have to built the road for.
 
 */
 
static void _DoCommandReturnBuildBridge1(class AIInstance *instance)
 
{
 
	if (!AIBridge::_BuildBridgeRoad1()) {
 
		AIInstance::DoCommandReturn(instance);
 
		return;
 
	}
 
@@ -87,25 +95,25 @@ static void _DoCommandReturnBuildBridge1
 
	if (vehicle_type == AIVehicle::VT_RAIL || vehicle_type == AIVehicle::VT_WATER) {
 
		return AIObject::DoCommand(end, start, type | bridge_id, CMD_BUILD_BRIDGE);
 
	}
 

	
 
	AIObject::SetCallbackVariable(0, start);
 
	AIObject::SetCallbackVariable(1, end);
 
	return AIObject::DoCommand(end, start, type | bridge_id, CMD_BUILD_BRIDGE, NULL, &_DoCommandReturnBuildBridge1);
 
	return AIObject::DoCommand(end, start, type | bridge_id, CMD_BUILD_BRIDGE, NULL, &::_DoCommandReturnBuildBridge1);
 
}
 

	
 
/* static */ bool AIBridge::_BuildBridgeRoad1()
 
{
 
	/* Build the piece of road on the 'start' side of the bridge */
 
	TileIndex end = AIObject::GetCallbackVariable(0);
 
	TileIndex start = AIObject::GetCallbackVariable(1);
 

	
 
	DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
 
	DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
 

	
 
	return AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &_DoCommandReturnBuildBridge2);
 
	return AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &::_DoCommandReturnBuildBridge2);
 
}
 

	
 
/* static */ bool AIBridge::_BuildBridgeRoad2()
 
{
 
	/* Build the piece of road on the 'end' side of the bridge */
 
	TileIndex end = AIObject::GetCallbackVariable(0);
src/ai/api/ai_changelog.hpp
Show inline comments
 
@@ -138,13 +138,13 @@
 
 *     used for loading and unloading as long as cargo is transfered from
 
 *     source to destination.
 
 * \li Make AIEngine:CanRefitCargo() not report refittability to mail by
 
 *     default for aircraft. It is not necessarily true. This means that even
 
 *     if the aircraft can carry mail (as secondary cargo) it does not return
 
 *     true if the aircraft cannot carry it as its only cargo.
 
 * \li Improve behaviour of (AIEngine|AIEventEnginePreview)::GetCargoType()
 
 * \li Improve behaviour of AIEngine::GetCargoType(), AIEventEnginePreview::GetCargoType()
 
 *     and AIEngine::CanRefitCargo() for articulated vehicles. For
 
 *     CanRefitCargo true is returned if at least one part can be refitted.
 
 *     For GetCargoType the first most used cargo type is returned.
 
 * \li AIIndustryType::GetConstructionCost() now returns -1 if the industry is
 
 *     neither buildable nor prospectable.
 
 * \li AIEngine::IsValidEngine will now return true if you have at least one
src/ai/api/ai_controller.hpp
Show inline comments
 
@@ -100,17 +100,17 @@ public:
 
	 * @param message The message Squirrel logged.
 
	 * @note Use AILog.Info/Warning/Error instead of 'print'.
 
	 */
 
	static void Print(bool error_msg, const char *message);
 

	
 
private:
 
	typedef std::map<const char *, const char *, StringCompare> LoadedLibraryList;
 
	typedef std::map<const char *, const char *, StringCompare> LoadedLibraryList; ///< The type for loaded libraries.
 

	
 
	uint ticks;
 
	LoadedLibraryList loaded_library;
 
	int loaded_library_count;
 
	uint ticks;                       ///< The amount of ticks we're sleeping.
 
	LoadedLibraryList loaded_library; ///< The libraries we loaded.
 
	int loaded_library_count;         ///< The amount of libraries.
 

	
 
	/**
 
	 * Register all classes that are known inside the NoAI API.
 
	 */
 
	void RegisterClasses();
 

	
src/ai/api/ai_error.hpp
Show inline comments
 
@@ -169,14 +169,14 @@ public:
 
	 * @param message The string representation of this error message, used for debug purposes.
 
	 */
 
	static void RegisterErrorMapString(AIErrorType ai_error_msg, const char *message);
 
#endif /* EXPORT_SKIP */
 

	
 
private:
 
	typedef std::map<StringID, AIErrorType> AIErrorMap;
 
	typedef std::map<AIErrorType, const char *> AIErrorMapString;
 
	typedef std::map<StringID, AIErrorType> AIErrorMap;           ///< The type for mapping between error (internal OpenTTD) StringID to the AI error type.
 
	typedef std::map<AIErrorType, const char *> AIErrorMapString; ///< The type for mapping between error type and textual representation.
 

	
 
	static AIErrorMap error_map;
 
	static AIErrorMapString error_map_string;
 
	static AIErrorMap error_map;              ///< The mapping between error (internal OpenTTD) StringID to the AI error type.
 
	static AIErrorMapString error_map_string; ///< The mapping between error type and textual representation.
 
};
 

	
 
#endif /* AI_ERROR_HPP */
src/ai/api/ai_event.cpp
Show inline comments
 
@@ -11,14 +11,15 @@
 

	
 
#include "../../stdafx.h"
 
#include "ai_event_types.hpp"
 

	
 
#include <queue>
 

	
 
/** The queue of events for an AI. */
 
struct AIEventData {
 
	std::queue<AIEvent *> stack;
 
	std::queue<AIEvent *> stack; ///< The actual queue.
 
};
 

	
 
/* static */ void AIEventController::CreateEventPointer()
 
{
 
	assert(AIObject::GetEventPointer() == NULL);
 

	
src/ai/api/ai_event_types.hpp
Show inline comments
 
@@ -71,15 +71,15 @@ public:
 
	 * Get the reason for crashing
 
	 * @return The reason for crashing
 
	 */
 
	CrashReason GetCrashReason() { return this->crash_reason; }
 

	
 
private:
 
	TileIndex crash_site;
 
	VehicleID vehicle;
 
	CrashReason crash_reason;
 
	TileIndex crash_site;     ///< The location of the crash.
 
	VehicleID vehicle;        ///< The crashed vehicle.
 
	CrashReason crash_reason; ///< The reason for crashing.
 
};
 

	
 
/**
 
 * Event Subsidy Offered, indicating someone offered a subsidy.
 
 */
 
class AIEventSubsidyOffer : public AIEvent {
 
@@ -106,13 +106,13 @@ public:
 
	 * Get the SubsidyID of the subsidy.
 
	 * @return The subsidy id.
 
	 */
 
	SubsidyID GetSubsidyID() { return this->subsidy_id; }
 

	
 
private:
 
	SubsidyID subsidy_id;
 
	SubsidyID subsidy_id; ///< The subsidy that got offered.
 
};
 

	
 
/**
 
 * Event Subsidy Offer Expired, indicating a subsidy will no longer be awarded.
 
 */
 
class AIEventSubsidyOfferExpired : public AIEvent {
 
@@ -139,13 +139,13 @@ public:
 
	 * Get the SubsidyID of the subsidy.
 
	 * @return The subsidy id.
 
	 */
 
	SubsidyID GetSubsidyID() { return this->subsidy_id; }
 

	
 
private:
 
	SubsidyID subsidy_id;
 
	SubsidyID subsidy_id; ///< The subsidy offer that expired.
 
};
 

	
 
/**
 
 * Event Subidy Awarded, indicating a subsidy is awarded to some company.
 
 */
 
class AIEventSubsidyAwarded : public AIEvent {
 
@@ -172,13 +172,13 @@ public:
 
	 * Get the SubsidyID of the subsidy.
 
	 * @return The subsidy id.
 
	 */
 
	SubsidyID GetSubsidyID() { return this->subsidy_id; }
 

	
 
private:
 
	SubsidyID subsidy_id;
 
	SubsidyID subsidy_id; ///< The subsidy that was awared.
 
};
 

	
 
/**
 
 * Event Subsidy Expired, indicating a route that was once subsidized no longer is.
 
 */
 
class AIEventSubsidyExpired : public AIEvent {
 
@@ -205,13 +205,13 @@ public:
 
	 * Get the SubsidyID of the subsidy.
 
	 * @return The subsidy id.
 
	 */
 
	 SubsidyID GetSubsidyID() { return this->subsidy_id; }
 

	
 
private:
 
	SubsidyID subsidy_id;
 
	SubsidyID subsidy_id; ///< The subsidy that expired.
 
};
 

	
 
/**
 
 * Event Engine Preview, indicating a manufacturer offer you to test a new engine.
 
 *  You can get the same information about the offered engine as a real user
 
 *  would see in the offer window. And you can also accept the offer.
 
@@ -275,30 +275,35 @@ public:
 
	 * Get the running cost of the offered engine.
 
	 * @return The running cost of the vehicle per year.
 
	 * @note Cost is per year; divide by 365 to get per day.
 
	 */
 
	Money GetRunningCost();
 

	
 
#ifdef DOXYGEN_SKIP
 
	/**
 
	 * Get the type of the offered engine.
 
	 * @return The type the engine has.
 
	 */
 
#ifdef DOXYGEN_SKIP
 
	AIVehicle::VehicleType GetVehicleType();
 
#else
 
	int32 GetVehicleType();
 
#endif
 

	
 
	/**
 
	 * Accept the engine preview.
 
	 * @return True when the accepting succeeded.
 
	 */
 
	bool AcceptPreview();
 

	
 
private:
 
	EngineID engine;
 
	EngineID engine; ///< The engine the preview is for.
 

	
 
	/**
 
	 * Check whether the engine from this preview is still valid.
 
	 * @return True iff the engine is still valid.
 
	 */
 
	bool IsEngineValid() const;
 
};
 

	
 
/**
 
 * Event Company New, indicating a new company has been created.
 
 */
 
@@ -326,13 +331,13 @@ public:
 
	 * Get the CompanyID of the company that has been created.
 
	 * @return The CompanyID of the company.
 
	 */
 
	AICompany::CompanyID GetCompanyID() { return this->owner; }
 

	
 
private:
 
	AICompany::CompanyID owner;
 
	AICompany::CompanyID owner; ///< The new company.
 
};
 

	
 
/**
 
 * Event Company In Trouble, indicating a company is in trouble and might go
 
 *  bankrupt soon.
 
 */
 
@@ -360,13 +365,13 @@ public:
 
	 * Get the CompanyID of the company that is in trouble.
 
	 * @return The CompanyID of the company in trouble.
 
	 */
 
	AICompany::CompanyID GetCompanyID() { return this->owner; }
 

	
 
private:
 
	AICompany::CompanyID owner;
 
	AICompany::CompanyID owner; ///< The company that is in trouble.
 
};
 

	
 
/**
 
 * Event Company Ask Merger, indicating a company can be bought (cheaply) by you.
 
 */
 
class AIEventCompanyAskMerger : public AIEvent {
 
@@ -408,14 +413,14 @@ public:
 
	 * Take over the company for this merger.
 
	 * @return true if the merger was a success.
 
	 */
 
	bool AcceptMerger();
 

	
 
private:
 
	AICompany::CompanyID owner;
 
	int32 value;
 
	AICompany::CompanyID owner; ///< The company that is in trouble.
 
	int32 value;                ///< The value of the company, i.e. the amount you would pay.
 
};
 

	
 
/**
 
 * Event Company Merger, indicating a company has been bought by another
 
 *  company.
 
 */
 
@@ -454,14 +459,14 @@ public:
 
	 * Get the CompanyID of the new owner.
 
	 * @return The CompanyID of the new owner.
 
	 */
 
	AICompany::CompanyID GetNewCompanyID() { return this->new_owner; }
 

	
 
private:
 
	AICompany::CompanyID old_owner;
 
	AICompany::CompanyID new_owner;
 
	AICompany::CompanyID old_owner; ///< The company that ended to exist.
 
	AICompany::CompanyID new_owner; ///< The company that's the end result of the merger.
 
};
 

	
 
/**
 
 * Event Company Bankrupt, indicating a company has gone bankrupt.
 
 */
 
class AIEventCompanyBankrupt : public AIEvent {
 
@@ -488,13 +493,13 @@ public:
 
	 * Get the CompanyID of the company that has gone bankrupt.
 
	 * @return The CompanyID of the company that has gone bankrupt.
 
	 */
 
	AICompany::CompanyID GetCompanyID() { return this->owner; }
 

	
 
private:
 
	AICompany::CompanyID owner;
 
	AICompany::CompanyID owner; ///< The company that has gone bankrupt.
 
};
 

	
 
/**
 
 * Event Vehicle Lost, indicating a vehicle can't find its way to its destination.
 
 */
 
class AIEventVehicleLost : public AIEvent {
 
@@ -521,13 +526,13 @@ public:
 
	 * Get the VehicleID of the vehicle that is lost.
 
	 * @return The VehicleID of the vehicle that is lost.
 
	 */
 
	VehicleID GetVehicleID() { return this->vehicle_id; }
 

	
 
private:
 
	VehicleID vehicle_id;
 
	VehicleID vehicle_id; ///< The vehicle that is lost.
 
};
 

	
 
/**
 
 * Event VehicleWaitingInDepot, indicating a vehicle has arrived a depot and is now waiting there.
 
 */
 
class AIEventVehicleWaitingInDepot : public AIEvent {
 
@@ -554,13 +559,13 @@ public:
 
	 * Get the VehicleID of the vehicle that is waiting in a depot.
 
	 * @return The VehicleID of the vehicle that is waiting in a depot.
 
	 */
 
	VehicleID GetVehicleID() { return this->vehicle_id; }
 

	
 
private:
 
	VehicleID vehicle_id;
 
	VehicleID vehicle_id; ///< The vehicle that is waiting in the depot.
 
};
 

	
 
/**
 
 * Event Vehicle Unprofitable, indicating a vehicle lost money last year.
 
 */
 
class AIEventVehicleUnprofitable : public AIEvent {
 
@@ -587,13 +592,13 @@ public:
 
	 * Get the VehicleID of the vehicle that lost money.
 
	 * @return The VehicleID of the vehicle that lost money.
 
	 */
 
	VehicleID GetVehicleID() { return this->vehicle_id; }
 

	
 
private:
 
	VehicleID vehicle_id;
 
	VehicleID vehicle_id; ///< The vehicle that is unprofitable.
 
};
 

	
 
/**
 
 * Event Industry Open, indicating a new industry has been created.
 
 */
 
class AIEventIndustryOpen : public AIEvent {
 
@@ -620,13 +625,13 @@ public:
 
	 * Get the IndustryID of the new industry.
 
	 * @return The IndustryID of the industry.
 
	 */
 
	IndustryID GetIndustryID() { return this->industry_id; }
 

	
 
private:
 
	IndustryID industry_id;
 
	IndustryID industry_id; ///< The industry that opened.
 
};
 

	
 
/**
 
 * Event Industry Close, indicating an industry is going to be closed.
 
 */
 
class AIEventIndustryClose : public AIEvent {
 
@@ -653,13 +658,13 @@ public:
 
	 * Get the IndustryID of the closing industry.
 
	 * @return The IndustryID of the industry.
 
	 */
 
	IndustryID GetIndustryID() { return this->industry_id; }
 

	
 
private:
 
	IndustryID industry_id;
 
	IndustryID industry_id; ///< The industry that closed.
 
};
 

	
 
/**
 
 * Event Engine Available, indicating a new engine is available.
 
 */
 
class AIEventEngineAvailable : public AIEvent {
 
@@ -686,13 +691,13 @@ public:
 
	 * Get the EngineID of the new engine.
 
	 * @return The EngineID of the new engine.
 
	 */
 
	EngineID GetEngineID() { return this->engine; }
 

	
 
private:
 
	EngineID engine;
 
	EngineID engine; ///< The engine that became available.
 
};
 

	
 
/**
 
 * Event Station First Vehicle, indicating a station has been visited by a vehicle for the first time.
 
 */
 
class AIEventStationFirstVehicle : public AIEvent {
 
@@ -727,14 +732,14 @@ public:
 
	 * Get the VehicleID of the first vehicle.
 
	 * @return The VehicleID of the first vehicle.
 
	 */
 
	VehicleID GetVehicleID() { return this->vehicle; }
 

	
 
private:
 
	StationID station;
 
	VehicleID vehicle;
 
	StationID station; ///< The station the vehicle arived at.
 
	VehicleID vehicle; ///< The vehicle that arrived at the station.
 
};
 

	
 
/**
 
 * Event Disaster Zeppeliner Crashed, indicating a zeppeliner has crashed on an airport and is blocking the runway.
 
 */
 
class AIEventDisasterZeppelinerCrashed : public AIEvent {
 
@@ -761,13 +766,13 @@ public:
 
	 * Get the StationID of the station containing the affected airport.
 
	 * @return The StationID of the station containing the affected airport.
 
	 */
 
	StationID GetStationID() { return this->station; }
 

	
 
private:
 
	StationID station;
 
	StationID station; ///< The station the zeppeliner crashed.
 
};
 

	
 
/**
 
 * Event Disaster Zeppeliner Cleared, indicating a previously crashed zeppeliner has been removed, and the airport is operating again.
 
 */
 
class AIEventDisasterZeppelinerCleared : public AIEvent {
 
@@ -794,13 +799,13 @@ public:
 
	 * Get the StationID of the station containing the affected airport.
 
	 * @return The StationID of the station containing the affected airport.
 
	 */
 
	StationID GetStationID() { return this->station; }
 

	
 
private:
 
	StationID station;
 
	StationID station; ///< The station the zeppeliner crashed.
 
};
 

	
 
/**
 
 * Event Town Founded, indicating a new town has been created.
 
 */
 
class AIEventTownFounded : public AIEvent {
 
@@ -827,10 +832,10 @@ public:
 
	 * Get the TownID of the town.
 
	 * @return The TownID of the town that was created.
 
	 */
 
	TownID GetTownID() { return this->town; }
 

	
 
private:
 
	TownID town;
 
	TownID town; ///< The town that got founded.
 
};
 

	
 
#endif /* AI_EVENT_TYPES_HPP */
src/ai/api/ai_execmode.hpp
Show inline comments
 
@@ -24,14 +24,14 @@
 
class AIExecMode : public AIObject {
 
public:
 
	/** Get the name of this class to identify it towards squirrel. */
 
	static const char *GetClassName() { return "AIExecMode"; }
 

	
 
private:
 
	AIModeProc *last_mode;
 
	AIObject *last_instance;
 
	AIModeProc *last_mode;   ///< The previous mode we were in.
 
	AIObject *last_instance; ///< The previous instace of the mode.
 

	
 
protected:
 
	/**
 
	 * The callback proc for Execute mode.
 
	 */
 
	static bool ModeProc();
src/ai/api/ai_list.cpp
Show inline comments
 
@@ -16,13 +16,15 @@
 

	
 
/**
 
 * Base class for any AIList sorter.
 
 */
 
class AIListSorter {
 
protected:
 
	AIList *list;
 
	AIList *list;           ///< The list that's being sorted.
 
	bool has_no_more_items; ///< Whether we have more items to iterate over.
 
	int32 item_next;        ///< The next item we will show.
 

	
 
public:
 
	/**
 
	 * Virtual dtor, needed to mute warnings.
 
	 */
 
	virtual ~AIListSorter() { }
 
@@ -42,32 +44,37 @@ public:
 
	 */
 
	virtual int32 Next() = 0;
 

	
 
	/**
 
	 * See if the sorter has reached the end.
 
	 */
 
	virtual bool IsEnd() = 0;
 
	bool IsEnd()
 
	{
 
		return this->list->buckets.empty() || this->has_no_more_items;
 
	}
 

	
 
	/**
 
	 * Callback from the list if an item gets removed.
 
	 */
 
	virtual void Remove(int item) = 0;
 
};
 

	
 
/**
 
 * Sort by value, ascending.
 
 */
 
class AIListSorterValueAscending : public AIListSorter {
 
private:
 
	AIList::AIListBucket::iterator bucket_iter;
 
	AIList::AIItemList *bucket_list;
 
	AIList::AIItemList::iterator bucket_list_iter;
 
	bool has_no_more_items;
 
	int32 item_next;
 
	AIList::AIListBucket::iterator bucket_iter;    ///< The iterator over the list to find the buckets.
 
	AIList::AIItemList *bucket_list;               ///< The current bucket list we're iterator over.
 
	AIList::AIItemList::iterator bucket_list_iter; ///< The iterator over the bucket list.
 

	
 
public:
 
	/**
 
	 * Create a new sorter.
 
	 * @param list The list to sort.
 
	 */
 
	AIListSorterValueAscending(AIList *list)
 
	{
 
		this->list = list;
 
		this->End();
 
	}
 

	
 
@@ -90,12 +97,15 @@ public:
 
	{
 
		this->bucket_list = NULL;
 
		this->has_no_more_items = true;
 
		this->item_next = 0;
 
	}
 

	
 
	/**
 
	 * Find the next item, and store that information.
 
	 */
 
	void FindNext()
 
	{
 
		if (this->bucket_list == NULL) {
 
			this->has_no_more_items = true;
 
			return;
 
		}
 
@@ -129,31 +139,28 @@ public:
 
		/* If we remove the 'next' item, skip to the next */
 
		if (item == this->item_next) {
 
			FindNext();
 
			return;
 
		}
 
	}
 

	
 
	bool IsEnd()
 
	{
 
		return this->list->buckets.empty() || this->has_no_more_items;
 
	}
 
};
 

	
 
/**
 
 * Sort by value, descending.
 
 */
 
class AIListSorterValueDescending : public AIListSorter {
 
private:
 
	AIList::AIListBucket::iterator bucket_iter;
 
	AIList::AIItemList *bucket_list;
 
	AIList::AIItemList::iterator bucket_list_iter;
 
	bool has_no_more_items;
 
	int32 item_next;
 
	AIList::AIListBucket::iterator bucket_iter;    ///< The iterator over the list to find the buckets.
 
	AIList::AIItemList *bucket_list;               ///< The current bucket list we're iterator over.
 
	AIList::AIItemList::iterator bucket_list_iter; ///< The iterator over the bucket list.
 

	
 
public:
 
	/**
 
	 * Create a new sorter.
 
	 * @param list The list to sort.
 
	 */
 
	AIListSorterValueDescending(AIList *list)
 
	{
 
		this->list = list;
 
		this->End();
 
	}
 

	
 
@@ -181,12 +188,15 @@ public:
 
	{
 
		this->bucket_list = NULL;
 
		this->has_no_more_items = true;
 
		this->item_next = 0;
 
	}
 

	
 
	/**
 
	 * Find the next item, and store that information.
 
	 */
 
	void FindNext()
 
	{
 
		if (this->bucket_list == NULL) {
 
			this->has_no_more_items = true;
 
			return;
 
		}
 
@@ -223,29 +233,26 @@ public:
 
		/* If we remove the 'next' item, skip to the next */
 
		if (item == this->item_next) {
 
			FindNext();
 
			return;
 
		}
 
	}
 

	
 
	bool IsEnd()
 
	{
 
		return this->list->buckets.empty() || this->has_no_more_items;
 
	}
 
};
 

	
 
/**
 
 * Sort by item, ascending.
 
 */
 
class AIListSorterItemAscending : public AIListSorter {
 
private:
 
	AIList::AIListMap::iterator item_iter;
 
	bool has_no_more_items;
 
	int32 item_next;
 
	AIList::AIListMap::iterator item_iter; ///< The iterator over the items in the map.
 

	
 
public:
 
	/**
 
	 * Create a new sorter.
 
	 * @param list The list to sort.
 
	 */
 
	AIListSorterItemAscending(AIList *list)
 
	{
 
		this->list = list;
 
		this->End();
 
	}
 

	
 
@@ -264,12 +271,15 @@ public:
 

	
 
	void End()
 
	{
 
		this->has_no_more_items = true;
 
	}
 

	
 
	/**
 
	 * Find the next item, and store that information.
 
	 */
 
	void FindNext()
 
	{
 
		if (this->item_iter == this->list->items.end()) {
 
			this->has_no_more_items = true;
 
			return;
 
		}
 
@@ -293,29 +303,26 @@ public:
 
		/* If we remove the 'next' item, skip to the next */
 
		if (item == this->item_next) {
 
			FindNext();
 
			return;
 
		}
 
	}
 

	
 
	bool IsEnd()
 
	{
 
		return this->list->items.empty() || this->has_no_more_items;
 
	}
 
};
 

	
 
/**
 
 * Sort by item, descending.
 
 */
 
class AIListSorterItemDescending : public AIListSorter {
 
private:
 
	AIList::AIListMap::iterator item_iter;
 
	bool has_no_more_items;
 
	int32 item_next;
 
	AIList::AIListMap::iterator item_iter; ///< The iterator over the items in the map.
 

	
 
public:
 
	/**
 
	 * Create a new sorter.
 
	 * @param list The list to sort.
 
	 */
 
	AIListSorterItemDescending(AIList *list)
 
	{
 
		this->list = list;
 
		this->End();
 
	}
 

	
 
@@ -335,12 +342,15 @@ public:
 

	
 
	void End()
 
	{
 
		this->has_no_more_items = true;
 
	}
 

	
 
	/**
 
	 * Find the next item, and store that information.
 
	 */
 
	void FindNext()
 
	{
 
		if (this->item_iter == this->list->items.end()) {
 
			this->has_no_more_items = true;
 
			return;
 
		}
 
@@ -364,17 +374,12 @@ public:
 
		/* If we remove the 'next' item, skip to the next */
 
		if (item == this->item_next) {
 
			FindNext();
 
			return;
 
		}
 
	}
 

	
 
	bool IsEnd()
 
	{
 
		return this->list->items.empty() || this->has_no_more_items;
 
	}
 
};
 

	
 

	
 

	
 
AIList::AIList()
 
{
src/ai/api/ai_object.cpp
Show inline comments
 
@@ -18,12 +18,16 @@
 
#include "../../tunnelbridge.h"
 

	
 
#include "../ai_storage.hpp"
 
#include "../ai_instance.hpp"
 
#include "ai_error.hpp"
 

	
 
/**
 
 * Get the storage associated with the current AIInstance.
 
 * @return The storage.
 
 */
 
static AIStorage *GetStorage()
 
{
 
	return AIInstance::GetStorage();
 
}
 

	
 
void AIObject::SetDoCommandDelay(uint ticks)
src/ai/api/ai_object.hpp
Show inline comments
 
@@ -145,12 +145,13 @@ protected:
 

	
 
	/**
 
	 * Get the internal value of allow_do_command. This can differ
 
	 * from CanSuspend() if the reason we are not allowed
 
	 * to execute a DoCommand is in squirrel and not the API.
 
	 * In that case use this function to restore the previous value.
 
	 * @return True iff DoCommands are allowed in the current scope.
 
	 */
 
	static bool GetAllowDoCommand();
 

	
 
	/**
 
	 * Get the pointer to store event data in.
 
	 */
src/ai/api/ai_order.cpp
Show inline comments
 
@@ -509,31 +509,31 @@ static void _DoCommandReturnSetOrderFlag
 

	
 
	AIOrderFlags current = GetOrderFlags(vehicle_id, order_position);
 

	
 
	EnforcePrecondition(false, (order_flags & AIOF_GOTO_NEAREST_DEPOT) == (current & AIOF_GOTO_NEAREST_DEPOT));
 

	
 
	if ((current & AIOF_NON_STOP_FLAGS) != (order_flags & AIOF_NON_STOP_FLAGS)) {
 
		return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (order_flags & AIOF_NON_STOP_FLAGS) << 4 | MOF_NON_STOP, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
 
		return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (order_flags & AIOF_NON_STOP_FLAGS) << 4 | MOF_NON_STOP, CMD_MODIFY_ORDER, NULL, &::_DoCommandReturnSetOrderFlags);
 
	}
 

	
 
	switch (order->GetType()) {
 
		case OT_GOTO_DEPOT:
 
			if ((current & AIOF_DEPOT_FLAGS) != (order_flags & AIOF_DEPOT_FLAGS)) {
 
				uint data = DA_ALWAYS_GO;
 
				if (order_flags & AIOF_SERVICE_IF_NEEDED) data = DA_SERVICE;
 
				if (order_flags & AIOF_STOP_IN_DEPOT) data = DA_STOP;
 
				return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (data << 4) | MOF_DEPOT_ACTION, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
 
				return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (data << 4) | MOF_DEPOT_ACTION, CMD_MODIFY_ORDER, NULL, &::_DoCommandReturnSetOrderFlags);
 
			}
 
			break;
 

	
 
		case OT_GOTO_STATION:
 
			if ((current & AIOF_UNLOAD_FLAGS) != (order_flags & AIOF_UNLOAD_FLAGS)) {
 
				return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (order_flags & AIOF_UNLOAD_FLAGS) << 2 | MOF_UNLOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
 
				return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (order_flags & AIOF_UNLOAD_FLAGS) << 2 | MOF_UNLOAD, CMD_MODIFY_ORDER, NULL, &::_DoCommandReturnSetOrderFlags);
 
			}
 
			if ((current & AIOF_LOAD_FLAGS) != (order_flags & AIOF_LOAD_FLAGS)) {
 
				return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (order_flags & AIOF_LOAD_FLAGS) >> 1 | MOF_LOAD, CMD_MODIFY_ORDER, NULL, &_DoCommandReturnSetOrderFlags);
 
				return AIObject::DoCommand(0, vehicle_id | (order_position << 20), (order_flags & AIOF_LOAD_FLAGS) >> 1 | MOF_LOAD, CMD_MODIFY_ORDER, NULL, &::_DoCommandReturnSetOrderFlags);
 
			}
 
			break;
 

	
 
		default: break;
 
	}
 

	
src/ai/api/ai_testmode.hpp
Show inline comments
 
@@ -26,14 +26,14 @@
 
class AITestMode : public AIObject {
 
public:
 
	/** Get the name of this class to identify it towards squirrel. */
 
	static const char *GetClassName() { return "AITestMode"; }
 

	
 
private:
 
	AIModeProc *last_mode;
 
	AIObject *last_instance;
 
	AIModeProc *last_mode;   ///< The previous mode we were in.
 
	AIObject *last_instance; ///< The previous instace of the mode.
 

	
 
protected:
 
	/**
 
	 * The callback proc for Testing mode.
 
	 */
 
	static bool ModeProc();
src/ai/api/ai_tunnel.cpp
Show inline comments
 
@@ -43,24 +43,32 @@
 
		::GetTileSlope(tile, &end_z);
 
	} while (start_z != end_z);
 

	
 
	return tile;
 
}
 

	
 
/**
 
 * Helper function to connect a just built tunnel to nearby roads.
 
 * @param instance The AI we have to built the road for.
 
 */
 
static void _DoCommandReturnBuildTunnel2(class AIInstance *instance)
 
{
 
	if (!AITunnel::_BuildTunnelRoad2()) {
 
		AIInstance::DoCommandReturn(instance);
 
		return;
 
	}
 

	
 
	/* This can never happen, as in test-mode this callback is never executed,
 
	 *  and in execute-mode, the other callback is called. */
 
	NOT_REACHED();
 
}
 

	
 
/**
 
 * Helper function to connect a just built tunnel to nearby roads.
 
 * @param instance The AI we have to built the road for.
 
 */
 
static void _DoCommandReturnBuildTunnel1(class AIInstance *instance)
 
{
 
	if (!AITunnel::_BuildTunnelRoad1()) {
 
		AIInstance::DoCommandReturn(instance);
 
		return;
 
	}
 
@@ -88,25 +96,25 @@ static void _DoCommandReturnBuildTunnel1
 
	/* For rail we do nothing special */
 
	if (vehicle_type == AIVehicle::VT_RAIL) {
 
		return AIObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL);
 
	}
 

	
 
	AIObject::SetCallbackVariable(0, start);
 
	return AIObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL, NULL, &_DoCommandReturnBuildTunnel1);
 
	return AIObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL, NULL, &::_DoCommandReturnBuildTunnel1);
 
}
 

	
 
/* static */ bool AITunnel::_BuildTunnelRoad1()
 
{
 
	/* Build the piece of road on the 'start' side of the tunnel */
 
	TileIndex end = AIObject::GetCallbackVariable(0);
 
	TileIndex start = AITunnel::GetOtherTunnelEnd(end);
 

	
 
	DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
 
	DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
 

	
 
	return AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &_DoCommandReturnBuildTunnel2);
 
	return AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &::_DoCommandReturnBuildTunnel2);
 
}
 

	
 
/* static */ bool AITunnel::_BuildTunnelRoad2()
 
{
 
	/* Build the piece of road on the 'end' side of the tunnel */
 
	TileIndex end = AIObject::GetCallbackVariable(0);
0 comments (0 inline, 0 general)