paulofmandown / rotLove

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

RNG reform! (fix #29, fix #11) #31

Closed airstruck closed 7 years ago

airstruck commented 7 years ago

More like rot.js, where ROT.RNG can be used statically, so there aren't tons of rng instances all over the place (that was a problem; would be a nightmare to hunt down all those rng instances and import seeds or state).

airstruck commented 7 years ago

This also fixes #11.

Like rot.js, the static ROT.RNG is seeded at startup from os time. It can be with instanced with :clone() which extends it on the fly, inheriting its state from that point, so clones should be seeded and ready to go.

It can also be instanced with :new(seed). The seed param is optional and defaults to os time. Instances are seeded in the constructor. Not part of rot.js, but useful and consistent with other classes.

airstruck commented 7 years ago

Second commit brings a bunch of constructors in line with rot.js. None of the constructors use the RNG, so it can be added later with :setRNG.

local foo = ROT.Foo:new(1, 2, rng)
-- becomes
local foo = ROT.Foo:new(1, 2):setRNG(rng)

It's all done in the base class so classes can use self._rng without initializing it (it defaults to the static ROT.RNG). Classes are also given getRNG and setRNG methods.

Putting it in the base class is sort of hacky, but beats copypasting into several classes and it's less hassle than introducing something like mixins.