SuperLib: Helper, Direction, Tile, ... libraries
Moderator: OpenTTD Developers
Re: SuperLib: Helper, Direction, Tile, ... libraries
the next question is then : what 90 % 100 will gave you
Re: SuperLib: Helper, Direction, Tile, ... libraries
% is a modulo operation ( http://en.wikipedia.org/wiki/Modulo_operation )krinn wrote:
the next question is then : what 90 % 100 will gave you
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
AIAI - AI for OpenTTD
Re: SuperLib: Helper, Direction, Tile, ... libraries
Well, seems you need more explain so :
Say GetInflationRate = 300
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
Code: 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
Re: SuperLib: Helper, Direction, Tile, ... libraries
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
110 % 100 => 10
Basically, if you do a division on paper and pen and do it the lazy way, you will first calculate how many whole parts you can break up the numerator in. Second you have a rest number to divide to get the decimal point result. What the modulo operator does here is that it returns the rest number.
There is also details on how it work on negative numbers etc. that I leave out now and suggest that your read up on wikipedia or a text book. The modulo operator is far too useful in programming to not knowing it.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Junctioneer (a traffic intersection simulator)
Re: SuperLib: Helper, Direction, Tile, ... libraries
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.
I'm not sure if I understand you. Are you trying to say this function returns 0 when you call it with money < 100?
Maybe try this:
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;
Edit: I read this thread again, and I can't think why the solution by Zuu would not work. In my AI I always used .tointeger() after a division to convert it back to integer, so of course it's a float then...
I think now Kogut just got confused by the division through 100. You maybe would do it intentially at the end of calculation, but its done first, to keep a low number for avoiding overflow.
Re: SuperLib: Helper, Direction, Tile, ... libraries
integer && integer gave an integer result, that's why the 0 result.
the real solve is easy you must force a float
the real solve is easy you must force a float
Code: Select all
return (money * 0.01 * _SuperLib_Money.GetInflationRate()).tointeger();
Re: SuperLib: Helper, Direction, Tile, ... libraries
Original:
Solution by Kogut, but with added "/100" at the end:
Personally I like this integer only solution better than using floats. The result of a floating point operation is always risky unless you have complete knowledge how different platforms etc. influence the result. I can't say that I know how the Squirrel floats change in behaviour on different platforms. I would guess that it uses native division rather than implementing its own division operation. Thus it depends on the native platform exactly what result you get.
Code: Select all
return money * _SuperLib_Money.GetInflationRate() / 100;
Code: Select all
return (money / 100) * _SuperLib_Money.GetInflationRate() + (money % 100) * _SuperLib_Money.GetInflationRate() / 100;
Personally I like this integer only solution better than using floats. The result of a floating point operation is always risky unless you have complete knowledge how different platforms etc. influence the result. I can't say that I know how the Squirrel floats change in behaviour on different platforms. I would guess that it uses native division rather than implementing its own division operation. Thus it depends on the native platform exactly what result you get.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Junctioneer (a traffic intersection simulator)
Re: SuperLib: Helper, Direction, Tile, ... libraries
Update - Version 36
Changes:
For more info on FS#5561: http://bugs.openttd.org/task/5561
Changes:
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)
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Junctioneer (a traffic intersection simulator)
Re: SuperLib: Helper, Direction, Tile, ... libraries
After refactoring of AIAI I have some candidates for SuperLib functions
Helper
Rail
Town
Is there a place for some of them in this library? I would provide modified SuperLib with wanted function(s) added in the correct coding style.
Helper
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)
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
AIAI - AI for OpenTTD
Re: SuperLib: Helper, Direction, Tile, ... libraries
I don't see any reason to not accept any of those methods based on the description of them.
Out of the Helper methods, I would prefere to place SellAllVehiclesStoppedInDepots() in the Vehicle sub library.
Ideally I think GetWeightOfOneCargoPiece(cargo_id) should go into a new Cargo sub library. However, I know that there is eg. GetPAXCargo() in Helper already, so because of that I agree to place GetWeightOfOneCargoPiece(cargo_id) in Helper.
As for the contribution, I would suggest that you clone the HG repository, make the changes and run hg diff to create a patch file rather than sending me a complete copy of SuperLib.
If you want to do it even prettier you can create one patch for each added method: (in this case, each added method is fairly independent of each other, so the point of splitting it up is not very high, but this is how to create a patch queue with hg)
Out of the Helper methods, I would prefere to place SellAllVehiclesStoppedInDepots() in the Vehicle sub library.
Ideally I think GetWeightOfOneCargoPiece(cargo_id) should go into a new Cargo sub library. However, I know that there is eg. GetPAXCargo() in Helper already, so because of that I agree to place GetWeightOfOneCargoPiece(cargo_id) in Helper.
As for the contribution, I would suggest that you clone the HG repository, make the changes and run hg diff to create a patch file rather than sending me a complete copy of SuperLib.
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
If you want to do it even prettier you can create one patch for each added method: (in this case, each added method is fairly independent of each other, so the point of splitting it up is not very high, but this is how to create a patch queue with hg)
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/
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Junctioneer (a traffic intersection simulator)
Re: SuperLib: Helper, Direction, Tile, ... libraries
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) ...
Re: SuperLib: Helper, Direction, Tile, ... libraries
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 ?)
Re: SuperLib: Helper, Direction, Tile, ... libraries
Quote from post in MinimalGS thread that is related to a SuperLib method which is not used in MinimalGS (v2). I therefore answer it here.
Also, yes there are situations when you may want to add content later, but use it with care as the possibility for users to discover that is more limited than when new pages are added.
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.
Also, yes there are situations when you may want to add content later, but use it with care as the possibility for users to discover that is more limited than when new pages are added.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Junctioneer (a traffic intersection simulator)
Re: SuperLib: Helper, Direction, Tile, ... libraries
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.
But really my comment was about the implementation that is within the minimalGS (main.nut line 37) where the same function appears.
Re: SuperLib: Helper, Direction, Tile, ... libraries
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.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Junctioneer (a traffic intersection simulator)
Re: SuperLib: Helper, Direction, Tile, ... libraries
hmmm, well, heu, hmmm...
yes
yes
Re: SuperLib: Helper, Direction, Tile, ... libraries
Heya,
Bug report for SuperLib v36:
SuperLib.Direction LINE 166 -> "in_direction" should be "dir", and causes a crash as is.
---
Also I have a suggestion function for that same class, similar to the function above (function _SuperLib_Direction::GetDirectionToTile(tile1, tile2)) with the error:
Bug report for SuperLib v36:
SuperLib.Direction LINE 166 -> "in_direction" should be "dir", and causes a crash as is.
---
Also I have a suggestion function for that same class, similar to the function above (function _SuperLib_Direction::GetDirectionToTile(tile1, tile2)) with the error:
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
}
Re: SuperLib: Helper, Direction, Tile, ... libraries
Sorry OT:
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)
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 ?)
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)
Re: SuperLib: Helper, Direction, Tile, ... libraries
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)
Re: SuperLib: Helper, Direction, Tile, ... libraries
Thanks for your contribution. I now see that I've missed a bug report some posts up. Sorry for this delay. No fix yet, but will make one soon.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Junctioneer (a traffic intersection simulator)
Who is online
Users browsing this forum: No registered users and 0 guests