2014-01-03 13:32:13 -05:00
CocoModel = require ' ./CocoModel '
LevelComponent = require ' ./LevelComponent '
LevelSystem = require ' ./LevelSystem '
ThangType = require ' ./ThangType '
module.exports = class Level extends CocoModel
2014-06-30 22:16:26 -04:00
@className: ' Level '
2014-04-22 14:11:08 -04:00
@schema: require ' schemas/models/level '
2014-06-30 22:16:26 -04:00
urlRoot: ' /db/level '
2014-10-18 17:51:43 -04:00
serialize: (supermodel, session, otherSession, cached=false) ->
o = @ denormalize supermodel , session , otherSession # hot spot to optimize
2014-01-03 13:32:13 -05:00
# Figure out Components
2014-09-03 12:33:21 -04:00
o.levelComponents = if cached then @ getCachedLevelComponents ( supermodel ) else $ . extend true , [ ] , ( lc . attributes for lc in supermodel . getModels LevelComponent )
2014-08-22 17:52:35 -04:00
@ sortThangComponents o . thangs , o . levelComponents , ' Level Thang '
2014-09-03 12:33:21 -04:00
@ fillInDefaultComponentConfiguration o . thangs , o . levelComponents # hot spot to optimize
2014-01-03 13:32:13 -05:00
# Figure out Systems
2014-09-02 15:33:34 -04:00
systemModels = $ . extend true , [ ] , ( ls . attributes for ls in supermodel . getModels LevelSystem )
2014-01-03 13:32:13 -05:00
o.systems = @ sortSystems o . systems , systemModels
@ fillInDefaultSystemConfiguration o . systems
2014-08-11 19:15:46 -04:00
# Figure out ThangTypes' Components
2014-09-03 12:33:21 -04:00
tmap = { }
2014-09-03 15:30:08 -04:00
tmap [ t . thangType ] = true for t in o . thangs ? [ ]
2014-09-22 01:10:52 -04:00
o.thangTypes = ( original: tt . get ( ' original ' ) , name: tt . get ( ' name ' ) , components: $ . extend ( true , [ ] , tt . get ( ' components ' ) ) for tt in supermodel . getModels ThangType when tmap [ tt . get ( ' original ' ) ] or ( tt . get ( ' components ' ) and not tt . notInLevel ) )
2014-08-22 17:52:35 -04:00
@ sortThangComponents o . thangTypes , o . levelComponents , ' ThangType '
2014-08-11 19:15:46 -04:00
@ fillInDefaultComponentConfiguration o . thangTypes , o . levelComponents
2014-09-03 22:19:04 -04:00
2014-01-03 13:32:13 -05:00
o
2014-09-03 22:19:04 -04:00
2014-09-03 12:33:21 -04:00
cachedLevelComponents: null
2014-09-03 22:19:04 -04:00
2014-09-03 12:33:21 -04:00
getCachedLevelComponents: (supermodel) ->
@ cachedLevelComponents ? = { }
levelComponents = supermodel . getModels LevelComponent
newLevelComponents = [ ]
for levelComponent in levelComponents
if levelComponent . hasLocalChanges ( )
newLevelComponents . push $ . extend ( true , { } , levelComponent . attributes )
continue
@ cachedLevelComponents [ levelComponent . id ] ? = @ cachedLevelComponents [ levelComponent . id ] = $ . extend ( true , { } , levelComponent . attributes )
newLevelComponents . push ( @ cachedLevelComponents [ levelComponent . id ] )
newLevelComponents
2014-09-03 22:19:04 -04:00
2014-10-18 17:51:43 -04:00
denormalize: (supermodel, session, otherSession) ->
2014-08-06 18:18:22 -04:00
o = $ . extend true , { } , @ attributes
2014-10-18 17:51:43 -04:00
if o . thangs and @ get ( ' type ' , true ) in [ ' hero ' , ' hero-ladder ' , ' hero-coop ' ]
2014-08-14 18:09:10 -04:00
for levelThang in o . thangs
2014-10-18 17:51:43 -04:00
@ denormalizeThang ( levelThang , supermodel , session , otherSession )
2014-08-06 18:18:22 -04:00
o
2014-10-18 17:51:43 -04:00
denormalizeThang: (levelThang, supermodel, session, otherSession) ->
2014-08-07 21:27:47 -04:00
levelThang . components ? = [ ]
2014-10-18 17:51:43 -04:00
isHero = /Hero Placeholder/ . test levelThang . id
if isHero and otherSession
# If it's a hero and there's another session, find the right session for it.
# If there is no other session (playing against default code, or on single player), clone all placeholders.
2014-10-19 20:38:10 -04:00
# TODO: actually look at the teams on these Thangs to determine which session should go with which placeholder.
2014-10-18 17:51:43 -04:00
if levelThang . id is ' Hero Placeholder 1 ' and session . get ( ' team ' ) is ' humans '
session = otherSession
else if levelThang . id is ' Hero Placeholder ' and session . get ( ' team ' ) is ' ogres '
session = otherSession
2014-09-15 16:15:18 -04:00
2014-08-14 19:34:55 -04:00
# Empty out placeholder Components and store their values if we're the hero placeholder.
2014-09-15 16:15:18 -04:00
if isHero
placeholders = { }
placeholdersUsed = { }
placeholderThangType = supermodel . getModelByOriginal ( ThangType , levelThang . thangType )
for defaultPlaceholderComponent in placeholderThangType . get ( ' components ' )
placeholders [ defaultPlaceholderComponent . original ] = defaultPlaceholderComponent
2014-08-31 18:08:52 -04:00
for thangComponent in levelThang . components
2014-08-14 19:34:55 -04:00
placeholders [ thangComponent . original ] = thangComponent
2014-08-31 18:08:52 -04:00
levelThang.components = [ ] # We have stored the placeholder values, so we can inherit everything else.
2014-08-15 15:09:56 -04:00
heroThangType = session ? . get ( ' heroConfig ' ) ? . thangType
levelThang.thangType = heroThangType if heroThangType
2014-08-14 19:34:55 -04:00
2014-09-21 16:26:56 -04:00
thangType = supermodel . getModelByOriginal ( ThangType , levelThang . thangType , (m) -> m . get ( ' components ' ) ? )
2014-08-31 18:08:52 -04:00
2014-08-06 18:18:22 -04:00
configs = { }
for thangComponent in levelThang . components
configs [ thangComponent . original ] = thangComponent
2014-08-11 01:09:13 -04:00
2014-09-21 16:26:56 -04:00
for defaultThangComponent in thangType ? . get ( ' components ' ) or [ ]
2014-08-06 18:18:22 -04:00
if levelThangComponent = configs [ defaultThangComponent . original ]
2014-08-14 18:09:10 -04:00
# Take the ThangType default Components and merge level-specific Component config into it
2014-08-06 18:18:22 -04:00
copy = $ . extend true , { } , defaultThangComponent . config
levelThangComponent.config = _ . merge copy , levelThangComponent . config
2014-08-11 01:09:13 -04:00
2014-08-06 18:18:22 -04:00
else
2014-08-14 18:09:10 -04:00
# Just add the Component as is
levelThangComponent = $ . extend true , { } , defaultThangComponent
levelThang . components . push levelThangComponent
2014-09-15 16:15:18 -04:00
if isHero and placeholderComponent = placeholders [ defaultThangComponent . original ]
placeholdersUsed [ placeholderComponent . original ] = true
2014-08-14 18:09:10 -04:00
placeholderConfig = placeholderComponent . config ? { }
if placeholderConfig . pos # Pull in Physical pos x and y
levelThangComponent . config . pos ? = { }
levelThangComponent.config.pos.x = placeholderConfig . pos . x
levelThangComponent.config.pos.y = placeholderConfig . pos . y
else if placeholderConfig . team # Pull in Allied team
levelThangComponent.config.team = placeholderConfig . team
else if placeholderConfig . programmableMethods
# Take the ThangType default Programmable and merge level-specific Component config into it
copy = $ . extend true , { } , placeholderConfig
levelThangComponent.config = _ . merge copy , levelThangComponent . config
2014-09-15 16:15:18 -04:00
if isHero
if equips = _ . find levelThang . components , { original: LevelComponent . EquipsID }
inventory = session ? . get ( ' heroConfig ' ) ? . inventory
equips . config ? = { }
equips.config.inventory = $ . extend true , { } , inventory if inventory
for original , placeholderComponent of placeholders when not placeholdersUsed [ original ]
levelThang . components . push placeholderComponent
2014-08-14 18:09:10 -04:00
2014-01-03 13:32:13 -05:00
sortSystems: (levelSystems, systemModels) ->
[ sorted , originalsSeen ] = [ [ ] , { } ]
visit = (system) ->
return if system . original of originalsSeen
systemModel = _ . find systemModels , { original: system . original }
2014-07-13 12:31:31 -04:00
return console . error ' Couldn \' t find model for original ' , system . original , ' from ' , systemModels unless systemModel
2014-01-03 13:32:13 -05:00
for d in systemModel . dependencies or [ ]
system2 = _ . find levelSystems , { original: d . original }
visit system2
2014-06-30 22:16:26 -04:00
#console.log 'sorted systems adding', systemModel.name
2014-09-02 15:33:34 -04:00
sorted . push { model: systemModel , config: $ . extend true , { } , system . config }
2014-01-03 13:32:13 -05:00
originalsSeen [ system . original ] = true
2014-08-25 23:34:46 -04:00
visit system for system in levelSystems ? [ ]
2014-01-03 13:32:13 -05:00
sorted
2014-08-22 17:52:35 -04:00
sortThangComponents: (thangs, levelComponents, parentType) ->
2014-01-03 13:32:13 -05:00
# Here we have to sort the Components by their dependencies.
# It's a bit tricky though, because we don't have either soft dependencies or priority levels.
# Example: Programmable must come last, since it has to override any Component-provided methods that any other Component might have created. Can't enumerate all soft dependencies.
2014-09-15 17:06:26 -04:00
# Example: Plans needs to come after everything except Programmable, since other Components that add plannable methods need to have done so by the time Plans is attached.
2014-01-03 13:32:13 -05:00
# Example: Collides doesn't depend on Allied, but if both exist, Collides must come after Allied. Soft dependency example. Can't just figure out a proper priority to take care of it.
2014-10-18 17:51:43 -04:00
# Example: Moves doesn't depend on Acts, but if both exist, Moves must come after Acts. Another soft dependency example.
2014-01-03 13:32:13 -05:00
# Decision? Just special case the sort logic in here until we have more examples than these two and decide how best to handle most of the cases then, since we don't really know the whole of the problem yet.
# TODO: anything that depends on Programmable will break right now.
2014-08-25 23:34:46 -04:00
for thang in thangs ? [ ]
2014-09-15 17:06:26 -04:00
programmableLevelComponent = null
plansLevelComponent = null
2014-01-03 13:32:13 -05:00
sorted = [ ]
visit = (c) ->
return if c in sorted
lc = _ . find levelComponents , { original: c . original }
2014-08-12 15:50:41 -04:00
console . error thang . id or thang . name , ' couldn \' t find lc for ' , c , ' of ' , levelComponents unless lc
2014-08-11 19:15:46 -04:00
return unless lc
2014-09-15 17:06:26 -04:00
if lc . name is ' Plans '
# Plans always comes second-to-last, behind Programmable
plansLevelComponent = c
visit c2 for c2 in _ . without thang . components , c , programmableLevelComponent
else if lc . name is ' Programmable '
2014-01-03 13:32:13 -05:00
# Programmable always comes last
2014-09-15 17:06:26 -04:00
programmableLevelComponent = c
2014-01-03 13:32:13 -05:00
visit c2 for c2 in _ . without thang . components , c
else
for d in lc . dependencies or [ ]
c2 = _ . find thang . components , { original: d . original }
2014-08-22 17:52:35 -04:00
unless c2
dependent = _ . find levelComponents , { original: d . original }
dependent = dependent ? . name or d . original
console . error parentType , thang . id or thang . name , ' does not have dependent Component ' , dependent , ' from ' , lc . name
2014-07-13 12:31:31 -04:00
visit c2 if c2
2014-06-30 22:16:26 -04:00
if lc . name is ' Collides '
2014-10-18 17:51:43 -04:00
if allied = _ . find levelComponents , { name: ' Allied ' }
allied = _ . find ( thang . components , { original: allied . original } )
visit allied if allied
if lc . name is ' Moves '
if acts = _ . find levelComponents , { name: ' Acts ' }
acts = _ . find ( thang . components , { original: acts . original } )
visit acts if acts
2014-06-30 22:16:26 -04:00
#console.log thang.id, 'sorted comps adding', lc.name
2014-01-03 13:32:13 -05:00
sorted . push c
for comp in thang . components
visit comp
thang.components = sorted
fillInDefaultComponentConfiguration: (thangs, levelComponents) ->
2014-09-16 20:31:00 -04:00
# This is slow, so I inserted some optimizations to speed it up by caching the eventual defaults of commonly-used Components.
@ defaultComponentConfigurations ? = { }
cached = 0
missed = 0
cachedConfigs = 0
2014-08-25 23:34:46 -04:00
for thang in thangs ? [ ]
2014-01-03 13:32:13 -05:00
for component in thang . components or [ ]
2014-09-16 20:31:00 -04:00
isPhysical = component . original is LevelComponent . PhysicalID
if not isPhysical and defaultConfiguration = _ . find @ defaultComponentConfigurations [ component . original ] , ( (d) -> _ . isEqual component , d . originalComponent )
component.config = defaultConfiguration . defaultedConfig
++ cached
continue
2014-01-03 13:32:13 -05:00
continue unless lc = _ . find levelComponents , { original: component . original }
2014-09-16 20:31:00 -04:00
unless isPhysical
originalComponent = $ . extend true , { } , component
2014-01-03 13:32:13 -05:00
component . config ? = { }
2014-09-16 23:19:52 -04:00
TreemaUtils . populateDefaults ( component . config , lc . configSchema ? { } , tv4 )
2014-08-28 20:57:39 -04:00
@lastType = ' component '
@lastOriginal = component . original
2014-09-16 20:31:00 -04:00
unless isPhysical
@ defaultComponentConfigurations [ component . original ] ? = [ ]
@ defaultComponentConfigurations [ component . original ] . push originalComponent: originalComponent , defaultedConfig: component . config
++ cachedConfigs
++ missed
#console.log 'cached', cached, 'missed', missed
2014-01-03 13:32:13 -05:00
fillInDefaultSystemConfiguration: (levelSystems) ->
for system in levelSystems ? [ ]
system . config ? = { }
2014-09-02 15:33:34 -04:00
TreemaUtils . populateDefaults ( system . config , system . model . configSchema , tv4 )
2014-08-28 20:57:39 -04:00
@lastType = ' system '
@lastOriginal = system . model . name
2014-08-29 20:18:03 -04:00
2014-01-03 13:32:13 -05:00
dimensions: ->
width = 0
height = 0
for thang in @ get ( ' thangs ' ) or [ ]
for component in thang . components
c = component . config
continue unless c ?
width = c . width if c . width ? and c . width > width
height = c . height if c . height ? and c . height > height
2014-06-30 22:16:26 -04:00
return { width: width , height: height }