Page 1 of 1

cEngineLib library to handle engines

Posted: 24 Jul 2013 19:22
by krinn
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

Re: cEngineLib library to handle engines

Posted: 09 Aug 2013 21:07
by krinn
I have upload version 5 of the library that should be (except bugfixes) the final version, also add some more functions.

Re: cEngineLib library to handle engines

Posted: 22 Aug 2013 09:29
by fanioz
hi..
seems like a liltle bug here

Re: cEngineLib library to handle engines

Posted: 22 Aug 2013 09:55
by krinn
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.

Re: cEngineLib library to handle engines

Posted: 28 Oct 2013 16:59
by R2dical
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).

Re: cEngineLib library to handle engines

Posted: 30 Oct 2013 13:35
by krinn
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.