Merge branch 'master' into jgrpp

# Conflicts:
#	src/company_cmd.cpp
#	src/core/overflowsafe_type.hpp
#	src/economy.cpp
#	src/engine_base.h
#	src/ground_vehicle.cpp
#	src/group_gui.cpp
#	src/industry_cmd.cpp
#	src/industry_gui.cpp
#	src/newgrf_commons.cpp
#	src/newgrf_engine.cpp
#	src/newgrf_industries.cpp
#	src/newgrf_object.cpp
#	src/newgrf_roadstop.cpp
#	src/newgrf_station.cpp
#	src/rail_gui.cpp
#	src/road_cmd.h
#	src/road_gui.cpp
#	src/saveload/afterload.cpp
#	src/script/api/script_log.cpp
#	src/script/api/script_log.hpp
#	src/settings_gui.cpp
#	src/settingsgen/settingsgen.cpp
#	src/station_cmd.cpp
#	src/station_cmd.h
#	src/station_gui.cpp
#	src/strgen/strgen.cpp
#	src/string_func.h
#	src/string_type.h
#	src/table/settings/network_private_settings.ini
#	src/tests/math_func.cpp
#	src/textfile_gui.cpp
#	src/timetable_gui.cpp
#	src/town_cmd.cpp
#	src/vehicle.cpp
#	src/waypoint_cmd.cpp
#	src/waypoint_cmd.h
#	src/widgets/dropdown.cpp
This commit is contained in:
Jonathan G Rennison
2023-06-03 19:16:42 +01:00
101 changed files with 987 additions and 964 deletions

View File

@@ -182,6 +182,7 @@ add_files(
script_league.hpp
script_list.hpp
script_log.hpp
script_log_types.hpp
script_map.hpp
script_marine.hpp
script_newgrf.hpp

View File

@@ -52,7 +52,7 @@
char log_message[1024];
seprintf(log_message, lastof(log_message), "Break: %s", message);
ScriptLog::Log(ScriptLog::LOG_SQ_ERROR, log_message);
ScriptLog::Log(ScriptLogTypes::LOG_SQ_ERROR, log_message);
/* Inform script developer that their script has been paused and
* needs manual action to continue. */
@@ -65,7 +65,7 @@
/* static */ void ScriptController::Print(bool error_msg, const char *message)
{
ScriptLog::Log(error_msg ? ScriptLog::LOG_SQ_ERROR : ScriptLog::LOG_SQ_INFO, message);
ScriptLog::Log(error_msg ? ScriptLogTypes::LOG_SQ_ERROR : ScriptLogTypes::LOG_SQ_INFO, message);
}
ScriptController::ScriptController(CompanyID company) :

View File

@@ -8,6 +8,7 @@
/** @file script_log.cpp Implementation of ScriptLog. */
#include "../../stdafx.h"
#include "script_log_types.hpp"
#include "script_log.hpp"
#include "../../core/alloc_func.hpp"
#include "../../debug.h"
@@ -18,80 +19,50 @@
/* static */ void ScriptLog::Info(const char *message)
{
ScriptLog::Log(LOG_INFO, message);
ScriptLog::Log(ScriptLogTypes::LOG_INFO, message);
}
/* static */ void ScriptLog::Warning(const char *message)
{
ScriptLog::Log(LOG_WARNING, message);
ScriptLog::Log(ScriptLogTypes::LOG_WARNING, message);
}
/* static */ void ScriptLog::Error(const char *message)
{
ScriptLog::Log(LOG_ERROR, message);
ScriptLog::Log(ScriptLogTypes::LOG_ERROR, message);
}
/* static */ void ScriptLog::Log(ScriptLog::ScriptLogType level, const char *message)
/* static */ void ScriptLog::Log(ScriptLogTypes::ScriptLogType level, const char *message)
{
if (ScriptObject::GetLogPointer() == nullptr) {
ScriptObject::GetLogPointer() = new LogData();
LogData *log = (LogData *)ScriptObject::GetLogPointer();
ScriptLogTypes::LogData &logdata = ScriptObject::GetLogData();
log->lines = CallocT<char *>(400);
log->type = CallocT<ScriptLog::ScriptLogType>(400);
log->count = 400;
log->pos = log->count - 1;
log->used = 0;
}
LogData *log = (LogData *)ScriptObject::GetLogPointer();
/* Limit the log to 400 lines. */
if (logdata.size() >= 400U) logdata.pop_front();
/* Go to the next log-line */
log->pos = (log->pos + 1) % log->count;
if (log->used != log->count) log->used++;
/* Free last message, and write new message */
free(log->lines[log->pos]);
log->lines[log->pos] = stredup(message);
log->type[log->pos] = level;
auto &line = logdata.emplace_back();
line.type = level;
/* Cut string after first \n */
char *p;
while ((p = strchr(log->lines[log->pos], '\n')) != nullptr) {
*p = '\0';
break;
}
const char *newline = strchr(message, '\n');
line.text = std::string(message, 0, newline == nullptr ? strlen(message) : newline - message);
char logc;
switch (level) {
case LOG_SQ_ERROR: logc = 'S'; break;
case LOG_ERROR: logc = 'E'; break;
case LOG_SQ_INFO: logc = 'P'; break;
case LOG_WARNING: logc = 'W'; break;
case LOG_INFO: logc = 'I'; break;
default: logc = '?'; break;
case ScriptLogTypes::LOG_SQ_ERROR: logc = 'S'; break;
case ScriptLogTypes::LOG_ERROR: logc = 'E'; break;
case ScriptLogTypes::LOG_SQ_INFO: logc = 'P'; break;
case ScriptLogTypes::LOG_WARNING: logc = 'W'; break;
case ScriptLogTypes::LOG_INFO: logc = 'I'; break;
default: logc = '?'; break;
}
/* Also still print to debug window */
DEBUG(script, level, "[%d] [%c] %s", (uint)ScriptObject::GetRootCompany(), logc, log->lines[log->pos]);
DEBUG(script, level, "[%d] [%c] %s", (uint)ScriptObject::GetRootCompany(), logc, line.text.c_str());
InvalidateWindowData(WC_SCRIPT_DEBUG, 0, ScriptObject::GetRootCompany());
}
/* static */ void ScriptLog::FreeLogPointer()
{
LogData *log = (LogData *)ScriptObject::GetLogPointer();
for (int i = 0; i < log->count; i++) {
free(log->lines[i]);
}
free(log->lines);
free(log->type);
delete log;
}
/* static */ void ScriptLog::LogOnce(ScriptLog::ScriptLogType level, std::string &&message)
/* static */ void ScriptLog::LogOnce(ScriptLogTypes::ScriptLogType level, std::string &&message)
{
if (ScriptObject::IsNewUniqueLogMessage(message)) {
ScriptLog::Log(level, message.c_str());

View File

@@ -22,32 +22,6 @@ class ScriptLog : public ScriptObject {
friend class ScriptController;
public:
/**
* Log levels; The value is also feed to DEBUG() lvl.
* This has no use for you, as script writer.
* @api -all
*/
enum ScriptLogType {
LOG_SQ_ERROR = 0, ///< Squirrel printed an error.
LOG_ERROR = 1, ///< User printed an error.
LOG_SQ_INFO = 2, ///< Squirrel printed some info.
LOG_WARNING = 3, ///< User printed some warning.
LOG_INFO = 4, ///< User printed some info.
};
/**
* Internal representation of the log-data inside the script.
* This has no use for you, as script writer.
* @api -all
*/
struct LogData {
char **lines; ///< The log-lines.
ScriptLog::ScriptLogType *type; ///< Per line, which type of log it was.
int count; ///< Total amount of log-lines possible.
int pos; ///< Current position in lines.
int used; ///< Total amount of used log-lines.
};
/**
* Print an Info message to the logs.
* @param message The message to log.
@@ -69,23 +43,17 @@ public:
*/
static void Error(const char *message);
/**
* Free the log pointer.
* @api -all
*/
static void FreeLogPointer();
/**
* Log this message once.
* @api -all
*/
static void LogOnce(ScriptLog::ScriptLogType level, std::string &&message);
static void LogOnce(ScriptLogTypes::ScriptLogType level, std::string &&message);
private:
/**
* Internal command to log the message in a common way.
*/
static void Log(ScriptLog::ScriptLogType level, const char *message);
static void Log(ScriptLogTypes::ScriptLogType level, const char *message);
};
#endif /* SCRIPT_LOG_HPP */

View File

@@ -0,0 +1,47 @@
/*
* 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_log_types.hpp Data types for script log messages. */
#ifndef SCRIPT_LOG_TYPES_HPP
#define SCRIPT_LOG_TYPES_HPP
#include <deque>
namespace ScriptLogTypes {
/**
* Log levels; The value is also feed to Debug() lvl.
* This has no use for you, as script writer.
* @api -all
*/
enum ScriptLogType {
LOG_SQ_ERROR = 0, ///< Squirrel printed an error.
LOG_ERROR = 1, ///< User printed an error.
LOG_SQ_INFO = 2, ///< Squirrel printed some info.
LOG_WARNING = 3, ///< User printed some warning.
LOG_INFO = 4, ///< User printed some info.
};
/**
* Internal representation of the log-data inside the script.
* This has no use for you, as script writer.
* @api -all
*/
struct LogLine {
std::string text; ///< The text
ScriptLogType type; ///< Text type
};
/**
* Internal representation of the log-data inside the script.
* This has no use for you, as script writer.
* @api -all
*/
using LogData = std::deque<LogLine>; ///< The log type
};
#endif /* SCRIPT_LOG_TYPES_HPP */

View File

@@ -306,7 +306,7 @@ ScriptObject::ActiveInstance::~ActiveInstance()
return GetStorage()->event_data;
}
/* static */ void *&ScriptObject::GetLogPointer()
/* static */ ScriptLogTypes::LogData &ScriptObject::GetLogData()
{
return GetStorage()->log_data;
}

View File

@@ -16,6 +16,7 @@
#include "../../core/random_func.hpp"
#include "script_types.hpp"
#include "script_log_types.hpp"
#include "../script_suspend.hpp"
#include "../squirrel.hpp"
@@ -317,7 +318,7 @@ protected:
/**
* Get the pointer to store log message in.
*/
static void *&GetLogPointer();
static ScriptLogTypes::LogData &GetLogData();
/**
* Get an allocated string with all control codes stripped off.

View File

@@ -185,7 +185,7 @@
0,
source_industry,
goal_industry,
std::min<SQInteger>(255, distance / 2),
ClampTo<uint8_t>(distance / 2),
AICE_STATION_GET_STATION_ID,
source_station ? 0 : 1,
std::min<SQInteger>(15u, num_platforms) << 4 | std::min<SQInteger>(15u, platform_length),

View File

@@ -174,7 +174,7 @@ const std::string ScriptText::GetEncodedText()
void ScriptText::_TextParamError(std::string msg)
{
if (this->GetActiveInstance()->IsTextParamMismatchAllowed()) {
ScriptLog::LogOnce(ScriptLog::LOG_ERROR, std::move(msg));
ScriptLog::LogOnce(ScriptLogTypes::LOG_ERROR, std::move(msg));
} else {
throw Script_FatalError(std::move(msg));
}

View File

@@ -78,7 +78,7 @@ struct ScriptListWindow : public Window {
this->vscroll = this->GetScrollbar(WID_SCRL_SCROLLBAR);
this->FinishInitNested(); // Initializes 'this->line_height' as side effect.
this->vscroll->SetCount((int)this->info_list->size() + 1);
this->vscroll->SetCount(this->info_list->size() + 1);
/* Try if we can find the currently selected AI */
this->selected = -1;
@@ -238,7 +238,7 @@ struct ScriptListWindow : public Window {
if (!gui_scope) return;
this->vscroll->SetCount((int)this->info_list->size() + 1);
this->vscroll->SetCount(this->info_list->size() + 1);
/* selected goes from -1 .. length of ai list - 1. */
this->selected = std::min(this->selected, this->vscroll->GetCount() - 2);
@@ -346,7 +346,7 @@ struct ScriptSettingsWindow : public Window {
}
}
this->vscroll->SetCount((int)this->visible_settings.size());
this->vscroll->SetCount(this->visible_settings.size());
}
void SetStringParameters(int widget) const override
@@ -720,10 +720,10 @@ struct ScriptDebugWindow : public Window {
int highlight_row; ///< The output row that matches the given string, or -1
Scrollbar *vscroll; ///< Cache of the vertical scrollbar.
ScriptLog::LogData *GetLogPointer() const
ScriptLogTypes::LogData &GetLogData() const
{
if (script_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer();
return (ScriptLog::LogData *)Company::Get(script_debug_company)->ai_instance->GetLogPointer();
if (script_debug_company == OWNER_DEITY) return Game::GetInstance()->GetLogData();
return Company::Get(script_debug_company)->ai_instance->GetLogData();
}
/**
@@ -861,9 +861,9 @@ struct ScriptDebugWindow : public Window {
/* If there are no active companies, don't display anything else. */
if (script_debug_company == INVALID_COMPANY) return;
ScriptLog::LogData *log = this->GetLogPointer();
ScriptLogTypes::LogData &log = this->GetLogData();
int scroll_count = (log == nullptr) ? 0 : log->used;
int scroll_count = (int)log.size();
if (this->vscroll->GetCount() != scroll_count) {
this->vscroll->SetCount(scroll_count);
@@ -871,16 +871,15 @@ struct ScriptDebugWindow : public Window {
this->SetWidgetDirty(WID_SCRD_SCROLLBAR);
}
if (log == nullptr) return;
if (log.empty()) return;
/* Detect when the user scrolls the window. Enable autoscroll when the
* bottom-most line becomes visible. */
if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
this->autoscroll = this->vscroll->GetPosition() + this->vscroll->GetCapacity() >= (int)log.size();
}
if (this->autoscroll) {
int scroll_pos = std::max(0, log->used - this->vscroll->GetCapacity());
if (this->vscroll->SetPosition(scroll_pos)) {
if (this->vscroll->SetPosition((int)log.size())) {
/* We need a repaint */
this->SetWidgetDirty(WID_SCRD_SCROLLBAR);
this->SetWidgetDirty(WID_SCRD_LOG_PANEL);
@@ -916,32 +915,31 @@ struct ScriptDebugWindow : public Window {
if (widget != WID_SCRD_LOG_PANEL) return;
ScriptLog::LogData *log = this->GetLogPointer();
if (log == nullptr) return;
ScriptLogTypes::LogData &log = this->GetLogData();
if (log.empty()) return;
Rect br = r.Shrink(WidgetDimensions::scaled.bevel);
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
if (log->lines[pos] == nullptr) break;
for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && (size_t)i < log.size(); i++) {
const ScriptLogTypes::LogLine &line = log[i];
TextColour colour;
switch (log->type[pos]) {
case ScriptLog::LOG_SQ_INFO: colour = TC_BLACK; break;
case ScriptLog::LOG_SQ_ERROR: colour = TC_WHITE; break;
case ScriptLog::LOG_INFO: colour = TC_BLACK; break;
case ScriptLog::LOG_WARNING: colour = TC_YELLOW; break;
case ScriptLog::LOG_ERROR: colour = TC_RED; break;
default: colour = TC_BLACK; break;
switch (line.type) {
case ScriptLogTypes::LOG_SQ_INFO: colour = TC_BLACK; break;
case ScriptLogTypes::LOG_SQ_ERROR: colour = TC_WHITE; break;
case ScriptLogTypes::LOG_INFO: colour = TC_BLACK; break;
case ScriptLogTypes::LOG_WARNING: colour = TC_YELLOW; break;
case ScriptLogTypes::LOG_ERROR: colour = TC_RED; break;
default: colour = TC_BLACK; break;
}
/* Check if the current line should be highlighted */
if (pos == this->highlight_row) {
if (i == this->highlight_row) {
GfxFillRect(br.left, tr.top, br.right, tr.top + this->resize.step_height - 1, PC_BLACK);
if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white.
}
DrawString(tr, log->lines[pos], colour, SA_LEFT | SA_FORCE);
DrawString(tr, line.text, colour, SA_LEFT | SA_FORCE);
tr.top += this->resize.step_height;
}
}
@@ -1064,11 +1062,11 @@ struct ScriptDebugWindow : public Window {
* This needs to be done in gameloop-scope, so the AI is suspended immediately. */
if (!gui_scope && data == script_debug_company && this->IsValidDebugCompany(script_debug_company) && this->break_check_enabled && !this->break_string_filter.IsEmpty()) {
/* Get the log instance of the active company */
ScriptLog::LogData *log = this->GetLogPointer();
ScriptLogTypes::LogData &log = this->GetLogData();
if (log != nullptr) {
if (!log.empty()) {
this->break_string_filter.ResetState();
this->break_string_filter.AddLine(log->lines[log->pos]);
this->break_string_filter.AddLine(log.back().text);
if (this->break_string_filter.GetState()) {
/* Pause execution of script. */
if (!this->IsDead()) {
@@ -1085,7 +1083,7 @@ struct ScriptDebugWindow : public Window {
}
/* Highlight row that matched */
this->highlight_row = log->pos;
this->highlight_row = (int)(log.size() - 1);
}
}
}
@@ -1094,8 +1092,7 @@ struct ScriptDebugWindow : public Window {
this->SelectValidDebugCompany();
ScriptLog::LogData *log = script_debug_company != INVALID_COMPANY ? this->GetLogPointer() : nullptr;
this->vscroll->SetCount((log == nullptr) ? 0 : log->used);
this->vscroll->SetCount(script_debug_company != INVALID_COMPANY ? this->GetLogData().size() : 0);
/* Update company buttons */
for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {

View File

@@ -146,42 +146,42 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
} else if (strcmp(key, "min_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.min_value = ClampToI32(res);
config.min_value = ClampTo<int32_t>(res);
items |= 0x004;
} else if (strcmp(key, "max_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.max_value = ClampToI32(res);
config.max_value = ClampTo<int32_t>(res);
items |= 0x008;
} else if (strcmp(key, "easy_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.easy_value = ClampToI32(res);
config.easy_value = ClampTo<int32_t>(res);
items |= 0x010;
} else if (strcmp(key, "medium_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.medium_value = ClampToI32(res);
config.medium_value = ClampTo<int32_t>(res);
items |= 0x020;
} else if (strcmp(key, "hard_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.hard_value = ClampToI32(res);
config.hard_value = ClampTo<int32_t>(res);
items |= 0x040;
} else if (strcmp(key, "random_deviation") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.random_deviation = ClampToI32(abs(res));
config.random_deviation = ClampTo<int32_t>(abs(res));
items |= 0x200;
} else if (strcmp(key, "custom_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.custom_value = ClampToI32(res);
config.custom_value = ClampTo<int32_t>(res);
items |= 0x080;
} else if (strcmp(key, "step_size") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.step_size = ClampToI32(res);
config.step_size = ClampTo<int32_t>(res);
} else if (strcmp(key, "flags") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;

View File

@@ -35,7 +35,6 @@ ScriptStorage::~ScriptStorage()
{
/* Free our pointers */
if (event_data != nullptr) ScriptEventController::FreeEventPointer();
if (log_data != nullptr) ScriptLog::FreeLogPointer();
}
/**
@@ -350,11 +349,11 @@ ScriptStorage *ScriptInstance::GetStorage()
return this->storage;
}
void *ScriptInstance::GetLogPointer()
ScriptLogTypes::LogData &ScriptInstance::GetLogData()
{
ScriptObject::ActiveInstance active(this);
return ScriptObject::GetLogPointer();
return ScriptObject::GetLogData();
}
/*

View File

@@ -15,6 +15,7 @@
#include <squirrel.h>
#include "squirrel.hpp"
#include "script_suspend.hpp"
#include "script_log_types.hpp"
#include "../command_type.h"
#include "../company_type.h"
@@ -96,7 +97,7 @@ public:
/**
* Get the log pointer of this script.
*/
void *GetLogPointer();
ScriptLogTypes::LogData &GetLogData();
/**
* Return a true/false reply for a DoCommand.

View File

@@ -18,6 +18,8 @@
#include "../story_type.h"
#include "../3rdparty/robin_hood/robin_hood.h"
#include "script_log_types.hpp"
#include "table/strings.h"
#include <vector>
@@ -72,7 +74,7 @@ private:
RailType rail_type; ///< The current railtype we build.
void *event_data; ///< Pointer to the event data storage.
void *log_data; ///< Pointer to the log data storage.
ScriptLogTypes::LogData log_data;///< Log data storage.
robin_hood::unordered_node_set<std::string> seen_unique_log_messages; ///< Messages which have already been logged once and don't need to be logged again
@@ -105,8 +107,7 @@ public:
/* calback_value (can't be set) */
road_type (INVALID_ROADTYPE),
rail_type (INVALID_RAILTYPE),
event_data (nullptr),
log_data (nullptr)
event_data (nullptr)
{ }
~ScriptStorage();