Codechange: Don't use globals for return values from vehicle command procs.

This commit is contained in:
Michael Lutz
2021-11-30 23:21:16 +01:00
parent 57b82e2e99
commit 41fa16f325
21 changed files with 88 additions and 118 deletions

View File

@@ -159,8 +159,6 @@ ScriptObject::ActiveInstance::~ActiveInstance()
/* static */ void ScriptObject::SetLastCommandRes(bool res)
{
GetStorage()->last_command_res = res;
/* Also store the results of various global variables */
SetNewVehicleID(_new_vehicle_id);
}
/* static */ bool ScriptObject::GetLastCommandRes()
@@ -178,16 +176,6 @@ ScriptObject::ActiveInstance::~ActiveInstance()
return GetStorage()->last_cmd_ret;
}
/* static */ void ScriptObject::SetNewVehicleID(VehicleID vehicle_id)
{
GetStorage()->new_vehicle_id = vehicle_id;
}
/* static */ VehicleID ScriptObject::GetNewVehicleID()
{
return GetStorage()->new_vehicle_id;
}
/* static */ void ScriptObject::SetAllowDoCommand(bool allow)
{
GetStorage()->allow_do_command = allow;

View File

@@ -192,11 +192,6 @@ protected:
*/
static const CommandDataBuffer &GetLastCommandResData();
/**
* Get the latest stored new_vehicle_id.
*/
static VehicleID GetNewVehicleID();
/**
* Store a allow_do_command per company.
* @param allow The new allow.
@@ -274,12 +269,6 @@ protected:
static char *GetString(StringID string);
private:
/**
* Store a new_vehicle_id per company.
* @param vehicle_id The new VehicleID.
*/
static void SetNewVehicleID(VehicleID vehicle_id);
/* Helper functions for DoCommand. */
static std::tuple<bool, bool, bool> DoCommandPrep();
static bool DoCommandProcessResult(const CommandCost &res, Script_SuspendCallbackProc *callback, bool estimate_only);

View File

@@ -94,8 +94,8 @@
if (!ScriptEngine::IsBuildable(engine_id)) return -1;
if (!ScriptCargo::IsValidCargo(cargo)) return -1;
CommandCost res = ::Command<CMD_BUILD_VEHICLE>::Do(DC_QUERY_COST, depot, engine_id, true, cargo, INVALID_CLIENT_ID);
return res.Succeeded() ? _returned_refit_capacity : -1;
auto [res, veh_id, refit_capacity, refit_mail] = ::Command<CMD_BUILD_VEHICLE>::Do(DC_QUERY_COST, depot, engine_id, true, cargo, INVALID_CLIENT_ID);
return res.Succeeded() ? refit_capacity : -1;
}
/* static */ VehicleID ScriptVehicle::CloneVehicle(TileIndex depot, VehicleID vehicle_id, bool share_orders)
@@ -143,8 +143,8 @@
if (!IsValidVehicle(vehicle_id)) return -1;
if (!ScriptCargo::IsValidCargo(cargo)) return -1;
CommandCost res = ::Command<CMD_REFIT_VEHICLE>::Do(DC_QUERY_COST, vehicle_id, cargo, 0, false, false, 0);
return res.Succeeded() ? _returned_refit_capacity : -1;
auto [res, refit_capacity, refit_mail] = ::Command<CMD_REFIT_VEHICLE>::Do(DC_QUERY_COST, vehicle_id, cargo, 0, false, false, 0);
return res.Succeeded() ? refit_capacity : -1;
}
/* static */ bool ScriptVehicle::RefitVehicle(VehicleID vehicle_id, CargoID cargo)

View File

@@ -270,7 +270,7 @@ void ScriptInstance::CollectGarbage()
/* static */ void ScriptInstance::DoCommandReturnVehicleID(ScriptInstance *instance)
{
instance->engine->InsertResult(ScriptObject::GetNewVehicleID());
instance->engine->InsertResult(EndianBufferReader::ToValue<VehicleID>(ScriptObject::GetLastCommandResData()));
}
/* static */ void ScriptInstance::DoCommandReturnSignID(ScriptInstance *instance)

View File

@@ -49,8 +49,6 @@ private:
Commands last_cmd; ///< The last cmd passed to a command.
CommandDataBuffer last_cmd_ret; ///< The extra data returned by the last command.
VehicleID new_vehicle_id; ///< The ID of the new Vehicle.
std::vector<int> callback_value; ///< The values which need to survive a callback.
RoadType road_type; ///< The current roadtype we build.
@@ -73,7 +71,6 @@ public:
last_command_res (true),
last_tile (INVALID_TILE),
last_cmd (CMD_END),
new_vehicle_id (0),
/* calback_value (can't be set) */
road_type (INVALID_ROADTYPE),
rail_type (INVALID_RAILTYPE),