The International Simutrans Forum

Development => Patches & Projects => Incorporated Patches and Solved Bug Reports => Topic started by: victor_18993 on August 13, 2026, 08:20:15 AM

Title: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 13, 2026, 08:20:15 AM
Simutrans already exposes a substantial part of the engine through the Squirrel Script API, but while working with it I have found some places where information already available in the C++ engine is either lost or only partially exposed to scripts.
Rather than redesigning the Script API, I would like to use this thread for small, incremental and backwards-compatible improvements where such gaps can be clearly demonstrated.
First change:
way_planner_x.get_step_cost()way_builder_t::is_allowed_step() already does two things internally:
However, the existing
way_planner_x.is_allowed_step() binding only exposes the boolean result and discards the calculated weight.
As a result,
sqai and
sqai_rail currently have to estimate these weights again in
astar.nut.
This patch adds:
way_planner_x.get_step_cost(from, to)It returns:
This is a pathfinding weight, not a construction price.
The change is completely additive.
is_allowed_step() is unchanged, existing scripts do not need any modification, and this patch does not modify
sqai,
sqai_rail or
astar.nut.
Five new
way_planner_x tests have been added, covering:
Test results:
Patch attached:
squirrel-api-01-step-cost.diffI would like to keep any further changes in this area similarly small and independent, so each improvement can be reviewed on its own merits.
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 13, 2026, 10:15:33 AM
The descriptor initialization fix is now in trunk as r12144.
way_builder_t now initializes
desc,
bridge_desc and
tunnel_desc to
NULL, so an unconfigured
way_planner_x now behaves deterministically instead of depending on uninitialized memory.
For an unconfigured planner:
Configured builders are unchanged.
A regression test was added for this case.
Final validation:
This fix is now complete in trunk.
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 13, 2026, 11:23:47 AM
The tram/elevated planner fix is now in trunk as r12145.
way_planner_x.set_build_types() now preserves the same tram and elevated way-type semantics as the normal way tool:
This does not change the Script API signature, and existing road and rail scripts are unaffected.
Final validation:
This change is now complete in trunk.
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 13, 2026, 05:16:05 PM
The tunnel planner addition is now in trunk as r12146.
A new
tunnel_planner_x.find_end() method is now available to Squirrel scripts. It exposes the existing native tunnel endpoint search without building anything.
This is an additive Script API change. It does not modify the existing tunnel tool or the AI implementations.
Final validation:
This gives scripts a native way to query a tunnel endpoint before deciding whether to build it.
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 14, 2026, 02:08:23 AM
API-04A is now in trunk as r12149.
command_x.build_bridge_at() now accepts an optional
max_length parameter:

command_x.build_bridge_at(pl, pos, bridge_desc [, max_length])

The existing 3-argument behaviour is unchanged and still defaults to a maximum search length of 10.
The Script API search is capped at 254 tiles because the native bridge endpoint search uses an 8-bit counter internally.
Final validation:
This is an additive API change. Existing scripts using the 3-argument form retain the previous behaviour.
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 14, 2026, 11:50:28 AM
EXEC-05 has now been integrated in r12150.
command_x.build_way() and
command_x.build_road() now accept an optional trailing
terraform argument:


command_x.build_way(pl, start, end, way, straight [, terraform])
command_x.build_road(pl, start, end, way, straight, keep_city_roads [, terraform])



The default remains
false, so existing scripts keep exactly the previous behaviour.
When
terraform=true, the command may use the native way-builder slope adjustment while constructing the way. This does not enable automatic bridges or tunnels.
The change also preserves the terraform policy through network tool execution, so the same command semantics are used on the authoritative side in multiplayer.
Regression coverage was added for the legacy/default behaviour, slope crossing,
build_road, adjacent-step construction, scenario restrictions, no automatic bridge construction, and tool-state reuse.
Full automated suite after the change: 264/264 passed.
This completes the executor side of the terraforming work. The next step will be the corresponding
way_planner_x change, so scripts can plan and execute using the same policy.
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 14, 2026, 05:44:58 PM
API-05 planner terraforming has now been integrated in r12151.
way_planner_x.set_build_types() now accepts an optional trailing
terraform parameter:


set_build_types(way [, terraform])



The default remains
false, so existing scripts keep the previous behaviour unchanged.
When enabled, the planner considers the same slope-changing capability exposed to
command_x.build_way() /
build_road() in r12150. This means scripts and AIs can now plan across certain slopes that the native way builder can handle through terraforming, instead of discovering that capability only when attempting construction.
The existing way-type composition is preserved, including tram and elevated ways, and enabling terraforming does not enable bridges or tunnels.
The planner remains a planning/query interface: an accepted step is not a guarantee that a later construction command will succeed, since the executor may still reject the operation for other reasons.
Regression coverage was added for:
The complete automated suite passes 271/271.
This closes the planner/executor terraforming work. The next Script API work will focus on auditing how failures and error results are currently exposed to scripts before proposing any further API changes.
Title: Re: Modernisation of the Squirrel Script API
Post by: Andarix on August 14, 2026, 06:35:53 PM
I believe the function 'command_x::build_bridge_at() (https://doc.simutrans-germany.com/Simutrans-Squirrel-API/classcommand__x.html#a74043c2a6801a099ba185fc6d5365471)' is no longer usable in its current form, since Simutrans no longer builds bridges with just a single click.

Are you also incorporating your changes into the Doxygen documentation?
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 14, 2026, 07:19:10 PM
Yes, I understand what you mean now.

build_bridge_at() still works by taking a starting tile, finding a suitable end and then building the bridge, but your point is whether that is still the right API for a Script AI now that bridge construction is naturally defined by two endpoints.

We already have bridge_planner_x.find_end() and command_x.build_bridge(start, end, ...), so I will review this part specifically and check which interface should be considered the proper one for scripts rather than assuming that the old one-click abstraction is still the best choice.

And yes, I am updating the Doxygen documentation together with the API changes. I want the final API behaviour and its documentation to stay in sync.
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 14, 2026, 08:04:31 PM
A small update on the Script API work.
Two more changes have now been integrated into trunk:
I am now looking at the next part from the point of view of actual Script AI usage, especially the terrain/route-building problems mentioned by Andarix.
The terraform support added in r12150/r12151 gives scripts the ability to ask the planner to consider slope changes and to let the executor perform them, but the current Script AIs do not use those new arguments yet.
One interesting finding from the current audit is that the C++
way_builder_t already has a fairly clean separation between calculating a route, calculating its monetary cost and actually building it. The native AI already uses this to compare alternatives before changing the map.
Script AI, however, currently has much less information available before construction. It can ask whether individual steps are possible, but it cannot yet inspect a complete engine-calculated route together with its real construction cost in the same way.
I am still investigating this before proposing any new API. I want to distinguish carefully between limitations of the Script API and things that are simply AI strategy problems, rather than adding functions that may not actually be needed.
I am also checking the existing automatic bridge/tunnel behaviour of the way tool, since there may already be useful engine functionality available through a less obvious path.
As before, the idea is to keep the changes small and incremental: first understand and reproduce the limitation, then decide whether an API change is actually justified.
Title: Re: Modernisation of the Squirrel Script API
Post by: Andarix on August 15, 2026, 01:29:40 PM
Quote from: victor_18993 on August 14, 2026, 08:04:31 PM... already has a fairly clean separation between calculating a route, calculating its monetary cost and actually building it. The native AI already uses this to compare alternatives before changing the map.
Script AI, however, currently has much less information available before construction. It can ask whether individual steps are possible, but it cannot yet inspect a complete engine-calculated route together with its real construction cost in the same way.
I am still investigating this before proposing any new API. I want to distinguish carefully between limitations of the Script API and things that are simply AI strategy problems, rather than adding functions that may not actually be needed.
I am also checking the existing automatic bridge/tunnel behaviour of the way tool, since there may already be useful engine functionality available through a less obvious path.
As before, the idea is to keep the changes small and incremental: first understand and reproduce the limitation, then decide whether an API change is actually justified.

sqai_rail already determines construction costs with a high degree of accuracy.

Check for 'build_cost' in the industry connection planner. I've linked two places here.

 industry_connection_planner.nut#L356 (https://github.com/Andarix/simutrans-scenarios/blob/164ee81f3868d9647252e703fa4378838362a7e9/ai/sqai/industry_connection_planner.nut#L356)
 industry_connection_planner.nut#L480 (https://github.com/Andarix/simutrans-scenarios/blob/164ee81f3868d9647252e703fa4378838362a7e9/ai/sqai/industry_connection_planner.nut#L480)

Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 15, 2026, 02:53:28 PM
Thanks, that is very useful.
I had not yet followed
build_cost through the industrial connection planner in enough detail. Looking at it now,
sqai_rail is already doing a much more complete pre-build estimate than I had assumed: route length, bridge cost, tree removal, stations, depot and an estimate for terraforming are all taken into account before construction.
So I think the question is narrower than "the Script AI cannot estimate construction cost".
What I still want to determine is whether this estimate is close enough to the result produced by the engine during the real build, or whether there are specific cases where the AI has to duplicate or approximate internal builder logic and starts to diverge.
I will compare the planned
build_cost against the actual construction cost in a few controlled cases — flat terrain, slopes, trees, bridges and combinations of them.
If the existing calculation is already sufficiently accurate, then there may be no reason to add a new API at all. If there is a systematic difference, we will at least have a concrete case showing exactly what information is missing.
Thanks for pointing me to those two places in the planner.
Title: Re: Modernisation of the Squirrel Script API
Post by: Andarix on August 15, 2026, 03:29:36 PM
For many functions, the option to display notifications is available at the beginning.

industry_connection_planner.nut#L44 (https://github.com/Andarix/simutrans-scenarios/blob/164ee81f3868d9647252e703fa4378838362a7e9/ai/sqai/industry_connection_planner.nut#L44)

The current method for calculating construction and maintenance costs is actually quite good. This is evident from the fact that sqai_rail relatively rarely goes bankrupt (pak64, pak64.german).

It runs into difficulties when routes are too long and profits are too low such as in pak128, where industrial profits are very low.

Whether a route is built depends on more factors than just construction costs.

And once the route is known, construction costs can be determined based on the object properties (which, in my view, are complete here).

However, the credit refunds vary; in some cases, a full credit is returned, while in others, one-hundredth of a credit is returned.
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 15, 2026, 05:31:42 PM
Squirrel Script API modernisation — current roadmap
A short roadmap update, now that the preparation work has progressed enough to give a clearer picture of the next steps.
The dates below are target windows, not fixed deadlines. If a regression, compatibility issue or unclear contract appears, I would rather stop and investigate it than force the migration to fit a date.
15–16 August — bundled Script AI fixes
We are currently closing two pre-existing consumer-side issues found while establishing the pre-3.2 baseline:
These are being handled as independent fixes, before the runtime migration, so they do not become mixed with Squirrel 3.2 compatibility work.
16–18 August — pre-migration tooling and regression baseline
The next target is to finish and integrate the preparation work that will be used to validate the runtime update:
The goal is to have these changes reviewable independently from the actual Squirrel update.
18–20 August — final pre-3.2 consumer baseline
sqai and
sqai_rail are being used as real consumers of the Script API in addition to the normal automated tests.
The remaining baseline work is focused on behaviour such as:
Once this is complete, we will freeze a final pre-3.2 reference consisting of API signatures, VM regressions and real consumer behaviour.
Around 20–24 August — Squirrel 3.2 migration
If the pre-migration gates are clean, the next major step will be the actual update from the current Squirrel 3.1.1-based snapshot to Squirrel 3.2.
This will not be a blind source replacement. Simutrans has several local VM customisations that need to be reconciled deliberately, including:
After the import, the same pre-3.2 API, VM and consumer baselines will be rerun against 3.2.
Late August — stabilisation and follow-up
Any issue found during the migration will be classified separately as:
Only the changes required for a safe migration will be included in this phase. Broader cleanup — for example further standardisation between
sqai and
sqai_rail — should remain a separate follow-up project rather than expanding the scope of the runtime update.
So the current sequence is essentially:
consumer fixes → preparation/tooling → final pre-3.2 baseline → Squirrel 3.2 → stabilisationIf everything continues to test cleanly, the aim is to have the Squirrel 3.2 work in active integration during the second half of August, but the validation gates remain more important than the dates.
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 15, 2026, 10:21:39 PM
Squirrel 3.2 migration — progress and updated roadmap
The pre-3.2 preparation has progressed faster than expected, so I can bring the roadmap forward a little.
The preparation stack is now integrated in trunk as r12158–r12162. This includes the generated API baseline, the refreshed Squirrel modification inventory, the repaired update workflow, the small upstream error-message correction, and the VM regression tests.
The final pre-migration baseline is also frozen: the Script API, customised VM behaviour, and the real
sqai /
sqai_rail consumers now have independent reference points that can be compared against the 3.2 result.
The actual Squirrel 3.2 runtime migration has now started in a separate development branch, based on r12162. It is not being developed directly in trunk, so intermediate migration errors or incomplete states should not affect the official builds while this work is in progress.
The upstream delta turned out to be relatively small and well bounded. The main areas requiring careful reconciliation are the
sq_getinstanceup API change, the new
bindenv compiler/VM behaviour, a string hashing change, and a few remaining library/runtime changes. Several Simutrans-specific VM areas we were concerned about are untouched by upstream 3.2, which has reduced the expected migration work.
Updated tentative roadmap
These are still target windows rather than promises. If a migration cut exposes a real semantic regression, I will prefer to stop and investigate it rather than keep the date.
So far, however, the work has been less complicated than the original conservative schedule assumed, and the Squirrel 3.2 migration is now actively underway.
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 16, 2026, 04:12:44 PM
Squirrel 3.2 has now been integrated into the official Simutrans trunk.
The runtime migration was committed in three independently tested revisions, followed by a separate documentation commit:
Post-integration validation was performed from a fresh checkout of r12168.
Results:
One behavioral point is now explicitly documented: scripts should not rely on Squirrel table iteration order unless that order is explicitly guaranteed by the API.
With this, the Squirrel 3.2 migration is complete and the post-integration validation is green.
Title: Re: Modernisation of the Squirrel Script API
Post by: prissi on August 17, 2026, 10:10:22 AM
Thank you very much for the hard work.
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 17, 2026, 11:00:52 AM
Thank you, Prissi.
I'm glad the migration is now fully integrated and validated.
I'll keep the follow-up work around the Script API and AI tooling separate, so this migration can remain closed and stable.
Title: Re: Modernisation of the Squirrel Script API
Post by: Yona-TYT on August 17, 2026, 02:02:21 PM
Thank you very much, Victor, you are doing an excellent job 🫡
Title: Re: Modernisation of the Squirrel Script API
Post by: victor_18993 on August 17, 2026, 02:55:28 PM
Thank you very much, Yona! 😊

You and Andarix have both been a huge help throughout this migration, and I really appreciate the support and feedback you have given me.

In many ways, though, the hardest and most interesting part starts now. My next goal is to work closely with both of you to identify which engine capabilities script authors actually need, expose them properly through the Script API, and strengthen this whole side of Simutrans.

Without people like you actively creating scenarios, AI and scripted content, it would be impossible to know what the API really needs. So this next stage is something I very much see as a joint effort.