This commit is contained in:
SamuXarick 2024-04-19 13:25:53 +01:00 committed by GitHub
commit 0bcfb627a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 202 additions and 118 deletions

View File

@ -27,8 +27,7 @@
#include "sound_func.h"
#include "cheat_type.h"
#include "company_base.h"
#include "ai/ai.hpp"
#include "game/game.hpp"
#include "script/script_trigger.hpp"
#include "company_func.h"
#include "effectvehicle_func.h"
#include "station_base.h"
@ -1351,8 +1350,7 @@ static void CrashAirplane(Aircraft *v)
newsitem = STR_NEWS_AIRCRAFT_CRASH;
}
AI::NewEvent(v->owner, new ScriptEventVehicleCrashed(v->index, vt, st == nullptr ? ScriptEventVehicleCrashed::CRASH_AIRCRAFT_NO_AIRPORT : ScriptEventVehicleCrashed::CRASH_PLANE_LANDING));
Game::NewEvent(new ScriptEventVehicleCrashed(v->index, vt, st == nullptr ? ScriptEventVehicleCrashed::CRASH_AIRCRAFT_NO_AIRPORT : ScriptEventVehicleCrashed::CRASH_PLANE_LANDING));
ScriptTrigger::NewEvent<ScriptEventVehicleCrashed>(v->owner, v->index, vt, st == nullptr ? ScriptEventVehicleCrashed::CRASH_AIRCRAFT_NO_AIRPORT : ScriptEventVehicleCrashed::CRASH_PLANE_LANDING);
NewsType newstype = NT_ACCIDENT;
if (v->owner != _local_company) {
@ -1418,8 +1416,7 @@ static void AircraftEntersTerminal(Aircraft *v)
v->index,
st->index
);
AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
ScriptTrigger::NewEvent<ScriptEventStationFirstVehicle>(v->owner, st->index, v->index);
}
v->BeginLoading();
@ -2062,7 +2059,7 @@ static void AircraftHandleDestTooFar(Aircraft *v, bool too_far)
if (!HasBit(v->flags, VAF_DEST_TOO_FAR)) {
SetBit(v->flags, VAF_DEST_TOO_FAR);
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
AI::NewEvent(v->owner, new ScriptEventAircraftDestTooFar(v->index));
ScriptTrigger::NewEvent<ScriptEventAircraftDestTooFar>(v->owner, v->index);
if (v->owner == _local_company) {
/* Post a news message. */
SetDParam(0, v->index);

View File

@ -20,7 +20,7 @@
#include "core/random_func.hpp"
#include "vehiclelist.h"
#include "road.h"
#include "ai/ai.hpp"
#include "script/script_trigger.hpp"
#include "news_func.h"
#include "strings_func.h"
#include "autoreplace_cmd.h"
@ -477,7 +477,7 @@ static CommandCost ReplaceFreeUnit(Vehicle **single_unit, DoCommandFlag flags, b
*single_unit = new_v;
AI::NewEvent(old_v->owner, new ScriptEventVehicleAutoReplaced(old_v->index, new_v->index));
ScriptTrigger::NewEvent<ScriptEventVehicleAutoReplaced>(old_v->owner, old_v->index, new_v->index);
}
/* Sell the old vehicle */
@ -636,7 +636,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon
/* Success ! */
if ((flags & DC_EXEC) != 0 && new_head != old_head) {
*chain = new_head;
AI::NewEvent(old_head->owner, new ScriptEventVehicleAutoReplaced(old_head->index, new_head->index));
ScriptTrigger::NewEvent<ScriptEventVehicleAutoReplaced>(old_head->owner, old_head->index, new_head->index);
}
/* Transfer cargo of old vehicles and sell them */
@ -705,7 +705,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon
TransferCargo(old_head, new_head, true);
*chain = new_head;
AI::NewEvent(old_head->owner, new ScriptEventVehicleAutoReplaced(old_head->index, new_head->index));
ScriptTrigger::NewEvent<ScriptEventVehicleAutoReplaced>(old_head->owner, old_head->index, new_head->index);
}
/* Sell the old vehicle */

View File

@ -19,8 +19,8 @@
#include "network/network_func.h"
#include "network/network_base.h"
#include "network/network_admin.h"
#include "ai/ai.hpp"
#include "ai/ai_config.hpp"
#include "script/script_trigger.hpp"
#include "company_manager_face.h"
#include "window_func.h"
#include "strings_func.h"
@ -31,7 +31,6 @@
#include "vehicle_base.h"
#include "vehicle_func.h"
#include "smallmap_gui.h"
#include "game/game.hpp"
#include "goal_base.h"
#include "story_base.h"
#include "company_cmd.h"
@ -629,8 +628,7 @@ Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY)
if (is_ai && (!_networking || _network_server)) AI::StartNew(c->index);
AI::BroadcastNewEvent(new ScriptEventCompanyNew(c->index), c->index);
Game::NewEvent(new ScriptEventCompanyNew(c->index));
ScriptTrigger::BroadcastNewEventExceptForCompany<ScriptEventCompanyNew>(c->index, c->index);
return c;
}
@ -740,7 +738,7 @@ static void HandleBankruptcyTakeover(Company *c)
c->bankrupt_timeout = TAKE_OVER_TIMEOUT;
AI::NewEvent(best->index, new ScriptEventCompanyAskMerger(c->index, c->bankrupt_value));
ScriptTrigger::NewEvent<ScriptEventCompanyAskMerger>(best->index, best->index, c->index, c->bankrupt_value);
if (IsInteractiveCompany(best->index)) {
ShowBuyCompanyDialog(c->index, false);
}
@ -959,8 +957,7 @@ CommandCost CmdCompanyCtrl(DoCommandFlag flags, CompanyCtrlAction cca, CompanyID
CompanyID c_index = c->index;
delete c;
AI::BroadcastNewEvent(new ScriptEventCompanyBankrupt(c_index));
Game::NewEvent(new ScriptEventCompanyBankrupt(c_index));
ScriptTrigger::BroadcastNewEvent<ScriptEventCompanyBankrupt>(c_index);
CompanyAdminRemove(c_index, (CompanyRemoveReason)reason);
if (StoryPage::GetNumItems() == 0 || Goal::GetNumItems() == 0) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);

View File

@ -40,8 +40,7 @@
#include "effectvehicle_func.h"
#include "roadveh.h"
#include "train.h"
#include "ai/ai.hpp"
#include "game/game.hpp"
#include "script/script_trigger.hpp"
#include "company_base.h"
#include "core/random_func.hpp"
#include "core/backup_type.hpp"
@ -247,7 +246,7 @@ static bool DisasterTick_Zeppeliner(DisasterVehicle *v)
SetDParam(0, GetStationIndex(v->tile));
AddTileNewsItem(STR_NEWS_DISASTER_ZEPPELIN, NT_ACCIDENT, v->tile);
AI::NewEvent(GetTileOwner(v->tile), new ScriptEventDisasterZeppelinerCrashed(GetStationIndex(v->tile)));
ScriptTrigger::NewEvent<ScriptEventDisasterZeppelinerCrashed>(GetTileOwner(v->tile), GetStationIndex(v->tile));
}
}
@ -265,7 +264,7 @@ static bool DisasterTick_Zeppeliner(DisasterVehicle *v)
if (IsValidTile(v->tile) && IsAirportTile(v->tile)) {
Station *st = Station::GetByTile(v->tile);
CLRBITS(st->airport.flags, RUNWAY_IN_block);
AI::NewEvent(GetTileOwner(v->tile), new ScriptEventDisasterZeppelinerCleared(st->index));
ScriptTrigger::NewEvent<ScriptEventDisasterZeppelinerCleared>(GetTileOwner(v->tile), st->index);
}
v->UpdatePosition(v->x_pos, v->y_pos, GetAircraftFlightLevel(v));
@ -389,8 +388,7 @@ static bool DisasterTick_Ufo(DisasterVehicle *v)
AddTileNewsItem(STR_NEWS_DISASTER_SMALL_UFO, NT_ACCIDENT, u->tile);
AI::NewEvent(u->owner, new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO));
Game::NewEvent(new ScriptEventVehicleCrashed(u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO));
ScriptTrigger::NewEvent<ScriptEventVehicleCrashed>(u->owner, u->index, u->tile, ScriptEventVehicleCrashed::CRASH_RV_UFO);
}
}

View File

@ -15,7 +15,7 @@
#include "news_func.h"
#include "network/network.h"
#include "network/network_func.h"
#include "ai/ai.hpp"
#include "script/script_trigger.hpp"
#include "aircraft.h"
#include "train.h"
#include "newgrf_engine.h"
@ -44,7 +44,6 @@
#include "core/container_func.hpp"
#include "cargo_type.h"
#include "water.h"
#include "game/game.hpp"
#include "cargomonitor.h"
#include "goal_base.h"
#include "story_base.h"
@ -598,8 +597,7 @@ static void CompanyCheckBankrupt(Company *c)
SetDParam(1, STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION);
SetDParamStr(2, cni->company_name);
AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, cni);
AI::BroadcastNewEvent(new ScriptEventCompanyInTrouble(c->index));
Game::NewEvent(new ScriptEventCompanyInTrouble(c->index));
ScriptTrigger::BroadcastNewEvent<ScriptEventCompanyInTrouble>(c->index);
break;
}
@ -2015,8 +2013,7 @@ static void DoAcquireCompany(Company *c, bool hostile_takeover)
SetDParamStr(3, cni->other_company_name);
SetDParam(4, c->bankrupt_value);
AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, cni);
AI::BroadcastNewEvent(new ScriptEventCompanyMerger(ci, _current_company));
Game::NewEvent(new ScriptEventCompanyMerger(ci, _current_company));
ScriptTrigger::BroadcastNewEvent<ScriptEventCompanyMerger>(ci, _current_company);
ChangeOwnershipOfCompanyItems(ci, _current_company);

View File

@ -19,7 +19,7 @@
#include "window_func.h"
#include "autoreplace_gui.h"
#include "string_func.h"
#include "ai/ai.hpp"
#include "script/script_trigger.hpp"
#include "core/pool_func.hpp"
#include "engine_gui.h"
#include "engine_func.h"
@ -955,7 +955,7 @@ static IntervalTimer<TimerGameCalendar> _calendar_engines_daily({TimerGameCalend
* boost that they wouldn't have gotten against other human companies. The check on
* the line below is just to make AIs not notice that they have a preview if they
* cannot build the vehicle. */
if (!IsVehicleTypeDisabled(e->type, true)) AI::NewEvent(e->preview_company, new ScriptEventEnginePreview(i));
if (!IsVehicleTypeDisabled(e->type, true)) ScriptTrigger::NewEvent<ScriptEventEnginePreview>(e->preview_company, e->preview_company, i);
if (IsInteractiveCompany(e->preview_company)) ShowEnginePreviewWindow(i);
}
}
@ -1091,6 +1091,9 @@ static void NewVehicleAvailable(Engine *e)
/* Only broadcast event if AIs are able to build this vehicle type. */
if (!IsVehicleTypeDisabled(e->type, true)) AI::BroadcastNewEvent(new ScriptEventEngineAvailable(index));
/* Only send the event to Game Scripts if engine can be built. */
if (!IsVehicleTypeDisabled(e->type, false)) Game::NewEvent(new ScriptEventEngineAvailable(index));
/* Only provide the "New Vehicle available" news paper entry, if engine can be built. */
if (!IsVehicleTypeDisabled(e->type, false) && (e->info.extra_flags & ExtraEngineFlags::NoNews) == ExtraEngineFlags::None) {
SetDParam(0, GetEngineCategoryName(index));

View File

@ -32,12 +32,11 @@
#include "animated_tile_func.h"
#include "effectvehicle_func.h"
#include "effectvehicle_base.h"
#include "ai/ai.hpp"
#include "script/script_trigger.hpp"
#include "core/pool_func.hpp"
#include "subsidy_func.h"
#include "core/backup_type.hpp"
#include "object_base.h"
#include "game/game.hpp"
#include "error.h"
#include "string_func.h"
#include "industry_cmd.h"
@ -507,8 +506,7 @@ static CommandCost ClearTile_Industry(TileIndex tile, DoCommandFlag flags)
}
if (flags & DC_EXEC) {
AI::BroadcastNewEvent(new ScriptEventIndustryClose(i->index));
Game::NewEvent(new ScriptEventIndustryClose(i->index));
ScriptTrigger::BroadcastNewEvent<ScriptEventIndustryClose>(i->index);
delete i;
}
return CommandCost(EXPENSES_CONSTRUCTION, indspec->GetRemovalCost());
@ -1742,8 +1740,7 @@ static void AdvertiseIndustryOpening(const Industry *ind)
SetDParam(1, ind->town->index);
}
AddIndustryNewsItem(ind_spc->new_industry_text, NT_INDUSTRY_OPEN, ind->index);
AI::BroadcastNewEvent(new ScriptEventIndustryOpen(ind->index));
Game::NewEvent(new ScriptEventIndustryOpen(ind->index));
ScriptTrigger::BroadcastNewEvent<ScriptEventIndustryOpen>(ind->index);
}
/**
@ -2986,8 +2983,7 @@ static void ChangeIndustryProduction(Industry *i, bool monthly)
/* Compute news category */
if (closeit) {
nt = NT_INDUSTRY_CLOSE;
AI::BroadcastNewEvent(new ScriptEventIndustryClose(i->index));
Game::NewEvent(new ScriptEventIndustryClose(i->index));
ScriptTrigger::BroadcastNewEvent<ScriptEventIndustryClose>(i->index);
} else {
switch (WhoCanServiceIndustry(i)) {
case 0: nt = NT_INDUSTRY_NOBODY; break;

View File

@ -24,8 +24,7 @@
#include "timer/timer_game_economy.h"
#include "vehicle_func.h"
#include "sound_func.h"
#include "ai/ai.hpp"
#include "game/game.hpp"
#include "script/script_trigger.hpp"
#include "depot_map.h"
#include "effectvehicle_func.h"
#include "roadstop_base.h"
@ -551,8 +550,7 @@ static void RoadVehCrash(RoadVehicle *v)
{
uint pass = v->Crash();
AI::NewEvent(v->owner, new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_RV_LEVEL_CROSSING));
Game::NewEvent(new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_RV_LEVEL_CROSSING));
ScriptTrigger::NewEvent<ScriptEventVehicleCrashed>(v->owner, v->index, v->tile, ScriptEventVehicleCrashed::CRASH_RV_LEVEL_CROSSING);
SetDParam(0, pass);
StringID newsitem = (pass == 1) ? STR_NEWS_ROAD_VEHICLE_CRASH_DRIVER : STR_NEWS_ROAD_VEHICLE_CRASH;
@ -702,8 +700,7 @@ static void RoadVehArrivesAt(const RoadVehicle *v, Station *st)
v->index,
st->index
);
AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
ScriptTrigger::NewEvent<ScriptEventStationFirstVehicle>(v->owner, st->index, v->index);
}
} else {
/* Check if station was ever visited before */
@ -716,8 +713,7 @@ static void RoadVehArrivesAt(const RoadVehicle *v, Station *st)
v->index,
st->index
);
AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
ScriptTrigger::NewEvent<ScriptEventStationFirstVehicle>(v->owner, st->index, v->index);
}
}
}

View File

@ -20,6 +20,7 @@ add_files(
script_scanner.hpp
script_storage.hpp
script_suspend.hpp
script_trigger.hpp
squirrel.cpp
squirrel.hpp
squirrel_class.hpp

View File

@ -17,6 +17,19 @@
*
* This version is not yet released. The following changes are not set in stone yet.
*
* API additions:
* \li GSGroup::GetOwner
* \li GSEventVehicleLost
* \li GSEventVehicleWaitingInDepot
* \li GSEventVehicleUnprofitable
* \li GSEventEngineAvailable
* \li GSEventDisasterZeppelinerCrashed
* \li GSEventDisasterZeppelinerCleared
* \li GSEventAircraftDestTooFar
* \li GSEventVehicleAutoReplaced
* \li GSEventCompanyAskMerger
* \li GSEventEnginePreview
*
* \b 14.0
*
* API additions:

View File

@ -106,6 +106,7 @@ int32_t ScriptEventEnginePreview::GetVehicleType()
bool ScriptEventEnginePreview::AcceptPreview()
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, ScriptObject::GetCompany() == (::CompanyID)this->owner);
if (!this->IsEngineValid()) return false;
return ScriptObject::Command<CMD_WANT_ENGINE_PREVIEW>::Do(this->engine);
}
@ -113,6 +114,7 @@ bool ScriptEventEnginePreview::AcceptPreview()
bool ScriptEventCompanyAskMerger::AcceptMerger()
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, ScriptObject::GetCompany() == (::CompanyID)this->buyer);
return ScriptObject::Command<CMD_BUY_COMPANY>::Do((::CompanyID)this->owner, false);
}

View File

@ -214,16 +214,18 @@ private:
* 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.
* @api ai
* @api ai game
*/
class ScriptEventEnginePreview : public ScriptEvent {
public:
#ifndef DOXYGEN_API
/**
* @param owner The company being offered the test engine.
* @param engine The engine offered to test.
*/
ScriptEventEnginePreview(EngineID engine) :
ScriptEventEnginePreview(Owner owner, EngineID engine) :
ScriptEvent(ET_ENGINE_PREVIEW),
owner((ScriptCompany::CompanyID)owner),
engine(engine)
{}
#endif /* DOXYGEN_API */
@ -235,6 +237,13 @@ public:
*/
static ScriptEventEnginePreview *Convert(ScriptEvent *instance) { return (ScriptEventEnginePreview *)instance; }
/**
* Get the company being offered the test engine.
* @return The company being offered to test the engine.
* @api -ai
*/
ScriptCompany::CompanyID GetCompanyID() { return this->owner; }
/**
* Get the name of the offered engine.
* @return The name the engine has.
@ -295,7 +304,8 @@ public:
bool AcceptPreview();
private:
EngineID engine; ///< The engine the preview is for.
ScriptCompany::CompanyID owner; ///< The company the engine preview is for.
EngineID engine; ///< The engine the preview is for.
/**
* Check whether the engine from this preview is still valid.
@ -373,17 +383,19 @@ private:
/**
* Event Company Ask Merger, indicating a company can be bought (cheaply) by you.
* @api ai
* @api ai game
*/
class ScriptEventCompanyAskMerger : public ScriptEvent {
public:
#ifndef DOXYGEN_API
/**
* @param buyer The company that can buy.
* @param owner The company that can be bought.
* @param value The value/costs of buying the company.
*/
ScriptEventCompanyAskMerger(Owner owner, Money value) :
ScriptEventCompanyAskMerger(Owner buyer, Owner owner, Money value) :
ScriptEvent(ET_COMPANY_ASK_MERGER),
buyer((ScriptCompany::CompanyID)buyer),
owner((ScriptCompany::CompanyID)owner),
value(value)
{}
@ -396,6 +408,14 @@ public:
*/
static ScriptEventCompanyAskMerger *Convert(ScriptEvent *instance) { return (ScriptEventCompanyAskMerger *)instance; }
/**
* Get the CompanyID of the company that can purchase the other.
* @return The CompanyID of the company that can purchase.
* @note If the company is bought this will become invalid.
* @api -ai
*/
ScriptCompany::CompanyID GetBuyerID() { return this->buyer; }
/**
* Get the CompanyID of the company that can be bought.
* @return The CompanyID of the company that can be bought.
@ -417,8 +437,9 @@ public:
bool AcceptMerger();
private:
ScriptCompany::CompanyID buyer; ///< The company that is buying.
ScriptCompany::CompanyID owner; ///< The company that is in trouble.
Money value; ///< The value of the company, i.e. the amount you would pay.
Money value; ///< The value of the company in trouble, i.e. the amount you would pay.
};
/**
@ -502,7 +523,7 @@ private:
/**
* Event Vehicle Lost, indicating a vehicle can't find its way to its destination.
* @api ai
* @api ai game
*/
class ScriptEventVehicleLost : public ScriptEvent {
public:
@ -535,7 +556,7 @@ private:
/**
* Event VehicleWaitingInDepot, indicating a vehicle has arrived a depot and is now waiting there.
* @api ai
* @api ai game
*/
class ScriptEventVehicleWaitingInDepot : public ScriptEvent {
public:
@ -568,7 +589,7 @@ private:
/**
* Event Vehicle Unprofitable, indicating a vehicle lost money last year.
* @api ai
* @api ai game
*/
class ScriptEventVehicleUnprofitable : public ScriptEvent {
public:
@ -667,7 +688,7 @@ private:
/**
* Event Engine Available, indicating a new engine is available.
* @api ai
* @api ai game
*/
class ScriptEventEngineAvailable : public ScriptEvent {
public:
@ -742,7 +763,7 @@ private:
/**
* Event Disaster Zeppeliner Crashed, indicating a zeppeliner has crashed on an airport and is blocking the runway.
* @api ai
* @api ai game
*/
class ScriptEventDisasterZeppelinerCrashed : public ScriptEvent {
public:
@ -775,7 +796,7 @@ private:
/**
* Event Disaster Zeppeliner Cleared, indicating a previously crashed zeppeliner has been removed, and the airport is operating again.
* @api ai
* @api ai game
*/
class ScriptEventDisasterZeppelinerCleared : public ScriptEvent {
public:
@ -843,7 +864,7 @@ private:
* Event AircraftDestTooFar, indicating the next destination of an aircraft is too far away.
* This event can be triggered when the current order of an aircraft changes, usually either when
* loading is done or when switched manually.
* @api ai
* @api ai game
*/
class ScriptEventAircraftDestTooFar : public ScriptEvent {
public:
@ -1111,7 +1132,7 @@ public:
/**
* Event VehicleAutoReplaced, indicating a vehicle has been auto replaced.
* @api ai
* @api ai game
*/
class ScriptEventVehicleAutoReplaced : public ScriptEvent {
public:

View File

@ -27,7 +27,14 @@
{
EnforceDeityOrCompanyModeValid(false);
const Group *g = ::Group::GetIfValid(group_id);
return g != nullptr && g->owner == ScriptObject::GetCompany();
return g != nullptr && (g->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity());
}
/* static */ ScriptCompany::CompanyID ScriptGroup::GetOwner(GroupID group_id)
{
if (!IsValidGroup(group_id)) return ScriptCompany::COMPANY_INVALID;
return (ScriptCompany::CompanyID)::Group::Get(group_id)->owner;
}
/* static */ ScriptGroup::GroupID ScriptGroup::CreateGroup(ScriptVehicle::VehicleType vehicle_type, GroupID parent_group_id)
@ -110,23 +117,29 @@
/* static */ SQInteger ScriptGroup::GetNumEngines(GroupID group_id, EngineID engine_id)
{
EnforceCompanyModeValid(-1);
if (group_id == GROUP_DEFAULT || group_id == GROUP_ALL) EnforceCompanyModeValid(-1);
EnforceDeityOrCompanyModeValid(-1);
if (!ScriptEngine::IsValidEngine(engine_id)) return -1;
bool valid_group = IsValidGroup(group_id);
if (!valid_group && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return -1;
if (valid_group && ScriptEngine::GetVehicleType(engine_id) != GetVehicleType(group_id)) return -1;
return GetGroupNumEngines(ScriptObject::GetCompany(), group_id, engine_id);
CompanyID company = (valid_group && ScriptCompanyMode::IsDeity()) ? ::Group::Get(group_id)->owner : ScriptObject::GetCompany();
return GetGroupNumEngines(company, group_id, engine_id);
}
/* static */ SQInteger ScriptGroup::GetNumVehicles(GroupID group_id, ScriptVehicle::VehicleType vehicle_type)
{
EnforceCompanyModeValid(-1);
if (group_id == GROUP_DEFAULT || group_id == GROUP_ALL) EnforceCompanyModeValid(-1);
EnforceDeityOrCompanyModeValid(-1);
bool valid_group = IsValidGroup(group_id);
if (!valid_group && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return -1;
if (!valid_group && (vehicle_type < ScriptVehicle::VT_RAIL || vehicle_type > ScriptVehicle::VT_AIR)) return -1;
return GetGroupNumVehicle(ScriptObject::GetCompany(), group_id, valid_group ? ::Group::Get(group_id)->vehicle_type : (::VehicleType)vehicle_type);
CompanyID company = (valid_group && ScriptCompanyMode::IsDeity()) ? ::Group::Get(group_id)->owner : ScriptObject::GetCompany();
return GetGroupNumVehicle(company, group_id, valid_group ? ::Group::Get(group_id)->vehicle_type : (::VehicleType)vehicle_type);
}
/* static */ bool ScriptGroup::MoveVehicle(GroupID group_id, VehicleID vehicle_id)
@ -163,10 +176,14 @@
/* static */ EngineID ScriptGroup::GetEngineReplacement(GroupID group_id, EngineID engine_id)
{
EnforceCompanyModeValid(::INVALID_ENGINE);
if (!IsValidGroup(group_id) && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return ::INVALID_ENGINE;
if (group_id == GROUP_DEFAULT || group_id == GROUP_ALL) EnforceCompanyModeValid(::INVALID_ENGINE);
EnforceDeityOrCompanyModeValid(::INVALID_ENGINE);
bool valid_group = IsValidGroup(group_id);
if (!valid_group && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return ::INVALID_ENGINE;
return ::EngineReplacementForCompany(Company::Get(ScriptObject::GetCompany()), engine_id, group_id);
CompanyID company = (valid_group && ScriptCompanyMode::IsDeity()) ? ::Group::Get(group_id)->owner : ScriptObject::GetCompany();
return ::EngineReplacementForCompany(Company::Get(company), engine_id, group_id);
}
/* static */ bool ScriptGroup::StopAutoReplace(GroupID group_id, EngineID engine_id)

View File

@ -37,6 +37,15 @@ public:
*/
static bool IsValidGroup(GroupID group_id);
/**
* Get the owner of a group.
* @param group_id The group to get the owner of.
* @pre IsValidGroup(group_id).
* @return The owner the group has.
* @api -ai
*/
static ScriptCompany::CompanyID GetOwner(GroupID group_id);
/**
* Create a new group.
* @param vehicle_type The type of vehicle to create a group for.
@ -131,7 +140,7 @@ public:
* @pre ScriptEngine::IsValidEngine(engine_id).
* @pre (IsValidGroup(group_id) && ScriptEngine::GetVehicleType(engine_id) == GetVehicleType(group_id)) ||
group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
* @game @pre ScriptCompanyMode::IsValid().
* @game @pre ScriptCompanyMode::IsValid() when group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
* @return The number of engines with id engine_id in the group with id group_id.
*/
static SQInteger GetNumEngines(GroupID group_id, EngineID engine_id);
@ -143,7 +152,7 @@ public:
* @pre IsValidGroup(group_id) || group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
* @pre IsValidGroup(group_id) || vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL ||
* vehicle_type == ScriptVehicle::VT_WATER || vehicle_type == ScriptVehicle::VT_AIR
* @game @pre ScriptCompanyMode::IsValid().
* @game @pre ScriptCompanyMode::IsValid() when group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
* @return The total number of vehicles in the group with id group_id and it's sub-groups.
* @note If the group is valid (neither GROUP_ALL nor GROUP_DEFAULT), the value of
* vehicle_type is retrieved from the group itself and not from the input value.
@ -191,7 +200,7 @@ public:
* @param engine_id_new The engine id to replace with.
* @pre IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL.
* @pre ScriptEngine.IsBuildable(engine_id_new).
* @game @pre ScriptCompanyMode::IsValid().
* @game @pre ScriptCompanyMode::IsValid() when group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
* @return True if and if the replacing was successfully started.
* @note To stop autoreplacing engine_id_old, call StopAutoReplace(group_id, engine_id_old).
*/

View File

@ -16,9 +16,12 @@
ScriptGroupList::ScriptGroupList(HSQUIRRELVM vm)
{
EnforceCompanyModeValid_Void();
EnforceDeityOrCompanyModeValid_Void();
bool is_deity = ScriptCompanyMode::IsDeity();
CompanyID owner = ScriptObject::GetCompany();
ScriptList::FillList<Group>(vm, this,
[owner](const Group *g) { return g->owner == owner; }
[owner, is_deity](const Group *g) { return g->owner == owner || is_deity; }
);
}

View File

@ -21,9 +21,6 @@
class ScriptGroupList : public ScriptList {
public:
#ifdef DOXYGEN_API
/**
* @game @pre ScriptCompanyMode::IsValid().
*/
ScriptGroupList();
/**

View File

@ -106,26 +106,28 @@ ScriptVehicleList_SharedOrders::ScriptVehicleList_SharedOrders(VehicleID vehicle
ScriptVehicleList_Group::ScriptVehicleList_Group(GroupID group_id)
{
EnforceCompanyModeValid_Void();
EnforceDeityOrCompanyModeValid_Void();
if (!ScriptGroup::IsValidGroup((ScriptGroup::GroupID)group_id)) return;
bool is_deity = ScriptCompanyMode::IsDeity();
CompanyID owner = ScriptObject::GetCompany();
ScriptList::FillList<Vehicle>(this,
[owner](const Vehicle *v) { return v->owner == owner && v->IsPrimaryVehicle(); },
[owner, is_deity](const Vehicle *v) { return (v->owner == owner || is_deity) && v->IsPrimaryVehicle(); },
[group_id](const Vehicle *v) { return v->group_id == group_id; }
);
}
ScriptVehicleList_DefaultGroup::ScriptVehicleList_DefaultGroup(ScriptVehicle::VehicleType vehicle_type)
{
EnforceCompanyModeValid_Void();
EnforceDeityOrCompanyModeValid_Void();
if (vehicle_type < ScriptVehicle::VT_RAIL || vehicle_type > ScriptVehicle::VT_AIR) return;
bool is_deity = ScriptCompanyMode::IsDeity();
CompanyID owner = ScriptObject::GetCompany();
ScriptList::FillList<Vehicle>(this,
[owner](const Vehicle *v) { return v->owner == owner && v->IsPrimaryVehicle(); },
[owner, is_deity](const Vehicle *v) { return (v->owner == owner || is_deity) && v->IsPrimaryVehicle(); },
[vehicle_type](const Vehicle *v) { return v->type == (::VehicleType)vehicle_type && v->group_id == ScriptGroup::GROUP_DEFAULT; }
);
}

View File

@ -0,0 +1,51 @@
/*
* 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/>.
*/
/** @file script_trigger.hpp Functionality to trigger events in AI and game scripts. */
#ifndef SCRIPT_TRIGGER_HPP
#define SCRIPT_TRIGGER_HPP
#include "../ai/ai.hpp"
#include "../game/game.hpp"
/**
* Main Script class. Contains functions needed to handle Script Events.
*/
class ScriptTrigger {
public:
/**
* Queue two new events, one for an AI, the other for the Game Script.
* @param company The company receiving the event.
*/
template <class ScriptEventType, typename ... Args>
static void NewEvent(CompanyID company, Args ... args) {
AI::NewEvent(company, new ScriptEventType(args...));
Game::NewEvent(new ScriptEventType(args...));
}
/**
* Broadcast a new event to all active AIs, and to the Game Script.
*/
template <class ScriptEventType, typename ... Args>
static void BroadcastNewEvent(Args ... args) {
AI::BroadcastNewEvent(new ScriptEventType(args...));
Game::NewEvent(new ScriptEventType(args...));
}
/**
* Broadcast a new event to all active AIs, and to the Game Script, except to one AI.
* @param skip_company The company to skip broadcasting for.
*/
template <class ScriptEventType, typename ... Args>
static void BroadcastNewEventExceptForCompany(CompanyID skip_company, Args ... args) {
AI::BroadcastNewEvent(new ScriptEventType(args...), skip_company);
Game::NewEvent(new ScriptEventType(args...));
}
};
#endif /* SCRIPT_TRIGGER_HPP */

View File

@ -27,8 +27,7 @@
#include "timer/timer_game_economy.h"
#include "vehicle_func.h"
#include "sound_func.h"
#include "ai/ai.hpp"
#include "game/game.hpp"
#include "script/script_trigger.hpp"
#include "engine_base.h"
#include "company_base.h"
#include "tunnelbridge_map.h"
@ -490,8 +489,7 @@ static void ShipArrivesAt(const Vehicle *v, Station *st)
v->index,
st->index
);
AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
ScriptTrigger::NewEvent<ScriptEventStationFirstVehicle>(v->owner, st->index, v->index);
}
}

View File

@ -12,7 +12,7 @@
#include "industry.h"
#include "town.h"
#include "news_func.h"
#include "ai/ai.hpp"
#include "script/script_trigger.hpp"
#include "station_base.h"
#include "strings_func.h"
#include "window_func.h"
@ -21,7 +21,6 @@
#include "core/pool_func.hpp"
#include "core/random_func.hpp"
#include "core/container_func.hpp"
#include "game/game.hpp"
#include "command_func.h"
#include "string_func.h"
#include "tile_cmd.h"
@ -60,8 +59,7 @@ void Subsidy::AwardTo(CompanyID company)
reftype.first, this->src, reftype.second, this->dst,
company_name
);
AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index));
Game::NewEvent(new ScriptEventSubsidyAwarded(this->index));
ScriptTrigger::BroadcastNewEvent<ScriptEventSubsidyAwarded>(this->index);
InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
}
@ -226,8 +224,7 @@ void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType ds
AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);
SetPartOfSubsidyFlag(s->src_type, s->src, POS_SRC);
SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST);
AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s->index));
Game::NewEvent(new ScriptEventSubsidyOffer(s->index));
ScriptTrigger::BroadcastNewEvent<ScriptEventSubsidyOffer>(s->index);
InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
}
@ -490,15 +487,13 @@ static IntervalTimer<TimerGameEconomy> _economy_subsidies_monthly({TimerGameEcon
if (!s->IsAwarded()) {
std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);
AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);
AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s->index));
Game::NewEvent(new ScriptEventSubsidyOfferExpired(s->index));
ScriptTrigger::BroadcastNewEvent<ScriptEventSubsidyOfferExpired>(s->index);
} else {
if (s->awarded == _local_company) {
std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);
AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);
}
AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index));
Game::NewEvent(new ScriptEventSubsidyExpired(s->index));
ScriptTrigger::BroadcastNewEvent<ScriptEventSubsidyExpired>(s->index);
}
delete s;
modified = true;

View File

@ -46,8 +46,7 @@
#include "depot_base.h"
#include "object_map.h"
#include "object_base.h"
#include "ai/ai.hpp"
#include "game/game.hpp"
#include "script/script_trigger.hpp"
#include "town_cmd.h"
#include "landscape_cmd.h"
#include "road_cmd.h"
@ -2185,8 +2184,7 @@ std::tuple<CommandCost, Money, TownID> CmdFoundTown(DoCommandFlag flags, TileInd
AddTileNewsItem(STR_NEWS_NEW_TOWN, NT_INDUSTRY_OPEN, tile, company_name);
}
AI::BroadcastNewEvent(new ScriptEventTownFounded(t->index));
Game::NewEvent(new ScriptEventTownFounded(t->index));
ScriptTrigger::BroadcastNewEvent<ScriptEventTownFounded>(t->index);
}
}
return { cost, 0, new_town };
@ -3272,8 +3270,7 @@ static CommandCost TownActionRoadRebuild(Town *t, DoCommandFlag flags)
AddNewsItem(
TimerGameEconomy::UsingWallclockUnits() ? STR_NEWS_ROAD_REBUILDING_MINUTES : STR_NEWS_ROAD_REBUILDING_MONTHS,
NT_GENERAL, NF_NORMAL, NR_TOWN, t->index, NR_NONE, UINT32_MAX, company_name);
AI::BroadcastNewEvent(new ScriptEventRoadReconstruction((ScriptCompany::CompanyID)(Owner)_current_company, t->index));
Game::NewEvent(new ScriptEventRoadReconstruction((ScriptCompany::CompanyID)(Owner)_current_company, t->index));
ScriptTrigger::BroadcastNewEvent<ScriptEventRoadReconstruction>((ScriptCompany::CompanyID)(Owner)_current_company, t->index);
}
return CommandCost();
}
@ -3427,8 +3424,7 @@ static CommandCost TownActionBuyRights(Town *t, DoCommandFlag flags)
SetDParam(2, t->index);
SetDParamStr(3, cni->company_name);
AddNewsItem(STR_MESSAGE_NEWS_FORMAT, NT_GENERAL, NF_COMPANY, NR_TOWN, t->index, NR_NONE, UINT32_MAX, cni);
AI::BroadcastNewEvent(new ScriptEventExclusiveTransportRights((ScriptCompany::CompanyID)(Owner)_current_company, t->index));
Game::NewEvent(new ScriptEventExclusiveTransportRights((ScriptCompany::CompanyID)(Owner)_current_company, t->index));
ScriptTrigger::BroadcastNewEvent<ScriptEventExclusiveTransportRights>((ScriptCompany::CompanyID)(Owner)_current_company, t->index);
}
return CommandCost();
}

View File

@ -22,8 +22,7 @@
#include "viewport_func.h"
#include "vehicle_func.h"
#include "sound_func.h"
#include "ai/ai.hpp"
#include "game/game.hpp"
#include "script/script_trigger.hpp"
#include "newgrf_station.h"
#include "effectvehicle_func.h"
#include "network/network.h"
@ -3014,8 +3013,7 @@ static void TrainEnterStation(Train *v, StationID station)
v->index,
st->index
);
AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
ScriptTrigger::NewEvent<ScriptEventStationFirstVehicle>(v->owner, st->index, v->index);
}
v->force_proceed = TFP_NONE;
@ -3152,8 +3150,7 @@ static uint TrainCrashed(Train *v)
/* do not crash train twice */
if (!(v->vehstatus & VS_CRASHED)) {
num = v->Crash();
AI::NewEvent(v->owner, new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_TRAIN));
Game::NewEvent(new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_TRAIN));
ScriptTrigger::NewEvent<ScriptEventVehicleCrashed>(v->owner, v->index, v->tile, ScriptEventVehicleCrashed::CRASH_TRAIN);
}
/* Try to re-reserve track under already crashed train too.

View File

@ -29,7 +29,7 @@
#include "autoreplace_func.h"
#include "autoreplace_gui.h"
#include "station_base.h"
#include "ai/ai.hpp"
#include "script/script_trigger.hpp"
#include "depot_func.h"
#include "network/network.h"
#include "core/pool_func.hpp"
@ -815,7 +815,7 @@ void Vehicle::HandlePathfindingResult(bool path_found)
this->ResetDepotUnbunching();
/* Notify user about the event. */
AI::NewEvent(this->owner, new ScriptEventVehicleLost(this->index));
ScriptTrigger::NewEvent<ScriptEventVehicleLost>(this->owner, this->index);
if (_settings_client.gui.lost_vehicle_warn && this->owner == _local_company) {
SetDParam(0, this->index);
AddVehicleAdviceNewsItem(STR_NEWS_VEHICLE_IS_LOST, this->index);
@ -1661,7 +1661,7 @@ void VehicleEnterDepot(Vehicle *v)
SetDParam(0, v->index);
AddVehicleAdviceNewsItem(STR_NEWS_TRAIN_IS_WAITING + v->type, v->index);
}
AI::NewEvent(v->owner, new ScriptEventVehicleWaitingInDepot(v->index));
ScriptTrigger::NewEvent<ScriptEventVehicleWaitingInDepot>(v->owner, v->index);
}
/* If we've entered our unbunching depot, record the round trip duration. */
@ -3026,7 +3026,7 @@ static IntervalTimer<TimerGameEconomy> _economy_vehicles_yearly({TimerGameEconom
TimerGameEconomy::UsingWallclockUnits() ? STR_NEWS_VEHICLE_UNPROFITABLE_PERIOD : STR_NEWS_VEHICLE_UNPROFITABLE_YEAR,
v->index);
}
AI::NewEvent(v->owner, new ScriptEventVehicleUnprofitable(v->index));
ScriptTrigger::NewEvent<ScriptEventVehicleUnprofitable>(v->owner, v->index);
}
v->profit_last_year = v->profit_this_year;

View File

@ -28,8 +28,7 @@
#include "effectvehicle_func.h"
#include "tunnelbridge_map.h"
#include "station_base.h"
#include "ai/ai.hpp"
#include "game/game.hpp"
#include "script/script_trigger.hpp"
#include "core/random_func.hpp"
#include "core/backup_type.hpp"
#include "timer/timer_game_calendar.h"
@ -992,8 +991,7 @@ static void FloodVehicle(Vehicle *v)
{
uint pass = v->Crash(true);
AI::NewEvent(v->owner, new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_FLOODED));
Game::NewEvent(new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_FLOODED));
ScriptTrigger::NewEvent<ScriptEventVehicleCrashed>(v->owner, v->index, v->tile, ScriptEventVehicleCrashed::CRASH_FLOODED);
SetDParam(0, pass);
AddTileNewsItem(STR_NEWS_DISASTER_FLOOD_VEHICLE, NT_ACCIDENT, v->tile);
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);