2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-30 00:07:38 +01:00
|
|
|
/** @file script_event_types.cpp Implementation of all EventTypes. */
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2011-01-22 11:33:16 +01:00
|
|
|
#include "../../stdafx.h"
|
2011-11-30 00:07:38 +01:00
|
|
|
#include "script_event_types.hpp"
|
|
|
|
#include "script_vehicle.hpp"
|
2011-12-19 22:00:32 +01:00
|
|
|
#include "script_log.hpp"
|
2009-01-12 18:11:45 +01:00
|
|
|
#include "../../strings_func.h"
|
|
|
|
#include "../../settings_type.h"
|
2009-03-18 20:32:13 +01:00
|
|
|
#include "../../engine_base.h"
|
2009-01-12 18:11:45 +01:00
|
|
|
#include "../../articulated_vehicles.h"
|
2014-04-25 17:40:32 +02:00
|
|
|
#include "../../string_func.h"
|
2021-11-01 21:30:34 +01:00
|
|
|
#include "../../economy_cmd.h"
|
|
|
|
#include "../../engine_cmd.h"
|
2023-09-13 16:11:34 +02:00
|
|
|
#include "../../3rdparty/nlohmann/json.hpp"
|
2009-01-12 18:11:45 +01:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
bool ScriptEventEnginePreview::IsEngineValid() const
|
2011-02-05 21:41:13 +01:00
|
|
|
{
|
|
|
|
const Engine *e = ::Engine::GetIfValid(this->engine);
|
2019-04-10 23:07:06 +02:00
|
|
|
return e != nullptr && e->IsEnabled();
|
2011-02-05 21:41:13 +01:00
|
|
|
}
|
|
|
|
|
2023-05-05 23:19:35 +02:00
|
|
|
std::optional<std::string> ScriptEventEnginePreview::GetName()
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2023-05-05 23:19:35 +02:00
|
|
|
if (!this->IsEngineValid()) return std::nullopt;
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2009-09-01 14:33:17 +02:00
|
|
|
::SetDParam(0, this->engine);
|
2012-01-08 22:48:05 +01:00
|
|
|
return GetString(STR_ENGINE_NAME);
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
CargoID ScriptEventEnginePreview::GetCargoType()
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2024-01-06 16:15:37 +01:00
|
|
|
if (!this->IsEngineValid()) return INVALID_CARGO;
|
2009-10-28 21:15:46 +01:00
|
|
|
CargoArray cap = ::GetCapacityOfArticulatedParts(this->engine);
|
|
|
|
|
2023-11-26 22:38:49 +01:00
|
|
|
auto it = std::max_element(std::cbegin(cap), std::cend(cap));
|
2024-01-06 16:15:37 +01:00
|
|
|
if (*it == 0) return INVALID_CARGO;
|
2009-10-28 21:15:46 +01:00
|
|
|
|
2023-11-26 22:38:49 +01:00
|
|
|
return CargoID(std::distance(std::cbegin(cap), it));
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2023-05-08 19:01:06 +02:00
|
|
|
int32_t ScriptEventEnginePreview::GetCapacity()
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2011-02-05 21:41:13 +01:00
|
|
|
if (!this->IsEngineValid()) return -1;
|
2009-09-01 14:33:17 +02:00
|
|
|
const Engine *e = ::Engine::Get(this->engine);
|
2009-03-14 00:49:12 +01:00
|
|
|
switch (e->type) {
|
2009-01-12 18:11:45 +01:00
|
|
|
case VEH_ROAD:
|
|
|
|
case VEH_TRAIN: {
|
2009-10-06 21:52:38 +02:00
|
|
|
CargoArray capacities = GetCapacityOfArticulatedParts(this->engine);
|
2023-11-26 22:30:31 +01:00
|
|
|
for (uint &cap : capacities) {
|
|
|
|
if (cap != 0) return cap;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
return -1;
|
2010-08-01 20:53:30 +02:00
|
|
|
}
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2009-03-14 00:49:12 +01:00
|
|
|
case VEH_SHIP:
|
|
|
|
case VEH_AIRCRAFT:
|
2009-03-18 20:32:13 +01:00
|
|
|
return e->GetDisplayDefaultCapacity();
|
2009-01-12 18:11:45 +01:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 19:01:06 +02:00
|
|
|
int32_t ScriptEventEnginePreview::GetMaxSpeed()
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2011-02-05 21:41:13 +01:00
|
|
|
if (!this->IsEngineValid()) return -1;
|
2009-09-01 14:33:17 +02:00
|
|
|
const Engine *e = ::Engine::Get(this->engine);
|
2023-05-08 19:01:06 +02:00
|
|
|
int32_t max_speed = e->GetDisplayMaxSpeed(); // km-ish/h
|
2009-02-01 17:10:06 +01:00
|
|
|
if (e->type == VEH_AIRCRAFT) max_speed /= _settings_game.vehicle.plane_speed;
|
|
|
|
return max_speed;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
Money ScriptEventEnginePreview::GetPrice()
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2011-02-05 21:41:13 +01:00
|
|
|
if (!this->IsEngineValid()) return -1;
|
2009-09-01 14:33:17 +02:00
|
|
|
return ::Engine::Get(this->engine)->GetCost();
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
Money ScriptEventEnginePreview::GetRunningCost()
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2011-02-05 21:41:13 +01:00
|
|
|
if (!this->IsEngineValid()) return -1;
|
2009-09-01 14:33:17 +02:00
|
|
|
return ::Engine::Get(this->engine)->GetRunningCost();
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2023-05-08 19:01:06 +02:00
|
|
|
int32_t ScriptEventEnginePreview::GetVehicleType()
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2011-11-30 00:15:35 +01:00
|
|
|
if (!this->IsEngineValid()) return ScriptVehicle::VT_INVALID;
|
2009-09-01 14:33:17 +02:00
|
|
|
switch (::Engine::Get(this->engine)->type) {
|
2011-11-30 00:15:35 +01:00
|
|
|
case VEH_ROAD: return ScriptVehicle::VT_ROAD;
|
|
|
|
case VEH_TRAIN: return ScriptVehicle::VT_RAIL;
|
|
|
|
case VEH_SHIP: return ScriptVehicle::VT_WATER;
|
|
|
|
case VEH_AIRCRAFT: return ScriptVehicle::VT_AIR;
|
2009-01-12 18:11:45 +01:00
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
bool ScriptEventEnginePreview::AcceptPreview()
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2023-03-02 21:22:37 +01:00
|
|
|
EnforceCompanyModeValid(false);
|
2011-02-05 21:41:13 +01:00
|
|
|
if (!this->IsEngineValid()) return false;
|
2021-11-05 23:55:23 +01:00
|
|
|
return ScriptObject::Command<CMD_WANT_ENGINE_PREVIEW>::Do(this->engine);
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
2009-09-01 14:57:04 +02:00
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
bool ScriptEventCompanyAskMerger::AcceptMerger()
|
2009-09-01 14:57:04 +02:00
|
|
|
{
|
2023-03-02 21:22:37 +01:00
|
|
|
EnforceCompanyModeValid(false);
|
2023-06-05 19:32:22 +02:00
|
|
|
return ScriptObject::Command<CMD_BUY_COMPANY>::Do((::CompanyID)this->owner, false);
|
2009-09-01 14:57:04 +02:00
|
|
|
}
|
2011-12-19 22:00:32 +01:00
|
|
|
|
2021-05-14 17:33:29 +02:00
|
|
|
ScriptEventAdminPort::ScriptEventAdminPort(const std::string &json) :
|
2014-04-25 17:40:32 +02:00
|
|
|
ScriptEvent(ET_ADMIN_PORT),
|
2021-05-14 17:33:29 +02:00
|
|
|
json(json)
|
2014-04-25 17:40:32 +02:00
|
|
|
{
|
|
|
|
}
|
2023-09-08 19:03:10 +02:00
|
|
|
/**
|
|
|
|
* Convert a JSON part fo Squirrel.
|
|
|
|
* @param vm The VM used.
|
|
|
|
* @param json The JSON part to convert to Squirrel.
|
|
|
|
*/
|
|
|
|
static bool ScriptEventAdminPortReadValue(HSQUIRRELVM vm, nlohmann::json &json)
|
2011-12-19 22:00:32 +01:00
|
|
|
{
|
2023-09-06 13:14:12 +02:00
|
|
|
switch (json.type()) {
|
|
|
|
case nlohmann::json::value_t::null:
|
|
|
|
sq_pushnull(vm);
|
|
|
|
break;
|
2011-12-19 22:00:32 +01:00
|
|
|
|
2023-09-06 13:14:12 +02:00
|
|
|
case nlohmann::json::value_t::boolean:
|
|
|
|
sq_pushbool(vm, json.get<bool>() ? 1 : 0);
|
|
|
|
break;
|
2011-12-19 22:00:32 +01:00
|
|
|
|
2023-09-06 13:14:12 +02:00
|
|
|
case nlohmann::json::value_t::string: {
|
|
|
|
auto value = json.get<std::string>();
|
|
|
|
sq_pushstring(vm, value.data(), value.size());
|
2011-12-19 22:00:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-09-06 13:14:12 +02:00
|
|
|
case nlohmann::json::value_t::number_integer:
|
|
|
|
case nlohmann::json::value_t::number_unsigned:
|
|
|
|
sq_pushinteger(vm, json.get<int64_t>());
|
2011-12-19 22:00:32 +01:00
|
|
|
break;
|
|
|
|
|
2023-09-06 13:14:12 +02:00
|
|
|
case nlohmann::json::value_t::object:
|
|
|
|
sq_newtable(vm);
|
2011-12-19 22:00:32 +01:00
|
|
|
|
2023-09-06 13:14:12 +02:00
|
|
|
for (auto &[key, value] : json.items()) {
|
|
|
|
sq_pushstring(vm, key.data(), key.size());
|
2013-10-03 18:47:24 +02:00
|
|
|
|
2023-09-08 19:03:10 +02:00
|
|
|
if (!ScriptEventAdminPortReadValue(vm, value)) {
|
2023-09-06 13:14:12 +02:00
|
|
|
return false;
|
2011-12-19 22:00:32 +01:00
|
|
|
}
|
|
|
|
|
2023-09-06 13:14:12 +02:00
|
|
|
sq_rawset(vm, -3);
|
2011-12-19 22:00:32 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-09-06 13:14:12 +02:00
|
|
|
case nlohmann::json::value_t::array:
|
|
|
|
sq_newarray(vm, 0);
|
2011-12-19 22:00:32 +01:00
|
|
|
|
2023-09-06 13:14:12 +02:00
|
|
|
for (auto &value : json) {
|
2023-09-08 19:03:10 +02:00
|
|
|
if (!ScriptEventAdminPortReadValue(vm, value)) {
|
2023-09-06 13:14:12 +02:00
|
|
|
return false;
|
2011-12-19 22:00:32 +01:00
|
|
|
}
|
|
|
|
|
2023-09-06 13:14:12 +02:00
|
|
|
sq_arrayappend(vm, -2);
|
2011-12-19 22:00:32 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-09-06 13:14:12 +02:00
|
|
|
/* These types are not supported by Squirrel. */
|
|
|
|
case nlohmann::json::value_t::number_float:
|
2011-12-19 22:00:32 +01:00
|
|
|
default:
|
2023-09-06 13:14:12 +02:00
|
|
|
return false;
|
2011-12-19 22:00:32 +01:00
|
|
|
}
|
|
|
|
|
2023-09-06 13:14:12 +02:00
|
|
|
return true;
|
2011-12-19 22:00:32 +01:00
|
|
|
}
|
2023-09-08 19:03:10 +02:00
|
|
|
|
|
|
|
SQInteger ScriptEventAdminPort::GetObject(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
auto json = nlohmann::json::parse(this->json, nullptr, false);
|
|
|
|
|
|
|
|
if (!json.is_object()) {
|
|
|
|
ScriptLog::Error("The root element in the JSON data from AdminPort has to be an object.");
|
|
|
|
|
|
|
|
sq_pushnull(vm);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto top = sq_gettop(vm);
|
|
|
|
if (!ScriptEventAdminPortReadValue(vm, json)) {
|
|
|
|
/* Rewind the stack, removing anything that might be left on top. */
|
|
|
|
sq_settop(vm, top);
|
|
|
|
|
|
|
|
ScriptLog::Error("Received invalid JSON data from AdminPort.");
|
|
|
|
|
|
|
|
sq_pushnull(vm);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|