+ Created function for generating a reproducable seed out of user code - as using JSON.stringify is not garanteed to be the same on every browser for objects.

This commit is contained in:
Dominik Maier 2014-05-13 04:32:29 +02:00
parent efa39ffcd9
commit 521bc31fa5

View file

@ -31,15 +31,13 @@ module.exports = class World
@systemMap = {}
@scriptNotes = []
# We want a seed thats not always 0 yet reproducable.
console.log "Seed: ", @getSeed()
@rand = new Rand @getSeed()
@frames = [new WorldFrame(@, 0)]
getSeed: ->
return 0 unless @userCodeMap #TODO: When does this happen?
#Packs all methods in all thangs in one big array, then sorts and joins them. Then returns a hash value of the result.
@hashString((methods for thangID, methods of @userCodeMap).reduce(((ret, methods) ->
(method for methodID, method of methods).reduce(((ret, method) -> ret.push(method)), ret)
ret.push method for methodID, method of methods
), []).sort().join())
getFrame: (frameIndex) ->
@ -497,4 +495,4 @@ module.exports = class World
#djb2 algorithm
hashString: (str) ->
(str.charCodeAt i for i in [0...str]).reduce(((hash, char) -> ((hash << 5) + hash) + char), 5381) # hash * 33 + c
(str.charCodeAt i for i in [0...str.length]).reduce(((hash, char) -> ((hash << 5) + hash) + char), 5381) # hash * 33 + c