Page 1 of 3

AI Common Libraries - (now with AILib.Direction)

Posted: 02 Aug 2009 13:35
by fanioz
The Idea :
>> Common library that would be useful for all AI writers. This should be written by, from and for all AI writers.

Scope :
>> Every problem that can be solved using squirrel, included but not limited to Math, String, even extension to current API (tile, vehicle, cargo, industry etc..)

Contributors :
>> All AI writer

Administrator
>> Yexo, fanioz

Registered Developer
>> Zutty, Zuu

Every body could register here :D

Re: AI Common Library

Posted: 02 Aug 2009 13:37
by fanioz
Project Host :
  • Lib common : Series of library that contain assorted function that hopefully to be useful for many AI writers.
  • Lib List : An improvement of API - AIList, which can call do command inside valuator and several other functions
  • Lib Tile : Improvement on API - AITile functions
  • Lib String : Provide string handling
  • Lib Direction : Provide direction handling for AI

License : GPl 2.0

Download Links (Bananas) Documentation
Check Out here

Download Links (Non-Bananas)

Re: AI Common Library

Posted: 02 Aug 2009 13:37
by fanioz
Library Tester

It's here to test AI Common Libraries Project functions.

Make sure you have the latest Libraries in 2nd post.
Put Test.tar inside your /ai folder
Load Lib_Test.sav in OpenTTD (nightly)
Look at AI's Log and Game Map
:D

edit : note that AILib.Tile test wasn't included yet

Re: AI Common Library

Posted: 02 Aug 2009 18:46
by Yexo
fanioz wrote:pload this to Bananas)
>> mmmmm............ I think Yexo :D
If you want me to do it, that's fine, but I think it should be the general maintainer of the library (I think we should appoint someone who is responsible for merging patching and keeping the latest version), and why would that not be you, fanioz? After all, you've taken the time to start it.

Let's be very clear from the start that this library is GPL v2 (I hope you all agree :p).

I've looked through your files, and they seems to be nice.Some comments:
1. Since r17043 it's possible to subclass API classes without problems, so you don't have to wrap all AIAbstractList functions.
2. AbstractList::SetItemValue returns ar without ever defining it.
3. Did you test CountIfRemoveAboveValue/CountIfRemoveValue? I'm not sure, but I think that "local tmp = this._list;" just makes another reference to the same list, so you still remove the items from the original list. Easiest solution (untested):

Code: Select all

local tmp = AIList().AddList(this._list);

Re: AI Common Library

Posted: 02 Aug 2009 19:06
by Blustuff
Yexo wrote:I'm not sure, but I think that "local tmp = this._list;" just makes another reference to the same list, so you still remove the items from the original list.
That's absolutely right. Since copy of objects is a common operation, Squirrel use the keyword "clone" to explicitely copy an objet. Note that it only clone one level, subobjects are not cloned. Hopefully this would work :

Code: Select all

local tmp = clone this._list;
but I think it won't since there probably no _cloned metamethod for AIAbstractList which is able to clone internal representation of the list. The solution of Yexo seems to be the best.

Re: AI Common Library

Posted: 03 Aug 2009 13:02
by fanioz
Yexo wrote:If you want me to do it, that's fine, but I think it should be the general maintainer of the library (I think we should appoint someone who is responsible for merging patching and keeping the latest version), and why would that not be you, fanioz? After all, you've taken the time to start it.
This will go to vote to ... :D I believe you will be the winner. :mrgreen:
Yexo wrote: Let's be very clear from the start that this library is GPL v2 (I hope you all agree :p).
Of course yes.
Yexo wrote: I've looked through your files, and they seems to be nice.Some comments:
1. Since r17043 it's possible to subclass API classes without problems, so you don't have to wrap all AIAbstractList functions.
I didn't know that, will try it to night :)
Yexo wrote: 2. AbstractList::SetItemValue returns ar without ever defining it.
Typos .. :oops:
Yexo wrote: 3. Did you test CountIfRemoveAboveValue/CountIfRemoveValue? I'm not sure, but I think that "local tmp = this._list;" just makes another reference to the same list, so you still remove the items from the original list. Easiest solution (untested):

Code: Select all

local tmp = AIList().AddList(this._list);
Thanks for better solution :D

Re: AI Common Library

Posted: 03 Aug 2009 13:06
by Yexo
fanioz wrote:
Yexo wrote: I've looked through your files, and they seems to be nice.Some comments:
1. Since r17043 it's possible to subclass API classes without problems, so you don't have to wrap all AIAbstractList functions.
I didn't know that, will try it to night :)
r17043 was committed after you posted your library, so it wasn't possible before :p (guess what was the reason for that commit)

Re: AI Common Library

Posted: 04 Aug 2009 13:21
by fanioz
Yexo wrote: r17043 was committed after you posted your library, so it wasn't possible before :p (guess what was the reason for that commit)
However... Thanks :D (thats happen too, long time ago :D )

==========
Okay All, :D
Have no time yet to generate html documentation now, please read the code instead, or read below :D

After fixing things around, named this as RC1 version, wich is contain :

1. AILib.Common :
--function ACall() an acall replacement to allow you execute do command with array as arguments
--function GetVersion() return the table of current version of OTTD ran
--Class AIRoadTypeList() to get available Road type in game as AIList
--Class AIPlaneTypeList() to get all plane type as AIList
(the last two class above would be removed if API made them)

2. AILib.List :
--extends AIList() with several new function
--constructor argument is now optional.
--Not overriding Valuate -- not changed
--function DoValuate -- if what you want is to execute do command inside valuator. It need "this" as argument
--Integrating AITileList() methode with new implementation.
--Emulate Queue mode (Pop, Peek, Push) <- ambiguous isn't ..?

3.AILib.Tile ::
-- extends AITile. Right now only two function available
-- Tile.IsMine() to check if I am the owner
-- Tile.IsCompetitorTile() to check if my competitor owned that.

However, my english is not that good, I really need your correction for such a function names, or documentation to make this library self-descriptive.

Another question :: Which is better, Make this all as single library or separate them for each category. ?

Re: AI Common Library

Posted: 04 Aug 2009 13:30
by Yexo
I haven't looked through your new source code yet, but your overview of functions is nice.
fanioz wrote:However, my english is not that good, I really need your correction for such a function names, or documentation to make this library self-descriptive.
All function names look ok.
Another question :: Which is better, Make this all as single library or separate them for each category. ?
I'm in favor of making a different library for each class you have above.

Re: AI Common Library

Posted: 04 Aug 2009 13:51
by AndersI
fanioz wrote:-- Tile.IsMine() to check if I am the owner
My first thought was that this would check if there was a mining industry on the tile. I'm playing too much Civilization (and clones), probably :-)
-- Tile.IsCompetitorTile() to check if my competitor owned that.
To be consistent, I think the names should be:

- Tile.IsMyTile() and Tile.IsCompetitorTile()

or

- Tile.IsMine() and Tile.IsCompetitors()

Re: AI Common Library

Posted: 04 Aug 2009 13:54
by Yexo
AndersI wrote:To be consistent, I think the names should be:

- Tile.IsMyTile() and Tile.IsCompetitorTile()
or
- Tile.IsMine() and Tile.IsCompetitors()
I prefer the first option (IsMyTile/IsCompetitorTile)

Re: AI Common Library

Posted: 05 Aug 2009 09:20
by fanioz
Yexo wrote:
AndersI wrote:To be consistent, I think the names should be:

- Tile.IsMyTile() and Tile.IsCompetitorTile()
or
- Tile.IsMine() and Tile.IsCompetitors()
I prefer the first option (IsMyTile/IsCompetitorTile)
Change in AILib.Tile


changed function name :
Tile.IsMine() => Tile.IsMyTile();

Added code : From this thread

Code: Select all

/**
 * Gets the TileIndex relatively from given offset. 
 * @param tile Start tile index
 * @param x The X coordinate.
 * @param y	The Y coordinate.
 * @return Tile index
 */ 
function Tile::AddOffset(tile, x, y)
{
	/* pre-condition */
	if (!AIMap.IsValidTile(tile)) return -1;
	local tx = AIMap.GetTileX(tile) + x;
	local ty = AIMap.GetTileY(tile) + y;
	if (tx < 1) return -1;
	if (ty < 1) return -1;
	if (tx > (AIMap.GetMapSizeX() - 2)) return -1;
	if (ty > (AIMap.GetMapSizeY() - 2)) return -1;
	/* ==== */
	return AIMap.GetTileIndex(tx, ty);
}

=====
Please look at the precondition, is the behaviour of AddOffset() function is ok?
also there is no INVALID_TILE enumeration, so I've decided to use -1 instead. Which is should be an Invalid Tile :D

Re: AI Common Library

Posted: 05 Aug 2009 09:49
by Zutty
This is quite a good idea. Are any of the functions in here of use to you?...

http://code.google.com/p/ottd-noai-path ... common.nut

Re: AI Common Library

Posted: 05 Aug 2009 09:56
by fanioz
Zutty wrote:This is quite a good idea. Are any of the functions in here of use to you?...

http://code.google.com/p/ottd-noai-path ... common.nut
Wow.......... :o very much functions there, i think I should divide them for each category. Thanks :mrgreen:

Re: AI Common Library

Posted: 06 Aug 2009 17:38
by svetovoi
The idea of such library is really great, it'll be very useful.
After reading this topic, I think there is thing in my AI, that could be in library (i.e. if it were in some library, I would not write it, and would be happy).
For own AI(yeah, I'm writting one) I've made a small event system, and today rewrited it to a library form.
Hope you find it useful.

Re: AI Common Library

Posted: 07 Aug 2009 02:17
by fanioz
ac84 wrote:The idea of such library is really great, it'll be very useful.
After reading this topic, I think there is thing in my AI, that could be in library (i.e. if it were in some library, I would not write it, and would be happy).
For own AI(yeah, I'm writting one) I've made a small event system, and today rewrited it to a library form.
Hope you find it useful.
:D thanks for contributing. It seems interesting, although I've only look at example

Re: AI Common Library

Posted: 07 Aug 2009 16:31
by Bilbo
Yexo wrote:Let's be very clear from the start that this library is GPL v2 (I hope you all agree :p).
Errr .... could it be GPL v2+? GPL v3 / GPL v3+ is gaining popularity, so unless someone is opposed to GPL v3, maybe we should stick to v2+ to allow combinging with GPL v3 code.

Re: AI Common Library

Posted: 07 Aug 2009 17:25
by SirkoZ
Really nice work, fanioz.
Vote for GPL: I say GPL v2. Why would anyone even want v3?

Re: AI Common Library

Posted: 07 Aug 2009 18:22
by Rubidium
Yexo wrote:Let's be very clear from the start that this library is GPL v2 (I hope you all agree :p).
I disagree with making it GPLv2; GPLv2 would mean that linking to it makes your AI GPLv2 too. I'd go for LGPLv2, which is the generally accepted 'GPLv2' for libraries.
Bilbo wrote:GPL v2+?
I dislike that; if Mr. Stallman writes e.g. GPL v4 that gives someone all rights to do with the stuff what you want to do, like claim it as your own, your library would be rereleasable under those rules. Not sure where you want that.

Re: AI Common Library

Posted: 07 Aug 2009 22:14
by Bilbo
And what about GPL v2 or v3 (without possibility of 4+)? The problem with GPL2 and GPL3 is that while they are essentially very similar, they are incompatible. And many new projects choose GPL3 or GPL3+ to start. So combining GPL2 and GPL3 code is impossible. LGPL2 would solve that too, though :)