[GS] FGCargoMonitor

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
idioty
Engineer
Engineer
Posts: 58
Joined: 21 Jun 2010 11:54

[GS] FGCargoMonitor

Post by idioty » 02 Nov 2013 18:27

I created a library to help monitoring the cargo changes: FGCargoMonitor
Minimum system: OpenTTD 1.3.0


Version 2 changes:
- minor bug fixes

Vesrion 3 changes:
- minor bug fixes
- the log automatically display the game date now
- with DisableGameDateFromLog() you can set to back the original log format.

Vesrion 4 changes:
- I fixed a bug: after bankrupt a company, an other company is removed from monitoring.


Links, dokcumetntations:
Full documentation: http://feca.homedns.org/documentations/ ... index.html
Full example: http://feca.homedns.org/documentations/ ... ample.html
Available on bananas: http://www.openttd.org/en/bananas/gslibrary/


Details:
With this library you can easily create own Cargo Goal type game in 5 steps:
- Create instance:

Code: Select all

this.cargoMonitor = FGCargoMonitor(this, FGCargoMonitor.LOGLEVEL_ALL);
- Update every day:

Code: Select all

this.cargoMonitor.Update();
- Subscribe to monitoring:

Code: Select all

this.cargoMonitor.SubscribeToMonitor(company_id, cargo_id, goal_id, FGCargoMonitor.REFRESH_IMMEDIATELY, FGCargoMonitor.MONITOR_ALL_PICKUP, null);
- Create a delegate method to tracking changes:

Code: Select all

function FGCargoMonitorTest::SendCargoUpdate(company_id, goal_id, transported)
- And unsubscribe if you don’t need anymore this update:

Code: Select all

this.cargoMonitor.UnSubscribeFromMonitor(company_id, goal_id);

You can subscribe to specific transport goals, example:
- How much coal transported from this city?
- How much steel transported from this industry?
- How much passenger picked up (and delivered) from all city?, etc…

Of course you can subscribe more than one monitor type to specific cargo, example:
- How much coal transported from this coal mine and how much coal transported from all coal mine?

You can subscribe to immediately updates or you can choice the monthly, yearly, etc… updates (REFRESH_TYPES)

Many functions are available to query a specific company and cargo, or given goal of company. But you must subscribe before qurey these monitor types. Example funcitons:

Code: Select all

FGCargoMonitor::AmountOfAllPickedupCargo(company_id, cargo_id)

Code: Select all

FGCargoMonitor::AmountOfAllGoal(company_id, goal_id)
, etc…

The save and load are easily:

Code: Select all

function FGCargoMonitorTeszt::Save() {
	return {
		cargoMonitor = this.cargoMonitor.SaveData(),
	}
}
function FGCargoMonitorTeszt::Load(version, table) {
	this.cargoMonitor.LoadFromData(table.cargoMonitor);
}

You can unsubscribe the speceific company with specefic goal:
- FGCargoMonitor::UnSubscribeFromMonitor(company_id, goal_id)
Also you can unsubscribe all monitor of specific company (if the company went bankrupt or merged):
- FGCargoMonitor::UnSubscribeCompanyFromMonitor(company_id)
Or you can stop all monitoring at the end of the game:
- FGCargoMonitor::UnSubscribeAllFromMonitor()


I tested all function, but errors may occur…
Have fun! :D
Attachments
Example.zip
Example how can use
(2.52 KiB) Downloaded 155 times
Last edited by idioty on 11 Jan 2014 15:49, edited 4 times in total.
Sorry my bad english, I don't speak english!

fabca2
Transport Coordinator
Transport Coordinator
Posts: 312
Joined: 14 Apr 2004 15:18
Location: Fr

Re: [GS] FGCargoMonitor

Post by fabca2 » 07 Nov 2013 12:32

Hello,
Subscribing without any Goal isn't possible ?
for example to compute town growing based upon given cargo ? (gamescript)
Regards.

idioty
Engineer
Engineer
Posts: 58
Joined: 21 Jun 2010 11:54

Re: [GS] FGCargoMonitor

Post by idioty » 14 Dec 2013 20:54

fabca2 wrote:Hello,
Subscribing without any Goal isn't possible ?
for example to compute town growing based upon given cargo ? (gamescript)
Regards.
Sorry, I I did'nt notice your comment.
Yes, you can subscribe without valid GSGoal, but this function required unique own goal ID per company.
Example:
SubscribeToMonitor(company_id_1, cargo_id_1, "goal_id_1", FGCargoMonitor.REFRESH_IMMEDIATELY, FGCargoMonitor.MONITOR_TOWN_DELIVERY, town_id_1);
SubscribeToMonitor(company_id_1, cargo_id_2, "goal_id_2", FGCargoMonitor.REFRESH_IMMEDIATELY, FGCargoMonitor.MONITOR_TOWN_DELIVERY, town_id_1);
SubscribeToMonitor(company_id_2, cargo_id_1, "goal_id_1", FGCargoMonitor.REFRESH_IMMEDIATELY, FGCargoMonitor.MONITOR_TOWN_DELIVERY, town_id_2);
SubscribeToMonitor(company_id_2, cargo_id_2, "goal_id_2", FGCargoMonitor.REFRESH_IMMEDIATELY, FGCargoMonitor.MONITOR_TOWN_DELIVERY, town_id_2);

As you can see, the goal_id can be string (or integer).
And with this goal_id can be identify which goal (town) updated: SendCargoUpdate(company_id, goal_id, transported)
Sorry my bad english, I don't speak english!

idioty
Engineer
Engineer
Posts: 58
Joined: 21 Jun 2010 11:54

Re: [GS] FGCargoMonitor

Post by idioty » 15 Dec 2013 12:03

I created a new version (3) of this script, the details readable in first post.
Sorry my bad english, I don't speak english!

idioty
Engineer
Engineer
Posts: 58
Joined: 21 Jun 2010 11:54

Re: [GS] FGCargoMonitor

Post by idioty » 11 Jan 2014 15:50

I created a new version (4) of this script, the details readable in first post.
Sorry my bad english, I don't speak english!

Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 2 guests