2016-01-15 03:06:03 +02:00
|
|
|
# I confess that this world model heavily borrows from INSTEAD engine. - A.Y.
|
|
|
|
|
2016-01-15 03:31:04 +02:00
|
|
|
undum = require('./undum.js')
|
2016-01-15 03:06:03 +02:00
|
|
|
RaconteurSituation = require('./situation.coffee')
|
|
|
|
obj = require('./obj.coffee')
|
|
|
|
markdown = require('./markdown.coffee')
|
|
|
|
|
|
|
|
way_to = (content, ref) ->
|
|
|
|
return "<a href='#{ref}' class='way' id='waylink-#{ref}'>#{content}</a>"
|
|
|
|
|
|
|
|
# jQuery was confused by this point where's the context so I did it vanilla-way
|
|
|
|
print = (content) ->
|
2016-01-16 19:04:00 +02:00
|
|
|
if content == ""
|
|
|
|
return
|
2016-01-15 03:06:03 +02:00
|
|
|
if typeof content == "function"
|
|
|
|
content = content()
|
|
|
|
block = document.getElementById("current-situation")
|
|
|
|
if block
|
|
|
|
block.innerHTML = block.innerHTML + markdown(content)
|
2016-01-15 16:04:09 +02:00
|
|
|
else
|
|
|
|
console.error("No current situation found.")
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
Array::remove = (e) -> @[t..t] = [] if (t = @indexOf(e)) > -1
|
|
|
|
|
|
|
|
addClass = (element, className) ->
|
|
|
|
if (element.classList)
|
|
|
|
element.classList.add(className)
|
|
|
|
else
|
|
|
|
element.className += ' ' + className
|
|
|
|
|
2016-01-16 18:24:23 +02:00
|
|
|
cls = (system) ->
|
|
|
|
system.clearContent()
|
|
|
|
system.clearContent("#intro")
|
|
|
|
|
2016-01-15 17:29:15 +02:00
|
|
|
update_ways = (ways, name) ->
|
2016-01-15 03:06:03 +02:00
|
|
|
content = ""
|
|
|
|
distances = []
|
|
|
|
if ways
|
2016-01-15 10:10:12 +02:00
|
|
|
document.querySelector(".ways h2").style.display = "block"
|
2016-01-15 03:06:03 +02:00
|
|
|
for way in ways
|
|
|
|
if undum.game.situations[way]?
|
2016-01-15 17:29:15 +02:00
|
|
|
title = undum.game.situations[way].title.fcall(this, name)
|
2016-01-15 03:06:03 +02:00
|
|
|
content += way_to(title, way)
|
|
|
|
distances.push({
|
|
|
|
key: way
|
|
|
|
distance: undum.game.situations[way].distance
|
|
|
|
})
|
2016-01-15 10:10:12 +02:00
|
|
|
else
|
|
|
|
document.querySelector(".ways h2").style.display = "none"
|
2016-01-15 03:06:03 +02:00
|
|
|
document.getElementById("ways").innerHTML = content
|
|
|
|
min = Infinity
|
|
|
|
min_key = []
|
|
|
|
for node in distances
|
|
|
|
if node.distance < min
|
|
|
|
min = node.distance
|
|
|
|
min_key = [node.key]
|
|
|
|
if node.distance == min
|
|
|
|
min_key.push(node.key)
|
|
|
|
if min < Infinity
|
|
|
|
for node in min_key
|
|
|
|
addClass(document.getElementById("waylink-#{node}"), "destination")
|
|
|
|
|
|
|
|
class SaletRoom extends RaconteurSituation
|
|
|
|
constructor: (spec) ->
|
|
|
|
RaconteurSituation.call(this, spec)
|
2016-01-16 18:24:23 +02:00
|
|
|
for index, value of spec
|
|
|
|
this[index] = value
|
2016-01-15 03:06:03 +02:00
|
|
|
return this
|
2016-01-15 17:29:15 +02:00
|
|
|
title: "Room"
|
2016-01-16 18:24:23 +02:00
|
|
|
objects: {}
|
2016-01-15 16:04:09 +02:00
|
|
|
extendSection: false
|
2016-01-15 03:06:03 +02:00
|
|
|
distance: Infinity # distance to the destination
|
2016-01-15 10:10:12 +02:00
|
|
|
clear: true # clear the screen on entering the room?
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
###
|
|
|
|
I call SaletRoom.exit every time the player exits to another room.
|
2016-01-15 10:10:12 +02:00
|
|
|
Unlike @after this gets called after the section is closed.
|
|
|
|
It's a styling difference.
|
2016-01-15 03:06:03 +02:00
|
|
|
###
|
2016-01-16 18:24:23 +02:00
|
|
|
exit: (character, system, to) =>
|
2016-01-15 10:10:12 +02:00
|
|
|
return true
|
|
|
|
|
|
|
|
###
|
|
|
|
I call SaletRoom.enter every time the player enters this room but before the section is opened.
|
|
|
|
Unlike @before this gets called before the current section is opened.
|
|
|
|
It's a styling difference.
|
|
|
|
|
|
|
|
The upstream Undum version does not allow you to redefine @enter function easily but allows custom @exit one.
|
|
|
|
It was renamed as @entering to achieve API consistency.
|
|
|
|
###
|
2016-01-16 18:24:23 +02:00
|
|
|
enter: (character, system, from) =>
|
2016-01-15 10:10:12 +02:00
|
|
|
return true
|
|
|
|
|
2016-01-15 03:06:03 +02:00
|
|
|
###
|
2016-01-15 10:10:12 +02:00
|
|
|
Salet's Undum version calls Situation.entering every time a situation is entered, and
|
2016-01-15 03:06:03 +02:00
|
|
|
passes it three arguments; The character object, the system object,
|
|
|
|
and a string referencing the previous situation, or null if there is
|
|
|
|
none (ie, for the starting situation).
|
|
|
|
|
|
|
|
My version of `enter` splits the location description from the effects.
|
|
|
|
Also if f == this.name (we're in the same location) the `before` and `after` callbacks are ignored.
|
|
|
|
###
|
2016-01-16 18:24:23 +02:00
|
|
|
entering: (character, system, f) =>
|
2016-01-15 16:04:09 +02:00
|
|
|
if @clear and f?
|
2016-01-16 18:24:23 +02:00
|
|
|
cls(system)
|
2016-01-15 10:10:12 +02:00
|
|
|
|
2016-01-15 03:06:03 +02:00
|
|
|
if f != @name and f?
|
|
|
|
@visited++
|
2016-01-15 10:10:12 +02:00
|
|
|
if undum.game.situations[f].exit?
|
|
|
|
undum.game.situations[f].exit(character, system, @name)
|
|
|
|
|
|
|
|
if @enter
|
|
|
|
@enter character, system, f
|
2016-01-15 03:06:03 +02:00
|
|
|
|
2016-01-15 16:04:09 +02:00
|
|
|
current_situation = ""
|
2016-01-15 03:06:03 +02:00
|
|
|
if not @extendSection
|
|
|
|
classes = if @classes then ' ' + @classes.join(' ') else ''
|
|
|
|
situation = document.getElementById('current-situation')
|
|
|
|
if situation?
|
2016-01-15 16:04:09 +02:00
|
|
|
situation.removeAttribute('id')
|
|
|
|
# Javascript DOM manipulation functions like jQuery's append() or document.createElement
|
|
|
|
# don't work like a typical printLn - they create *DOM nodes*.
|
|
|
|
# You can't leave an unclosed tag just like that. So we have to buffer the output.
|
|
|
|
current_situation = "<section id='current-situation' data-situation='#{@name}' class='situation-#{@name}#{classes}'>"
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
if f != @name and @before?
|
2016-01-15 17:29:15 +02:00
|
|
|
current_situation += markdown(@before.fcall(this, character, system, f))
|
2016-01-15 03:06:03 +02:00
|
|
|
|
2016-01-16 18:24:23 +02:00
|
|
|
current_situation += @look character, system, f
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
if f != @name and @after?
|
2016-01-15 17:29:15 +02:00
|
|
|
current_situation += markdown(@after.fcall(this, character, system, f))
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
if not @extendSection
|
2016-01-15 16:04:09 +02:00
|
|
|
current_situation += "</section>"
|
|
|
|
|
|
|
|
system.write(current_situation)
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
if @choices
|
|
|
|
system.writeChoices(system.getSituationIdChoices(@choices, @minChoices, @maxChoices))
|
|
|
|
|
2016-01-16 18:24:23 +02:00
|
|
|
###
|
|
|
|
An internal function to get the room's description and the descriptions of
|
|
|
|
every object in this room.
|
|
|
|
###
|
2016-01-16 19:04:00 +02:00
|
|
|
look: (character, system, f) ->
|
2016-01-15 17:29:15 +02:00
|
|
|
update_ways(@ways, @name)
|
2016-01-15 16:04:09 +02:00
|
|
|
retval = ""
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
# Print the room description
|
|
|
|
if @content
|
2016-01-15 16:04:09 +02:00
|
|
|
retval += markdown(@content.fcall(this, character, system, f))
|
2016-01-15 03:06:03 +02:00
|
|
|
|
2016-01-16 18:24:23 +02:00
|
|
|
for name, thing of @objects
|
2016-01-15 16:04:09 +02:00
|
|
|
retval += thing.look()
|
|
|
|
|
|
|
|
return retval
|
2016-01-15 03:06:03 +02:00
|
|
|
|
2016-01-16 18:24:23 +02:00
|
|
|
###
|
|
|
|
Puts an object in this room.
|
|
|
|
###
|
|
|
|
take: (thing) =>
|
|
|
|
@objects[thing.name] = thing
|
|
|
|
# BUG: for some really weird reason if the call is made in init function or
|
|
|
|
# during the initialization, this ALSO puts the thing in the start room.
|
|
|
|
undum.game.situations["start"].objects = {}
|
|
|
|
|
2016-01-16 18:43:20 +02:00
|
|
|
drop: (name) =>
|
|
|
|
delete @objects[name]
|
|
|
|
|
2016-01-15 03:06:03 +02:00
|
|
|
###
|
|
|
|
Object action. A function or a string which comes when you click on the object link.
|
|
|
|
You could interpret this as an EXAMINE verb or USE one, it's your call.
|
|
|
|
###
|
2016-01-16 18:24:23 +02:00
|
|
|
act: (character, system, action) =>
|
2016-01-15 03:06:03 +02:00
|
|
|
if (link = action.match(/^_act_(.+)$/)) #object action
|
2016-01-16 18:24:23 +02:00
|
|
|
for name, thing of @objects
|
|
|
|
if name == link[1]
|
2016-01-16 18:43:20 +02:00
|
|
|
# If it's takeable, the player can take this object.
|
2016-01-15 03:06:03 +02:00
|
|
|
# If not, we check the "act" function.
|
2016-01-16 18:43:20 +02:00
|
|
|
if thing.takeable
|
2016-01-15 03:06:03 +02:00
|
|
|
character.sandbox.inventory.push thing
|
2016-01-16 18:43:20 +02:00
|
|
|
@drop name
|
2016-01-16 18:24:23 +02:00
|
|
|
cls(system)
|
|
|
|
@entering.fcall(this, character, system, @name)
|
2016-01-15 03:06:03 +02:00
|
|
|
return print(thing.take.fcall(thing, character, system))
|
|
|
|
if thing.act
|
|
|
|
return print(thing.act.fcall(thing, character, system))
|
|
|
|
# the loop is done but no return came - match not found
|
|
|
|
console.error("Could not find #{link[1]} in current room.")
|
2016-01-15 10:10:12 +02:00
|
|
|
|
|
|
|
# default Raconteur action
|
|
|
|
return RaconteurSituation.prototype.act.call(this, character, system, action)
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
# Marks every room in the game with distance to this room
|
2016-01-16 18:24:23 +02:00
|
|
|
destination: () =>
|
2016-01-15 03:06:03 +02:00
|
|
|
@distance = 0
|
|
|
|
|
|
|
|
candidates = [this]
|
|
|
|
while candidates.length > 0
|
|
|
|
current_room = candidates.shift()
|
|
|
|
if current_room.ways
|
|
|
|
for node in current_room.ways
|
|
|
|
if node.distance == Infinity
|
|
|
|
node.distance = current_room.distance + 1
|
|
|
|
candidates.push(node)
|
|
|
|
|
|
|
|
room = (name, spec) ->
|
2016-01-16 18:24:23 +02:00
|
|
|
spec ?= {}
|
|
|
|
spec.name = name
|
|
|
|
retval = new SaletRoom(spec)
|
2016-01-16 19:04:00 +02:00
|
|
|
retval.register()
|
|
|
|
return retval
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
module.exports = room
|