cEngineLib library to handle engines

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
krinn
Transport Coordinator
Transport Coordinator
Posts: 336
Joined: 29 Dec 2010 19:36

cEngineLib library to handle engines

Post by krinn » 24 Jul 2013 19:22

This library handle engines properties by recording them in a base and returning results from it when query.
So this remove the newGRF problems we could met because AIEngine cannot answer some queries until a vehicle was built with that engine.
It should support any newGRF, but a special handling for 2cc trainset is include as it was need.


The library offer some other helpers for engine & vehicle, mostly aim at train : function to track information, a more advance CanPullCargo function that allow you to bypass ai_special_flag setting from newGRF. An automated function to find best engine base on your own heuristic (but one is provide per default).

Everything in it is made to do its work alone, the library is able to buy and sell vehicle itself to discover new properties of a never built engine, test train locomotive compatibility with wagons, or apply a constrain : gives me the best wagon with that train or gives me the best train and wagon to carry wood...

This is a generic implementation of a class i use in DictatorAI to handle my engines, so now everyone could benefits from it too, and get more accurate information to base their heuristic to find out what could be the best engine.
The library is made so it doesn't even need to be instanciate, but you need to keep its import name, so to use it just add:

Code: Select all

import("Library.cEngineLib", "cEngineLib", 8);
The API documentation is inside the library, and some notes and examples are provided in the readme.
You can get it from your favorite bananas dealer. The source code is there : http://dev.openttdcoop.org/projects/ailib-cengine

Thank you for using it & reporting bugs.

Here's an incomplete list of functions from the API :
Engine functions:
  • * GetBestEngine : to find out the best engine.
  • * Incompatibility & blacklisting functions and their handling
  • * EngineFilter : a function to remove classic conditions out of list of engines
  • * GetLength : get the length of an engine unrefit or refit to a cargo (like AIVehicle.GetLength but works on engine)
  • * GetCapacity : same as AIEngine.GetCapacity but can report the capacity of the engine when refit to a cargo
  • * IsLocomotive: is engine a locomotive
  • * CanPullCargo : like AIEngine.CanPullCargo but can bypass the ai_special_flags from newGRF
  • * GetPrice : Get price of an engine including refit costs if need.
Vehicle functions:
  • * VehicleGetWeight : Get The total weight of a vehicle (with handling of freight_trains)
  • * VehicleGetMaxTractiveEffort : Get the total tractive effort from the vehicle
  • * VehicleGetMaxPower : Get the total power from the vehicle
  • * VehicleGetRailTypeUse : Get the railtype the vehicle is running on
  • * VehicleGetMaxSpeed : Get the max speed the vehicle could do (handling railtype limit and wagons speed limit)
  • * VehicleGetNumberOfLocomotive : Get number of locos present in the vehicle
  • * VehicleLackPower : Answer if your vehicle need more loco to carry its cargo load (handle slope_steepness, freight_train and train_acceleration_model)
  • * VehicleGetNumberOfWagons : Get number of wagons vehicle have (excluding loco engine)
  • * VehicleRestrictLength : Sell wagons from vehicle to keep it under a length limit
  • * GetMaxWagons : Get maximum number of wagons that could be use within a length limit
Misc functions:
  • * IsDepotTile : Get if the tile have any depot
  • * GetDepotType : Get the type of depot found at a tile
  • * RailTypeGetFastestType : Get the railtype that can reach the highest speed
  • * RailTypeGetSpeed : Get the speed of a railtype
Last edited by krinn on 19 Jan 2015 15:15, edited 3 times in total.

krinn
Transport Coordinator
Transport Coordinator
Posts: 336
Joined: 29 Dec 2010 19:36

Re: cEngineLib library to handle engines

Post by krinn » 09 Aug 2013 21:07

I have upload version 5 of the library that should be (except bugfixes) the final version, also add some more functions.

User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

Re: cEngineLib library to handle engines

Post by fanioz » 22 Aug 2013 09:29

hi..
seems like a liltle bug here
Attachments
Lindingstone Transport, 12th Jan 1983.png
Lindingstone Transport, 12th Jan 1983.png (23.85 KiB) Viewed 2937 times
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image

krinn
Transport Coordinator
Transport Coordinator
Posts: 336
Joined: 29 Dec 2010 19:36

Re: cEngineLib library to handle engines

Post by krinn » 22 Aug 2013 09:55

Thank you fanioz, the problem with little bug in lib, is that it makes the AI crash, so there's no tiny bug.

I've upload v6 with the fix.

R2dical
Traffic Manager
Traffic Manager
Posts: 165
Joined: 18 Mar 2013 22:22

Re: cEngineLib library to handle engines

Post by R2dical » 28 Oct 2013 16:59

Thank you for this library, I am enjoying its features :)

I found 2 bugs, functions named cEngine, not cEngineLib, on lines 772 and 938, version 6 (Same issue as previous bug I think, renaming correctly causes those functions to work).

krinn
Transport Coordinator
Transport Coordinator
Posts: 336
Joined: 29 Dec 2010 19:36

Re: cEngineLib library to handle engines

Post by krinn » 30 Oct 2013 13:35

Thank you R2dical, i have commit the fixes and update the library version in bananas.
I suppose everyone can guess what was the original name of the library in my AI, and of course i still use it as derivative name into my AI hence why i didn't catch them.

Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 3 guests