Merge branch 'master' into jgrpp

# Conflicts:
#	src/music.cpp
#	src/script/api/script_company.cpp
#	src/script/api/script_event_types.cpp
#	src/script/api/script_group.cpp
#	src/script/api/script_object.cpp
#	src/script/api/script_road.cpp
This commit is contained in:
Jonathan G Rennison 2023-03-11 00:06:13 +00:00
commit 25ed7c2b53
61 changed files with 844 additions and 52 deletions

View File

@ -0,0 +1,71 @@
"""
Script to scan the OpenTTD's script API for functions that miss checks for the
function being called from the right mode (deity or company mode).
When a function calls either ScriptObject::Command or ScriptObject::GetCompany
then the function is considered dangerous. When one of the mode enforcement
macros from script_error.hpp, i.e. EnforceDeityMode, EnforceCompanyModeValid or
EnforceDeityOrCompanyModeValid, are called in the function, then we consider
that the function has mode enforcement.
Any dangerous function for which no enforcement is found are emitted as errors.
"""
import glob
import re
import sys
def check_mode_enforcement(path):
errors = []
with open(path, "r") as reader:
mode_enforcement_found = False
dangerous_function = False
for line in reader:
# Line does not start with a tab and have <word>::<word>. That looks like the begin of a function, so reset the state.
if re.match(r"^[^\t].*\w::\w", line):
mode_enforcement_found = False
dangerous_function = False
currentFunction = line
continue
if re.match(
r"\t(EnforceDeityMode|EnforceCompanyModeValid|EnforceDeityOrCompanyModeValid|EnforceDeityOrCompanyModeValid_Void)\(",
line,
):
# Mode enforcement macro found
mode_enforcement_found = True
continue
if re.match(r".*(ScriptObject::Command|ScriptObject::GetCompany).*", line):
# Dangerous function found
dangerous_function = True
continue
# Line with only a closing bracket. That looks like the end of a function, so check for the dangerous function without mode enforcement
if re.match(r"^}$", line) and dangerous_function and not mode_enforcement_found:
function_name = currentFunction.rstrip("\n").replace("/* static */ ", "")
errors.append(f"{path}: {function_name}")
return errors
def main():
errors = []
for path in sorted(glob.glob("src/script/api/*.cpp")):
# Skip a number of files that yield only false positives
if path.endswith(("script_object.cpp", "script_companymode.cpp", "script_controller.cpp", "script_game.cpp")):
continue
errors.extend(check_mode_enforcement(path))
if errors:
print("Mode enforcement was expected in the following files/functions:")
print("\n".join(errors))
sys.exit(1)
print("OK")
if __name__ == "__main__":
main()

View File

@ -210,11 +210,10 @@ def main():
errors.append(f"ERROR: {string} is (possibly) no longer needed.")
if errors:
for error in errors:
print(error)
print("\n".join(errors))
sys.exit(1)
else:
print("OK")
print("OK")
if __name__ == "__main__":

View File

@ -0,0 +1,22 @@
name: Script missing mode enforcement
on:
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}
jobs:
script-missing-mode-enforcement:
name: Script missing mode enforcement
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Check for finding script functions that require company/deity mode enforcement/checks
run: |
set -ex
python3 .github/script-missing-mode-enforcement.py

490
CODINGSTYLE.md Normal file
View File

@ -0,0 +1,490 @@
**Why a set coding style is important** <br>
This project is an open source project. To get open source working as intended, many people should be able to comprehend the code. This implies that there is a well documented and uniform flow of code.
That does not necessarily mean that a contributor should not write optimised yet cryptic code - one should always care about performance. However, other developers need to understand the code which means complicated parts of code **must** have good documentation.
**Why we require that EVERYTHING is documented**<br>
What is simple to some might appear very complicated to others. Documentation helps these others. Anyone should be able to improve the code. But the main reason is, that when patch contributors want their patches added to the common codebase, the code needs to be reviewed by one or more developers. Documentation makes reviewing much easier.
## Coding style for OpenTTD
### Functions
* Function names use [CamelCase](http://www.wikipedia.org/wiki/Camelcase) without underscores.
* Opening curly bracket **{** for a function starts on the next line.
* Use Foo() instead of Foo(void).
```c++
void ThisIsAFunction()
{
}
```
### Variables
* Variable names are all lowercase, and use "_" as separator.
* Global variables are preceded by an underscore. ("_") Use descriptive names because leading underscores are often used for system / compiler variables.
* Own members of classes should always be referenced using "this->"
* Pointers and references should have their reference symbol next to the name (compatibility with current code).
* Variables that are declared below one another should have their type, name or reference operator, and assignment operator aligned by spaces.
* There are set names for many variables. Those are (but not limited to): Vehicle *u, *v, *w; Station *st; Town *t; Window *w; Engine *e.
* For multiple instances, use numbers "*t1, *t2" or postfixes "*st_from, *st_to".
* Declare variables upon first usage.
* Declare iterators in their loop.
* There is a space between '*' and 'const' in "const pointers"
```c++
int number = 10;
Vehicle *u_first_wagon = v->next;
int value = v->value;
uint32 _global_variable = 3750;
static const char * const _global_array[] = {
"first string",
"second string",
"another string",
"last string followed by comma",
};
protected:
char protected_array[10];
for (int i = 0;;);
```
* Give the variables expedient names, this increases the readability of the code
```c++
bool is_maglev_train = true;
if (!is_maglev_train) DoSomething();
```
* Some people like to introduce copies of variables to increase readability, which can waste memory. However, in some cases, especially in code pieces which are often called, it makes sense to cache some calculated variables.
```c++
/* Unoptimized code:
* foo is not touched inside the loop!
*/
for (uint8 i = 0; i < 100000; i++) {
/* Do something */
if (value_to_check == (foo * 4) % 5 + 6) DoSomething();
/* Do something else */
}
/* Better:
* The used value of foo is calculated outside the loop.
*/
const uint32 bar = (foo * 4) % 5 + 6;
for (uint8 i = 0; i < 100000; i++) {
/* Do something */
if (value_to_check == bar) DoSomething();
/* Do something else */
}
```
### Enumerations / static consts
* Enumerations store integers that belong logically together (railtypes, string IDs, etc.).
* Enumeration names also use CamelCase.
* Unscoped enumerators are all caps with "_" between the words.
* Scoped enumerators are use CamelCase.
* Enums are not used to store single numbers.
* Enums have consecutive numbers OR
* Enums have consecutive powers of two. Powers of two (bits) are written in hex or with the shift operator.
* Enums may have special enumerators: "_BEGIN", "\_END", and "INVALID\_"). See example.
* The invalid always has 0xFF, 0xFFFF, 0xFFFFFFFF as a value.
* Other special values are consecutively less than the invalid.
* Variables that hold enumerators should have the type of the enumeration.
```c++
enum DiagDirection {
DIAGDIR_BEGIN = 0,
DIAGDIR_NE = 0,
DIAGDIR_SE = 1,
DIAGDIR_SW = 2,
DIAGDIR_NW = 3,
DIAGDIR_END,
INVALID_DIAGDIR = 0xFF,
BROKEN_DIAGDIR = 0xFE,
};
enum {
DEPOT_SERVICE = (1 << 0),
DEPOT_MASS_SEND = (1 << 1),
DEPOT_DONT_CANCEL = (1 << 2),
DEPOT_LOCATE_HANGAR = (1 << 3),
};
DiagDirection enterdir = DIAGDIR_NE;
```
* Numbers that store single or uncorrelated data are static consts. Those may use the naming conventions of enums.
Example:
```c++
static const int MAXIMUM_STATIONS = 42;
```
* Enums are useful in GUI code: When widgets are enumerated, they are easier to access during many operations. Additionally changes caused by modified widget sequences require less code adapting. If a widget is used like this, its enum name should be present in a comment behind the corresponding widget definition.
```c++
/** Enum referring to the widgets of the build rail depot window */
enum BuildRailDepotWidgets {
BRDW_CLOSEBOX = 0,
BRDW_CAPTION,
BRDW_BACKGROUND,
BRDW_DEPOT_NE,
BRDW_DEPOT_SE,
BRDW_DEPOT_SW,
BRDW_DEPOT_NW,
};
/* ... */
/** Widget definition of the build rail depot window */
static const Widget _build_depot_widgets[] = {
{ WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_..}, // BRDW_CLOSEBOX
{ WWT_CAPTION, RESIZE_NONE, 7, 11, 139, 0, 13, STR_..., STR_...}, // BRDW_CAPTION
{ WWT_PANEL, RESIZE_NONE, 7, 0, 139, 14, 121, 0x0, STR_NULL}, // BRDW_BACKGROUND
{ WWT_PANEL, RESIZE_NONE, 14, 71, 136, 17, 66, 0x0, STR_..}, // BRDW_DEPOT_NE
{ WWT_PANEL, RESIZE_NONE, 14, 71, 136, 69, 118, 0x0, STR_..}, // BRDW_DEPOT_SE
{ WWT_PANEL, RESIZE_NONE, 14, 3, 68, 69, 118, 0x0, STR_..}, // BRDW_DEPOT_SW
{ WWT_PANEL, RESIZE_NONE, 14, 3, 68, 17, 66, 0x0, STR_..}, // BRDW_DEPOT_NW
{ WIDGETS_END},
};
```
* Comments on the enum values should start with "///<" to enable doxygen documentation
```c++
enum SomeEnumeration {
SE_BEGIN = 0, ///< Begin of the enumeration, used for iterations
SE_FOO = 0, ///< Used for xy
SE_BAR, ///< Another value of the enumeration
SE_SUB, ///< Special case for z
SE_END, ///< End of the enumeration, used for iterations
};
```
### Control flow
* Put a space before the parentheses in **if**, **switch**, **for** and **while** statements.
* Use curly braces and put the contained statements on their own lines (e.g., don't put them directly after the **if**).
* Opening curly bracket **{** stays on the first line, closing curly bracket **}** gets a line to itself (except for the **}** preceding an **else**, which should be on the same line as the **else**).
* When only a single statement is contained, the brackets can be omitted. In this case, put the single statement on the same line as the preceding keyword (**if**, **while**, etc.). Note that this is only allowed for if statements without an **else** clause.
* All fall throughs must be documented, using a **FALLTHROUGH** define/macro.
* The NOT_REACHED() macro can be used in default constructs that should never be reached.
* Unconditional loops are written with **`for (;;) {`**
```c++
if (a == b) {
Foo();
} else {
Bar();
}
if (very_large_checks &&
spread_over_two_lines) {
Foo();
}
if (a == b) Foo();
switch (a) {
case 0: DoSomethingShort(); break;
case 1:
DoSomething();
/* FALL THROUGH */
case 2:
DoMore();
b = a;
break;
case 3: {
int r = 2;
DoEvenMore(a);
/* FALL THROUGH */
}
case 4: {
int q = 345;
DoIt();
break;
}
default:
NOT_REACHED();
}
for (int i = 0; i < 10; i++) {
Foo();
Bar();
}
```
### Classes
* Classes names also use CamelCase.
* Classes should have "public", "protected", and "private" sections.
* Within these section the order is: types, static const members, static members, members, constructors / destructors, static methods, methods.
* Deviations from above order are allowed when the code dictates it (e.g. a static const is needed for a typedef)
* Methods and members ought to be grouped logically.
* All those sorting rules sometimes conflict which one another. Please use common sense what increases legibility of the code in such a case.
* The method implementation should indicate if it is virtual or similar by using a comment.
* Very short methods can have one-line definition (if defined in the class scope).
```c++
class ThisIsAClass {
public:
typedef Titem_ *ItemPtr;
private:
static const int MAX_SIZE = 500;
int size;
ItemPtr *items;
public:
explicit ThisIsAClass();
~ThisIsAClass();
int GetSize() { return this->size; }
virtual void Method();
};
/*virtual*/ void Class::Method()
{
this->size *= 2;
}
```
### Templates
Templates are a very powerful C++ tool, but they can easily confuse beginners. Thus:
* Templates are to be documented in a very clear and verbose manner. Never assume anything in the documentation.
* the template keyword and the template layout get a separate line. typenames are either "T" or preceded by a "T", integers get a single capital letter or a descriptive name preceded by "T".
```c++
template <typename T, typename Tsomething, int N, byte Tnumber_of_something>
int Func();
```
* If you are writing one or more template class in the dedicated header file, use file.hpp for its name instead of file.h. This will let others know that it is template library (includes also implementation), not just header with declarations.
### Other important rules
* Put a space before and after binary operators: "a + b", "a == b", "a & b", "a <<= b", etc.. Exceptions are ".", "->" and "[]" (no spaces) and "," (just space after it).
* Put parenthesis where it improves readability: "*(b++)" instead of "*b++", and "if ((a & b) && c == 2)" instead of "if (a & b && c == 2)".
* Do not put external declarations in implementation (i.e. cpp) files.
* Use const where possible.
* Do not typedef enums and structs.
* Don't treat non-flags as flags: use "if (char_pointer != nullptr && *char_pointer != '\0')", not "if (char_pointer && *char_pointer)".
* Use "free(p)" instead of "if (p != nullptr) free(p)". "free(nullptr)" doesn't hurt anyone.
* No trailing whitespace. The Github workflows will not allow tabs or space on the end of lines.
* Only use tabs to indent from the start of the line.
* Line length is unlimited. In practice it may be useful to split a long line. When splitting, add two tabs in front of the second part.
* The '#' of preprocessor instructions goes into the first column of a line. Indenting is done after the '#' (using tabs again).
* Use /* */ for single line comments.
* Use // at the end of a command line to indicate comments.
** However, use /* */ after # preprocessor statements as // causes warnings in some compilers and/or might have unwanted side effects.
* C++ is defined using the ASCII character set. Do not use other character sets, not even in comments.
* OpenTTD includes some Objective-C sources (*.mm, used by OSX), which has a special object method invocation syntax: "[ obj doStuff:foo ]". Please use spaces on the inside of the brackets to differentiate from the C array syntax.
## Documentation
We use [Doxygen](http://doxygen.org/) to automatically generate documentation from the source code. It scans the source files for *recognizable* comments.
* **Make your comments recognizable.**
Doxygen only comments starting with the following style:
```c++
/**
///<
```
Use /** for multi-line comment blocks. Use ///< for single line comments for variables. Use //!< for single-line comments in the NoAI .hpp files.
For comments blocks inside a function always use /* */ or //. Use // only if the comment is on the same line as an instruction, otherwise use /* */.
### Files
* Put a @file command in a JavaDoc style comment at the start of the file, followed by a description.
```c++
/**
* @file
* This is the brief description.
* This is the detailed description here and on the following lines.
*/
```
> ### Warning
> If a file lacks a **file comment block** then NO entities in that file will be documented by Doxygen!
### Functions
* The documentation should be as close to the actual code as possible to maximise the chance of staying in sync.
* Comments for functions go in the .cpp file.
* Comments for inlines go in the .h/.hpp file.
* Small inlines can have a short 3 or 4 line JavaDoc style comment.
* Completely document larger functions.
* Document obvious parameters and return values too!
```c++
/**
* A brief explanation of what the function does and/or what purpose it serves.
* Then follows a more detailed explanation of the function that can span multiple lines.
*
* @param foo Explanation of the foo parameter
* @param bar Explanation of the bar parameter
* @return The sum of foo and bar (@return can be omitted if the return type is void)
*
* @see SomeOtherFunc()
* @see SOME_ENUM
*
* @bug Some bug description
* @bug Another bug description which continues in the next line
* and ends with the following blank line
*
* @todo Some to-do entry
*/
static int FooBar(int foo, int bar)
{
return foo + bar;
}
```
### Classes
* Document structs similarly to functions:
```c++
/**
* A short description of the struct.
* More detailed description of the its usage.
*
* @see [link to anything of interest]
*/
struct foo {
}
```
### JavaDoc structural commands
This table shows the commands you should use with OpenTTD. The full list is [here](http://www.stack.nl/~dimitri/doxygen/commands.html).
| Command | Action | Example |
| ------- | -------- | ------- |
| **@attention** | Starts a paragraph where a message that needs attention may be entered. The paragraph will be indented. | @attention Whales crossing! |
| **@brief** | Starts a paragraph that serves as a brief description. For classes and files the brief description will be used in lists and at the start of the documentation page. For class and file members, the brief description will be placed at the declaration of the member and prepended to the detailed description. A brief description may span several lines (although it is advised to keep it brief!). | @brief This is the brief description. |
| **@bug** | Starts a paragraph where one or more bugs may be reported. The paragraph will be indented. Multiple adjacent @bug commands will be joined into a single paragraph. Each bug description will start on a new line. Alternatively, one @bug command may mention several bugs. | @bug Memory leak in here? |
| **@note** | Starts a paragraph where a note can be entered. The paragraph will be indented. | @note Might be slow |
| **@todo** | Starts a paragraph where a TODO item is described. The description will also add an item to a separate TODO list. The two instances of the description will be cross-referenced. Each item in the TODO list will be preceded by a header that indicates the origin of the item. | @todo Better error checking |
| **@warning** | Starts a paragraph where one or more warning messages may be entered. The paragraph will be indented. | @warning Not thread safe! |
| | <small>**Function related commands**</small> | |
| **@return** | Starts a return value description for a function. | @return a character pointer |
| **@param** | Starts a parameter description for a function parameter with name <parameter-name>. Followed by a description of the parameter. The existence of the parameter is checked and a warning is given if the documentation of this (or any other) parameter is missing or not present in the function declaration or definition.<br><br>The @param command has an optional attribute specifying the direction of the attribute. Possible values are "in" and "out". | @param n The number of bytes to copy<br>@param[out] dest The memory area to copy to.<br>@param[in] src The memory area to copy from. |
| **@see** | Starts a paragraph where one or more cross-references to classes, functions, methods, variables, files or URL may be specified. Two names joined by either :: or # are understood as referring to a class and one of its members. One of several overloaded methods or constructors may be selected by including a parenthesized list of argument types after the method name. [Here](http://www.stack.nl/~dimitri/doxygen/autolink.html) you can find detailed information about this feature. | @see OtherFunc() |
| **@b** | Displays the following word using a bold font. Equivalent to &lt;b&gt;word&lt;/b&gt;. To put multiple words in bold use &lt;b&gt;multiple words&lt;/b&gt;.| ...@b this and @b that... |
| **@c / @p** | Displays the parameter <word> using a typewriter font. You can use this command to refer to member function parameters in the running text. To have multiple words in typewriter font use &lt;tt&gt;multiple words&lt;/tt&gt;. | ... the @p x and @p y coordinates are used to... |
| **@arg / @li** | This command has one argument that continues until the first blank line or until another @arg / @li is encountered. The command can be used to generate a simple, not nested list of arguments. Each argument should start with an @arg / @li command. | @arg @c AlignLeft left alignment.<br>@arg @c AlignCenter center alignment.<br>@arg @c AlignRight right alignment |
| **@n** | Forces a new line. Equivalent to and inspired by the printf function. |@n |
### More on Doxygen and JavaDoc
Doxygen knows two different kinds of comments:
* *Brief descriptions*: one-liners that describe the function roughly ([example](http://docs.openttd.org/annotated.html))
* *Detailed descriptions*: as the name suggests, these contain the detailed function/purpose of the entity they describe ([example](http://docs.openttd.org/structBridge.html))
You can omit either one or put them into different places but there's only one brief and one detailed description allowed for the same entity.
Doxygen knows three modes for documenting an entity:
* Before the entity
* After the entity
* In a different file
The latter is a little harder to maintain since the prototype of the entity it describes then is stored in several places (e.g. the .h file and the file with the descriptions). Also while it makes the code easier to read it also makes it easier to omit the important step of updating the description of an entity if it was changed - and we all know why that shouldn't happen ;)<br>
Because of those reasons, we will only use the first two documentation schemes.
Doxygen supports both Qt and JavaDoc comment styles:
* Qt style example: **int i; //!< The counter for the main loop**
* JavaDoc style example: **int i; /*\*< The counter for the main loop \*/**
It also supports more comment styles but those two are the ones which are standardized. For OTTD we'll be using the JavaDoc style. One of the reasons is that it has a feature that the Qt style doesn't offer: JavaDoc style comment blocks will automatically start a brief description which ends at the first dot followed by a space or new line. Everything after that will also be part of the detailed description.
The general structure of a JavaDoc style comment is
```c++
/**
* This is the brief description. And this sentence contains some further explanations that will appear in the detailed description only.
*/
```
and the resulting descriptions of that block would be:
* *Brief description*: This is the brief description.
* *Detailed description*: This is the brief description. And this sentence contains some further explanations that will appear in the detailed description only.
The distinction between the brief and detailed descriptions is made by the dot followed by a space/newline, so if you want to use that inside the brief description you need to escape the space/newline:
```c++
/**
* This is a brief description (e.g.\ using only a few words). Details go here.
*/
```
If you're doing a one-line comment, use:
```c++
int i; ///< This is the description.
```
Also in the comment block you can include so-called structural commands which tell Doxygen what follows. In general, their area of effect begins after the command word and ends when a blank line or some other command is encountered. Also, multiple occurrences of the same structural command within a comment block or the referring entity will be joined in the documentation output usually.
## Commit message
To achieve a coherent whole and to make changelog writing easier, here are some guidelines for commit messages.
There is a check-script on the git server (also available for clients, see below) to make sure we all follow those rules.
The first line of a message must match:
```
<pre>
<keyword>( #<issue>| <commit>(, (<keyword> #<issue>|<commit>))*)?: ([<section])? <Details>
</pre>
```
Keywords are:
* Add, Feature: Adding new stuff. Difference between "Feature" and "Add" is somewhat subjective. "Feature" for user-point-of-view stuff, "Add" for other.
* Change: Changing behaviour from user-point-of-view.
* Remove: Removing something from user-point-of-view.
* Codechange, Cleanup: Changes without intentional change of behaviour from user-point-of-view. Difference between "Codechange" and "Cleanup" is somewhat subjective.
* Fix, Revert: Fixing stuff.
* Doc, Update: Documentation changes, version increments, translator commits.
* Prepare: Preparation for bigger changes. Rarely used.
If you commit a fix for an [issue](https://github.com/OpenTTD/OpenTTD/issues), add the corresponding issue number in the form of #NNNN. Do it as well if you implement a feature with a matching entry.
In the case of bugfixes, if you know what revision the bug was introduced (eg regression), please mention that revision as well just after the prefix. Finding the trouble-causing revision is highly encouraged as it makes backporting/branching/releases that much easier.
To further structure the changelog, you can add sections. Example are:
* "Network" for network specific changes
* "NewGRF" for NewGRF additions
* "YAPP", "NPF", for changes in these features
* "OSX", "Debian", "win32", for OS-specific packaging changes
Further explanations, general bitching, etc. don't go into the first line. Use a new line for those.
Complete examples:
* Fix: [YAPF] Infinite loop in pathfinder.
* Fix #5926: [YAPF] Infinite loop in pathfinder.
* Fix 80dffae130: Warning about unsigned unary minus.
* Fix #6673, 99bb3a95b4: Store the map variety setting in the samegame.
* Revert d9065fbfbe, Fix #5922: ClientSizeChanged is only called via WndProcGdi which already has the mutex.
* Fix #1264, Fix #2037, Fix #2038, Fix #2110: Rewrite the autoreplace kernel.
## Other tips
### Remove trailing whitespace
To find out if/where you have trailing whitespace, you can use the following (unix/bash) command:
```
grep -n -R --include "*.[ch]" '[ ]$' * | grep --invert-match ".diff" | grep --invert-match ".patch"
```
Automatically removing whitespace is also possible with the following shell script (Note that it only checks .c, .cpp, .h, .hpp and .mm files):
```
#!/bin/sh
IFS='
'
for i in Makefile `find . -name \*.c -o -name \*.cpp -o -name \*.h -o -name \*.hpp -o -name \*.mm`
do
(
echo '%s/[ ]\{1,\}$/'
echo w
echo q
) | ed $i 2> /dev/null > /dev/null
done
```
### Install the client-side git commit hooks
The client-side hooks perform various checks when you commit changes locally.
* Whitespace and indentation checks.
* **Coding style** checks.
Get the hooks:
```
git clone https://github.com/OpenTTD/OpenTTD-git-hooks.git openttd_hooks
```
Install the hooks, assuming "openttd" is your work tree folder:
```
cd openttd/.git/hooks
ln -s -t . ../../../openttd_hooks/hooks/*
```

View File

@ -289,7 +289,7 @@ enum MusicTrackType {
/** Metadata about a music track. */
struct MusicSongInfo {
char songname[32]; ///< name of song displayed in UI
std::string songname; ///< name of song displayed in UI
byte tracknr; ///< track number of song displayed in UI
const char *filename; ///< file on disk containing song (when used in MusicSet class, this pointer is owned by MD5File object for the file)
MusicTrackType filetype; ///< decoder required for song file

View File

@ -853,7 +853,7 @@ public:
/* Position scrollbar to selected group */
for (uint i = 0; i < this->rows; i++) {
if (this->groups[i]->index == sel) {
this->vscroll->SetPosition(Clamp(i - this->vscroll->GetCapacity() / 2, 0, std::max(this->vscroll->GetCount() - this->vscroll->GetCapacity(), 0)));
this->vscroll->SetPosition(i - this->vscroll->GetCapacity() / 2);
break;
}
}

View File

@ -2997,7 +2997,7 @@ STR_LAI_TREE_NAME_CACTUS_PLANTS :Plantes de Cact
STR_LAI_STATION_DESCRIPTION_RAILROAD_STATION :Estació de Ferrocarril
STR_LAI_STATION_DESCRIPTION_AIRCRAFT_HANGAR :Hangar d'Avions
STR_LAI_STATION_DESCRIPTION_AIRPORT :Aeroport
STR_LAI_STATION_DESCRIPTION_TRUCK_LOADING_AREA :Àrea de càrrega de camions
STR_LAI_STATION_DESCRIPTION_TRUCK_LOADING_AREA :Moll de càrrega de camions
STR_LAI_STATION_DESCRIPTION_BUS_STATION :Parada d'autobús
STR_LAI_STATION_DESCRIPTION_SHIP_DOCK :Moll per vaixells
STR_LAI_STATION_DESCRIPTION_BUOY :Boia
@ -5087,6 +5087,18 @@ STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION :{WHITE}... mass
STR_ERROR_AIRCRAFT_NOT_ENOUGH_RANGE :{WHITE}... l'avió no té prou autonomia
# Extra messages which go on the third line of errors, explaining why orders failed
STR_ERROR_NO_RAIL_STATION :{WHITE}No hi ha cap estació ferroviària
STR_ERROR_NO_BUS_STATION :{WHITE}No hi ha cap estació d'autobusos
STR_ERROR_NO_TRUCK_STATION :{WHITE}No hi ha cap estació de camions
STR_ERROR_NO_DOCK :{WHITE}No hi ha cap port
STR_ERROR_NO_AIRPORT :{WHITE}No hi ha cap aeroport/heliport
STR_ERROR_NO_STOP_COMPATIBLE_ROAD_TYPE :{WHITE}No hi ha parades amb un tipus de carretera compatible
STR_ERROR_NO_STOP_COMPATIBLE_TRAM_TYPE :{WHITE}No hi ha parades amb un tipus de tramvia compatible
STR_ERROR_NO_STOP_ARTICULATED_VEHICLE :{WHITE}No hi ha parades que siguin adequades per a vehicles de carretera articulats.{}Aquest tipus de vehicles necessiten una estació de pas, no pas una estació final
STR_ERROR_AIRPORT_NO_PLANES :{WHITE}L'avió no pot aterrar en aquest aeroport
STR_ERROR_AIRPORT_NO_HELICOPTERS :{WHITE}L'helicòpter no pot aterrar en aquest aeroport
STR_ERROR_NO_RAIL_WAYPOINT :{WHITE}No hi ha cap punt de pas ferroviari
STR_ERROR_NO_BUOY :{WHITE}No hi ha cap boia
# Timetable related errors
STR_ERROR_CAN_T_TIMETABLE_VEHICLE :{WHITE}Impossible establir l'horari del vehicle...

View File

@ -2358,6 +2358,8 @@ STR_NETWORK_CLIENT_LIST_NEW_COMPANY :(New company)
STR_NETWORK_CLIENT_LIST_NEW_COMPANY_TOOLTIP :{BLACK}Create a new company and join it
STR_NETWORK_CLIENT_LIST_PLAYER_ICON_SELF_TOOLTIP :{BLACK}This is you
STR_NETWORK_CLIENT_LIST_PLAYER_ICON_HOST_TOOLTIP :{BLACK}This is the host of the game
STR_NETWORK_CLIENT_LIST_CLIENT_COMPANY_COUNT :{BLACK}{NUM} client{P "" s} - {NUM}/{NUM} compan{P y ies}
STR_NETWORK_CLIENT_LIST_CLIENT_COMPANY_COUNT_TOOLTIP :{BLACK}The number of currently connected clients, number of companies and maximum number of companies allowed by the server administrator
# Matches ConnectionType
###length 5
@ -5084,6 +5086,18 @@ STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION :{WHITE}... too
STR_ERROR_AIRCRAFT_NOT_ENOUGH_RANGE :{WHITE}... aircraft has not enough range
# Extra messages which go on the third line of errors, explaining why orders failed
STR_ERROR_NO_RAIL_STATION :{WHITE}There is no railway station
STR_ERROR_NO_BUS_STATION :{WHITE}There is no bus station
STR_ERROR_NO_TRUCK_STATION :{WHITE}There is no lorry station
STR_ERROR_NO_DOCK :{WHITE}There is no dock
STR_ERROR_NO_AIRPORT :{WHITE}There is no airport/heliport
STR_ERROR_NO_STOP_COMPATIBLE_ROAD_TYPE :{WHITE}There are no stops with a compatible road type
STR_ERROR_NO_STOP_COMPATIBLE_TRAM_TYPE :{WHITE}There are no stops with a compatible tram type
STR_ERROR_NO_STOP_ARTICULATED_VEHICLE :{WHITE}There are no stops which are suitable for articulated road vehicles.{}Articulated road vehicles require a drive-through stop, not a bay stop
STR_ERROR_AIRPORT_NO_PLANES :{WHITE}This plane cannot land at this heliport
STR_ERROR_AIRPORT_NO_HELICOPTERS :{WHITE}This helicopter cannot land at this airport
STR_ERROR_NO_RAIL_WAYPOINT :{WHITE}There is no railway waypoint
STR_ERROR_NO_BUOY :{WHITE}There is no buoy
# Timetable related errors
STR_ERROR_CAN_T_TIMETABLE_VEHICLE :{WHITE}Can't timetable vehicle...

View File

@ -5086,8 +5086,18 @@ STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION :{WHITE}... liia
STR_ERROR_AIRCRAFT_NOT_ENOUGH_RANGE :{WHITE}... ilma-aluksen toimintasäde ei riitä
# Extra messages which go on the third line of errors, explaining why orders failed
STR_ERROR_NO_RAIL_STATION :{WHITE}Rautatieasemaa ei ole
STR_ERROR_NO_BUS_STATION :{WHITE}Linja-autopysäkkiä ei ole
STR_ERROR_NO_TRUCK_STATION :{WHITE}Lastauslaituria ei ole
STR_ERROR_NO_DOCK :{WHITE}Satamaa ei ole
STR_ERROR_NO_AIRPORT :{WHITE}Lento-/helikopterikenttää ei ole
STR_ERROR_NO_STOP_COMPATIBLE_ROAD_TYPE :{WHITE}Ei ole pysäkkejä sopivalla tien tyypillä
STR_ERROR_NO_STOP_COMPATIBLE_TRAM_TYPE :{WHITE}Ei ole pysäkkejä sopivalla raitiotien tyypillä
STR_ERROR_NO_STOP_ARTICULATED_VEHICLE :{WHITE}Ei ole nivelajoneuvoille sopivia pysäkkejä.{}Nivelletyt ajoneuvot vaativat pysäkin, joka ei ole kääntöpysäkki vaan läpiajettava
STR_ERROR_AIRPORT_NO_PLANES :{WHITE}Tämä lentokone ei voi laskeutua tälle helikopterikentälle
STR_ERROR_AIRPORT_NO_HELICOPTERS :{WHITE}Tämä helikopteri ei voi laskeutua tälle lentokentälle
STR_ERROR_NO_RAIL_WAYPOINT :{WHITE}Rautatien reittipistettä ei ole
STR_ERROR_NO_BUOY :{WHITE}Poijua ei ole
# Timetable related errors
STR_ERROR_CAN_T_TIMETABLE_VEHICLE :{WHITE}Ei voi asettaa aikataulua.

View File

@ -5084,6 +5084,9 @@ STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION :{WHITE}... prea
STR_ERROR_AIRCRAFT_NOT_ENOUGH_RANGE :{WHITE}... avionul nu are o rază de acțiune suficientă
# Extra messages which go on the third line of errors, explaining why orders failed
STR_ERROR_NO_STOP_ARTICULATED_VEHICLE :{WHITE}Nu există opriri care să fie potrivite pentru vehiculele rutiere articulate.{}Vehiculele rutiere articulate necesită o oprire de drum, nu o oprire într-o rampă.
STR_ERROR_NO_RAIL_WAYPOINT :{WHITE}Nu există niciun punct intermediar feroviar
STR_ERROR_NO_BUOY :{WHITE}Nu există geamanduri
# Timetable related errors
STR_ERROR_CAN_T_TIMETABLE_VEHICLE :{WHITE}Nu pot programa vehiculul...

View File

@ -128,7 +128,6 @@ bool MusicSet::FillSetDetails(IniFile *ini, const char *path, const char *full_f
for (uint i = 0; i < lengthof(this->songinfo); i++) {
const char *filename = this->files[i].filename;
if (names == nullptr || StrEmpty(filename) || this->files[i].check_result == MD5File::CR_NO_FILE) {
this->songinfo[i].songname[0] = '\0';
continue;
}
@ -141,11 +140,10 @@ bool MusicSet::FillSetDetails(IniFile *ini, const char *path, const char *full_f
this->songinfo[i].cat_index = atoi(item->value->c_str());
char *songname = GetMusicCatEntryName(filename, this->songinfo[i].cat_index);
if (songname == nullptr) {
DEBUG(grf, 1, "Base music set song missing from CAT file: %s/%d", filename, this->songinfo[i].cat_index);
this->songinfo[i].songname[0] = '\0';
DEBUG(grf, 0, "Base music set song missing from CAT file: %s/%d", filename, this->songinfo[i].cat_index);
continue;
}
strecpy(this->songinfo[i].songname, songname, lastof(this->songinfo[i].songname));
this->songinfo[i].songname = songname;
free(songname);
} else {
this->songinfo[i].filetype = MTT_STANDARDMIDI;
@ -166,7 +164,7 @@ bool MusicSet::FillSetDetails(IniFile *ini, const char *path, const char *full_f
if (this->songinfo[i].filetype == MTT_STANDARDMIDI) {
if (item != nullptr && item->value.has_value() && !item->value->empty()) {
strecpy(this->songinfo[i].songname, item->value->c_str(), lastof(this->songinfo[i].songname));
this->songinfo[i].songname = item->value.value();
} else {
DEBUG(grf, 0, "Base music set song name missing: %s", filename);
return false;

View File

@ -43,7 +43,7 @@ struct MusicSystem {
uint set_index; ///< index of song in set
PlaylistEntry(const MusicSet *set, uint set_index) : MusicSongInfo(set->songinfo[set_index]), set(set), set_index(set_index) { }
bool IsValid() const { return !StrEmpty(this->songname); }
bool IsValid() const { return !this->songname.empty(); }
};
typedef std::vector<PlaylistEntry> Playlist;
@ -147,18 +147,20 @@ void MusicSystem::ChangePlaylist(PlaylistChoices pl)
{
assert(pl < PLCH_MAX && pl >= PLCH_ALLMUSIC);
this->displayed_playlist = this->standard_playlists[pl];
this->active_playlist = this->displayed_playlist;
this->selected_playlist = pl;
this->playlist_position = 0;
if (pl != PLCH_THEMEONLY) _settings_client.music.playlist = pl;
if (this->selected_playlist != PLCH_THEMEONLY) _settings_client.music.playlist = this->selected_playlist;
if (_game_mode != GM_MENU || pl == PLCH_THEMEONLY) {
this->displayed_playlist = this->standard_playlists[pl];
this->active_playlist = this->displayed_playlist;
this->selected_playlist = pl;
this->playlist_position = 0;
if (_settings_client.music.shuffle) {
this->Shuffle();
/* Shuffle() will also Play() if necessary, only start once */
} else if (_settings_client.music.playing) {
this->Play();
if (_settings_client.music.shuffle) {
this->Shuffle();
/* Shuffle() will also Play() if necessary, only start once */
} else if (_settings_client.music.playing) {
this->Play();
}
}
InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
@ -178,6 +180,8 @@ void MusicSystem::ChangeMusicSet(const std::string &set_name)
this->ChangePlaylist(this->selected_playlist);
InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_OPTIONS, 0, true);
InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0, 1, true);
InvalidateWindowData(WC_MUSIC_WINDOW, 0, 1, true);
}
/** Enable shuffle mode and restart playback */
@ -486,7 +490,12 @@ struct MusicTrackSelectionWindow : public Window {
this->SetWidgetLoweredState(WID_MTS_ALL + i, i == _settings_client.music.playlist);
}
this->SetWidgetDisabledState(WID_MTS_CLEAR, _settings_client.music.playlist <= 3);
this->SetDirty();
if (data == 1) {
this->ReInit();
} else {
this->SetDirty();
}
}
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
@ -611,9 +620,9 @@ static const NWidgetPart _nested_music_track_selection_widgets[] = {
NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
/* Left panel. */
NWidget(NWID_VERTICAL),
NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_PLAYLIST_TRACK_INDEX, STR_NULL),
NWidget(WWT_PANEL, COLOUR_GREY, WID_MTS_LIST_LEFT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_ADD_TRACK), EndContainer(),
NWidget(NWID_SPACER), SetMinimalSize(0, 2),
NWidget(WWT_LABEL, COLOUR_GREY), SetFill(1, 0), SetDataTip(STR_PLAYLIST_TRACK_INDEX, STR_NULL),
NWidget(WWT_PANEL, COLOUR_GREY, WID_MTS_LIST_LEFT), SetFill(1, 1), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_ADD_TRACK), EndContainer(),
NWidget(NWID_SPACER), SetFill(1, 0), SetMinimalSize(0, 2),
EndContainer(),
/* Middle buttons. */
NWidget(NWID_VERTICAL),
@ -630,9 +639,9 @@ static const NWidgetPart _nested_music_track_selection_widgets[] = {
EndContainer(),
/* Right panel. */
NWidget(NWID_VERTICAL),
NWidget(WWT_LABEL, COLOUR_GREY, WID_MTS_PLAYLIST), SetDataTip(STR_PLAYLIST_PROGRAM, STR_NULL),
NWidget(WWT_PANEL, COLOUR_GREY, WID_MTS_LIST_RIGHT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_REMOVE_TRACK), EndContainer(),
NWidget(NWID_SPACER), SetMinimalSize(0, 2),
NWidget(WWT_LABEL, COLOUR_GREY, WID_MTS_PLAYLIST), SetFill(1, 0), SetDataTip(STR_PLAYLIST_PROGRAM, STR_NULL),
NWidget(WWT_PANEL, COLOUR_GREY, WID_MTS_LIST_RIGHT), SetFill(1, 1), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_REMOVE_TRACK), EndContainer(),
NWidget(NWID_SPACER), SetFill(1, 0), SetMinimalSize(0, 2),
EndContainer(),
EndContainer(),
EndContainer(),
@ -770,7 +779,11 @@ struct MusicWindow : public Window {
UpdateDisabledButtons();
this->SetDirty();
if (data == 1) {
this->ReInit();
} else {
this->SetDirty();
}
}
void OnClick(Point pt, int widget, int click_count) override

View File

@ -412,9 +412,8 @@ protected:
/* show highlighted item with a different colour */
if (highlight) {
Rect r = {name.left, y, info.right, y + (int)this->resize.step_height - 1};
Rect ir = r.Shrink(WidgetDimensions::scaled.bevel);
GfxFillRect(ir.left, ir.top, ir.right, ir.bottom, PC_GREY);
Rect r = {std::min(name.left, info.left), y, std::max(name.right, info.right), y + (int)this->resize.step_height - 1};
GfxFillRect(r.Shrink(WidgetDimensions::scaled.bevel), PC_GREY);
}
/* offsets to vertically centre text and icons */

View File

@ -1566,7 +1566,7 @@ static ChangeInfoResult ShipVehicleChangeInfo(uint engine, int numinfo, int prop
ei->cargo_type = ctype;
} else {
ei->cargo_type = CT_INVALID;
grfmsg(2, "RailVehicleChangeInfo: Invalid cargo type %d, using first refittable", ctype);
grfmsg(2, "ShipVehicleChangeInfo: Invalid cargo type %d, using first refittable", ctype);
}
break;
}

View File

@ -94,6 +94,7 @@ extern uint8 GetAirportNoiseLevelForDistance(const struct AirportSpec *as, uint
/* static */ SQInteger ScriptAirport::GetNumHangars(TileIndex tile)
{
EnforceDeityOrCompanyModeValid(-1);
if (!::IsValidTile(tile)) return -1;
if (!::IsTileType(tile, MP_STATION)) return -1;
@ -106,6 +107,7 @@ extern uint8 GetAirportNoiseLevelForDistance(const struct AirportSpec *as, uint
/* static */ TileIndex ScriptAirport::GetHangarOfAirport(TileIndex tile)
{
EnforceDeityOrCompanyModeValid(INVALID_TILE);
if (!::IsValidTile(tile)) return INVALID_TILE;
if (!::IsTileType(tile, MP_STATION)) return INVALID_TILE;
if (GetNumHangars(tile) < 1) return INVALID_TILE;

View File

@ -19,6 +19,7 @@
/* static */ bool ScriptBaseStation::IsValidBaseStation(StationID station_id)
{
EnforceDeityOrCompanyModeValid(false);
const BaseStation *st = ::BaseStation::GetIfValid(station_id);
return st != nullptr && (st->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity() || st->owner == OWNER_NONE);
}

View File

@ -71,6 +71,7 @@ static void _DoCommandReturnBuildBridge1(class ScriptInstance *instance)
/* static */ bool ScriptBridge::BuildBridge(ScriptVehicle::VehicleType vehicle_type, BridgeID bridge_id, TileIndex start, TileIndex end)
{
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, start != end);
EnforcePrecondition(false, ::IsValidTile(start) && ::IsValidTile(end));
EnforcePrecondition(false, TileX(start) == TileX(end) || TileY(start) == TileY(end));
@ -107,6 +108,8 @@ static void _DoCommandReturnBuildBridge1(class ScriptInstance *instance)
/* static */ bool ScriptBridge::_BuildBridgeRoad1()
{
EnforceDeityOrCompanyModeValid(false);
/* Build the piece of road on the 'start' side of the bridge */
TileIndex end = ScriptObject::GetCallbackVariable(0);
TileIndex start = ScriptObject::GetCallbackVariable(1);
@ -119,6 +122,8 @@ static void _DoCommandReturnBuildBridge1(class ScriptInstance *instance)
/* static */ bool ScriptBridge::_BuildBridgeRoad2()
{
EnforceDeityOrCompanyModeValid(false);
/* Build the piece of road on the 'end' side of the bridge */
TileIndex end = ScriptObject::GetCallbackVariable(0);
TileIndex start = ScriptObject::GetCallbackVariable(1);

View File

@ -43,6 +43,7 @@
{
CCountedPtr<Text> counter(name);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, name != nullptr);
const std::string &text = name->GetDecodedText();
EnforcePreconditionEncodedText(false, text);
@ -64,6 +65,7 @@
{
CCountedPtr<Text> counter(name);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, name != nullptr);
const std::string &text = name->GetDecodedText();
EnforcePreconditionEncodedText(false, text);
@ -90,6 +92,7 @@
/* static */ bool ScriptCompany::SetPresidentGender(Gender gender)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, gender == GENDER_MALE || gender == GENDER_FEMALE);
EnforcePrecondition(false, GetPresidentGender(ScriptCompany::COMPANY_SELF) != gender);
@ -246,8 +249,6 @@
{
EnforceDeityMode(false);
EnforcePrecondition(false, expenses_type < (ExpensesType)::EXPENSES_END);
EnforcePrecondition(false, (int64)delta >= INT32_MIN);
EnforcePrecondition(false, (int64)delta <= INT32_MAX);
EnforcePrecondition(false, tile == INVALID_TILE || ::IsValidTile(tile));
company = ResolveCompanyID(company);
@ -276,6 +277,7 @@
/* static */ bool ScriptCompany::SetAutoRenewStatus(bool autorenew)
{
EnforceCompanyModeValid(false);
return ScriptObject::DoCommand(0, 0, autorenew ? 1 : 0, CMD_CHANGE_COMPANY_SETTING, "company.engine_renew");
}
@ -289,7 +291,9 @@
/* static */ bool ScriptCompany::SetAutoRenewMonths(SQInteger months)
{
EnforceCompanyModeValid(false);
months = Clamp<SQInteger>(months, INT16_MIN, INT16_MAX);
return ScriptObject::DoCommand(0, 0, months, CMD_CHANGE_COMPANY_SETTING, "company.engine_renew_months");
}
@ -303,6 +307,7 @@
/* static */ bool ScriptCompany::SetAutoRenewMoney(Money money)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, money >= 0);
EnforcePrecondition(false, (int64)money <= UINT32_MAX);
return ScriptObject::DoCommand(0, 0, money, CMD_CHANGE_COMPANY_SETTING, "company.engine_renew_money");
@ -318,11 +323,13 @@
/* static */ bool ScriptCompany::SetPrimaryLiveryColour(LiveryScheme scheme, Colours colour)
{
EnforceCompanyModeValid(false);
return ScriptObject::DoCommand(0, scheme, colour, CMD_SET_COMPANY_COLOUR);
}
/* static */ bool ScriptCompany::SetSecondaryLiveryColour(LiveryScheme scheme, Colours colour)
{
EnforceCompanyModeValid(false);
return ScriptObject::DoCommand(0, scheme | 1 << 8, colour, CMD_SET_COMPANY_COLOUR);
}

View File

@ -139,6 +139,7 @@ public:
* Set the name of your company.
* @param name The new name of the company (can be either a raw string, or a ScriptText object).
* @pre name != null && len(name) != 0.
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_NAME_IS_NOT_UNIQUE
* @return True if the name was changed.
*/
@ -156,6 +157,7 @@ public:
* Set the name of your president.
* @param name The new name of the president (can be either a raw string, or a ScriptText object).
* @pre name != null && len(name) != 0.
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_NAME_IS_NOT_UNIQUE
* @return True if the name was changed.
*/
@ -173,6 +175,7 @@ public:
* Set the gender of the president of your company.
* @param gender The new gender for your president.
* @pre GetPresidentGender(ScriptCompany.COMPANY_SELF) != gender.
* @game @pre ScriptCompanyMode::IsValid().
* @return True if the gender was changed.
* @note When successful a random face will be created.
* @api -game
@ -247,8 +250,6 @@ public:
* @return True, if the bank balance was changed.
* @game @pre ScriptCompanyMode::IsDeity().
* @pre ResolveCompanyID(company) != COMPANY_INVALID.
* @pre delta >= -2**31
* @pre delta < 2**31
* @note You need to create your own news message to inform about costs/gifts that you create using this command.
* @api -ai
*/
@ -347,6 +348,7 @@ public:
/**
* Set whether autorenew is enabled for your company.
* @param autorenew The new autorenew status.
* @game @pre ScriptCompanyMode::IsValid().
* @return True if autorenew status has been modified.
* @api -game
*/
@ -364,6 +366,7 @@ public:
* Set the number of months before/after max age to autorenew an engine for your company.
* @param months The new months between autorenew.
* The value will be clamped to MIN(int16) .. MAX(int16).
* @game @pre ScriptCompanyMode::IsValid().
* @return True if autorenew months has been modified.
* @api -game
*/
@ -380,6 +383,7 @@ public:
/**
* Set the minimum money needed to autorenew an engine for your company.
* @param money The new minimum required money for autorenew to work.
* @game @pre ScriptCompanyMode::IsValid().
* @return True if autorenew money has been modified.
* @pre money >= 0
* @pre money < 2**32
@ -399,6 +403,7 @@ public:
* Set primary colour for your company.
* @param scheme Livery scheme to set.
* @param colour Colour to set.
* @game @pre ScriptCompanyMode::IsValid().
* @return False if unable to set primary colour of the livery scheme (e.g. colour in use).
*/
static bool SetPrimaryLiveryColour(LiveryScheme scheme, Colours colour);
@ -407,6 +412,7 @@ public:
* Set secondary colour for your company.
* @param scheme Livery scheme to set.
* @param colour Colour to set.
* @game @pre ScriptCompanyMode::IsValid().
* @return False if unable to set secondary colour of the livery scheme.
*/
static bool SetSecondaryLiveryColour(LiveryScheme scheme, Colours colour);

View File

@ -29,7 +29,7 @@ ScriptCompanyMode::~ScriptCompanyMode()
/* static */ bool ScriptCompanyMode::IsValid()
{
return ScriptObject::GetCompany() != OWNER_DEITY;
return ::Company::IsValidID(ScriptObject::GetCompany());
}
/* static */ bool ScriptCompanyMode::IsDeity()

View File

@ -50,7 +50,7 @@ public:
/**
* Check whether a company mode is valid. In other words, are commands
* being executed under some company.
* being executed under some company and does the company still exist?
* @return true When a company mode is valid.
* @post !ScriptCompanyMode::IsDeity().
*/

View File

@ -16,6 +16,7 @@
ScriptDepotList::ScriptDepotList(ScriptTile::TransportType transport_type)
{
EnforceDeityOrCompanyModeValid_Void();
::TileType tile_type;
switch (transport_type) {
default: return;

View File

@ -23,6 +23,7 @@
/* static */ bool ScriptEngine::IsValidEngine(EngineID engine_id)
{
EnforceDeityOrCompanyModeValid(false);
const Engine *e = ::Engine::GetIfValid(engine_id);
if (e == nullptr || !e->IsEnabled()) return false;
@ -34,6 +35,7 @@
/* static */ bool ScriptEngine::IsBuildable(EngineID engine_id)
{
EnforceDeityOrCompanyModeValid(false);
const Engine *e = ::Engine::GetIfValid(engine_id);
return e != nullptr && ::IsEngineBuildable(engine_id, e->type, ScriptObject::GetCompany());
}

View File

@ -15,6 +15,7 @@
ScriptEngineList::ScriptEngineList(ScriptVehicle::VehicleType vehicle_type)
{
EnforceDeityOrCompanyModeValid_Void();
for (const Engine *e : Engine::IterateType((::VehicleType)vehicle_type)) {
if (ScriptCompanyMode::IsDeity() || HasBit(e->company_avail, ScriptObject::GetCompany())) this->AddItem(e->index);
}

View File

@ -53,14 +53,31 @@
* @param returnval The value to return on failure.
*/
#define EnforceCompanyModeValid(returnval) \
EnforcePrecondition(returnval, ScriptCompanyMode::IsValid())
EnforcePreconditionCustomError(returnval, ScriptCompanyMode::IsValid(), ScriptError::ERR_PRECONDITION_INVALID_COMPANY)
/**
* Helper to enforce the precondition that we are in a deity mode.
* @param returnval The value to return on failure.
*/
#define EnforceDeityMode(returnval) \
EnforcePrecondition(returnval, ScriptCompanyMode::IsDeity())
EnforcePreconditionCustomError(returnval, ScriptCompanyMode::IsDeity(), ScriptError::ERR_PRECONDITION_INVALID_COMPANY)
/**
* Helper to enforce the precondition that the company mode is valid or that we are a deity.
* @param returnval The value to return on failure.
*/
#define EnforceDeityOrCompanyModeValid(returnval) \
EnforcePreconditionCustomError(returnval, ScriptCompanyMode::IsDeity() || ScriptCompanyMode::IsValid(), ScriptError::ERR_PRECONDITION_INVALID_COMPANY)
/**
* Helper to enforce the precondition that the company mode is valid or that we are a deity.
*/
#define EnforceDeityOrCompanyModeValid_Void() \
if (!(ScriptCompanyMode::IsDeity() || ScriptCompanyMode::IsValid())) { \
ScriptObject::SetLastError(ScriptError::ERR_PRECONDITION_INVALID_COMPANY); \
return; \
}
/**
* Class that handles all error related functions.

View File

@ -109,12 +109,14 @@ int32 ScriptEventEnginePreview::GetVehicleType()
bool ScriptEventEnginePreview::AcceptPreview()
{
EnforceCompanyModeValid(false);
if (!this->IsEngineValid()) return false;
return ScriptObject::DoCommand(0, this->engine, 0, CMD_WANT_ENGINE_PREVIEW);
}
bool ScriptEventCompanyAskMerger::AcceptMerger()
{
EnforceCompanyModeValid(false);
return ScriptObject::DoCommand(0, this->owner, 0, CMD_BUY_COMPANY);
}

View File

@ -289,6 +289,7 @@ public:
/**
* Accept the engine preview.
* @game @pre ScriptCompanyMode::IsValid().
* @return True when the accepting succeeded.
*/
bool AcceptPreview();
@ -410,6 +411,7 @@ public:
/**
* Take over the company for this merger.
* @game @pre ScriptCompanyMode::IsValid().
* @return true if the merger was a success.
*/
bool AcceptMerger();

View File

@ -9,6 +9,7 @@
#include "../../stdafx.h"
#include "script_game.hpp"
#include "script_error.hpp"
#include "../../command_type.h"
#include "../../settings_type.h"
#include "../../network/network.h"

View File

@ -32,6 +32,7 @@
/* static */ bool ScriptGameSettings::SetValue(const char *setting, SQInteger value)
{
EnforceDeityOrCompanyModeValid(false);
if (!IsValid(setting)) return false;
const SettingDesc *sd = GetSettingFromName(setting);

View File

@ -22,12 +22,14 @@
/* static */ bool ScriptGroup::IsValidGroup(GroupID group_id)
{
EnforceDeityOrCompanyModeValid(false);
const Group *g = ::Group::GetIfValid(group_id);
return g != nullptr && g->owner == ScriptObject::GetCompany();
}
/* static */ ScriptGroup::GroupID ScriptGroup::CreateGroup(ScriptVehicle::VehicleType vehicle_type, GroupID parent_group_id)
{
EnforceCompanyModeValid(GROUP_INVALID);
if (!ScriptObject::DoCommand(0, (::VehicleType)vehicle_type, parent_group_id, CMD_CREATE_GROUP, nullptr, &ScriptInstance::DoCommandReturnGroupID)) return GROUP_INVALID;
/* In case of test-mode, we return GroupID 0 */
@ -36,6 +38,7 @@
/* static */ bool ScriptGroup::DeleteGroup(GroupID group_id)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidGroup(group_id));
return ScriptObject::DoCommand(0, group_id, 0, CMD_DELETE_GROUP);
@ -52,6 +55,7 @@
{
CCountedPtr<Text> counter(name);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidGroup(group_id));
EnforcePrecondition(false, name != nullptr);
const std::string &text = name->GetDecodedText();
@ -71,6 +75,7 @@
/* static */ bool ScriptGroup::SetParent(GroupID group_id, GroupID parent_group_id)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidGroup(group_id));
EnforcePrecondition(false, IsValidGroup(parent_group_id));
@ -87,6 +92,7 @@
/* static */ bool ScriptGroup::EnableAutoReplaceProtection(GroupID group_id, bool enable)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidGroup(group_id));
return ScriptObject::DoCommand(0, group_id | GroupFlags::GF_REPLACE_PROTECTION, enable ? 1 : 0, CMD_SET_GROUP_FLAG);
@ -101,6 +107,7 @@
/* static */ SQInteger ScriptGroup::GetNumEngines(GroupID group_id, EngineID engine_id)
{
EnforceCompanyModeValid(-1);
if (!IsValidGroup(group_id) && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return -1;
return GetGroupNumEngines(ScriptObject::GetCompany(), group_id, engine_id);
@ -108,6 +115,7 @@
/* static */ SQInteger ScriptGroup::GetNumVehicles(GroupID group_id, ScriptVehicle::VehicleType vehicle_type)
{
EnforceCompanyModeValid(-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;
@ -117,6 +125,7 @@
/* static */ bool ScriptGroup::MoveVehicle(GroupID group_id, VehicleID vehicle_id)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT);
EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
@ -125,6 +134,7 @@
/* static */ bool ScriptGroup::EnableWagonRemoval(bool enable_removal)
{
EnforceCompanyModeValid(false);
if (HasWagonRemoval() == enable_removal) return true;
return ScriptObject::DoCommand(0, 0, enable_removal ? 1 : 0, CMD_CHANGE_COMPANY_SETTING, "company.renew_keep_length");
@ -132,11 +142,13 @@
/* static */ bool ScriptGroup::HasWagonRemoval()
{
EnforceCompanyModeValid(false);
return ::Company::Get(ScriptObject::GetCompany())->settings.renew_keep_length;
}
/* static */ bool ScriptGroup::SetAutoReplace(GroupID group_id, EngineID engine_id_old, EngineID engine_id_new)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL);
EnforcePrecondition(false, ScriptEngine::IsBuildable(engine_id_new));
@ -145,6 +157,7 @@
/* 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;
return ::EngineReplacementForCompany(Company::Get(ScriptObject::GetCompany()), engine_id, group_id);
@ -152,6 +165,7 @@
/* static */ bool ScriptGroup::StopAutoReplace(GroupID group_id, EngineID engine_id)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL);
return ScriptObject::DoCommand(0, group_id << 16, (::INVALID_ENGINE << 16) | engine_id, CMD_SET_AUTOREPLACE);
@ -202,6 +216,7 @@
/* static */ bool ScriptGroup::SetPrimaryColour(GroupID group_id, ScriptCompany::Colours colour)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidGroup(group_id));
return ScriptObject::DoCommand(0, group_id, colour << 16, CMD_SET_GROUP_LIVERY);
@ -209,6 +224,7 @@
/* static */ bool ScriptGroup::SetSecondaryColour(GroupID group_id, ScriptCompany::Colours colour)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidGroup(group_id));
return ScriptObject::DoCommand(0, group_id, (1 << 8) | (colour << 16), CMD_SET_GROUP_LIVERY);

View File

@ -41,6 +41,7 @@ public:
* Create a new group.
* @param vehicle_type The type of vehicle to create a group for.
* @param parent_group_id The parent group id to create this group under, INVALID_GROUP for top-level.
* @game @pre ScriptCompanyMode::IsValid().
* @return The GroupID of the new group, or an invalid GroupID when
* it failed. Check the return value using IsValidGroup(). In test-mode
* 0 is returned if it was successful; any other value indicates failure.
@ -52,6 +53,7 @@ public:
* given group will move to the GROUP_DEFAULT.
* @param group_id The group to delete.
* @pre IsValidGroup(group_id).
* @game @pre ScriptCompanyMode::IsValid().
* @return True if and only if the group was successfully deleted.
*/
static bool DeleteGroup(GroupID group_id);
@ -70,6 +72,7 @@ public:
* @param name The name for the group (can be either a raw string, or a ScriptText object).
* @pre IsValidGroup(group_id).
* @pre name != null && len(name) != 0
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_NAME_IS_NOT_UNIQUE
* @return True if and only if the name was changed.
*/
@ -89,6 +92,7 @@ public:
* @param parent_group_id The parent group to set.
* @pre IsValidGroup(group_id).
* @pre IsValidGroup(parent_group_id).
* @game @pre ScriptCompanyMode::IsValid().
* @return True if and only if the parent group was changed.
*/
static bool SetParent(GroupID group_id, GroupID parent_group_id);
@ -107,6 +111,7 @@ public:
* @param group_id The group to change the protection for.
* @param enable True if protection should be enabled.
* @pre IsValidGroup(group_id).
* @game @pre ScriptCompanyMode::IsValid().
* @return True if and only if the protection was successfully changed.
*/
static bool EnableAutoReplaceProtection(GroupID group_id, bool enable);
@ -124,6 +129,7 @@ public:
* @param group_id The group to get the number of engines in.
* @param engine_id The engine id to count.
* @pre IsValidGroup(group_id) || group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
* @game @pre ScriptCompanyMode::IsValid().
* @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);
@ -135,6 +141,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().
* @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.
@ -148,6 +155,7 @@ public:
* @param vehicle_id The vehicle to move to the group.
* @pre IsValidGroup(group_id) || group_id == GROUP_DEFAULT.
* @pre ScriptVehicle::IsPrimaryVehicle(vehicle_id).
* @game @pre ScriptCompanyMode::IsValid().
* @return True if and only if the vehicle was successfully moved to the group.
* @note A vehicle can be in only one group at the same time. To remove it from
* a group, move it to another or to GROUP_DEFAULT. Moving the vehicle to the
@ -161,12 +169,14 @@ public:
* If enabled, wagons are removed from the end of the vehicle until it
* fits in the same number of tiles as it did before.
* @param keep_length If true, wagons will be removed if the new engine is longer.
* @game @pre ScriptCompanyMode::IsValid().
* @return True if and only if the value was successfully changed.
*/
static bool EnableWagonRemoval(bool keep_length);
/**
* Get the current status of wagon removal.
* @game @pre ScriptCompanyMode::IsValid().
* @return Whether or not wagon removal is enabled.
*/
static bool HasWagonRemoval();
@ -179,6 +189,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().
* @return True if and if the replacing was successfully started.
* @note To stop autoreplacing engine_id_old, call StopAutoReplace(group_id, engine_id_old).
*/
@ -189,6 +200,7 @@ public:
* @param group_id The group to get the replacement from.
* @param engine_id The engine that is being replaced.
* @pre IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL.
* @game @pre ScriptCompanyMode::IsValid().
* @return The EngineID that is replacing engine_id or an invalid EngineID
* in case engine_id is not begin replaced.
*/
@ -199,6 +211,7 @@ public:
* @param group_id The group to stop replacing the engine in.
* @param engine_id The engine id to stop replacing with another engine.
* @pre IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL.
* @game @pre ScriptCompanyMode::IsValid().
* @return True if and if the replacing was successfully stopped.
*/
static bool StopAutoReplace(GroupID group_id, EngineID engine_id);
@ -232,6 +245,7 @@ public:
* @param group_id The group id to set the colour of.
* @param colour Colour to set.
* @pre IsValidGroup(group_id).
* @game @pre ScriptCompanyMode::IsValid().
* @return True iff the colour was set successfully.
*/
static bool SetPrimaryColour(GroupID group_id, ScriptCompany::Colours colour);
@ -241,6 +255,7 @@ public:
* @param group_id The group id to set the colour of.
* @param colour Colour to set.
* @pre IsValidGroup(group_id).
* @game @pre ScriptCompanyMode::IsValid().
* @return True iff the colour was set successfully.
*/
static bool SetSecondaryColour(GroupID group_id, ScriptCompany::Colours colour);

View File

@ -9,12 +9,14 @@
#include "../../stdafx.h"
#include "script_grouplist.hpp"
#include "script_error.hpp"
#include "../../group.h"
#include "../../safeguards.h"
ScriptGroupList::ScriptGroupList()
{
EnforceDeityOrCompanyModeValid_Void();
for (const Group *g : Group::Iterate()) {
if (g->owner == ScriptObject::GetCompany()) this->AddItem(g->index);
}

View File

@ -52,6 +52,7 @@
{
CCountedPtr<Text> counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidIndustry(industry_id));
return ScriptObject::DoCommand(0, industry_id, 0, CMD_INDUSTRY_SET_TEXT, text != nullptr ? text->GetEncodedText().c_str() : "");
@ -267,6 +268,7 @@
/* static */ bool ScriptIndustry::SetExclusiveSupplier(IndustryID industry_id, ScriptCompany::CompanyID company_id)
{
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidIndustry(industry_id));
auto company = ScriptCompany::ResolveCompanyID(company_id);
@ -286,6 +288,7 @@
/* static */ bool ScriptIndustry::SetExclusiveConsumer(IndustryID industry_id, ScriptCompany::CompanyID company_id)
{
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidIndustry(industry_id));
auto company = ScriptCompany::ResolveCompanyID(company_id);

View File

@ -85,6 +85,7 @@ public:
* Set the custom text of an industry, shown in the GUI.
* @param industry_id The industry to set the custom text of.
* @param text The text to set it to (can be either a raw string, or a ScriptText object). If null, or an empty string, is passed, the text will be removed.
* @pre ScriptCompanyMode::IsDeity().
* @pre IsValidIndustry(industry_id).
* @return True if the action succeeded.
* @api -ai
@ -286,6 +287,7 @@ public:
* @param industry_id The index of the industry.
* @param company_id The company to set (ScriptCompany::COMPANY_INVALID to reset).
* @pre IsValidIndustry(industry_id).
* @pre ScriptCompanyMode::IsDeity().
* @return True if the action succeeded.
* @api -ai
*/
@ -306,6 +308,7 @@ public:
* @param industry_id The index of the industry.
* @param company_id The company to set (ScriptCompany::COMPANY_INVALID to reset).
* @pre IsValidIndustry(industry_id).
* @pre ScriptCompanyMode::IsDeity().
* @return True if the action succeeded.
* @api -ai
*/

View File

@ -118,6 +118,7 @@
/* static */ bool ScriptIndustryType::BuildIndustry(IndustryType industry_type, TileIndex tile)
{
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, CanBuildIndustry(industry_type));
EnforcePrecondition(false, ScriptMap::IsValidTile(tile));
@ -128,6 +129,7 @@
/* static */ bool ScriptIndustryType::ProspectIndustry(IndustryType industry_type)
{
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, CanProspectIndustry(industry_type));
uint32 seed = ScriptBase::Rand();

View File

@ -23,6 +23,7 @@
{
CCountedPtr<Text> counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, text != nullptr);
const std::string &encoded = text->GetEncodedText();
EnforcePreconditionEncodedText(false, encoded);

View File

@ -62,6 +62,7 @@ public:
* @pre text != null.
* @pre company == COMPANY_INVALID || ResolveCompanyID(company) != COMPANY_INVALID.
* @pre The \a reference condition must be fulfilled.
* @pre ScriptCompanyMode::IsDeity().
*/
static bool Create(NewsType type, Text *text, ScriptCompany::CompanyID company, NewsReferenceType ref_type, SQInteger reference);
};

View File

@ -319,7 +319,7 @@ ScriptObject::ActiveInstance::~ActiveInstance()
throw Script_FatalError("You are not allowed to execute any DoCommand (even indirect) in your constructor, Save(), Load(), and any valuator.");
}
if (ScriptCompanyMode::IsValid() && !::Company::IsValidID(ScriptObject::GetCompany())) {
if (!ScriptCompanyMode::IsDeity() && !ScriptCompanyMode::IsValid()) {
ScriptObject::SetLastError(ScriptError::ERR_PRECONDITION_INVALID_COMPANY);
return false;
}

View File

@ -38,6 +38,7 @@
/* static */ bool ScriptObjectType::BuildObject(ObjectType object_type, SQInteger view, TileIndex tile)
{
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, IsValidObjectType(object_type));
EnforcePrecondition(false, view >= 0 && view < GetViews(object_type));
EnforcePrecondition(false, ScriptMap::IsValidTile(tile));

View File

@ -385,6 +385,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
/* static */ bool ScriptOrder::SetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to) && jump_to != ORDER_CURRENT);
@ -395,6 +396,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
/* static */ bool ScriptOrder::SetOrderCondition(VehicleID vehicle_id, OrderPosition order_position, OrderCondition condition)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
EnforcePrecondition(false, condition >= OC_LOAD_PERCENTAGE && condition <= OC_REMAINING_LIFETIME);
@ -405,6 +407,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
/* static */ bool ScriptOrder::SetOrderCompareFunction(VehicleID vehicle_id, OrderPosition order_position, CompareFunction compare)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
EnforcePrecondition(false, compare >= CF_EQUALS && compare <= CF_IS_FALSE);
@ -415,6 +418,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
/* static */ bool ScriptOrder::SetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position, SQInteger value)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
EnforcePrecondition(false, order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position));
EnforcePrecondition(false, value >= 0 && value < 2048);
@ -426,6 +430,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
/* static */ bool ScriptOrder::SetStopLocation(VehicleID vehicle_id, OrderPosition order_position, StopLocation stop_location)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
EnforcePrecondition(false, ScriptVehicle::GetVehicleType(vehicle_id) == ScriptVehicle::VT_RAIL);
EnforcePrecondition(false, IsGotoStationOrder(vehicle_id, order_position));
@ -439,6 +444,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
/* static */ bool ScriptOrder::SetOrderRefit(VehicleID vehicle_id, OrderPosition order_position, CargoID refit_cargo)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
EnforcePrecondition(false, IsGotoStationOrder(vehicle_id, order_position) || (IsGotoDepotOrder(vehicle_id, order_position) && refit_cargo != CT_AUTO_REFIT));
EnforcePrecondition(false, ScriptCargo::IsValidCargo(refit_cargo) || refit_cargo == CT_AUTO_REFIT || refit_cargo == CT_NO_REFIT);
@ -450,6 +456,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
/* static */ bool ScriptOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, ScriptOrderFlags order_flags)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
@ -458,6 +465,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
/* static */ bool ScriptOrder::AppendConditionalOrder(VehicleID vehicle_id, OrderPosition jump_to)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to));
@ -469,6 +477,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
/* IsValidVehicleOrder is not good enough because it does not allow appending. */
if (order_position == ORDER_CURRENT) order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
EnforcePrecondition(false, order_position >= 0 && order_position <= ::Vehicle::Get(vehicle_id)->GetNumManualOrders());
EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
@ -522,6 +531,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
/* IsValidVehicleOrder is not good enough because it does not allow appending. */
if (order_position == ORDER_CURRENT) order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
EnforcePrecondition(false, order_position >= 0 && order_position <= ::Vehicle::Get(vehicle_id)->GetNumManualOrders());
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, jump_to) && jump_to != ORDER_CURRENT);
@ -536,6 +546,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
{
order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, order_position);
@ -546,6 +557,7 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
{
next_order = ScriptOrder::ResolveOrderPosition(vehicle_id, next_order);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, next_order));
int order_pos = ScriptOrderPositionToRealOrderPosition(vehicle_id, next_order);
@ -582,6 +594,7 @@ static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance)
order_position = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
if (_settings_game.order.nonstop_only && ::Vehicle::Get(vehicle_id)->IsGroundVehicle()) order_flags |= OF_NON_STOP_INTERMEDIATE;
@ -642,6 +655,7 @@ static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance)
order_position_move = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position_move);
order_position_target = ScriptOrder::ResolveOrderPosition(vehicle_id, order_position_target);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_move));
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position_target));
EnforcePrecondition(false, order_position_move != order_position_target);
@ -653,6 +667,7 @@ static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance)
/* static */ bool ScriptOrder::CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(main_vehicle_id));
@ -661,6 +676,7 @@ static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance)
/* static */ bool ScriptOrder::ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(main_vehicle_id));
@ -669,6 +685,7 @@ static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance)
/* static */ bool ScriptOrder::UnshareOrders(VehicleID vehicle_id)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, ScriptVehicle::IsPrimaryVehicle(vehicle_id));
return ScriptObject::DoCommand(0, vehicle_id | CO_UNSHARE << 30, 0, CMD_CLONE_ORDER);

View File

@ -356,6 +356,7 @@ public:
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @pre IsValidVehicleOrder(vehicle_id, jump_to).
* @pre order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position).
* @game @pre ScriptCompanyMode::IsValid().
* @return Whether the order has been/can be changed.
* @api -game
*/
@ -369,6 +370,7 @@ public:
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @pre order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position).
* @pre condition >= OC_LOAD_PERCENTAGE && condition <= OC_UNCONDITIONALLY.
* @game @pre ScriptCompanyMode::IsValid().
* @return Whether the order has been/can be changed.
* @api -game
*/
@ -382,6 +384,7 @@ public:
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @pre order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position).
* @pre compare >= CF_EQUALS && compare <= CF_IS_FALSE.
* @game @pre ScriptCompanyMode::IsValid().
* @return Whether the order has been/can be changed.
* @api -game
*/
@ -395,6 +398,7 @@ public:
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @pre order_position != ORDER_CURRENT && IsConditionalOrder(vehicle_id, order_position).
* @pre value >= 0 && value < 2048.
* @game @pre ScriptCompanyMode::IsValid().
* @return Whether the order has been/can be changed.
* @api -game
*/
@ -409,6 +413,7 @@ public:
* @pre ScriptVehicle::GetVehicleType(vehicle_id) == ScriptVehicle::VT_RAIL.
* @pre IsGotoStationOrder(vehicle_id, order_position).
* @pre stop_location >= STOPLOCATION_NEAR && stop_location <= STOPLOCATION_FAR
* @game @pre ScriptCompanyMode::IsValid().
* @return Whether the order has been/can be changed.
* @api -game
*/
@ -422,6 +427,7 @@ public:
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @pre IsGotoStationOrder(vehicle_id, order_position) || (IsGotoDepotOrder(vehicle_id, order_position) && refit_cargo != CT_AUTO_REFIT).
* @pre ScriptCargo::IsValidCargo(refit_cargo) || refit_cargo == CT_AUTO_REFIT || refit_cargo == CT_NO_REFIT
* @game @pre ScriptCompanyMode::IsValid().
* @return Whether the order has been/can be changed.
* @api -game
*/
@ -434,6 +440,7 @@ public:
* @param order_flags The flags given to the order.
* @pre ScriptVehicle::IsPrimaryVehicle(vehicle_id).
* @pre AreOrderFlagsValid(destination, order_flags).
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
* @exception ScriptOrder::ERR_ORDER_TOO_MANY
* @exception ScriptOrder::ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION
@ -448,6 +455,7 @@ public:
* @param jump_to The OrderPosition to jump to if the condition is true.
* @pre ScriptVehicle::IsPrimaryVehicle(vehicle_id).
* @pre IsValidVehicleOrder(vehicle_id, jump_to).
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
* @exception ScriptOrder::ERR_ORDER_TOO_MANY
* @return True if and only if the order was appended.
@ -464,6 +472,7 @@ public:
* @pre ScriptVehicle::IsPrimaryVehicle(vehicle_id)
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @pre AreOrderFlagsValid(destination, order_flags).
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
* @exception ScriptOrder::ERR_ORDER_TOO_MANY
* @exception ScriptOrder::ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION
@ -480,6 +489,7 @@ public:
* @pre ScriptVehicle::IsPrimaryVehicle(vehicle_id).
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @pre IsValidVehicleOrder(vehicle_id, jump_to).
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
* @exception ScriptOrder::ERR_ORDER_TOO_MANY
* @return True if and only if the order was inserted.
@ -492,6 +502,7 @@ public:
* @param vehicle_id The vehicle to remove the order from.
* @param order_position The order to remove from the order list.
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
* @return True if and only if the order was removed.
* @api -game
@ -512,6 +523,7 @@ public:
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @pre AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_position), order_flags).
* @pre (order_flags & OF_GOTO_NEAREST_DEPOT) == (GetOrderFlags(vehicle_id, order_position) & OF_GOTO_NEAREST_DEPOT).
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
* @return True if and only if the order was changed.
* @api -game
@ -526,6 +538,7 @@ public:
* @pre IsValidVehicleOrder(vehicle_id, order_position_move).
* @pre IsValidVehicleOrder(vehicle_id, order_position_target).
* @pre order_position_move != order_position_target.
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
* @return True if and only if the order was moved.
* @note If the order is moved to a lower place (e.g. from 7 to 2)
@ -541,6 +554,7 @@ public:
* @param vehicle_id The vehicle that should skip some orders.
* @param next_order The order the vehicle should skip to.
* @pre IsValidVehicleOrder(vehicle_id, next_order).
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
* @return True if and only the current order was changed.
* @api -game
@ -554,6 +568,7 @@ public:
* @param main_vehicle_id The vehicle to copy the orders from.
* @pre ScriptVehicle::IsPrimaryVehicle(vehicle_id).
* @pre ScriptVehicle::IsPrimaryVehicle(main_vehicle_id).
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
* @exception ScriptOrder::ERR_ORDER_TOO_MANY
* @exception ScriptOrder::ERR_ORDER_AIRCRAFT_NOT_ENOUGH_RANGE
@ -569,6 +584,7 @@ public:
* @param main_vehicle_id The vehicle to share the orders with.
* @pre ScriptVehicle::IsPrimaryVehicle(vehicle_id).
* @pre ScriptVehicle::IsPrimaryVehicle(main_vehicle_id).
* @game @pre ScriptCompanyMode::IsValid().
* @exception ScriptError::ERR_OWNED_BY_ANOTHER_COMPANY
* @exception ScriptOrder::ERR_ORDER_AIRCRAFT_NOT_ENOUGH_RANGE
* @return True if and only if the sharing succeeded.
@ -581,6 +597,7 @@ public:
* After unsharing orders, the orders list of the vehicle is empty.
* @param vehicle_id The vehicle to remove from the shared order list.
* @pre ScriptVehicle::IsPrimaryVehicle(vehicle_id).
* @game @pre ScriptCompanyMode::IsValid().
* @return True if and only if the unsharing succeeded.
* @api -game
*/

View File

@ -66,6 +66,7 @@
/* static */ bool ScriptRail::IsRailTypeAvailable(RailType rail_type)
{
EnforceDeityOrCompanyModeValid(false);
if ((::RailType)rail_type >= RAILTYPE_END) return false;
return ScriptCompanyMode::IsDeity() || ::HasRailtypeAvail(ScriptObject::GetCompany(), (::RailType)rail_type);

View File

@ -9,13 +9,14 @@
#include "../../stdafx.h"
#include "script_railtypelist.hpp"
#include "script_companymode.hpp"
#include "script_error.hpp"
#include "../../rail.h"
#include "../../safeguards.h"
ScriptRailTypeList::ScriptRailTypeList()
{
EnforceDeityOrCompanyModeValid_Void();
for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
if (ScriptCompanyMode::IsDeity() || ::HasRailtypeAvail(ScriptObject::GetCompany(), rt)) this->AddItem(rt);
}

View File

@ -63,6 +63,7 @@
/* static */ bool ScriptRoad::IsRoadTypeAvailable(RoadType road_type)
{
EnforceDeityOrCompanyModeValid(false);
return (::RoadType)road_type < ROADTYPE_END && ::HasRoadTypeAvail(ScriptObject::GetCompany(), (::RoadType)road_type) && !HasBit(GetRoadTypeInfo((::RoadType)road_type)->extra_flags, RXTF_NOT_AVAILABLE_AI_GS);
}
@ -502,6 +503,7 @@ static bool NeighbourHasReachableRoad(::RoadType rt, TileIndex start_tile, DiagD
/* static */ bool ScriptRoad::_BuildRoadInternal(TileIndex start, TileIndex end, bool one_way, bool full)
{
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, start != end);
EnforcePrecondition(false, ::IsValidTile(start));
EnforcePrecondition(false, ::IsValidTile(end));

View File

@ -15,6 +15,7 @@
ScriptRoadTypeList::ScriptRoadTypeList(ScriptRoad::RoadTramTypes rtts)
{
EnforceDeityOrCompanyModeValid_Void();
for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
if (!HasBit(rtts, GetRoadTramType(rt))) continue;
if ((ScriptCompanyMode::IsDeity() || ::HasRoadTypeAvail(ScriptObject::GetCompany(), rt)) &&

View File

@ -20,6 +20,7 @@
/* static */ bool ScriptSign::IsValidSign(SignID sign_id)
{
EnforceDeityOrCompanyModeValid(false);
const Sign *si = ::Sign::GetIfValid(sign_id);
return si != nullptr && (si->owner == ScriptObject::GetCompany() || si->owner == OWNER_DEITY);
}
@ -35,6 +36,7 @@
{
CCountedPtr<Text> counter(name);
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, IsValidSign(sign_id));
EnforcePrecondition(false, name != nullptr);
const std::string &text = name->GetDecodedText();
@ -62,6 +64,7 @@
/* static */ bool ScriptSign::RemoveSign(SignID sign_id)
{
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, IsValidSign(sign_id));
return ScriptObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, "");
}
@ -70,6 +73,7 @@
{
CCountedPtr<Text> counter(name);
EnforceDeityOrCompanyModeValid(INVALID_SIGN);
EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));
EnforcePrecondition(INVALID_SIGN, name != nullptr);
const std::string &text = name->GetDecodedText();

View File

@ -20,6 +20,7 @@
/* static */ bool ScriptStation::IsValidStation(StationID station_id)
{
EnforceDeityOrCompanyModeValid(false);
const Station *st = ::Station::GetIfValid(station_id);
return st != nullptr && (st->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity() || st->owner == OWNER_NONE);
}
@ -238,6 +239,7 @@ template<bool Tfrom, bool Tvia>
/* static */ bool ScriptStation::OpenCloseAirport(StationID station_id)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidStation(station_id));
EnforcePrecondition(false, HasStationType(station_id, STATION_AIRPORT));

View File

@ -287,6 +287,7 @@ public:
/**
* Toggle the open/closed state of an airport.
* @param station_id The airport to modify.
* @game @pre ScriptCompanyMode::IsValid().
* @pre IsValidStation(station_id).
* @pre HasStationType(station_id, STATION_AIRPORT).
* @return True if the state was toggled successfully.

View File

@ -18,6 +18,7 @@
ScriptStationList::ScriptStationList(ScriptStation::StationType station_type)
{
EnforceDeityOrCompanyModeValid_Void();
for (Station *st : Station::Iterate()) {
if ((st->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity()) && (st->facilities & station_type) != 0) this->AddItem(st->index);
}

View File

@ -32,6 +32,7 @@
/* static */ bool ScriptSubsidy::Create(CargoID cargo_type, SubsidyParticipantType from_type, SQInteger from_id, SubsidyParticipantType to_type, SQInteger to_id)
{
EnforceDeityMode(false);
EnforcePrecondition(false, ScriptCargo::IsValidCargo(cargo_type));
EnforcePrecondition(false, from_type == SPT_INDUSTRY || from_type == SPT_TOWN);
EnforcePrecondition(false, to_type == SPT_INDUSTRY || to_type == SPT_TOWN);

View File

@ -55,6 +55,7 @@ public:
* @param to_type The type of the subsidy on the 'to' side.
* @param to_id The ID of the 'to' side.
* @return True if the action succeeded.
* @pre ScriptCompanyMode::IsDeity().
* @pre ScriptCargo::IsValidCargo(cargo_type)
* @pre from_type == SPT_INDUSTRY || from_type == SPT_TOWN.
* @pre to_type == SPT_INDUSTRY || to_type == SPT_TOWN.

View File

@ -22,6 +22,7 @@
/* static */ bool ScriptTile::IsBuildable(TileIndex tile)
{
EnforceDeityOrCompanyModeValid(false);
if (!::IsValidTile(tile)) return false;
switch (::GetTileType(tile)) {
@ -274,6 +275,7 @@
/* static */ bool ScriptTile::DemolishTile(TileIndex tile)
{
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, ::IsValidTile(tile));
return ScriptObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);

View File

@ -44,6 +44,7 @@
{
CCountedPtr<Text> counter(name);
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidTown(town_id));
std::string text;
if (name != nullptr) {
@ -58,6 +59,7 @@
{
CCountedPtr<Text> counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidTown(town_id));
return ScriptObject::DoCommand(::Town::Get(town_id)->xy, town_id, 0, CMD_TOWN_SET_TEXT, text != nullptr ? text->GetEncodedText().c_str() : "");
@ -125,6 +127,7 @@
/* static */ bool ScriptTown::SetCargoGoal(TownID town_id, ScriptCargo::TownEffect towneffect_id, SQInteger goal)
{
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidTown(town_id));
EnforcePrecondition(false, ScriptCargo::IsValidTownEffect(towneffect_id));
@ -155,6 +158,7 @@
/* static */ bool ScriptTown::SetGrowthRate(TownID town_id, SQInteger days_between_town_growth)
{
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidTown(town_id));
uint16 growth_rate;
switch (days_between_town_growth) {
@ -283,6 +287,7 @@
CCountedPtr<Text> counter(name);
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, ScriptCompanyMode::IsDeity() || _settings_game.economy.found_town != TF_FORBIDDEN);
EnforcePrecondition(false, ::IsValidTile(tile));
EnforcePrecondition(false, size == TOWN_SIZE_SMALL || size == TOWN_SIZE_MEDIUM || size == TOWN_SIZE_LARGE)

View File

@ -149,6 +149,7 @@ public:
* @param town_id The town to rename
* @param name The new name of the town. If null, or an empty string, is passed, the town name will be reset to the default name.
* @pre IsValidTown(town_id).
* @pre ScriptCompanyMode::IsDeity().
* @return True if the action succeeded.
* @api -ai
*/
@ -159,6 +160,7 @@ public:
* @param town_id The town to set the custom text of.
* @param text The text to set it to (can be either a raw string, or a ScriptText object). If null, or an empty string, is passed, the text will be removed.
* @pre IsValidTown(town_id).
* @pre ScriptCompanyMode::IsDeity().
* @return True if the action succeeded.
* @api -ai
*/
@ -236,6 +238,7 @@ public:
* The value will be clamped to 0 .. MAX(uint32).
* @pre IsValidTown(town_id).
* @pre ScriptCargo::IsValidTownEffect(towneffect_id).
* @pre ScriptCompanyMode::IsDeity().
* @return True if the action succeeded.
* @api -ai
*/
@ -392,6 +395,7 @@ public:
* The value will be clamped to 0 .. MAX(uint32).
* @pre IsValidTown(town_id).
* @pre houses > 0.
* @pre ScriptCompanyMode::IsDeity().
* @return True if the action succeeded.
* @api -ai
*/

View File

@ -79,6 +79,7 @@ static void _DoCommandReturnBuildTunnel1(class ScriptInstance *instance)
/* static */ bool ScriptTunnel::BuildTunnel(ScriptVehicle::VehicleType vehicle_type, TileIndex start)
{
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, ::IsValidTile(start));
EnforcePrecondition(false, vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_ROAD);
EnforcePrecondition(false, vehicle_type != ScriptVehicle::VT_RAIL || ScriptRail::IsRailTypeAvailable(ScriptRail::GetCurrentRailType()));
@ -105,6 +106,8 @@ static void _DoCommandReturnBuildTunnel1(class ScriptInstance *instance)
/* static */ bool ScriptTunnel::_BuildTunnelRoad1()
{
EnforceDeityOrCompanyModeValid(false);
/* Build the piece of road on the 'start' side of the tunnel */
TileIndex end = ScriptObject::GetCallbackVariable(0);
TileIndex start = ScriptTunnel::GetOtherTunnelEnd(end);
@ -117,6 +120,8 @@ static void _DoCommandReturnBuildTunnel1(class ScriptInstance *instance)
/* static */ bool ScriptTunnel::_BuildTunnelRoad2()
{
EnforceDeityOrCompanyModeValid(false);
/* Build the piece of road on the 'end' side of the tunnel */
TileIndex end = ScriptObject::GetCallbackVariable(0);
TileIndex start = ScriptTunnel::GetOtherTunnelEnd(end);

View File

@ -27,6 +27,7 @@
/* static */ bool ScriptVehicle::IsValidVehicle(VehicleID vehicle_id)
{
EnforceDeityOrCompanyModeValid(false);
const Vehicle *v = ::Vehicle::GetIfValid(vehicle_id);
return v != nullptr && (v->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity()) && (v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && ::Train::From(v)->IsFreeWagon()));
}

View File

@ -20,6 +20,7 @@
ScriptVehicleList::ScriptVehicleList()
{
EnforceDeityOrCompanyModeValid_Void();
for (const Vehicle *v : Vehicle::Iterate()) {
if ((v->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity()) && (v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && ::Train::From(v)->IsFreeWagon()))) this->AddItem(v->index);
}
@ -27,6 +28,7 @@ ScriptVehicleList::ScriptVehicleList()
ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id)
{
EnforceDeityOrCompanyModeValid_Void();
if (!ScriptBaseStation::IsValidBaseStation(station_id)) return;
for (const Vehicle *v : Vehicle::Iterate()) {
@ -43,6 +45,7 @@ ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id)
ScriptVehicleList_Depot::ScriptVehicleList_Depot(TileIndex tile)
{
EnforceDeityOrCompanyModeValid_Void();
if (!ScriptMap::IsValidTile(tile)) return;
DestinationID dest;
@ -100,6 +103,7 @@ ScriptVehicleList_SharedOrders::ScriptVehicleList_SharedOrders(VehicleID vehicle
ScriptVehicleList_Group::ScriptVehicleList_Group(GroupID group_id)
{
EnforceDeityOrCompanyModeValid_Void();
if (!ScriptGroup::IsValidGroup((ScriptGroup::GroupID)group_id)) return;
for (const Vehicle *v : Vehicle::Iterate()) {
@ -111,6 +115,7 @@ ScriptVehicleList_Group::ScriptVehicleList_Group(GroupID group_id)
ScriptVehicleList_DefaultGroup::ScriptVehicleList_DefaultGroup(ScriptVehicle::VehicleType vehicle_type)
{
EnforceDeityOrCompanyModeValid_Void();
if (vehicle_type < ScriptVehicle::VT_RAIL || vehicle_type > ScriptVehicle::VT_AIR) return;
for (const Vehicle *v : Vehicle::Iterate()) {

View File

@ -17,6 +17,7 @@
/* static */ bool ScriptWaypoint::IsValidWaypoint(StationID waypoint_id)
{
EnforceDeityOrCompanyModeValid(false);
const Waypoint *wp = ::Waypoint::GetIfValid(waypoint_id);
return wp != nullptr && (wp->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity() || wp->owner == OWNER_NONE);
}

View File

@ -17,6 +17,7 @@
ScriptWaypointList::ScriptWaypointList(ScriptWaypoint::WaypointType waypoint_type)
{
EnforceDeityOrCompanyModeValid_Void();
for (const Waypoint *wp : Waypoint::Iterate()) {
if ((wp->facilities & waypoint_type) &&
(wp->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity() || wp->owner == OWNER_NONE)) this->AddItem(wp->index);

View File

@ -875,9 +875,7 @@ struct ScriptDebugWindow : public Window {
}
if (this->autoscroll) {
int scroll_pos = std::max(0, log->used - this->vscroll->GetCapacity());
if (scroll_pos != this->vscroll->GetPosition()) {
this->vscroll->SetPosition(scroll_pos);
if (this->vscroll->SetPosition(scroll_pos)) {
/* We need a repaint */
this->SetWidgetDirty(WID_SCRD_SCROLLBAR);
this->SetWidgetDirty(WID_SCRD_LOG_PANEL);

View File

@ -772,10 +772,8 @@ public:
*/
bool SetPosition(int position)
{
assert(position >= 0);
assert(this->count <= this->cap ? (position == 0) : (position + this->cap <= this->count));
uint16 old_pos = this->pos;
this->pos = position;
this->pos = Clamp(position, 0, std::max(this->count - this->cap, 0));
return this->pos != old_pos;
}
@ -794,7 +792,7 @@ public:
case SS_BIG: difference *= this->cap; break;
default: break;
}
return this->SetPosition(Clamp(this->pos + difference, 0, std::max(this->count - this->cap, 0)));
return this->SetPosition(this->pos + difference);
}
/**