Workers & Resources: Soviet Republic

Workers & Resources: Soviet Republic

Μη επαρκής αριθμός βαθμολογιών
Pyrois scripting guide
Από ChalwaBechatex
Guide to script scenarios and modifing game properties in limited scope.
   
Βραβείο
Αγαπημένο
Αγαπημένο
Μη αγαπημένο
Introduction
Pyrois script programming guide
Introduction
Pyrois is a custom interpreted scripting language made by Michal Kuchárik (Tau). Used as scripting language for games like: https://gtm.you1.cn/storesteam/app/692170/Virtual[/i
Guide structure
This guide is made of 3 modules:
- syntax - explaining features of language
- API - describing W&R:SR API for scripting
- modding guide - details of developing and deploying script to game workshop

This guide doesn't contain explanation of basic programming concepts. You can look for tutorial for example for C language, it's syntax is the most similar to this script. https://www.w3schools.com/c/index.php

Current sources
- Archived documentation page[web.archive.org]
- Robots[/i]_Robotprogrammingsimulator/]Author's game using this scripts
Scripts in workshop
- citizen's workshop
- chalwa github[github.com]
- robs github[github.com]

Thanks to robs for review and examples.
Syntax
Comments
Comments may break lines of code between `)` of instruction and `{` of body. Try using comments in line above instructions like 'if' or 'while'.
- '//' - comment line

Operators
- `:` - declaring type of argument in function
- `=` - assign. Can be used to implicitly cast eg. float to int
- `+` - add
- `-` - subtract
- `*` - multiply
- `/` - divide
- `%` - modulo
- `^` - power
- `&` - logic AND
- `|` - logic OR
- `!` - logic NOT
- `>` - greater
- `<` - less
- `?` - equal
- '()' - bracket for math operation priority
- '[]' - value at index of array
- '{}' - inside of code structure
- `,` - arguments separator
- `;` - end of line
- `.` - struct value/function getter or float type decimal part separator

Examples:
- `if(_vehicleType ? VEHICLETYPE_SHIP)` - if vehicleType is ship
- `if(!(_buildingType ? BUILDINGTYPE_UNIVERSITY))` - if buildingType is NOT a university
- `result = (2 + 3) * 10;` - result will be 50
- `result = 31 % 5;` - result will be 1
- `percent = (29.0/100.0);` - result will be about 0.29

Delimiters
- '""' - string
- '''' - char

Definitions
All definitions are global in scope of running script.
Before mentioning some entity in code it must be declared in lines before (up).
Parameter names in functions are colliding with names of variables do not use same names.

- 'include' - use all entities declared in this file
- 'include("include/SOVIETInstructions.txt");'

- 'defineVariable'
- `defineVariable(Building, _building);`
- `defineVariable(int, _vehicleIndex);`

- 'defineArray'
- definition examples:
- `defineArray(Plane[20], _planes);`
- `defineArray(int[64], _numbers);`
- invocation examples:
- `_currentPlane = _planes;`

- 'defineInstruction' - functions working as indirect reference to code in game (API exposed by devs). They are not returning any value but they can pass value as reference arguments.
- definition examples:
- `defineInstruction(ScreenMark_ClearAll, 1015);` - arguments: name, ID
- `defineInstruction(Window_OpenBuildingWindow, 1007, int);` - arguments: name, ID, argument 1 type
- `defineInstruction(Objectives_CreateNewString, 1020, string, string);` - arguments: name, ID, argument 1 type, argument 2 type
- invocation examples:
- 'ScreenMark_ClearAll();'
- 'WindowOpenBuildingWindow(selectedBuilding);'
- `Objectives_CreateNewString("Test window", "your test text");`

- 'defineFunction' - subroutines or functions returning value. First argument is a Name of function, second returned type, then invocation arguments names prefixed with `type :`. Note that arguments names are in fact declared variables (global!) and they are colliding variables namespace. Recursion is not working.
- definition examples:
```
defineFunction(ResourcesGetFromEventType, float, int: typeOfEvent)
{
if(typeOfEvent ? 0)
{
_returnValue = _resources.eletronics;
}
elseif(typeOfEvent ? 1)
{
_returnValue = _resources.mcomponents;
}
//...
return(_returnValue);
}

defineFunction(InitScriptConstants, void)
{
UNKNOWN = 0;
HEADING_BORDER = 1;
HEADING_AIRPORT = 2;
//...
returnVoid();
}
```
- invocation examples:
- `_resources = ResourcesGetFromEventType(_eventTypeId);`
- 'InitScriptConstants()'

- 'defineStruct' - user defined type - basically container for data and related instructions. Fixed ID is a problematic "feature" of this language. I'm concerned if it will cause conficts between working scripts or updates of API.
- definition examples:
```
defineStruct(Resources, 104)
{
defineStructVariable(Resources, float, workers);
defineStructVariable(Resources, float, eletric);
defineStructVariable(Resources, float, vehicles);
//...
defineStructInstruction(Resources, GetFromBuilding, 50000, int);
};
defineStruct(StatRecord, 105)
{
//...
defineStructArray(StatRecord, float[16], Tourism_SpendUSD);
defineStructInstruction(StatRecord, GetPresent, 60000);
defineStructInstruction(StatRecord, GetFromPresentToDate_DMY, 60001, int, int, int);
};
```
- invocation examples:
```
resources.GetFromBuilding(buildingID);
_workersInBuidling = _resources.workers;

_record.GetPresent();
_touristDollars = record.TourismSpendUSD[0];
```

- 'defineStructVariable' - same as defineStruct but inside struct. Arguments: struct name, type, name. Examples above.
- 'defineStructArray' - same as defineArray but inside struct. Arguments: struct name, array type, name. Examples above.
- 'defineStructInstruction' - same as defineInstruction but inside struct. Arguments: struct name, name, ID, arg1 type, arg2 type... . Examples above.

Statements
- 'if' - logic decision statement can be used with more than one 'elseif' and 'else' block at the end. You can use logic operators and bracket to chain logic expressions or use nested 'if' statements.
- example:
```
if(_random < 2)
{
_multiply = 1.0;
_maxDeliveryTime = 10.0;
}
elseif(_random < 4)
{
_multiply = 1.5;
_maxDeliveryTime = 10.0;
}
elseif(_random < 7)
{
_multiply = 2.0;
_maxDeliveryTime = 12.0;
}
else()
{
_multiply = 4.0;
_maxDeliveryTime = 18.0;
}
```
- 'elseif'
- 'else'
- 'for' - loop executing specified number of iterations
- example:
```
for (i = 0, i < width, i = i + 1)
{
for (j = 0, j < length, j = j + 1)
{
ar.x = i;
ar.y = j;
}
}
```
- 'while' - loop executing as long as logic value is true
- example:
```
while(1 ? 1)
{
// this will run forever
}
```
- 'return' - mandatory for declarations of functions returning any non void type. Must return type expected in function declaration.
- Declaration example:
```
defineFunction(Multiply, float, float: a, float: b)
{
return(result);
}
```
- 'returnVoid' - mandatory for void functions declarations.
- Declaration example:
```
defineFunction(InitScriptConstants, void)
{
MINDAYSNEXT_EVENT = 30;
START_YEAR = 1960;
returnVoid();
}
```
- 'main' - keyword marks entry point to script. You must declare only one function with this name per script.
- example:
```
defineFunction(main, void)
{
// Do something
end();
}
```
- 'end' - exit point of script. Must be used at the end of main function declaration.

types
- 'void' - used for declaring function with no type returned
- 'char' - short integer can be used with '' to get ASCII code of alphanumeric symbol. Used in game API as boolean where 0 is False and 1 is True.
- 'int' - integer
- 'float' - floating point number
- 'vec2' - struct with float: x,y members
- 'vec3' - struct with float: x,y,z members
- 'vec4' - struct with float: x,y,z,m members
- 'mat2x2' - struct with vec2: r0,r1 members
- 'mat3x3' - struct with vec3: r0,r1,r2 members
- 'mat4x4' - struct with vec4: r0,r1,r2,r3 members
- 'string' - this is constant literal cannot be edited
Creating script mod
Creating script mod
Creating workshop item
- Open game
- Workshop
- Your items
- (+) create new item
- select type: script
- select .png thumbnail image, name and description .txt file

Mod sctructure
- Find your mod in C:\Program Files (x86)\Steam\steamapps\common\SovietRepublic\mediasoviet\workshopwip
- Paste "C:\Program Files (x86)\Steam\steamapps\common\SovietRepublic\media_soviet\scripts\SOVIETInstructions.txt" file to your mod folder\include
- Add script files with .txt extension to main folder. Eg: "myScript.txt", "myScriptCancel.txt". All script files must contain 'main' function and reference sovietInstructions with:
include("include/SOVIETInstructions.txt");
- Optionally for each script add thumbnail image with same name as script: "myScript.png", "myScriptCancel.png"
- in workshopconfig.ini reference all scripts:
$OBJECT_SCRIPT myScript "My script in game tooltip description" $OBJECT_SCRIPT myScriptCancel "My script cancel in game tooltip description"
- Change visibility of workshop item
- Save changes

Tips
Notepad++ highlights
- Download notepad++[notepad-plus-plus.org]
- Tab syntax/user defined/define language.../import
- select "C:\Program Files (x86)\Steam\steamapps\common\SovietRepublic\media_soviet\scripts\notepad++pyrois.xml" or
- "C:\Program Files (x86)\Steam\steamapps\common\SovietRepublic\mediasoviet\scripts\notepad++pyroisblack.xml"
- tab syntax/Pyrois
- Enjoy colored syntax of your script
Definitions files
You can move definitions of functions, structs and variables to different files to make them more readable. Keep in mind all files use same namespace so don't overwrite existing names.
Reference your file with:
include("include/declarations.txt");
Game constants.
To use game constants included in SOVIETInstructions.txt you must explicitiely call function
InitConstants();
Due to fact that this language doesn't support C-like constans, all of them are variables that need setup.
User defined structs
When declaring custom struct you must declare struct index. Indexes from 100 to 105 are used but you can use indexes higher, starting from 106.
Debugging
For this moment only method for debugging is adding Window_ShowText. You can sreamline your work a bit using fact that this language is interpreted so you don't have to close game. Just save .txt file and run script in game again.
Licence
To avoid any frictions in community please add to your code permissive licence like MIT. Just copy this file and add to mod files https://github.com/chalwachalwa/Aerial-delivery---pyrois-script/blob/master/license.txt . Don't be cosmonaut ;P
API
This section was too long to post on steam guides, please check github version https://github.com/chalwachalwa/Aerial-delivery---pyrois-script/wiki/API
1 σχόλια
jockeril 24 Μαϊ 2023, 2:34 
Great guide with good explanation - love it ! thanks mate