Skip to content

Commit

Permalink
Change: apparently working detection of FIRS economy using industry f…
Browse files Browse the repository at this point in the history
…ingerprinting
  • Loading branch information
andythenorth committed Aug 31, 2021
1 parent ab15ba6 commit f5fcf32
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/gs/constants.pynut
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GIT_VERSION <- "${git_info.get_version()}";
20 changes: 20 additions & 0 deletions src/gs/firs.pynut
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
firs <- {};

firs.industries <- {};

<tal:industries repeat="industry registered_industries">
local industry = {};
industry.numeric_id <- ${industry.numeric_id};
industry.economy_variations <- {}
<tal:economies repeat="economy registered_economies">
industry.economy_variations[${economy.numeric_id}] <- {};
industry.economy_variations[${economy.numeric_id}].accept_cargo_types <- [${gs_helper.get_accept_cargos_as_array_contents(industry, economy)}];
</tal:economies>
firs["industries"]["${industry.id}"] <- industry;
</tal:industries>

firs.economy_fingerprints <- {}
<tal:economies repeat="economy registered_economies">
firs.economy_fingerprints[${economy.numeric_id}] <- "${gs_helper.get_economy_fingerprint(registered_industries, economy)}";
</tal:economies>

2 changes: 1 addition & 1 deletion src/gs/lang/english.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
STR_WELCOME :Hello {COMPANY}!{}{}You are playing a game with Minimal GS - a boilerplate for GS authors.
STR_WELCOME :Hello {COMPANY}!{}{}You are playing a game with FIRS test GS{}{}Git version of this GS is {NUM}
70 changes: 62 additions & 8 deletions src/gs/main.pynut
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file is part of MinimalGS, which is a GameScript for OpenTTD
* Copyright (C) 2012-2013 Leif Linse
*
* MinimalGS is free software; you can redistribute it and/or modify it
* MinimalGS is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
Expand All @@ -13,7 +13,7 @@
*
* You should have received a copy of the GNU General Public License
* along with MinimalGS; If not, see <http://www.gnu.org/licenses/> or
* write to the Free Software Foundation, Inc., 51 Franklin Street,
* write to the Free Software Foundation, Inc., 51 Franklin Street,
* Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
Expand Down Expand Up @@ -42,11 +42,13 @@ Story <- SuperLib.Story;

/** Import other source code files **/
require("version.nut"); // get SELF_VERSION
require("constants.nut");
require("firs.nut");
//require("some_file.nut");
//..


class MainClass extends GSController
class MainClass extends GSController
{
_loaded_data = null;
_loaded_from_version = null;
Expand Down Expand Up @@ -79,8 +81,8 @@ function MainClass::Start()
{
// Some OpenTTD versions are affected by a bug where all API methods
// that create things in the game world during world generation will
// return object id 0 even if the created object has a different ID.
// In that case, the easiest workaround is to delay Init until the
// return object id 0 even if the created object has a different ID.
// In that case, the easiest workaround is to delay Init until the
// game has started.
if (Helper.HasWorldGenBug()) GSController.Sleep(1);

Expand All @@ -90,6 +92,58 @@ function MainClass::Start()
// execute our GS further in world generation)
GSController.Sleep(1);

Log.Info(GIT_VERSION, Log.LVL_INFO);

/*
foreach(industry_id, industry_properties in firs.industries) {
Log.Info(industry_id, Log.LVL_INFO);
foreach(key, value in industry_properties) {
Log.Info(key, Log.LVL_INFO);
Log.Info(value, Log.LVL_INFO);
}
foreach(economy in industry_properties.economy_variations) {
foreach(cargo_label in economy.accept_cargo_types) {
Log.Info(cargo_label, Log.LVL_INFO);
}
}
Log.Info("--", Log.LVL_INFO);
}
*/
local industry_types_list = GSIndustryTypeList();
local industry_type_ids = [];
local economy_matched = false;
foreach (industry_type, _ in industry_types_list) {
if (economy_matched == true) {
break;
}
local industry_fingerprint = "Accepts:"
local accepted_cargo_as_labels = [];
foreach (cargo, _ in GSIndustryType.GetAcceptedCargo(industry_type)) {
accepted_cargo_as_labels.push(GSCargo.GetCargoLabel(cargo));
}
accepted_cargo_as_labels.sort();
foreach (cargo in accepted_cargo_as_labels) {
industry_fingerprint = industry_fingerprint + " " + cargo;
}
industry_fingerprint = industry_fingerprint + " Produces:";
local produced_cargo_as_labels = [];
foreach (cargo, _ in GSIndustryType.GetProducedCargo(industry_type)) {
produced_cargo_as_labels.push(GSCargo.GetCargoLabel(cargo));
}
produced_cargo_as_labels.sort();
foreach (cargo in produced_cargo_as_labels) {
industry_fingerprint = industry_fingerprint + " " + cargo;
}
foreach (economy, economy_fingerprint in firs.economy_fingerprints) {
if (economy_fingerprint == industry_fingerprint) {
Log.Info("matched!", Log.LVL_INFO);
Log.Info(economy, Log.LVL_INFO);
economy_matched = true;
break;
}
}
}

// Game has now started and if it is a single player game,
// company 0 exist and is the human company.

Expand All @@ -114,7 +168,7 @@ function MainClass::Start()
}
}
last_loop_date = current_date;

// Loop with a frequency of five days
local ticks_used = GSController.GetTick() - loop_start_tick;
GSController.Sleep(max(1, 5 * 74 - ticks_used));
Expand Down Expand Up @@ -159,7 +213,7 @@ function MainClass::HandleEvents()
local company_id = company_event.GetCompanyID();

// Here you can welcome the new company
Story.ShowMessage(company_id, GSText(GSText.STR_WELCOME, company_id));
Story.ShowMessage(company_id, GSText(GSText.STR_WELCOME, company_id, GIT_VERSION));
break;
}

Expand Down Expand Up @@ -198,7 +252,7 @@ function MainClass::Save()
return this._loaded_data != null ? this._loaded_data : {};
}

return {
return {
some_data = null,
//some_other_data = this._some_variable,
};
Expand Down
54 changes: 43 additions & 11 deletions src/render_gs.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,57 @@
import firs
import utils
from polar_fox import git_info
import codecs # used for writing files - more unicode friendly than standard open() module

import sys
import shutil
import os

currentdir = os.curdir
from time import time

import sys

sys.path.append(os.path.join("src")) # add to the module search path
import firs
import utils
from polar_fox import git_info

import codecs # used for writing files - more unicode friendly than standard open() module
registered_cargos = firs.registered_cargos
registered_industries = firs.registered_industries
registered_economies = firs.registered_economies

from chameleon import PageTemplateLoader # chameleon used in most template cases

# setup the places we look for templates
nut_templates = PageTemplateLoader(
os.path.join(currentdir, "src", "gs"), format="text"
)
nut_templates = PageTemplateLoader(os.path.join(currentdir, "src", "gs"), format="text")

# get args passed by makefile
makefile_args = utils.get_makefile_args(sys)

gs_src = os.path.join(currentdir, "src", "gs")
gs_dst = os.path.join(firs.generated_files_path, "gs")


class GSHelper(object):
# helps keep templating a bit less mad, and avoids add squirrel specific things to industry.py where I don't want them (as of August 2021 - might change later)
def get_accept_cargos_as_array_contents(self, industry, economy):
result = []
if industry.get_property("accept_cargo_types", economy) is not None:
for cargo in industry.get_accept_cargo_types(economy):
result.append('"' + cargo + '"')
return ",".join(result)

def get_economy_fingerprint(self, registered_industries, economy):
result = ""
# as of August 2021, port and wharf were sufficiently unique, and at least one of them is in every economy
# !! this could use a guard to enforce uniqueness
for industry in registered_industries:
if industry.id in ['port', 'wharf']:
if industry.economy_variations[economy.id].enabled:
fingerprint_industry = industry
break;
result = result + "Accepts: " + " ".join(sorted(fingerprint_industry.get_accept_cargo_types(economy)))
result = result + " Produces:"
for cargo_label, prod_multiplier in sorted(fingerprint_industry.get_prod_cargo_types(economy)):
result = result + " " + cargo_label
return result


def main():
start = time()
print("[RENDER GS] render_gs.py")
Expand All @@ -45,6 +70,8 @@ def main():

nuts = [
# alphabetise for simplicity
"constants",
"firs",
"info",
"main",
"version",
Expand All @@ -53,7 +80,12 @@ def main():
nut_template = nut_templates[nut_name + ".pynut"]
dst_file = codecs.open(os.path.join(gs_dst, nut_name + ".nut"), "w", "utf8")
result = nut_template(
makefile_args=makefile_args, git_info=git_info
gs_helper=GSHelper(),
makefile_args=makefile_args,
git_info=git_info,
registered_industries=registered_industries,
registered_cargos=registered_cargos,
registered_economies=registered_economies,
)
dst_file.write(result)
dst_file.close()
Expand Down

0 comments on commit f5fcf32

Please sign in to comment.