@@ -358,12 +358,21 @@ void sq_setdebughook(HSQUIRRELVM v);
#define sq_isclass(o) ((o)._type==OT_CLASS)
#define sq_isinstance(o) ((o)._type==OT_INSTANCE)
#define sq_isbool(o) ((o)._type==OT_BOOL)
#define sq_isweakref(o) ((o)._type==OT_WEAKREF)
#define sq_type(o) ((o)._type)
/* Limit the total number of ops that can be consumed by an operation */
struct SQOpsLimiter {
SQOpsLimiter(HSQUIRRELVM v, SQInteger ops, const char *label);
~SQOpsLimiter();
private:
HSQUIRRELVM _v;
SQInteger _ops;
};
/* deprecated */
#define sq_createslot(v,n) sq_newslot(v,n,SQFalse)
#define SQ_OK (0)
#define SQ_ERROR (-1)
@@ -1320,6 +1320,19 @@ void *sq_realloc(void* p,SQUnsignedInteg
void sq_free(void *p,SQUnsignedInteger size)
{
SQ_FREE(p,size);
}
SQOpsLimiter::SQOpsLimiter(HSQUIRRELVM v, SQInteger ops, const char *label) : _v(v)
this->_ops = v->_ops_till_suspend_error_threshold;
if (this->_ops == INT64_MIN) {
v->_ops_till_suspend_error_threshold = v->_ops_till_suspend - ops;
v->_ops_till_suspend_error_label = label;
SQOpsLimiter::~SQOpsLimiter()
this->_v->_ops_till_suspend_error_threshold = this->_ops;
@@ -22,12 +22,17 @@
* \li AIVehicle::IsPrimaryVehicle
*
* API removals:
* \li AIError::ERR_PRECONDITION_TOO_MANY_PARAMETERS, that error is never returned anymore.
* Other changes:
* \li AIGroupList accepts an optional filter function
* \li AIIndustryList accepts an optional filter function
* \li AISignList accepts an optional filter function
* \li AISubsidyList accepts an optional filter function
* \li AITownList accepts an optional filter function
* \li AIVehicleList accepts an optional filter function
* \b 13.0
* API additions:
* \li AICargo::GetWeight
@@ -82,12 +82,17 @@
* \li GSIndustry::SetProductionLevel
* \li GSError::ERR_PRECONDITION_TOO_MANY_PARAMETERS, that error is never returned anymore.
* \li GSGroupList accepts an optional filter function
* \li GSIndustryList accepts an optional filter function
* \li GSSignList accepts an optional filter function
* \li GSSubsidyList accepts an optional filter function
* \li GSTownList accepts an optional filter function
* \li GSVehicleList accepts an optional filter function
* \li GSCargo::GetWeight
@@ -11,14 +11,14 @@
#include "script_grouplist.hpp"
#include "script_error.hpp"
#include "../../group.h"
#include "../../safeguards.h"
ScriptGroupList::ScriptGroupList()
ScriptGroupList::ScriptGroupList(HSQUIRRELVM vm)
EnforceCompanyModeValid_Void();
CompanyID owner = ScriptObject::GetCompany();
for (const Group *g : Group::Iterate()) {
if (g->owner == owner) this->AddItem(g->index);
ScriptList::FillList<Group>(vm, this,
[owner](const Group *g) { return g->owner == owner; }
);
@@ -17,13 +17,38 @@
* @note Neither ScriptGroup::GROUP_ALL nor ScriptGroup::GROUP_DEFAULT is in this list.
* @api ai game
* @ingroup ScriptList
*/
class ScriptGroupList : public ScriptList {
public:
#ifdef DOXYGEN_API
/**
* @game @pre ScriptCompanyMode::IsValid().
ScriptGroupList();
* Apply a filter when building the list.
* @param filter_function The function which will be doing the filtering.
* @param params The params to give to the filters (minus the first param,
* which is always the index-value).
* @note You can write your own filters and use them. Just remember that
* the first parameter should be the index-value, and it should return
* a bool.
* @note Example:
* function IsType(group_id, type)
* {
* return ScriptGroup.GetVehicleType(group_id) == type;
* }
* ScriptGroupList(IsType, ScriptVehicle.VT_ROAD);
ScriptGroupList(void *filter_function, int params, ...);
#else
* The constructor wrapper from Squirrel.
ScriptGroupList(HSQUIRRELVM vm);
#endif /* DOXYGEN_API */
#endif /* SCRIPT_GROUPLIST_HPP */
@@ -10,26 +10,24 @@
#include "../../stdafx.h"
#include "script_industrylist.hpp"
#include "../../industry.h"
ScriptIndustryList::ScriptIndustryList()
ScriptIndustryList::ScriptIndustryList(HSQUIRRELVM vm)
for (const Industry *i : Industry::Iterate()) {
this->AddItem(i->index);
ScriptList::FillList<Industry>(vm, this);
ScriptIndustryList_CargoAccepting::ScriptIndustryList_CargoAccepting(CargoID cargo_id)
if (i->IsCargoAccepted(cargo_id)) this->AddItem(i->index);
ScriptList::FillList<Industry>(this,
[cargo_id](const Industry *i) { return i->IsCargoAccepted(cargo_id); }
ScriptIndustryList_CargoProducing::ScriptIndustryList_CargoProducing(CargoID cargo_id)
if (i->IsCargoProduced(cargo_id)) this->AddItem(i->index);
[cargo_id](const Industry *i) { return i->IsCargoProduced(cargo_id); }
@@ -16,13 +16,38 @@
* Creates a list of industries that are currently on the map.
class ScriptIndustryList : public ScriptList {
ScriptIndustryList();
* ScriptIndustryList(ScriptIndustry.HasDock);
* function IsType(industry_id, type)
* return ScriptIndustry.GetIndustryType(industry_id) == type;
* ScriptIndustryList(IsType, 0);
ScriptIndustryList(void *filter_function, int params, ...);
ScriptIndustryList(HSQUIRRELVM vm);
* Creates a list of industries that accepts a given cargo.
@@ -8,15 +8,13 @@
/** @file script_list.cpp Implementation of ScriptList. */
#include "script_list.hpp"
#include "script_controller.hpp"
#include "../../debug.h"
#include "../../core/backup_type.hpp"
#include "../../script/squirrel.hpp"
#include <../squirrel/sqvm.h>
* Base class for any ScriptList sorter.
@@ -866,18 +864,13 @@ SQInteger ScriptList::Valuate(HSQUIRRELV
/* Don't allow docommand from a Valuator, as we can't resume in
* mid C++-code. */
bool backup_allow = ScriptObject::GetAllowDoCommand();
ScriptObject::SetAllowDoCommand(false);
/* Limit the total number of ops that can be consumed by a valuate operation */
SQInteger new_ops_error_threshold = vm->_ops_till_suspend_error_threshold;
if (vm->_ops_till_suspend_error_threshold == INT64_MIN) {
new_ops_error_threshold = vm->_ops_till_suspend - MAX_VALUATE_OPS;
vm->_ops_till_suspend_error_label = "valuator function";
AutoRestoreBackup ops_error_threshold_backup(vm->_ops_till_suspend_error_threshold, new_ops_error_threshold);
SQOpsLimiter limiter(vm, MAX_VALUATE_OPS, "valuator function");
/* Push the function to call */
sq_push(vm, 2);
for (ScriptListMap::iterator iter = this->items.begin(); iter != this->items.end(); iter++) {
/* Check for changing of items. */
@@ -39,12 +39,113 @@ private:
ScriptListSorter *sorter; ///< Sorting algorithm
SorterType sorter_type; ///< Sorting type
bool sort_ascending; ///< Whether to sort ascending or descending
bool initialized; ///< Whether an iteration has been started
int modifications; ///< Number of modification that has been done. To prevent changing data while valuating.
protected:
template<typename T, class ItemValid, class ItemFilter>
static void FillList(ScriptList *list, ItemValid item_valid, ItemFilter item_filter)
for (const T *item : T::Iterate()) {
if (!item_valid(item)) continue;
if (!item_filter(item)) continue;
list->AddItem(item->index);
template<typename T, class ItemValid>
static void FillList(ScriptList *list, ItemValid item_valid)
ScriptList::FillList<T>(list, item_valid, [](const T *) { return true; });
template<typename T>
static void FillList(ScriptList *list)
ScriptList::FillList<T>(list, [](const T *) { return true; });
static void FillList(HSQUIRRELVM vm, ScriptList *list, ItemValid item_valid)
int nparam = sq_gettop(vm) - 1;
if (nparam >= 1) {
/* Make sure the filter function is really a function, and not any
* other type. It's parameter 2 for us, but for the user it's the
* first parameter they give. */
SQObjectType valuator_type = sq_gettype(vm, 2);
if (valuator_type != OT_CLOSURE && valuator_type != OT_NATIVECLOSURE) {
throw sq_throwerror(vm, "parameter 1 has an invalid type (expected function)");
if (nparam < 1) {
ScriptList::FillList<T>(list, item_valid);
} else {
/* Limit the total number of ops that can be consumed by a filter operation, if a filter function is present */
SQOpsLimiter limiter(vm, MAX_VALUATE_OPS, "list filter function");
ScriptList::FillList<T>(list, item_valid,
[vm, nparam, backup_allow](const T *item) {
/* Push the root table as instance object, this is what squirrel does for meta-functions. */
sq_pushroottable(vm);
/* Push all arguments for the valuator function. */
sq_pushinteger(vm, item->index);
for (int i = 0; i < nparam - 1; i++) {
sq_push(vm, i + 3);
/* Call the function. Squirrel pops all parameters and pushes the return value. */
if (SQ_FAILED(sq_call(vm, nparam + 1, SQTrue, SQTrue))) {
ScriptObject::SetAllowDoCommand(backup_allow);
throw sq_throwerror(vm, "failed to run filter");
SQBool add = SQFalse;
/* Retrieve the return value */
switch (sq_gettype(vm, -1)) {
case OT_BOOL:
sq_getbool(vm, -1, &add);
break;
default:
throw sq_throwerror(vm, "return value of filter is not valid (not bool)");
/* Pop the return value. */
sq_poptop(vm);
return add;
/* Pop the filter function */
static void FillList(HSQUIRRELVM vm, ScriptList *list)
ScriptList::FillList<T>(vm, list, [](const T *) { return true; });
typedef std::set<SQInteger> ScriptItemList; ///< The list of items inside the bucket
typedef std::map<SQInteger, ScriptItemList> ScriptListBucket; ///< The bucket list per value
typedef std::map<SQInteger, SQInteger> ScriptListMap; ///< List per item
ScriptListMap items; ///< The items in the list
@@ -11,12 +11,12 @@
#include "script_signlist.hpp"
#include "script_sign.hpp"
#include "../../signs_base.h"
ScriptSignList::ScriptSignList()
ScriptSignList::ScriptSignList(HSQUIRRELVM vm)
for (const Sign *s : Sign::Iterate()) {
if (ScriptSign::IsValidSign(s->index)) this->AddItem(s->index);
ScriptList::FillList<Sign>(vm, this,
[](const Sign *s) { return ScriptSign::IsValidSign(s->index); }
@@ -16,10 +16,32 @@
* Create a list of signs your company has created.
class ScriptSignList : public ScriptList {
ScriptSignList();
* function Contains(sign_id, str)
* local name = ScriptSign.GetName(sign_id);
* return name != null && name.find(str) != null;
* ScriptSignList(Contains, "something");
ScriptSignList(void *filter_function, int params, ...);
ScriptSignList(HSQUIRRELVM);
#endif /* SCRIPT_SIGNLIST_HPP */
@@ -10,12 +10,10 @@
#include "script_subsidylist.hpp"
#include "../../subsidy_base.h"
ScriptSubsidyList::ScriptSubsidyList()
ScriptSubsidyList::ScriptSubsidyList(HSQUIRRELVM vm)
for (const Subsidy *s : Subsidy::Iterate()) {
this->AddItem(s->index);
ScriptList::FillList<Subsidy>(vm, this);
@@ -16,10 +16,31 @@
* Creates a list of all current subsidies.
class ScriptSubsidyList : public ScriptList {
ScriptSubsidyList();
* function IsType(subsidy_id, type)
* return ScriptSubsidy.GetSourceType(subsidy_id) == type;
* ScriptSubsidyList(IsType, ScriptSubsidy.SPT_TOWN);
ScriptSubsidyList(void *filter_function, int params, ...);
ScriptSubsidyList(HSQUIRRELVM vm);
#endif /* SCRIPT_SUBSIDYLIST_HPP */
@@ -10,17 +10,15 @@
#include "script_townlist.hpp"
#include "../../town.h"
ScriptTownList::ScriptTownList()
ScriptTownList::ScriptTownList(HSQUIRRELVM vm)
for (const Town *t : Town::Iterate()) {
this->AddItem(t->index);
ScriptList::FillList<Town>(vm, this);
ScriptTownEffectList::ScriptTownEffectList()
for (int i = TE_BEGIN; i < TE_END; i++) {
this->AddItem(i);
@@ -16,13 +16,35 @@
* Creates a list of towns that are currently on the map.
class ScriptTownList : public ScriptList {
ScriptTownList();
* ScriptTownList(ScriptTown.IsActionAvailable, ScriptTown.TOWN_ACTION_BRIBE);
* function MinPopulation(town_id, pop)
* return ScriptTown.GetPopulation(town_id) >= pop;
* ScriptTownList(MinPopulation, 1000);
ScriptTownList(void *filter_function, int params, ...);
ScriptTownList(HSQUIRRELVM vm);
* Creates a list of all TownEffects known in the game.
@@ -13,99 +13,27 @@
#include "script_map.hpp"
#include "script_station.hpp"
#include "../../depot_map.h"
#include "../../vehicle_base.h"
#include "../../vehiclelist_func.h"
#include "../../train.h"
ScriptVehicleList::ScriptVehicleList(HSQUIRRELVM vm)
EnforceDeityOrCompanyModeValid_Void();
if (nparam >= 1 && vm->_ops_till_suspend_error_threshold == INT64_MIN) {
vm->_ops_till_suspend_error_label = "vehicle filter function";
bool is_deity = ScriptCompanyMode::IsDeity();
for (const Vehicle *v : Vehicle::Iterate()) {
if (v->owner != owner && !is_deity) continue;
if (!v->IsPrimaryVehicle() && !(v->type == VEH_TRAIN && ::Train::From(v)->IsFreeWagon())) continue;
/* No filter, just add the item. */
this->AddItem(v->index);
continue;
sq_pushinteger(vm, v->index);
ScriptList::FillList<Vehicle>(vm, this,
[is_deity, owner](const Vehicle *v) {
return (is_deity || v->owner == owner) && (v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && ::Train::From(v)->IsFreeWagon()));
case OT_BOOL: {
SQBool add;
if (add) this->AddItem(v->index);
default: {
ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id)
if (!ScriptBaseStation::IsValidBaseStation(station_id)) return;
@@ -179,25 +107,25 @@ ScriptVehicleList_SharedOrders::ScriptVe
ScriptVehicleList_Group::ScriptVehicleList_Group(GroupID group_id)
if (!ScriptGroup::IsValidGroup((ScriptGroup::GroupID)group_id)) return;
if (v->owner == owner && v->IsPrimaryVehicle()) {
if (v->group_id == group_id) this->AddItem(v->index);
ScriptList::FillList<Vehicle>(this,
[owner](const Vehicle *v) { return v->owner == owner && v->IsPrimaryVehicle(); },
[group_id](const Vehicle *v) { return v->group_id == group_id; }
ScriptVehicleList_DefaultGroup::ScriptVehicleList_DefaultGroup(ScriptVehicle::VehicleType vehicle_type)
if (vehicle_type < ScriptVehicle::VT_RAIL || vehicle_type > ScriptVehicle::VT_AIR) return;
if (v->type == (::VehicleType)vehicle_type && v->group_id == ScriptGroup::GROUP_DEFAULT) this->AddItem(v->index);
[vehicle_type](const Vehicle *v) { return v->type == (::VehicleType)vehicle_type && v->group_id == ScriptGroup::GROUP_DEFAULT; }
Status change: