Re: SuperLib: Helper, Direction, Tile, ... libraries
Posted: 22 Jul 2013 00:14
the next question is then : what 90 % 100 will gave you
The place to talk about Transport Tycoon
https://www.tt-forums.net/
% is a modulo operation ( http://en.wikipedia.org/wiki/Modulo_operation )krinn wrote:
the next question is then : what 90 % 100 will gave you
Code: Select all
(money % 100) * _SuperLib_Money.GetInflationRate();
90 % 100 => 90krinn wrote:Well, seems you need more explain so :
Say GetInflationRate = 300Code: Select all
(money % 100) * _SuperLib_Money.GetInflationRate();
Say we will use your exemple of 90 for money
So it should gave (0.9) * 300 = 270
In real it gives (0) * 300 = 0
And you propose this fix : 0 + 90 * 300 = 27000
Well, if the function is buggy and return 0 instead of 270, i don't think your fix is any better by returning 27000
Kogut wrote:I am not sure whatever it is OK that money%100 is ignored (for example Inflate(90) will always return 0).Code: Select all
return (money / 100) * _SuperLib_Money.GetInflationRate();
Maybe
?Code: Select all
return (money / 100) * _SuperLib_Money.GetInflationRate() + (money % 100) * _SuperLib_Money.GetInflationRate();
On the other hand - it is hard to imagine that such amount of money will be used in AI code.
Code: Select all
return (((money).tofloat() / 100) * _SuperLib_Money.GetInflationRate()).tointeger()
Code: Select all
return (money / 100) * _SuperLib_Money.GetInflationRate() + (money % 100) * _SuperLib_Money.GetInflationRate();
Code: Select all
return (money / 100) * _SuperLib_Money.GetInflationRate() + ((money % 100) * _SuperLib_Money.GetInflationRate())/100;
Code: Select all
return (money * 0.01 * _SuperLib_Money.GetInflationRate()).tointeger();
Code: Select all
return money * _SuperLib_Money.GetInflationRate() / 100;
Code: Select all
return (money / 100) * _SuperLib_Money.GetInflationRate() + (money % 100) * _SuperLib_Money.GetInflationRate() / 100;
Code: Select all
Fix:
- Money.Inflate was discarding amounts below 100 money units.
Feature:
- Helper.HasWorldGenBug() - Method to check if OpenTTD is affected by FS#5561 (DoCommands returning
id 0 for all objects created in world gen)
Code: Select all
//from AIAI by Kogut
//estimates how weight of vehicle will change after loading one piece of cargo_id cargo
//it is a guess, but there is no better method for predicting this value
//returns expected weight of single piece of cargo_id
//GetWeightOfOneCargoPiece(cargo_id)
//from AIAI by Kogut
//iterates over vehicles, all stopped in depots are sold
//SellAllVehiclesStoppedInDepots()
//from Rondje om de kerk
//computes square root of i using Babylonian method
//returns n that is the highest integer that is lower or equal to the square root of integer i
//Sqrt(i)
//from Rondje om the kerk
//attempt to construct HQ in the biggest city, returns true if HQ exists on exiting function, false otherwise
//if HQ exists on calling function it will not be moved and function will return true
//BuildCompanyHQ()
Code: Select all
//from AIAI by Kogut
//attempts to locate brake van - wagon appearing in some newgrfs (for example - Japanese Train Set v2.1a), required to start freight train
//search is limited to ones usable on specified railtype
//returns engine_id of the fastest one or null if none was found
//GetBrakeVan(railtype)
//from AdmiralAI by Thijs Marinussen
//constructs RailDepot and connects it to railway supplied in path parameter, may perform some landscaping to allow construction of the depot
//Returns a depot tile if successful, null otherwise
//BuildDepot(path);
Code: Select all
//from AIAI by Kogut, based on function from AdmiralAI by Yexo
//Plant trees around town town_id to improve rating till it is at least min_rating, stop if amount of available money drops below money_threshold
//return true on success (rating is at least as high as min_rating), false otherwise
//PlantTreesToImproveRating(town_id, min_rating, money_threshold)
Code: Select all
hg clone http://hg.openttdcoop.org/superlib superlib
cd superlib
# make changes
vim vehicle.nut
vim rail.nut
#...
vim changelog.txt
hg diff > changes.patch
# Upload changes.patch
Code: Select all
hg clone http://hg.openttdcoop.org/superlib superlib
cd superlib
# init hg queues
hg qinit
# make change 1
hg qnew "10-vehicle-depot-sell.patch"
vim superlib/vehicle.nut
vim changelog.txt
hg qrefresh -m "Add: Vehicle.SellAllVehiclesStoppedInDepots()"
# make change 2
hg qnew "20-rail-methods.patch"
vim superlib/rail.nut
vim changelog.txt
hg qrefresh -m "Add: Additional rail helpers"
# Upload the patches in .hg/patches/
sqrt() is already included in Squirrel math apiKogut wrote:After refactoring of AIAI I have some candidates for SuperLib functions
Helper
Code: Select all
... //from Rondje om de kerk //computes square root of i using Babylonian method //returns n that is the highest integer that is lower or equal to the square root of integer i //Sqrt(i) ...
But the math api isn't include in openttd...HGUS wrote:sqrt() is already included in Squirrel math api
If you look further down in story.nut, you will see that there is a Story.NewStoryPage2 which does what you want. I did not want to break scripts relaying on the old return value which may blindly update to next SuperLib due to Bananas constraints. So therefore there is a second version with a different return value.krinn wrote:I get a look at it, i suggest (not critical) it would be better if function NewStoryPage(company, title, ...) return an array of [page_id, pe, pe, pe...]
With page_id user may use the id to re-add other elements in the page, but your function is there to add multi-elements in a row, so re-use of page_id is not really as useful as editing existing element id.
And as it's then a need to return an array for all "pe"_ids it's then logic to add page_id as 0 index while you're at it, and user get all ids he may need to edit elements or add new element to a page.
As i said, really not critical, as it's an example and it already do its task, as the usage is clear even right now.
Neither MinimalGS version 1 nor version 2 has that method. You probably refer to http://devs.openttd.org/~zuu/goal-gui/G ... -GS-v2.tar which is linked to from the Story Book wiki page. But that is not the same as Minimal GS.krinn wrote:Quiet funny you already did it in fact
But really my comment was about the implementation that is within the minimalGS (main.nut line 37) where the same function appears.
Code: Select all
/* Returns the direction tile2 is from tile1.
* Tiles do not have to be adjacent or exactly on compass axis.
* If directly on the axis it will be NE/SE/SW/NW, else it will
* be N/E/S/W.
*/
static function GetDirectionBetweenTile(tile1, tile2);
Code: Select all
function _SuperLib_Direction::GetDirectionBetweenTile(tile1, tile2)
{
local rel_x = AIMap.GetTileX(tile2) - AIMap.GetTileX(tile1);
local rel_y = AIMap.GetTileY(tile2) - AIMap.GetTileY(tile1);
// Same tile
if(rel_x == 0 && rel_y == 0) return _SuperLib_Direction.DIR_INVALID;
// On the same axis.
else if (rel_x == 0 && rel_y > 0) return _SuperLib_Direction.DIR_NW;
else if (rel_x == 0 && rel_y < 0) return _SuperLib_Direction.DIR_SE;
else if (rel_x > 0 && rel_y == 0) return _SuperLib_Direction.DIR_NE;
else if (rel_x < 0 && rel_y == 0) return _SuperLib_Direction.DIR_SW;
// Off axis.
else if (rel_x > 0 && rel_y > 0) return _SuperLib_Direction.DIR_N;
else if (rel_x < 0 && rel_y < 0) return _SuperLib_Direction.DIR_S;
else if (rel_x > 0 && rel_y < 0) return _SuperLib_Direction.DIR_E;
else if (rel_x < 0 && rel_y > 0) return _SuperLib_Direction.DIR_W;
// Error
return _SuperLib_Direction.DIR_INVALID
}
Actually it is, and it is a wrapper to the standard library api, so it is very much faster than any scripted method...krinn wrote:But the math api isn't include in openttd...HGUS wrote:sqrt() is already included in Squirrel math api
(And even if, did you check the squirrel sqrt use the the babylonian method too ?)
The function I suggested is different in that it will always return a direction (except if the tiles are the same). The current superlib function will only return N,E,S,W,NE, etc if it lies EXACTLY on these compass points. i,e for any given radius it will only return a direction for 8 distinct tiles (the 4 diagonals and 4 adjacents), all others return INVALID. The suggested function is useful to analyse tiles that are quite far from each other.HGus wrote:And about R2dical suggestion, you don't need to create a new api function, just copy the code in the existing one, cause it is same code rewritten (I'm not sure for better readability or speed, but more compact)