<-- -->

Free Web Hosting : Free Hosting : Troubled Teens : Report Abuse

AI Lib

Forum | Index

AI Lib is the AI library some of my AIs are based on.

Download version 0.72 (Delphi 3) for Civ Evo 0.7.5 (459kb)

The archive includes some precompiled demonstration programs in the test folder. Other test projects are nonfunctional or don't have user interfaces.

0.70 is more modular - it's easy to add/remove a module.

AI Lib is still in development so it isn't easy to use yet. I will try to improve this as it develops.

You should extract with directories enabled as the archive contains subdirectories.

You should extract source archives to the same directory (which you extracted AI Lib to).

Using AI Lib

As a working AI you can specialise

This approach is suitable if you have some ideas but little (or no) code. The suggested technique is to derive a class from TBasicAI which will reprisent an instance (to control one player) of your AI. As you think of ideas you can override the methods in TBasicAI to implement them (usually you should call the inherited method unless you are completely replacing it). You can see examples of this technique in Lene AI and Banshee AI.

  1. Choose a name for your AI, say 'MyAI'.
  2. Create a directory (eg called MyAI).
  3. Load the project for Lene into Delphi and save it in MyAI as MyAI.dpr. (Delphi will convert the file paths for you.)
  4. Load the module AIunit.pas (from Lene's directory) and save it in the MyAI directory.
  5. Edit your AIunit.pas (in the MyAI directory) - change the AIName constant to 'MyAI' (this will be used for the INI file if you use one.)
  6. (Optional) copy Lene.ini to MyAI.ini.
  7. Copy LeneAI.ai.txt to MyAI.ai.txt
  8. Edit MyAI.ai.txt - Change the #NAME setting from Lene to MyAI.
  9. Change the #PATH setting from src\lene\LeneAI.dll to the path for your DLL (the same as for MyAI.dpr) eg src\MyAI\MyAI.dll.
  10. Copy Lene.bmp to MyAI.bmp - you should edit this graphic to something appropriate before you release your AI (you might be able to edit it by double-clicking the file).

You should be able to compile MyAI.dpr to MyAI.dll and select it in the Civ Evo start screen.

The AI class in AIunit.pas is called TAI. You don't have to use this name - if you want to call it TMyAI just change it in AIunit.pas and in MyAI.dpr. From Lene's and Banshee's AIunits you can see examples of overriding TBasicAI methods to specialise the behaviour of the AI. When you have an idea you should override the method from TBasicAI (look at TBasicAI in basicai.pas to see which methods are available) to implement your custom code.

As individual modules to add features to your AI

This approach makes sense if you already have a working AI and would like to add or improve a feature.

For short sections of code inside a module it is reasonable to copy-and-paste.

For larger sections of code (eg a whole module) it's desirable to use the module (rather than copy-and-pasting code) so that you don't have to update it for new versions of Civ Evo and so that you can include bugfixes and performance improvements from future module versions easily. If you think a module is too large (ie forces you to include features you don't use) let me know and I can simplify it.

In theory using a module should be as easy as reading it's interface section (to see how to use it) and adding it to your project (so the compiler can find it) and Uses clause for each module which needs that feature. (If you publish your source code you should let people know if AI Lib is required and which version.)

There are two reasons why it can be harder:

AI Lib uses a custom version of AIClasses. The key changes:
different CLocation.Distance() algorithm.
additional external server variable (ActualServer - initialise the same as Server)

This means that you should use the AI Lib version of AIClasses or make parallel changes in your version.

AI Lib modules tend to have several dependencies
(Trivial) You need to add the dependent modules to your project.
Most modules assume the features of TModularAI.

If your AI is reprisented as a class derived from TCustomAI (eg if it is based on StdAI) you should be able to derive it from TModularAI instead. TModularAI implements some debugging features (eg exception trap on DoTurn) but they shouldn't be an intolerable burden. If you want to use features in TBasicAI you derive from TBasicAI and override methods for which you already have custom code eg DoTurn.

If your AI cannot be reprisented as a class derived from TCustomAI then you have the alternatives of re-organising your AI so that it can be derived from TModularAI or re-implementing the interface of ModularAI in a custom ModularAI module.

The important features of TModularAI 0.16 are: (see ModularAI interface)
cRO - a pTribe set to the current player's RO,
CurAI - the index of the current player
ActualServer - an auxilliary Server variable - initialise the same as Server.
loading - a boolean flag to indicate if the AI is currently loading (required for persistent objects/data)
Player_InitialTurn, - before begin_turn (start and after load)
Player_BeginTurn,
Player_EndTurn,
Player_Change, - can be called during loading.
Player_Creating, - called before turns
Player_Destroying - Events (TEventGenerator - Events module).

For the events, you need to call .Signal(CurAI) when the event logically occurs (see the implementation of ModularAI). Player_Change is called after CurAI has been updated. You must maintain a distinction between Player_Creating and Player_BeginTurn etc if you use (AI Lib modules which use) persistent data (TStaticObject) or objects (TPersistentObject) - a quick way to check is to see if either (interface or implementation) uses clause includes Persistent. Also if any module you are using uses Persistent you must have added it to your project (View...Units).

Customising AI Lib modules

Ideally you can specialise modules by deriving from their classes (or using them internally via the implementation uses clause).

If you find that you need to edit an AI Lib module, I recommend that you make a copy of the module and store it in a different directory (eg the same directory as your AI). That way you can't accidentally overwrite your changes when you get a new version of AI Lib.

It's not necessary to publish changes but if you want to you can e-mail the custom module to me and I will evaluate including your changes in AI Lib. I have to make sure that modules are reasonably easy to use. You will be credited with the changes (you could add your name to the changelog at the end of the file or place a comment at the start).

There is an example module, template.pas which demonstrates how to implement various types of data. Look at small modules for simple examples.

Top