paulofmandown / rotLove

Roguelike Toolkit in Love. A Love2D/lua port of rot.js
http://paulofmandown.github.io/rotLove/
Other
261 stars 26 forks source link

Dice module #14

Closed timothymtorres closed 8 years ago

timothymtorres commented 8 years ago

I was curious to see if you would be interested in adding a roguelike dice library that I made into rotLove. I think it would fit perfectly with the rest of the modules that currently exist and shouldn't be too much hassle.

Currently I am working on setting up documentation for RL-Dice and refactoring the code, both of which should be finished soon! Once that's done, I will setup a pull request for rotLove with RL-Dice included if you are on board with this idea.

Zireael07 commented 8 years ago

@timothymtorres: At the moment I am using both rotLove and your module. Being able to combine the two to use the same seed would be awesome, as currently your module is clustering quite a bit (in three games in a row, for i=1,6 do dice.roll('3d6') end yielded results of: 14, 11, 13, 11, 10, 6 in that very order each time

timothymtorres commented 8 years ago

@Zireael07 That is weird behavior. I intentionally did not add a random seed to the dice module because I figured users of the library would set their own seed in the main game somewhere.

For further clarification where you are having this problem? Are you setting the seed internally inside the dice module or externally somewhere else? I have never tested where the seed is being set, since I have always assumed whenever you are requiring modules that they are all being affected by the same seed.

Later this weekend when I have time, I can run some tests to be sure.

Zireael07 commented 8 years ago

@timothymtorres: I am setting the seed in my own game's main.lua, by using ROTLove's rng:randomseed() function.

I seem to have missed how to set/apply this seed to your module. Any help?

timothymtorres commented 8 years ago

Okay so I took a peak at ROT's RNG code tonight. It appears the randomseed function in the module does not use Lua's math.randomseed() at all. Basically this means that any calls to math.random() will follow the same sequence every time you run a program unless you do one of two things to fix the problem.

  1. (Simple solution) Set math.randomseed(os.time()) somewhere in your main file.
  2. (Complex solution) Modify rl-dice.lua and repalce math.random() with whichever ROT.RNG module you are using (ex. ROT.RNG.MWC:random() )

I've looked at how @paulofmandown coded his RNG's into some of his terrain generation modules. It gives you the option to select which RNG you will be using (except of course for the default math.random)

function ROT.Map.IceyMaze:__init(width, height, rng, regularity)
    assert(ROT and ROT.RNG.Twister, 'require rot or require RandomLua, IceyMaze requires twister() be available')
    ROT.Map.IceyMaze.super.__init(self, width, height)
    self.__name     ='IceyMaze'
    self._regularity= regularity and regularity or 0
    self._rng       =rng and rng or ROT.RNG.Twister:new()
    if not rng then self._rng:randomseed() end
end

Later on when I attempt to add RL-Dice to Rot I'll add the feature for it to be used with the other RNGs, but in the meantime I'd strongly encourage you to stick with just setting math.randomseed somewhere in your main file.

timothymtorres commented 8 years ago

@Zireael07 I am proud to announce that RL-Dice is now added to rotLove!

https://github.com/paulofmandown/rotLove/pull/16

Just waiting on Paul to merge and generate the docs for the new module. With the new ROT.Dice module you no longer need to set math.randomseed() as rot's RNG modules are used instead.

Also be aware that while I was doing this, I discovered a bug from RL-Dice that affects negative bonuses and rerolls. My pattern matching apparently missed adding a pos/neg sign to the number, so every number defaults to positive for bonus and rerolls. This has been fixed in this pull request, and in the new comment I added to RL-Dice.