2016-01-15 03:06:03 +02:00
|
|
|
# I confess that this world model heavily borrows from INSTEAD engine. - A.Y.
|
|
|
|
|
2016-02-01 09:22:23 +02:00
|
|
|
require('./salet.coffee')
|
2016-01-15 03:06:03 +02:00
|
|
|
obj = require('./obj.coffee')
|
|
|
|
markdown = require('./markdown.coffee')
|
2016-01-19 05:03:01 +02:00
|
|
|
cycle = require('./cycle.coffee')
|
2016-01-22 17:07:34 +02:00
|
|
|
|
2016-02-01 09:22:23 +02:00
|
|
|
assert = (msg, assertion) -> console.assert assertion, msg
|
2016-01-15 03:06:03 +02:00
|
|
|
|
2016-02-01 10:51:48 +02:00
|
|
|
Function.prototype.fcall = Function.prototype.call;
|
|
|
|
Boolean.prototype.fcall = () ->
|
|
|
|
return this
|
|
|
|
String.prototype.fcall = () ->
|
|
|
|
return this
|
|
|
|
|
2016-01-15 03:06:03 +02:00
|
|
|
way_to = (content, ref) ->
|
|
|
|
return "<a href='#{ref}' class='way' id='waylink-#{ref}'>#{content}</a>"
|
|
|
|
|
|
|
|
Array::remove = (e) -> @[t..t] = [] if (t = @indexOf(e)) > -1
|
|
|
|
|
2016-01-24 09:53:03 +02:00
|
|
|
class SaletRoom
|
2016-01-15 03:06:03 +02:00
|
|
|
constructor: (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-19 05:03:01 +02:00
|
|
|
visited: 0
|
2016-01-15 17:29:15 +02:00
|
|
|
title: "Room"
|
2016-01-16 18:24:23 +02:00
|
|
|
objects: {}
|
2016-01-24 08:33:39 +02:00
|
|
|
|
2016-01-20 01:56:10 +02:00
|
|
|
# room illustration image, VN-style. Can be a GIF or WEBM. Can be a function.
|
|
|
|
pic: false
|
|
|
|
|
2016-01-22 17:07:34 +02:00
|
|
|
canView: true
|
|
|
|
canChoose: true
|
|
|
|
priority: 1
|
|
|
|
displayOrder: 1
|
|
|
|
tags: []
|
|
|
|
choices: ""
|
|
|
|
optionText: "Choice"
|
|
|
|
|
2016-01-20 01:56:10 +02:00
|
|
|
dsc: false # room description
|
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
|
|
|
|
2016-02-01 09:22:23 +02:00
|
|
|
entering: (system, from) =>
|
2016-01-22 17:07:34 +02:00
|
|
|
|
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-02-01 09:22:23 +02:00
|
|
|
exit: (system, to) =>
|
2016-01-15 10:10:12 +02:00
|
|
|
return true
|
2016-01-18 12:29:14 +02:00
|
|
|
|
2016-01-15 10:10:12 +02:00
|
|
|
###
|
|
|
|
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-02-01 09:22:23 +02:00
|
|
|
enter: (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-25 18:05:32 +02:00
|
|
|
entering: (system, f) =>
|
2016-01-15 16:04:09 +02:00
|
|
|
if @clear and f?
|
2016-01-25 18:05:32 +02:00
|
|
|
system.view.clearContent()
|
2016-01-15 10:10:12 +02:00
|
|
|
|
2016-01-15 03:06:03 +02:00
|
|
|
if f != @name and f?
|
|
|
|
@visited++
|
2016-01-25 18:05:32 +02:00
|
|
|
if system.rooms[f].exit?
|
|
|
|
system.rooms[f].exit system, @name
|
2016-01-18 12:29:14 +02:00
|
|
|
|
2016-01-15 10:10:12 +02:00
|
|
|
if @enter
|
2016-01-25 18:05:32 +02:00
|
|
|
@enter system, f
|
2016-01-15 03:06:03 +02:00
|
|
|
|
2016-01-25 18:05:32 +02:00
|
|
|
room_content = ""
|
2016-01-15 03:06:03 +02:00
|
|
|
if not @extendSection
|
|
|
|
classes = if @classes then ' ' + @classes.join(' ') else ''
|
2016-01-25 18:05:32 +02:00
|
|
|
room = document.getElementById('current-room')
|
|
|
|
if room?
|
|
|
|
room.removeAttribute('id')
|
2016-01-15 16:04:09 +02:00
|
|
|
# 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.
|
2016-01-25 18:05:32 +02:00
|
|
|
room_content = "<section id='current-room' data-room='#{@name}' class='room-#{@name}#{classes}'>"
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
if f != @name and @before?
|
2016-01-25 18:05:32 +02:00
|
|
|
room_content += markdown(@before.fcall(this, system, f))
|
2016-01-15 03:06:03 +02:00
|
|
|
|
2016-01-25 18:05:32 +02:00
|
|
|
room_content += @look system, f
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
if f != @name and @after?
|
2016-01-25 18:05:32 +02:00
|
|
|
room_content += markdown(@after.fcall(this, system, f))
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
if not @extendSection
|
2016-01-25 18:05:32 +02:00
|
|
|
room_content += "</section>"
|
2016-01-15 16:04:09 +02:00
|
|
|
|
2016-02-01 09:22:23 +02:00
|
|
|
system.view.write(room_content)
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
if @choices
|
2016-02-01 09:22:23 +02:00
|
|
|
system.view.writeChoices(system, system.getSituationIdChoices(@choices, @maxChoices))
|
2016-01-15 03:06:03 +02:00
|
|
|
|
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-02-01 09:22:23 +02:00
|
|
|
look: (system, f) =>
|
|
|
|
system.view.updateWays(system, @ways, @name)
|
2016-01-15 16:04:09 +02:00
|
|
|
retval = ""
|
2016-01-15 03:06:03 +02:00
|
|
|
|
2016-01-20 01:56:10 +02:00
|
|
|
if @pic
|
2016-02-01 09:22:23 +02:00
|
|
|
retval += '<div class="pic">'+system.view.pictureTag(@pic.fcall(this, system, f))+'</div>'
|
2016-01-20 01:56:10 +02:00
|
|
|
|
2016-01-15 03:06:03 +02:00
|
|
|
# Print the room description
|
2016-01-18 12:29:14 +02:00
|
|
|
if @dsc
|
2016-02-01 10:51:48 +02:00
|
|
|
dsc = @dsc.fcall(this, system, f).toString()
|
|
|
|
retval += markdown(dsc)
|
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.
|
2016-01-24 09:53:03 +02:00
|
|
|
salet.rooms["start"].objects = {}
|
2016-01-16 18:24:23 +02:00
|
|
|
|
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-02-01 09:22:23 +02:00
|
|
|
act: (system, action) =>
|
2016-01-19 05:03:01 +02:00
|
|
|
if (link = action.match(/^_(act|cycle)_(.+)$/)) #object action
|
2016-01-16 18:24:23 +02:00
|
|
|
for name, thing of @objects
|
2016-01-19 05:03:01 +02:00
|
|
|
if name == link[2]
|
|
|
|
if link[1] == "act"
|
|
|
|
# If it's takeable, the player can take this object.
|
|
|
|
# If not, we check the "act" function.
|
|
|
|
if thing.takeable
|
2016-02-01 10:51:48 +02:00
|
|
|
system.character.inventory.push thing
|
2016-01-19 05:03:01 +02:00
|
|
|
@drop name
|
2016-02-01 10:51:48 +02:00
|
|
|
system.view.clearContent()
|
|
|
|
@entering.fcall(this, system, @name)
|
|
|
|
return system.view.write(thing.take.fcall(thing, system).toString())
|
2016-01-19 05:03:01 +02:00
|
|
|
if thing.act
|
2016-02-01 10:51:48 +02:00
|
|
|
return system.view.write(thing.act.fcall(thing, system).toString())
|
2016-01-19 05:03:01 +02:00
|
|
|
elseif link[1] == "cycle"
|
|
|
|
# TODO object cyclewriter
|
2016-01-15 03:06:03 +02:00
|
|
|
# the loop is done but no return came - match not found
|
|
|
|
console.error("Could not find #{link[1]} in current room.")
|
2016-01-18 12:29:14 +02:00
|
|
|
|
2016-01-19 05:03:01 +02:00
|
|
|
# we're done with objects, now check the regular actions
|
|
|
|
actionClass = action.match(/^_(\w+)_(.+)$/)
|
|
|
|
that = this
|
|
|
|
|
|
|
|
responses = {
|
|
|
|
writer: (ref) ->
|
2016-02-01 09:22:23 +02:00
|
|
|
content = that.writers[ref].fcall(that, system, action)
|
2016-01-19 05:03:01 +02:00
|
|
|
output = markdown(content)
|
2016-02-01 10:51:48 +02:00
|
|
|
system.view.write(output)
|
2016-01-19 05:03:01 +02:00
|
|
|
replacer: (ref) ->
|
2016-02-01 09:22:23 +02:00
|
|
|
content = that.writers[ref].fcall(that, system, action)
|
2016-02-01 12:10:34 +02:00
|
|
|
system.view.replace(content, '#'+ref)
|
2016-01-19 05:03:01 +02:00
|
|
|
inserter: (ref) ->
|
2016-02-01 09:22:23 +02:00
|
|
|
content = that.writers[ref].fcall(that, system, action)
|
2016-01-19 05:03:01 +02:00
|
|
|
output = markdown(content)
|
2016-02-01 10:51:48 +02:00
|
|
|
system.view.write(output, '#'+ref)
|
2016-01-19 05:03:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (actionClass)
|
|
|
|
# Matched a special action class
|
|
|
|
[responder, ref] = [actionClass[1], actionClass[2]]
|
|
|
|
|
|
|
|
if(!@writers.hasOwnProperty(actionClass[2]))
|
|
|
|
throw new Error("Tried to call undefined writer: #{action}");
|
|
|
|
responses[responder](ref);
|
|
|
|
else if (@actions.hasOwnProperty(action))
|
2016-02-01 09:22:23 +02:00
|
|
|
@actions[action].call(this, system, action);
|
2016-01-19 05:03:01 +02:00
|
|
|
else
|
|
|
|
throw new Error("Tried to call undefined action: #{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)
|
|
|
|
|
2016-02-01 09:22:23 +02:00
|
|
|
register: (salet) =>
|
2016-01-19 05:03:01 +02:00
|
|
|
if not @name?
|
|
|
|
console.error("Situation has no name")
|
|
|
|
return this
|
2016-01-24 09:53:03 +02:00
|
|
|
salet.rooms[@name] = this
|
2016-01-19 05:03:01 +02:00
|
|
|
return this
|
|
|
|
|
|
|
|
writers:
|
2016-02-01 10:51:48 +02:00
|
|
|
cyclewriter: (salet) ->
|
|
|
|
cycle(this.cycle, this.name, salet.character)
|
2016-01-19 05:03:01 +02:00
|
|
|
|
2016-02-01 09:22:23 +02:00
|
|
|
room = (name, salet, spec) ->
|
2016-01-16 18:24:23 +02:00
|
|
|
spec ?= {}
|
|
|
|
spec.name = name
|
2016-02-01 09:22:23 +02:00
|
|
|
return new SaletRoom(spec).register(salet)
|
2016-01-15 03:06:03 +02:00
|
|
|
|
|
|
|
module.exports = room
|