AI:AIInfo

From OpenTTD
(Difference between revisions)
Jump to: navigation, search
(Settings)
(Add information about all parameters of AddSetting)
Line 49: Line 49:
 
The new lines of course are those about Settings. In SetSetting it is important to define all properties. If not, an error will be given. Now in the GUI on your AI there will be an option to alter NameOfSetting between the values of 0 and 10, or a preset value if your have a difficulty setting different than 'Custom'.
 
The new lines of course are those about Settings. In SetSetting it is important to define all properties. If not, an error will be given. Now in the GUI on your AI there will be an option to alter NameOfSetting between the values of 0 and 10, or a preset value if your have a difficulty setting different than 'Custom'.
  
The flags parameter can be set to AICONFIG_BOOLEAN to make the setting a boolean choice.
+
Available parameters for AddSetting:
 +
{|
 +
|-
 +
! Name
 +
! Type
 +
! Required
 +
! Description
 +
|-
 +
| name
 +
| string
 +
| yes
 +
| The name of the setting which you reference in your AI in this.GetSetting(name) (member of AIController). The characters ',' and '=' are not allowed.
 +
|-
 +
| description
 +
| string
 +
| yes
 +
| A description of what the setting does. Visible for the user in the AI Settings window.
 +
|-
 +
| min_value
 +
| integer
 +
| Only if flags doesn't contain AICONFIG_RANDOM.
 +
| The minimum value of the setting
 +
|-
 +
| max_value
 +
| integer
 +
| Only if flags doesn't contain AICONFIG_RANDOM.
 +
| The maximum value of the setting.
 +
|-
 +
| easy_value
 +
| integer
 +
| yes
 +
| The default value on the easy difficulty level.
 +
|-
 +
| medium_value
 +
| integer
 +
| yes
 +
| The default value on the medium difficulty level.
 +
|-
 +
| hard_value
 +
| integer
 +
| yes
 +
| The default value on the hard difficulty level.
 +
|-
 +
| custom_value
 +
| integer
 +
| yes
 +
| The default value on the custom difficulty level.
 +
|-
 +
| random_deviation
 +
| integer
 +
| no, forbidden if flags contain AICONFIG_RANDOM
 +
| When the game is started, the value for the setting will be randomized between user_specified_value - random_deviation and user_specified_value + random_deviation.
 +
|-
 +
| step_size
 +
| integer
 +
| no
 +
| The amount the value is changed for every time the user clicks one of the buttons.
 +
|-
 +
| flags
 +
| bitset
 +
| yes
 +
| You can specify the following flags (combine via the binary or operator): AICONFIG_RANDOM and AICONFIG_BOOLEAN. See below for a description about what they do.
 +
|}
 +
 
 +
The flags parameter can be set to AICONFIG_BOOLEAN to make the setting a boolean choice. If you do so, don't specify min_value or max_value. All of easy_value, medium_value, hard_value and custom_value should be either 0 or 1 (not false/true).
 +
Flags can also be set to AICONFIG_RANDOM. If you do so and the users doesn't explicitly set the value, it'll get a random value between min_value and max_value.
 +
It's possible to combine AICONFIG_BOOLEAN and AICONFIG_RANDOM by using: flags = AICONFIG_BOOLEAN | AICONFIG_RANDOM
  
 
Now inside your AI you can request the value of this setting, by doing:
 
Now inside your AI you can request the value of this setting, by doing:

Revision as of 20:38, 5 February 2009


info.nut is a file in which you tell NoAI basic things about your AI. It used to be part of main.nut, but it has been separated to make debugging easier (see here if you're interested in details).

info.nut structure

The internal structure of info.nut is very simple and follows a template presented below.

class MyNewAI extends AIInfo {
  function GetAuthor()      { return "Newbie AI Writer"; }
  function GetName()        { return "MyNewAI"; }
  function GetDescription() { return "An example AI by following the tutorial at http://wiki.openttd.org/"; }
  function GetVersion()     { return 1; }
  function GetDate()        { return "2007-03-17"; }
  function CreateInstance() { return "MyNewAI"; }
  function CanLoadFromVersion(version) {
    return (version == 1);
  }
}

/* Tell the core we are an AI */
RegisterAI(MyNewAI());

Where of course you should change the names however you like to call everything. Just don't forget the RegisterAI(), and don't forget to extend AIInfo. If something goes wrong, check if you did: RegisterAI(MyNewAI()). Mind the () after MyNewAI!


Settings

It is possible to allow users to change settings in your AI. The only requirement is that you tell them which settings. You can do this by altering your info.nut in the following way:

class MyNewAI extends AIInfo {
  function GetAuthor()      { return "Newbie AI Writer"; }
  function GetName()        { return "MyNewAI"; }
  function GetDescription() { return "An example AI by following the tutorial at http://wiki.openttd.org/"; }
  function GetVersion()     { return 1; }
  function GetDate()        { return "2007-03-17"; }
  function CreateInstance() { return "MyNewAI"; }
  function CanLoadFromVersion(version) {
    return (version == 1);
  }
  function GetSettings() {
    AddSetting({name = "NameOfSetting", description = "Description of setting", min_value = 0, max_value = 10, easy_value = 10, medium_value = 5, hard_value = 0, custom_value = 5, flags = 0});
  }
}

/* Tell the core we are an AI */
RegisterAI(MyNewAI());

The new lines of course are those about Settings. In SetSetting it is important to define all properties. If not, an error will be given. Now in the GUI on your AI there will be an option to alter NameOfSetting between the values of 0 and 10, or a preset value if your have a difficulty setting different than 'Custom'.

Available parameters for AddSetting:

Name Type Required Description
name string yes The name of the setting which you reference in your AI in this.GetSetting(name) (member of AIController). The characters ',' and '=' are not allowed.
description string yes A description of what the setting does. Visible for the user in the AI Settings window.
min_value integer Only if flags doesn't contain AICONFIG_RANDOM. The minimum value of the setting
max_value integer Only if flags doesn't contain AICONFIG_RANDOM. The maximum value of the setting.
easy_value integer yes The default value on the easy difficulty level.
medium_value integer yes The default value on the medium difficulty level.
hard_value integer yes The default value on the hard difficulty level.
custom_value integer yes The default value on the custom difficulty level.
random_deviation integer no, forbidden if flags contain AICONFIG_RANDOM When the game is started, the value for the setting will be randomized between user_specified_value - random_deviation and user_specified_value + random_deviation.
step_size integer no The amount the value is changed for every time the user clicks one of the buttons.
flags bitset yes You can specify the following flags (combine via the binary or operator): AICONFIG_RANDOM and AICONFIG_BOOLEAN. See below for a description about what they do.

The flags parameter can be set to AICONFIG_BOOLEAN to make the setting a boolean choice. If you do so, don't specify min_value or max_value. All of easy_value, medium_value, hard_value and custom_value should be either 0 or 1 (not false/true). Flags can also be set to AICONFIG_RANDOM. If you do so and the users doesn't explicitly set the value, it'll get a random value between min_value and max_value. It's possible to combine AICONFIG_BOOLEAN and AICONFIG_RANDOM by using: flags = AICONFIG_BOOLEAN | AICONFIG_RANDOM

Now inside your AI you can request the value of this setting, by doing:

this.GetSetting("NameOfSetting");

There are a few things to remember:

  • Settings never change during runtime. You set them before you start a game, and that is final.
  • Settings are always integers.
Personal tools