2014-01-03 13:32:13 -05:00
Vector = require ' ./vector '
Rectangle = require ' ./rectangle '
2014-07-16 18:57:53 -04:00
Ellipse = require ' ./ellipse '
LineSegment = require ' ./line_segment '
2014-01-03 13:32:13 -05:00
WorldFrame = require ' ./world_frame '
Thang = require ' ./thang '
ThangState = require ' ./thang_state '
Rand = require ' ./rand '
WorldScriptNote = require ' ./world_script_note '
{ now , consolidateThangs , typedArraySupport } = require ' ./world_utils '
Component = require ' lib/world/component '
System = require ' lib/world/system '
2014-08-21 19:27:52 -04:00
PROGRESS_UPDATE_INTERVAL = 100
2014-08-22 15:39:29 -04:00
DESERIALIZATION_INTERVAL = 10
2014-08-23 00:35:08 -04:00
REAL_TIME_BUFFER_MIN = 2 * PROGRESS_UPDATE_INTERVAL
2014-08-24 01:24:00 -04:00
REAL_TIME_BUFFER_MAX = 3 * PROGRESS_UPDATE_INTERVAL
REAL_TIME_BUFFERED_WAIT_INTERVAL = 0.5 * PROGRESS_UPDATE_INTERVAL
2014-08-26 01:05:24 -04:00
REAL_TIME_COUNTDOWN_DELAY = 3000 # match CountdownScreen
2014-08-11 14:04:38 -04:00
ITEM_ORIGINAL = ' 53e12043b82921000051cdf9 '
2014-11-18 00:30:44 -05:00
EXISTS_ORIGINAL = ' 524b4150ff92f1f4f8000024 '
2014-11-10 13:51:46 -05:00
COUNTDOWN_LEVELS = [ ' sky-span ' ]
2014-01-03 13:32:13 -05:00
module.exports = class World
2014-06-30 22:16:26 -04:00
@className: ' World '
2014-01-31 19:16:59 -05:00
age: 0
ended: false
2014-05-10 21:24:50 -04:00
preloading: false # Whether we are just preloading a world in case we soon cast it
debugging: false # Whether we are just rerunning to debug a world we've already cast
headless: false # Whether we are just simulating for goal states instead of all serialized results
2014-08-21 19:27:52 -04:00
framesSerializedSoFar: 0
2014-01-31 19:16:59 -05:00
apiProperties: [ ' age ' , ' dt ' ]
2014-08-24 01:24:00 -04:00
realTimeBufferMax: REAL_TIME_BUFFER_MAX / 1000
2014-05-10 21:24:50 -04:00
constructor: (@userCodeMap, classMap) ->
2014-01-03 13:32:13 -05:00
# classMap is needed for deserializing Worlds, Thangs, and other classes
2014-07-16 18:57:53 -04:00
@classMap = classMap ? { Vector: Vector , Rectangle: Rectangle , Thang: Thang , Ellipse: Ellipse , LineSegment: LineSegment }
2014-01-03 13:32:13 -05:00
Thang . resetThangIDs ( )
@ userCodeMap ? = { }
@thangs = [ ]
@thangMap = { }
@systems = [ ]
@systemMap = { }
@scriptNotes = [ ]
2014-05-15 17:54:31 -04:00
@rand = new Rand 0 # Existence System may change this seed
2014-01-03 13:32:13 -05:00
@frames = [ new WorldFrame ( @ , 0 ) ]
2014-05-22 22:05:05 -04:00
destroy: ->
@ goalManager ? . destroy ( )
thang . destroy ( ) for thang in @ thangs
@ [ key ] = undefined for key of @
@destroyed = true
@destroy = ->
2014-01-03 13:32:13 -05:00
getFrame: (frameIndex) ->
# Optimize it a bit--assume we have all if @ended and are at the previous frame otherwise
frames = @ frames
if @ ended
frame = frames [ frameIndex ]
else if frameIndex
frame = frames [ frameIndex - 1 ] . getNextFrame ( )
frames . push frame
else
frame = frames [ 0 ]
@age = frameIndex * @ dt
frame
getThangByID: (id) ->
@ thangMap [ id ]
setThang: (thang) ->
2014-09-29 14:47:12 -04:00
thang.stateChanged = true
2014-01-03 13:32:13 -05:00
for old , i in @ thangs
2014-09-23 00:50:48 -04:00
console . error ' world trying to set ' , thang , ' over ' , old unless old ? and thang ?
2014-01-03 13:32:13 -05:00
if old . id is thang . id
@ thangs [ i ] = thang
@ thangMap [ thang . id ] = thang
2014-08-25 00:39:34 -04:00
thangDialogueSounds: (startFrame=0) ->
return [ ] unless startFrame < @ frames . length
2014-01-03 13:32:13 -05:00
[ sounds , seen ] = [ [ ] , { } ]
2014-08-25 00:39:34 -04:00
for frameIndex in [ startFrame . . . @ frames . length ]
frame = @ frames [ frameIndex ]
2014-01-03 13:32:13 -05:00
for thangID , state of frame . thangStateMap
2014-06-30 22:16:26 -04:00
continue unless state . thang . say and sayMessage = state . getStateForProp ' sayMessage '
soundKey = state . thang . spriteName + ' : ' + sayMessage
2014-01-03 13:32:13 -05:00
unless seen [ soundKey ]
sounds . push [ state . thang . spriteName , sayMessage ]
seen [ soundKey ] = true
sounds
setGoalManager: (@goalManager) ->
addError: (error) ->
( @ runtimeErrors ? = [ ] ) . push error
( @ unhandledRuntimeErrors ? = [ ] ) . push error
2014-09-23 21:21:27 -04:00
loadFrames: (loadedCallback, errorCallback, loadProgressCallback, preloadedCallback, skipDeferredLoading, loadUntilFrame) ->
2014-01-03 13:32:13 -05:00
return if @ aborted
2014-08-25 00:39:34 -04:00
console . log ' Warning: loadFrames called on empty World (no thangs). ' unless @ thangs . length
2014-08-26 01:05:24 -04:00
continueLaterFn = =>
2014-09-23 21:21:27 -04:00
@ loadFrames ( loadedCallback , errorCallback , loadProgressCallback , preloadedCallback , skipDeferredLoading , loadUntilFrame ) unless @ destroyed
2014-08-26 01:05:24 -04:00
if @ realTime and not @ countdownFinished
2014-11-10 13:51:46 -05:00
@realTimeSpeedFactor = 1
unless @ showsCountdown
2014-11-11 10:57:29 -05:00
if @ levelID in [ ' thornbush-farm ' , ' back-to-back ' , ' ogre-encampment ' , ' woodland-cleaver ' , ' shield-rush ' , ' peasant-protection ' , ' munchkin-swarm ' ]
2014-11-10 13:51:46 -05:00
@realTimeSpeedFactor = 3
2014-10-30 19:21:08 -04:00
if @ showsCountdown
return setTimeout @ finishCountdown ( continueLaterFn ) , REAL_TIME_COUNTDOWN_DELAY
else
@ finishCountdown continueLaterFn
2014-01-03 13:32:13 -05:00
t1 = now ( )
@ t0 ? = t1
2014-08-23 00:35:08 -04:00
@ worldLoadStartTime ? = t1
@ lastRealTimeUpdate ? = 0
2014-08-25 00:39:34 -04:00
frameToLoadUntil = if loadUntilFrame then loadUntilFrame + 1 else @ totalFrames # Might stop early if debugging.
2014-01-03 13:32:13 -05:00
i = @ frames . length
2014-08-22 17:59:32 -04:00
while i < frameToLoadUntil and i < @ totalFrames
2014-08-25 00:39:34 -04:00
return unless @ shouldContinueLoading t1 , loadProgressCallback , skipDeferredLoading , continueLaterFn
@ adjustFlowSettings loadUntilFrame if @ debugging
2014-08-24 01:24:00 -04:00
try
@ getFrame ( i )
2014-08-25 00:39:34 -04:00
++ i # Increment this after we have succeeded in getting the frame, otherwise we'll have to do that frame again
2014-08-24 01:24:00 -04:00
catch error
2014-08-25 00:39:34 -04:00
@ addError error # Not an Aether.errors.UserCodeError; maybe we can't recover
2014-08-24 01:24:00 -04:00
unless @ preloading or @ debugging
for error in ( @ unhandledRuntimeErrors ? [ ] )
return unless errorCallback error # errorCallback tells us whether the error is recoverable
@unhandledRuntimeErrors = [ ]
2014-09-23 21:21:27 -04:00
@ finishLoadingFrames loadProgressCallback , loadedCallback , preloadedCallback
2014-08-24 01:24:00 -04:00
2014-09-23 21:21:27 -04:00
finishLoadingFrames: (loadProgressCallback, loadedCallback, preloadedCallback) ->
2014-05-10 21:24:50 -04:00
unless @ debugging
2014-05-09 17:48:43 -04:00
@ended = true
system . finish @ thangs for system in @ systems
2014-09-23 21:21:27 -04:00
if @ preloading
preloadedCallback ( )
else
2014-05-10 21:24:50 -04:00
loadProgressCallback ? 1
loadedCallback ( )
2014-04-28 18:05:54 -04:00
2014-08-26 01:05:24 -04:00
finishCountdown: (continueLaterFn) -> =>
return if @ destroyed
@countdownFinished = true
continueLaterFn ( )
2014-08-23 00:35:08 -04:00
shouldDelayRealTimeSimulation: (t) ->
return false unless @ realTime
2014-09-25 14:17:04 -04:00
timeSinceStart = ( t - @ worldLoadStartTime ) * @ realTimeSpeedFactor
2014-08-23 00:35:08 -04:00
timeLoaded = @ frames . length * @ dt * 1000
timeBuffered = timeLoaded - timeSinceStart
2014-09-25 14:17:04 -04:00
timeBuffered > REAL_TIME_BUFFER_MAX * @ realTimeSpeedFactor
2014-08-23 00:35:08 -04:00
shouldUpdateRealTimePlayback: (t) ->
return false unless @ realTime
2014-08-24 01:24:00 -04:00
return false if @ frames . length * @ dt is @ lastRealTimeUpdate
timeLoaded = @ frames . length * @ dt * 1000
2014-09-25 14:17:04 -04:00
timeSinceStart = ( t - @ worldLoadStartTime ) * @ realTimeSpeedFactor
2014-08-23 00:35:08 -04:00
remainingBuffer = @ lastRealTimeUpdate * 1000 - timeSinceStart
2014-09-25 14:17:04 -04:00
remainingBuffer < REAL_TIME_BUFFER_MIN * @ realTimeSpeedFactor
2014-08-23 00:35:08 -04:00
2014-08-25 00:39:34 -04:00
shouldContinueLoading: (t1, loadProgressCallback, skipDeferredLoading, continueLaterFn) ->
t2 = now ( )
if @ realTime
shouldUpdateProgress = @ shouldUpdateRealTimePlayback t2
shouldDelayRealTimeSimulation = not shouldUpdateProgress and @ shouldDelayRealTimeSimulation t2
else
shouldUpdateProgress = t2 - t1 > PROGRESS_UPDATE_INTERVAL
shouldDelayRealTimeSimulation = false
return true unless shouldUpdateProgress or shouldDelayRealTimeSimulation
# Stop loading frames for now; continue in a moment.
if shouldUpdateProgress
@lastRealTimeUpdate = @ frames . length * @ dt if @ realTime
#console.log 'we think it is now', (t2 - @worldLoadStartTime) / 1000, 'so delivering', @lastRealTimeUpdate
loadProgressCallback ? @ frames . length / @ totalFrames unless @ preloading
t1 = t2
if t2 - @ t0 > 1000
console . log ' Loaded ' , @ frames . length , ' of ' , @ totalFrames , ' (+ ' + ( t2 - @ t0 ) . toFixed ( 0 ) + ' ms) ' unless @ realTime
@t0 = t2
if skipDeferredLoading
continueLaterFn ( )
else
delay = if shouldDelayRealTimeSimulation then REAL_TIME_BUFFERED_WAIT_INTERVAL else 0
setTimeout continueLaterFn , delay
false
adjustFlowSettings: (loadUntilFrame) ->
for thang in @ thangs when thang . isProgrammable
userCode = @ userCodeMap [ thang . id ] ? { }
for methodName , aether of userCode
framesToLoadFlowBefore = if methodName is ' plan ' or methodName is ' makeBid ' then 200 else 1 # Adjust if plan() is taking even longer
aether._shouldSkipFlow = @ frames . length < loadUntilFrame - framesToLoadFlowBefore
2014-05-11 20:42:32 -04:00
finalizePreload: (loadedCallback) ->
@preloading = false
loadedCallback ( ) if @ ended
2014-01-03 13:32:13 -05:00
abort: ->
@aborted = true
2014-08-23 20:26:56 -04:00
addFlagEvent: (flagEvent) ->
@ flagHistory . push flagEvent
2014-08-23 00:35:08 -04:00
2014-01-03 13:32:13 -05:00
loadFromLevel: (level, willSimulate=true) ->
2014-09-25 16:17:41 -04:00
@levelID = level . slug
2014-08-11 14:04:38 -04:00
@levelComponents = level . levelComponents
@thangTypes = level . thangTypes
2014-10-20 12:41:28 -04:00
@ loadScriptsFromLevel level
2014-01-03 13:32:13 -05:00
@ loadSystemsFromLevel level
@ loadThangsFromLevel level , willSimulate
2014-11-10 13:51:46 -05:00
@showsCountdown = @ levelID in COUNTDOWN_LEVELS or _ . any ( @ thangs , (t) -> ( t . programmableProperties and ' findFlags ' in t . programmableProperties ) or t . inventory ? . flag )
2014-01-03 13:32:13 -05:00
system . start @ thangs for system in @ systems
loadSystemsFromLevel: (level) ->
# Remove old Systems
@systems = [ ]
@systemMap = { }
# Load new Systems
for levelSystem in level . systems
systemModel = levelSystem . model
config = levelSystem . config
2014-06-30 22:16:26 -04:00
systemClass = @ loadClassFromCode systemModel . js , systemModel . name , ' system '
2014-01-03 13:32:13 -05:00
#console.log "using db system class ---\n", systemClass, "\n--- from code ---n", systemModel.js, "\n---"
system = new systemClass @ , config
@ addSystems system
null
loadThangsFromLevel: (level, willSimulate) ->
# Remove old Thangs
@thangs = [ ]
@thangMap = { }
# Load new Thangs
2014-08-25 23:34:46 -04:00
toAdd = ( @ loadThangFromLevel thangConfig , level . levelComponents , level . thangTypes for thangConfig in level . thangs ? [ ] )
2014-08-11 14:04:38 -04:00
@extraneousThangs = consolidateThangs toAdd if willSimulate # Combine walls, for example; serialize the leftovers later
@ addThang thang for thang in toAdd
2014-01-03 13:32:13 -05:00
null
2014-08-11 14:04:38 -04:00
loadThangFromLevel: (thangConfig, levelComponents, thangTypes, equipBy=null) ->
components = [ ]
2014-11-18 00:30:44 -05:00
for component , componentIndex in thangConfig . components
2014-08-11 14:04:38 -04:00
componentModel = _ . find levelComponents , (c) ->
c . original is component . original and c . version . major is ( component . majorVersion ? 0 )
componentClass = @ loadClassFromCode componentModel . js , componentModel . name , ' component '
components . push [ componentClass , component . config ]
2014-11-18 00:30:44 -05:00
if component . original is ITEM_ORIGINAL
isItem = true
component.config.ownerID = equipBy if equipBy
else if component . original is EXISTS_ORIGINAL
existsConfigIndex = componentIndex
if isItem and existsConfigIndex ?
# For memory usage performance, make sure these don't get any tracked properties assigned.
components [ existsConfigIndex ] [ 1 ] = { exists: false , stateless: true }
2014-08-11 14:04:38 -04:00
thangTypeOriginal = thangConfig . thangType
thangTypeModel = _ . find thangTypes , (t) -> t . original is thangTypeOriginal
return console . error thangConfig . id ? equipBy , ' could not find ThangType for ' , thangTypeOriginal unless thangTypeModel
thangTypeName = thangTypeModel . name
thang = new Thang @ , thangTypeName , thangConfig . id
try
thang . addComponents components . . .
catch e
2014-08-11 20:09:36 -04:00
console . error ' couldn \' t load components for ' , thangTypeOriginal , thangConfig . id , ' because ' , e . toString ( ) , e . stack
2014-08-11 14:04:38 -04:00
thang
addThang: (thang) ->
@ thangs . unshift thang # Interactions happen in reverse order of specification/drawing
@ setThang thang
@ updateThangState thang
thang . updateRegistration ( )
thang
2014-01-03 13:32:13 -05:00
loadScriptsFromLevel: (level) ->
@scriptNotes = [ ]
@scripts = [ ]
@ addScripts level . scripts . . .
2014-06-30 22:16:26 -04:00
loadClassFromCode: (js, name, kind='component') ->
2014-01-03 13:32:13 -05:00
# Cache them based on source code so we don't have to worry about extra compilations
@ componentCodeClassMap ? = { }
@ systemCodeClassMap ? = { }
2014-06-30 22:16:26 -04:00
map = if kind is ' component ' then @ componentCodeClassMap else @ systemCodeClassMap
2014-01-03 13:32:13 -05:00
c = map [ js ]
return c if c
2014-07-13 19:40:03 -04:00
try
c = map [ js ] = eval js
catch err
console . error " Couldn ' t compile #{ kind } code: " , err , " \n " , js
c = map [ js ] = { }
2014-01-03 13:32:13 -05:00
c.className = name
c
updateThangState: (thang) ->
@ frames [ @ frames . length - 1 ] . thangStateMap [ thang . id ] = thang . getState ( )
size: ->
@ calculateBounds ( ) unless @ width ? and @ height ?
return [ @ width , @ height ] if @ width ? and @ height ?
getBounds: ->
@ calculateBounds ( ) unless @ bounds ?
return @ bounds
calculateBounds: ->
bounds = { left: 0 , top: 0 , right: 0 , bottom: 0 }
hasLand = _ . some @ thangs , ' isLand '
2014-08-06 11:24:50 -04:00
for thang in @ thangs when thang . isLand or ( not hasLand and thang . rectangle ) # Look at Lands only
2014-01-03 13:32:13 -05:00
rect = thang . rectangle ( ) . axisAlignedBoundingBox ( )
bounds.left = Math . min ( bounds . left , rect . x - rect . width / 2 )
bounds.right = Math . max ( bounds . right , rect . x + rect . width / 2 )
bounds.bottom = Math . min ( bounds . bottom , rect . y - rect . height / 2 )
bounds.top = Math . max ( bounds . top , rect . y + rect . height / 2 )
@width = bounds . right - bounds . left
@height = bounds . top - bounds . bottom
@bounds = bounds
[ @ width , @ height ]
publishNote: (channel, event) ->
event ? = { }
channel = ' world: ' + channel
2014-10-20 16:57:32 -04:00
for script in @ scripts ? [ ]
2014-01-03 13:32:13 -05:00
continue if script . channel isnt channel
2014-01-23 21:14:25 -05:00
scriptNote = new WorldScriptNote script , event
2014-01-03 13:32:13 -05:00
continue if scriptNote . invalid
@ scriptNotes . push scriptNote
return unless @ goalManager
@ goalManager . submitWorldGenerationEvent ( channel , event , @ frames . length )
2014-04-28 18:05:54 -04:00
2014-05-13 13:43:39 -04:00
getGoalState: (goalID) ->
@ goalManager . getGoalState ( goalID )
2014-03-28 21:54:16 -04:00
setGoalState: (goalID, status) ->
@ goalManager . setGoalState ( goalID , status )
2014-01-03 13:32:13 -05:00
endWorld: (victory=false, delay=3, tentative=false) ->
2014-05-12 11:35:46 -04:00
@totalFrames = Math . min ( @ totalFrames , @ frames . length + Math . floor ( delay / @ dt ) ) # end a few seconds later
2014-01-03 13:32:13 -05:00
@victory = victory # TODO: should just make this signify the winning superteam
@victoryIsTentative = tentative
status = if @ victory then ' won ' else ' lost '
@ publishNote status
console . log " The world ended in #{ status } on frame #{ @ totalFrames } "
addSystems: (systems...) ->
@systems = @ systems . concat systems
for system in systems
@ systemMap [ system . constructor . className ] = system
getSystem: (systemClassName) ->
@ systemMap ? [ systemClassName ]
addScripts: (scripts...) ->
@scripts = ( @ scripts ? [ ] ) . concat scripts
2014-01-13 19:58:17 -05:00
addTrackedProperties: (props...) ->
@trackedProperties = ( @ trackedProperties ? [ ] ) . concat props
2014-08-22 00:23:45 -04:00
serialize: ->
2014-01-03 13:32:13 -05:00
# Code hotspot; optimize it
2014-11-18 00:30:44 -05:00
@ freeMemoryBeforeFinalSerialization ( ) if @ ended
2014-08-22 00:23:45 -04:00
startFrame = @ framesSerializedSoFar
endFrame = @ frames . length
2014-08-22 17:59:32 -04:00
#console.log "... world serializing frames from", startFrame, "to", endFrame, "of", @totalFrames
2014-01-03 13:32:13 -05:00
[ transferableObjects , nontransferableObjects ] = [ 0 , 0 ]
2014-10-18 20:32:01 -04:00
delete flag . processed for flag in @ flagHistory
o = { totalFrames: @ totalFrames , maxTotalFrames: @ maxTotalFrames , frameRate: @ frameRate , dt: @ dt , victory: @ victory , userCodeMap: { } , trackedProperties: { } , flagHistory: @ flagHistory }
2014-01-14 17:03:55 -05:00
o . trackedProperties [ prop ] = @ [ prop ] for prop in @ trackedProperties or [ ]
2014-01-13 19:58:17 -05:00
2014-01-03 13:32:13 -05:00
for thangID , methods of @ userCodeMap
serializedMethods = o . userCodeMap [ thangID ] = { }
for methodName , method of methods
2014-02-05 16:39:41 -05:00
serializedMethods [ methodName ] = method . serialize ? ( ) ? method # serialize the method again if it has been deserialized
2014-01-03 13:32:13 -05:00
t0 = now ( )
o.trackedPropertiesThangIDs = [ ]
o.trackedPropertiesPerThangIndices = [ ]
o.trackedPropertiesPerThangKeys = [ ]
o.trackedPropertiesPerThangTypes = [ ]
trackedPropertiesPerThangValues = [ ] # We won't send these, just the offsets and the storage buffer
2014-08-22 15:39:29 -04:00
o.trackedPropertiesPerThangValuesOffsets = [ ] # Needed to reconstruct ArrayBufferViews on other end, since Firefox has bugs transfering those: https://bugzilla.mozilla.org/show_bug.cgi?id=841904 and https://bugzilla.mozilla.org/show_bug.cgi?id=861925 # Actually, as of January 2014, it should be fixed. So we could try to undo the workaround.
2014-01-03 13:32:13 -05:00
transferableStorageBytesNeeded = 0
2014-08-21 19:27:52 -04:00
nFrames = endFrame - startFrame
2014-01-03 13:32:13 -05:00
for thang in @ thangs
# Don't serialize empty trackedProperties for stateless Thangs which haven't changed (like obstacles).
2014-08-22 15:39:29 -04:00
# Check both, since sometimes people mark stateless Thangs but then change them, and those should still be tracked, and the inverse doesn't work on the other end (we'll just think it doesn't exist then).
# If streaming the world, a thang marked stateless that actually change will get messed up. I think.
continue if thang . stateless and not _ . some ( thang . trackedPropertiesUsed , Boolean )
2014-01-03 13:32:13 -05:00
o . trackedPropertiesThangIDs . push thang . id
trackedPropertiesIndices = [ ]
trackedPropertiesKeys = [ ]
trackedPropertiesTypes = [ ]
for used , propIndex in thang . trackedPropertiesUsed
2014-08-22 15:39:29 -04:00
continue unless used
2014-01-03 13:32:13 -05:00
trackedPropertiesIndices . push propIndex
trackedPropertiesKeys . push thang . trackedPropertiesKeys [ propIndex ]
trackedPropertiesTypes . push thang . trackedPropertiesTypes [ propIndex ]
o . trackedPropertiesPerThangIndices . push trackedPropertiesIndices
o . trackedPropertiesPerThangKeys . push trackedPropertiesKeys
o . trackedPropertiesPerThangTypes . push trackedPropertiesTypes
trackedPropertiesPerThangValues . push [ ]
o . trackedPropertiesPerThangValuesOffsets . push [ ]
for type in trackedPropertiesTypes
transferableStorageBytesNeeded += ThangState . transferableBytesNeededForType ( type , nFrames )
if typedArraySupport
o.storageBuffer = new ArrayBuffer ( transferableStorageBytesNeeded )
else
o.storageBuffer = [ ]
storageBufferOffset = 0
for trackedPropertiesValues , thangIndex in trackedPropertiesPerThangValues
trackedPropertiesValuesOffsets = o . trackedPropertiesPerThangValuesOffsets [ thangIndex ]
for type , propIndex in o . trackedPropertiesPerThangTypes [ thangIndex ]
[ storage , bytesStored ] = ThangState . createArrayForType type , nFrames , o . storageBuffer , storageBufferOffset
trackedPropertiesValues . push storage
trackedPropertiesValuesOffsets . push storageBufferOffset
++ transferableObjects if bytesStored
++ nontransferableObjects unless bytesStored
if typedArraySupport
storageBufferOffset += bytesStored
else
# Instead of one big array with each storage as a view into it, they're all separate, so let's keep 'em around for flattening.
storageBufferOffset += storage . length
o . storageBuffer . push storage
o.specialKeysToValues = [ null , Infinity , NaN ]
# Whatever is in specialKeysToValues index 0 will be default for anything missing, so let's make sure it's null.
# Don't think we can include undefined or it'll be treated as a sparse array; haven't tested performance.
o.specialValuesToKeys = { }
for specialValue , i in o . specialKeysToValues
o . specialValuesToKeys [ specialValue ] = i
t1 = now ( )
o.frameHashes = [ ]
2014-08-21 19:27:52 -04:00
for frameIndex in [ startFrame . . . endFrame ]
2014-08-21 20:30:46 -04:00
o . frameHashes . push @ frames [ frameIndex ] . serialize ( frameIndex - startFrame , o . trackedPropertiesThangIDs , o . trackedPropertiesPerThangIndices , o . trackedPropertiesPerThangTypes , trackedPropertiesPerThangValues , o . specialValuesToKeys , o . specialKeysToValues )
2014-01-03 13:32:13 -05:00
t2 = now ( )
unless typedArraySupport
flattened = [ ]
for storage in o . storageBuffer
for value in storage
flattened . push value
o.storageBuffer = flattened
2014-08-21 19:27:52 -04:00
#console.log 'Allocating memory:', (t1 - t0).toFixed(0), 'ms; assigning values:', (t2 - t1).toFixed(0), 'ms, so', ((t2 - t1) / nFrames).toFixed(3), 'ms per frame for', nFrames, 'frames'
2014-06-30 22:16:26 -04:00
#console.log 'Got', transferableObjects, 'transferable objects and', nontransferableObjects, 'nontransferable; stored', transferableStorageBytesNeeded, 'bytes transferably'
2014-01-03 13:32:13 -05:00
o.thangs = ( t . serialize ( ) for t in @ thangs . concat ( @ extraneousThangs ? [ ] ) )
o.scriptNotes = ( sn . serialize ( ) for sn in @ scriptNotes )
if o . scriptNotes . length > 200
2014-06-30 22:16:26 -04:00
console . log ' Whoa, serializing a lot of WorldScriptNotes here: ' , o . scriptNotes . length
2014-11-18 00:30:44 -05:00
@ freeMemoryAfterEachSerialization ( ) unless @ ended
2014-08-21 19:27:52 -04:00
{ serializedWorld: o , transferableObjects: [ o . storageBuffer ] , startFrame: startFrame , endFrame: endFrame }
2014-01-03 13:32:13 -05:00
2014-10-23 21:11:12 -04:00
@deserialize: (o, classMap, oldSerializedWorldFrames, finishedWorldCallback, startFrame, endFrame, level, streamingWorld) ->
2014-01-03 13:32:13 -05:00
# Code hotspot; optimize it
2014-06-30 22:16:26 -04:00
#console.log 'Deserializing', o, 'length', JSON.stringify(o).length
2014-01-03 13:32:13 -05:00
#console.log JSON.stringify(o)
2014-06-30 22:16:26 -04:00
#console.log 'Got special keys and values:', o.specialValuesToKeys, o.specialKeysToValues
2014-01-03 13:32:13 -05:00
perf = { }
perf.t0 = now ( )
2014-08-21 19:27:52 -04:00
nFrames = endFrame - startFrame
2014-10-08 01:28:53 -04:00
if streamingWorld
w = streamingWorld
# Make sure we get any Aether updates from the new frames into the already-deserialized streaming world Aethers.
for thangID , methods of o . userCodeMap
for methodName , serializedAether of methods
for aetherStateKey in [ ' flow ' , ' metrics ' , ' style ' , ' problems ' ]
2014-10-09 12:53:57 -04:00
w . userCodeMap [ thangID ] ? = { }
w . userCodeMap [ thangID ] [ methodName ] ? = { }
2014-10-08 01:28:53 -04:00
w . userCodeMap [ thangID ] [ methodName ] [ aetherStateKey ] = serializedAether [ aetherStateKey ]
else
w = new World o . userCodeMap , classMap
2014-10-18 20:32:01 -04:00
[ w . totalFrames , w . maxTotalFrames , w . frameRate , w . dt , w . scriptNotes , w . victory , w . flagHistory ] = [ o . totalFrames , o . maxTotalFrames , o . frameRate , o . dt , o . scriptNotes ? [ ] , o . victory , o . flagHistory ]
2014-01-14 17:03:55 -05:00
w [ prop ] = val for prop , val of o . trackedProperties
2014-01-03 13:32:13 -05:00
perf.t1 = now ( )
2014-08-21 20:30:46 -04:00
if w . thangs . length
2014-08-22 00:23:45 -04:00
for thangConfig in o . thangs when not w . thangMap [ thangConfig . id ]
2014-10-23 21:11:12 -04:00
w . thangs . push thang = Thang . deserialize ( thangConfig , w , classMap , level . levelComponents )
2014-08-21 20:30:46 -04:00
w . setThang thang
else
2014-10-23 21:11:12 -04:00
w.thangs = ( Thang . deserialize ( thang , w , classMap , level . levelComponents ) for thang in o . thangs )
2014-08-21 20:30:46 -04:00
w . setThang thang for thang in w . thangs
2014-01-03 13:32:13 -05:00
w.scriptNotes = ( WorldScriptNote . deserialize ( sn , w , classMap ) for sn in o . scriptNotes )
perf.t2 = now ( )
o.trackedPropertiesThangs = ( w . getThangByID thangID for thangID in o . trackedPropertiesThangIDs )
o.trackedPropertiesPerThangValues = [ ]
for trackedPropertyTypes , thangIndex in o . trackedPropertiesPerThangTypes
o . trackedPropertiesPerThangValues . push ( trackedPropertiesValues = [ ] )
trackedPropertiesValuesOffsets = o . trackedPropertiesPerThangValuesOffsets [ thangIndex ]
for type , propIndex in trackedPropertyTypes
2014-08-21 19:27:52 -04:00
storage = ThangState . createArrayForType ( type , nFrames , o . storageBuffer , trackedPropertiesValuesOffsets [ propIndex ] ) [ 0 ]
2014-01-03 13:32:13 -05:00
unless typedArraySupport
# This could be more efficient
i = trackedPropertiesValuesOffsets [ propIndex ]
storage = o . storageBuffer . slice i , i + storage . length
trackedPropertiesValues . push storage
perf.t3 = now ( )
perf.batches = 0
2014-08-22 15:39:29 -04:00
perf.framesCPUTime = 0
2014-08-21 19:27:52 -04:00
w.frames = [ ] unless streamingWorld
clearTimeout @ deserializationTimeout if @ deserializationTimeout
@deserializationTimeout = _ . delay @ deserializeSomeFrames , 1 , o , w , finishedWorldCallback , perf , startFrame , endFrame
2014-10-02 18:33:23 -04:00
w # Return in-progress deserializing world
2014-01-03 13:32:13 -05:00
# Spread deserialization out across multiple calls so the interface stays responsive
2014-08-21 19:27:52 -04:00
@deserializeSomeFrames: (o, w, finishedWorldCallback, perf, startFrame, endFrame) =>
2014-01-03 13:32:13 -05:00
++ perf . batches
startTime = now ( )
2014-08-21 19:27:52 -04:00
for frameIndex in [ w . frames . length . . . endFrame ]
2014-08-21 20:30:46 -04:00
w . frames . push WorldFrame . deserialize ( w , frameIndex - startFrame , o . trackedPropertiesThangIDs , o . trackedPropertiesThangs , o . trackedPropertiesPerThangKeys , o . trackedPropertiesPerThangTypes , o . trackedPropertiesPerThangValues , o . specialKeysToValues , o . frameHashes [ frameIndex - startFrame ] , w . dt * frameIndex )
2014-08-22 15:39:29 -04:00
elapsed = now ( ) - startTime
if elapsed > DESERIALIZATION_INTERVAL and frameIndex < endFrame - 1
#console.log " Deserialization not finished, let's do it again soon. Have:", w.frames.length, ", wanted from", startFrame, "to", endFrame
perf . framesCPUTime += elapsed
2014-08-21 19:27:52 -04:00
@deserializationTimeout = _ . delay @ deserializeSomeFrames , 1 , o , w , finishedWorldCallback , perf , startFrame , endFrame
2014-01-03 13:32:13 -05:00
return
2014-08-21 19:27:52 -04:00
@deserializationTimeout = null
2014-08-22 15:39:29 -04:00
perf . framesCPUTime += elapsed
2014-08-21 19:27:52 -04:00
@ finishDeserializing w , finishedWorldCallback , perf , startFrame , endFrame
2014-01-03 13:32:13 -05:00
2014-08-21 19:27:52 -04:00
@finishDeserializing: (w, finishedWorldCallback, perf, startFrame, endFrame) ->
2014-01-03 13:32:13 -05:00
perf.t4 = now ( )
w.ended = true
2014-08-22 15:39:29 -04:00
nFrames = endFrame - startFrame
totalCPUTime = perf . t3 - perf . t0 + perf . framesCPUTime
2014-08-24 01:24:00 -04:00
#console.log 'Deserialization:', totalCPUTime.toFixed(0) + 'ms (' + (totalCPUTime / nFrames).toFixed(3) + 'ms per frame).', perf.batches, 'batches. Did', startFrame, 'to', endFrame, 'in', (perf.t4 - perf.t0).toFixed(0) + 'ms wall clock time.'
2014-01-03 13:32:13 -05:00
if false
2014-06-30 22:16:26 -04:00
console . log ' Deserializing--constructing new World: ' , ( perf . t1 - perf . t0 ) . toFixed ( 2 ) + ' ms '
console . log ' Deserializing--Thangs and ScriptNotes: ' , ( perf . t2 - perf . t1 ) . toFixed ( 2 ) + ' ms '
console . log ' Deserializing--reallocating memory: ' , ( perf . t3 - perf . t2 ) . toFixed ( 2 ) + ' ms '
2014-08-22 15:39:29 -04:00
console . log ' Deserializing--WorldFrames: ' , ( perf . t4 - perf . t3 ) . toFixed ( 2 ) + ' ms wall clock time, ' , ( perf . framesCPUTime ) . toFixed ( 2 ) + ' ms CPU time '
2014-01-03 13:32:13 -05:00
finishedWorldCallback w
findFirstChangedFrame: (oldWorld) ->
2014-08-22 00:23:45 -04:00
return 0 unless oldWorld
2014-01-03 13:32:13 -05:00
for newFrame , i in @ frames
oldFrame = oldWorld . frames [ i ]
2014-08-22 00:23:45 -04:00
break unless oldFrame and ( ( newFrame . hash is oldFrame . hash ) or not newFrame . hash ? or not oldFrame . hash ? ) # undefined gets in there when streaming at the last frame of each batch for some reason
firstChangedFrame = i
2014-08-22 15:39:29 -04:00
if @ frames . length is @ totalFrames
if @ frames [ i ]
console . log ' First changed frame is ' , firstChangedFrame , ' with hash ' , @ frames [ i ] . hash , ' compared to ' , oldWorld . frames [ i ] ? . hash
else
console . log ' No frames were changed out of all ' , @ frames . length
2014-08-22 00:23:45 -04:00
firstChangedFrame
2014-01-03 13:32:13 -05:00
2014-11-18 00:30:44 -05:00
freeMemoryBeforeFinalSerialization: ->
@levelComponents = null
@thangTypes = null
freeMemoryAfterEachSerialization: ->
@ frames [ i ] = null for frame , i in @ frames when i < @ frames . length - 1
2014-11-18 14:46:42 -05:00
pointsForThang: (thangID, camera=null) ->
2014-01-03 13:32:13 -05:00
# Optimized
@ pointsForThangCache ? = { }
cacheKey = thangID
allPoints = @ pointsForThangCache [ cacheKey ]
unless allPoints
allPoints = [ ]
lastFrameIndex = @ frames . length - 1
lastPos = x: null , y: null
for frameIndex in [ lastFrameIndex . . 0 ] by - 1
frame = @ frames [ frameIndex ]
if pos = frame . thangStateMap [ thangID ] ? . getStateForProp ' pos '
pos = camera . worldToSurface { x: pos . x , y: pos . y } if camera # without z
if not lastPos . x ? or ( Math . abs ( lastPos . x - pos . x ) + Math . abs ( lastPos . y - pos . y ) ) > 1
lastPos = pos
2014-02-07 14:49:16 -05:00
allPoints . push lastPos . y , lastPos . x unless lastPos . y is 0 and lastPos . x is 0
2014-01-03 13:32:13 -05:00
allPoints . reverse ( )
@ pointsForThangCache [ cacheKey ] = allPoints
2014-11-18 14:46:42 -05:00
return allPoints
2014-01-03 13:32:13 -05:00
actionsForThang: (thangID, keepIdle=false) ->
# Optimized
@ actionsForThangCache ? = { }
2014-06-30 22:16:26 -04:00
cacheKey = thangID + ' _ ' + Boolean ( keepIdle )
2014-01-03 13:32:13 -05:00
cached = @ actionsForThangCache [ cacheKey ]
return cached if cached
states = ( frame . thangStateMap [ thangID ] for frame in @ frames )
actions = [ ]
lastAction = ' '
for state , i in states
action = state ? . getStateForProp ' action '
continue unless action and ( action isnt lastAction or state . actionActivated )
continue unless state . action isnt ' idle ' or keepIdle
actions . push { frame: i , pos: state . pos , name: action }
lastAction = action
@ actionsForThangCache [ cacheKey ] = actions
return actions
2014-01-14 16:16:30 -05:00
getTeamColors: ->
teamConfigs = @ teamConfigs or { }
colorConfigs = { }
colorConfigs [ teamName ] = config . color for teamName , config of teamConfigs
2014-01-14 17:03:55 -05:00
colorConfigs
2014-02-06 19:05:41 -05:00
teamForPlayer: (n) ->
playableTeams = @ playableTeams ? [ ' humans ' ]
playableTeams [ n % playableTeams . length ]