Page 3 of 9

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 05 Nov 2011 20:19
by Zuu
Update - Version 14
  • Fix possible hang up in Tile::GetBridgeAboveStart (Kogut)
  • New Money functions from Kogut/Brumi
  • Fix: Road.BuildMagicDTRSInTown did only return the correct depot location for stop_length = 2 (Runner11)
Thank you Kogut and Runner11 for your provided bug reports and help with the changes included in this version.

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 06 Nov 2011 10:03
by Kogut
Kogut wrote: Inflate function may be used in function _SuperLib_Road::ConvertRailCrossingToBridge

Code: Select all

	/* Now we know it is possible to bridge/tunnel the rail from tile_before to tile_after */
	
	// Make sure we can afford the construction
	AICompany.SetLoanAmount(AICompany.GetMaxLoanAmount());
	if (AICompany.GetBankBalance(AICompany.COMPANY_SELF) < 20000) //<--Here

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 06 Nov 2011 10:17
by Kogut
YA bug

Code: Select all

function _SuperLib_Money::Inflate(money)
{
	return money * GetInflationRate() / 100;
}
should be replaced with

Code: Select all

function _SuperLib_Money::Inflate(money)
{
	return money * _SuperLib_Money.GetInflationRate() / 100;
}

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 06 Nov 2011 14:34
by Zuu
Update - version 15

Changes:
  • Use Money.Inflate in Road.ConvertRailCrossingToBridge (Kogut)
  • Fix bug in Money.Inflate (Kogut)

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 01 Dec 2011 21:29
by Zuu
Update - version 16 + NoGo edition

Changes
  • Removed all usages of HasNext etc. and replaced with foreach loops.
  • Simplified Airport::GetAirportTile a bit (Yexo)
SuperLib-v16.tar
Standard AI edition
(254.5 KiB) Downloaded 143 times
NoGo/GameScript edition
None of these changes are really large. However, the first one was necessary to get the GameScirpt version of SuperLib to work. For simplicity I now decided to keep both editions at the same version. Later the GameScript edition might get its own separate version number if there is a general interest for it.

The NoGo edition is basically just a copy of SuperLib where AI in AIMap, AILog etc. has been renamed to GSMap, GSLog etc. Thus some parts might not work correctly for Game Scripts. In that case it is valuable that people report back this so that the functions can be fixed or removed from the Game Script port of SuperLib. For this reason the GameScript edition is currently limited to these sub-libraries:
  • Direction
  • Helper
  • Industry
  • Log
  • Scorelist
  • Tile
  • Town
If anyone want to try more sub libraries they can grab the NoGo translation script and modify the white list so that it includes more sub libraries. If you find out anything useful by doing this, please post back.

For now I keep the NoGo edition attached here, but ideally it should probably get a home of its own. Below you also find a demo that demonstrates how to include SuperLib in a GameScript.
NoGoSuperLib-v16.zip
GameScript edition. Unzip and place the folder in your GameScript folder. Mind that your GameScript will need to use the GPL2 license as you are inlining GPL2 code in your script.
(21.26 KiB) Downloaded 149 times
NoGoSuperLibDemo.zip
Requires a NoGo OpenTTD binary. Includes NoGoSuperLib-v16. Unpack and place in the "game" folder.
(22.55 KiB) Downloaded 156 times

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 06 Dec 2011 09:45
by Kogut
It may be nice to add GetMailCargo function, clone of the GetPAXCargo

helper.nut
new function declaration:

Code: Select all

	/* Get the cargo ID of the mail cargo */
	static function GetMailCargo();
new function

Code: Select all

function _SuperLib_Helper::GetMailCargo()
{
	if(!AICargo.IsValidCargo(_SuperLib_Helper_private_mail_cargo))
	{
		local cargo_list = AICargoList();
		cargo_list.Valuate(AICargo.HasCargoClass, AICargo.CC_MAIL);
		cargo_list.KeepValue(1);
		cargo_list.Valuate(AICargo.GetTownEffect);
		cargo_list.KeepValue(AICargo.TE_MAIL);

		if(cargo_list.Count() > 1) // In theory it is possible to create newgrf with multiple mail cargos
		{
			// Check which mail cargo that has biggest availability in the biggest town
			local town_list = AITownList();
			town_list.Valuate(AITown.GetPopulation);
			town_list.KeepTop(1);

			local top_town = town_list.Begin();
			local town_tile = AITown.GetLocation(top_town);
			if(AITown.IsValidTown(top_town))
			{
				foreach(cargo_id, _ in cargo_list)
				{
					local radius = 5;
					local acceptance = AITile.GetCargoAcceptance(town_tile, cargo_id, 1, 1, radius);

					cargo_list.SetValue(cargo_id, acceptance);
				}

				// Keep the most accepted mail cargo
				cargo_list.Sort(AIAbstractList.SORT_BY_VALUE, AIAbstractList.SORT_DESCENDING);
				cargo_list.KeepTop(1);
			}
		}

		if(!AICargo.IsValidCargo(cargo_list.Begin()))
		{
			_SuperLib_Log.Error("Mail cargo do not exist", _SuperLib_Log.LVL_INFO);
			return -1;
		}

		// Remember the cargo id of mail
		_SuperLib_Helper_private_mail_cargo = cargo_list.Begin();
		return cargo_list.Begin();
	}

	return _SuperLib_Helper_private_mail_cargo;
}
Additional private variable (after "_SuperLib_Helper_private_pax_cargo <- -1;")

Code: Select all

_SuperLib_Helper_private_mail_cargo <- -1;
There is significant code duplication but creating function to check which cargo has biggest availability in the biggest town may be overkill.

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 10 Dec 2011 18:30
by Zuu
Update - v17

This update moves from OpenTTD API version 1.0 to 1.1. This has been necessary in order to allow AIs that use this library to declare to OpenTTD that they require 1.1. Because when an AI declare that it needs 1.1, it also loses eg. AIAbstractList which does not only affect the AI but also all libraries that it uses.

For a full list of API changes from 1.0 to 1.1 see the documentation: http://noai.openttd.org/docs/1.1.3/ai__ ... _8hpp.html

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 13 Dec 2011 01:55
by teshiron
In _SuperLib_Road::ConvertRailCrossingToBridge(), could you please change it not to get a max loan every time? Or at least use _SuperLib_Money::RestoreLoan() afterward? This is in road.nut, line 1254.

My AI makes loan payments in small amounts over time, and if it constantly gets reset to max loan each time I replace a level crossing, it'll never get paid off.

Thanks!

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 13 Dec 2011 08:16
by Zuu
Sounds like a valid request. I will take a look at it when I have time to do that.

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 17 Dec 2011 20:12
by MinchinWeb
Hey Zuu,

I'm working on releasing a GameScript version of WmDOT (my AI) that uses some of the functions of your SuperLib. Would you be able to release a "proper" library version, now that the NoGo branch supports libraries? I think the only thing currently missing is the "library.nut" file.

As well, in the first post, you have a link to a SVN server, but it returns a 404 error.

Thanks for your coding work!

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 17 Dec 2011 20:51
by planetmaker
MinchinWeb wrote:As well, in the first post, you have a link to a SVN server, but it returns a 404 error.
The NoAI projects were migrated to the DevZone in order to only need maintain one 3rd-party project site: http://dev.openttdcoop.org/projects/superlib

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 18 Dec 2011 13:27
by Zuu
Update - version 18

Changelog
  • Road.ConvertRailCrossingToBridge does now restore the loan before it returns
  • Road.ConvertRailCrossingToBridge does now only take a loan if bank balance is less than twice the maximum loan
  • Include library.nut in the NoGo edition
  • Nogo edition is now offered as .tar so that you don't need to unpack it to use it. Just place it in your game/library directory and use the exact same import syntax as for AIs.

    Code: Select all

    import("util.superlib", "SuperLib", 18);
  • (Automatically) include *.txt in the AI edition
SuperLib-v18.tar
AI edition
(260 KiB) Downloaded 135 times
SuperLib-nogo-v18.tar
Game Script edition
(82 KiB) Downloaded 139 times

Answers to previous questions
MinchinWeb wrote:I'm working on releasing a GameScript version of WmDOT (my AI) that uses some of the functions of your SuperLib. Would you be able to release a "proper" library version, now that the NoGo branch supports libraries? I think the only thing currently missing is the "library.nut" file.
Yes it's possible - see above :-)
MinchinWeb wrote:As well, in the first post, you have a link to a SVN server, but it returns a 404 error.
I've updated the link in the first post to point to the URL that planetmaker pointed out. Thanks for noticing me about this problem.
teshiron wrote:In _SuperLib_Road::ConvertRailCrossingToBridge(), could you please change it not to get a max loan every time? Or at least use _SuperLib_Money::RestoreLoan() afterward? This is in road.nut, line 1254.

My AI makes loan payments in small amounts over time, and if it constantly gets reset to max loan each time I replace a level crossing, it'll never get paid off.

Thanks!
I hope that the two changes I made are good enough. It will restore the loan afterwards, and if you have plenty of money it will not even take a loan.

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 18 Dec 2011 14:28
by Kogut
Kogut wrote:It may be nice to add GetMailCargo function, clone of the GetPAXCargo

-clicky snip-

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 18 Dec 2011 15:23
by Zuu
Kogut wrote:
Kogut wrote:It may be nice to add GetMailCargo function, clone of the GetPAXCargo

-clicky snip-
Sorry, I missed that. Thanks for reminding me.

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 18 Dec 2011 15:57
by Zuu
Update - version 19

Changelog
  • Add: Helper.GetMailCargo() (thanks Kogut)
SuperLib-v19.tar
(260 KiB) Downloaded 146 times
SuperLib-nogo-v19.tar
(83 KiB) Downloaded 142 times

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 20 Dec 2011 23:23
by Zuu
Version 19 for NoGo/GameScript has now been uploaded to bananas. Because I selected the wrong license at first, I had to make 19.1. In your code you still import version 19. The .1 is just displayed in bananas.

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 21 Dec 2011 07:17
by peter1138
Zuu wrote:Mind that your GameScript will need to use the GPL2 license as you are inlining GPL2 code in your script.
This is only relevant if you wish to distribute your script.

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 21 Dec 2011 07:43
by planetmaker
Quite seriously, Zuu could just release his library under the agpl :-)

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 24 Dec 2011 01:52
by 11Runner
The Helper.SetSign() method does not return the SignID of the created sign. Could that be implemented?

EDIT:

I apologize, I did not look at your post on the CityDomination thread closely enough. I still think returning the SignID might be something to look into...

Re: SuperLib: Helper, Direction, Tile, ... libraries

Posted: 24 Dec 2011 10:46
by Zuu
Since the function is a void now, it shouldn't break anything if it is changed to return the sign id on success or -1 if no sign is placed.