Elevator and stairs (JACL example)

From IFWiki

Revision as of 07:48, 27 January 2008 by StuartAllen (talk | contribs) (missing word in text.)

(This sample can be played online.)

The guy who did the Inform example wasn't wrong...

#!../bin/jacl

attribute OUTSIDE_LIFT

variable FLOOR_MAPPING ground_floor first_floor second_floor

{+intro
clear
write "^^<h2>Elevator and stairs example</h2>^^"
look
}

{+eachturn
execute "+lift_action"
}

location ground_floor : ground floor
 short		the "ground floor"
 has		OUTSIDE_LIFT
 up		first_floor

{look
write "<p>You are on the ground floor. The "
if here(north) != nowhere
   write "open "
else
   write "closed "
endif
write "door door to an elevator is to the north and a set of stairs lead up. "
write "Beside the door is a call button.^"
}

{movement
break +door_warning
break false
}

object stairs : stairs
 mass		scenery

{examine
if here = ground_floor
   write "<p>The go stairs up.^"
endif
if here = first_floor
   write "<p>The go stairs up and down.^"
endif
if here = second_floor
   write "<p>The go stairs down.^"
endif
}

{take
if here = first_floor
   write "<p>Do you want to take the stairs up, or take the stairs down?^"
endif
if here = ground_floor
   proxy "up"
endif
if here = second_floor
   proxy "down"
endif
}

{take_up
proxy "up"
}

{take_down
proxy "down"
}

grammar take *here up >take_up

{+take_up
write "<p>You can't take " noun1{the} " up.^"
}

grammar take *here down >take_down

{+take_down
write "<p>You can't take " noun1{the} " down.^"
}

object call_button_g: call button
 short		a "call button"
 floor		0

{examine
execute +examine_call_button<this
}

{press : move
execute +press_call_button<this
}

location lift : lift
 short		the "elevator"

{look
write "<p>You are inside the elevator. The door to the south is "
if here(south) != nowhere
   write "open, "
else
   write "closed, "
endif
write "and there is the usual panel of buttons beside it.^"
}

{movement
if COMPASS = south : COMPASS = out
   if destination = nowhere
      write "<p>The lift doors are currently closed.^"
      break
endall
if destination = false
   write "<p>The only way out of the lift is to the west.^"
   break
endif
break false
}
 
object c_button: east eastern close door button control 1
 short		a "close doors button"

{press : move
if lift_obj(turns_till_doors_close) = 0
   write "<p>The doors are already closed.^"
   set TIME = false
   break
endif
write "<p>The lift doors slide closed.^"
execute "+lift_close"
}

object o_button: east eastern open door button control 1
 short		an "open doors button"

{press : move
if lift_obj(turns_till_doors_close) > 0
   write "<p>The doors are already open.^"
   set TIME = false
   break
endif
if lift_obj(moving_to) != arrived
   write "<p>The lift doors refuse to open while the lift is in motion.^"
   break
endif
write "<p>The lift doors slide open.^"
execute "+lift_open"
}

{+floor_button_press
if lift_obj(moving_to) != arrived
   write "<p>The button lights as you press it, only to go off again when you "
   write "release it.^"
   break
endif
if lift_obj(floor) = arg[0](floor)
   if here(out) = nowhere
      write "<p>The lift doors slide open.^"
      execute "+lift_open"
      break
   endif
   write "<p>The button lights as you press it, only to go off again when you "
   write "release it.^"
   break
endall
set lift_obj(moving_to) = arg[0](floor)
ensure arg[0] has ON
write "<p>The button lights up as you press it.^"
}

object g_button: ground floor button g
 short		a "ground floor button"
 floor		0

{press : move
execute +floor_button_press<self
}

object 1_button: first floor button 1
 short		a "first floor button"
 floor		1

{press : move
execute +floor_button_press<self
}

object 2_button: second floor button 2
 short		a "second floor button"
 floor		2

{press : move
execute +floor_button_press<self
}

object panel : panel of buttons
 short		a "panel of buttons"

{examine
write "<p>The panel consists of five buttons labelled ground, one, two, "
write "open and close. "
if g_button has ON
   write "The button for the ground floor is currently lit."
endif
if 1_button has ON
   write "The button for the first floor is currently lit."
endif
if 2_button has ON
   write "The button for the second floor is currently lit."
endif
write ^
execute examine_display
}

string led_display	G 1 2

object display : led display
 short		an "LED display"

{examine
write "<p>Above the panel is an LED display with a large red '"
write led_display[lift_obj(floor)] "'.^"
}

# Elements of lift object:
constant floor						5
constant moving_to					6
	constant arrived				42; POSSIBLE VALUE OF moving_to
constant turns_till_doors_close 	7
constant delta						8

object lift_obj: lift door doors
 short		name "east lift"
 moving_to	arrived

{examine
if lift_obj(turns_till_doors_close) > 0;
   write "<p>The lift doors are open.^"
else
   write "<p>The lift doors are closed.^"
endif
}

{enter
if here = lift
   write "<p>You are already in the lift.^"
   set TIME = false
   break
endif
proxy "east"
}

{open
if lift_obj(turns_till_doors_close) > 0;
   write "<p>The lift doors are already open.^"
   break
endif
if here = lift
   write "<p>(by pressing the open doors button)^"
   proxy "press open doors button"
   break
endif
write "<p>(by pressing the call button)^"
proxy "press call button"
}

{close
if lift_obj(turns_till_doors_close) = 0;
   write "<p>The lift doors are already closed.^"
   break
endif
if here = lift
   write "<p>(by pressing the close doors button)^"
   proxy "press close doors button"
   break
endif
write "<p>There is no way to close the lift doors from the outside.^"
}

location first_floor : first floor
 short		the "first floor"
 has		OUTSIDE_LIFT
 down		ground_floor
 up		second_floor

{look
write "<p>You are on the first floor. The "
if here(north) != nowhere
   write "open "
else
   write "closed "
endif
write "door door to an elevator is to the north and a set of stairs lead up "
write "and down from here. "
write "Beside the door is a call button.^"
}

{movement
break +door_warning
break false
}

object call_button_1: call button
 short		a "call button"
 floor		1

{examine
execute +examine_call_button<this
}

{press : move
execute +press_call_button<this
}

location second_floor : second floor
 short		the "second floor"
 has		OUTSIDE_LIFT
 down		first_floor

{look
write "<p>You are on the second floor. The "
if here(north) != nowhere
   write "open "
else
   write "closed "
endif
write "door door to an elevator is to the north and a set of stairs lead "
write "down. Beside the door is a call button.^"
}

{movement
break +door_warning
break false
}

object call_button_2: call button
 short		a "call button"
 floor		2

{examine
execute +examine_call_button<this
}

{press : move
execute +press_call_button<this
}

{+lift_close
set lift_obj(turns_till_doors_close) = 0
set noun3 = FLOOR_MAPPING[lift_obj(floor)]
set lift(south) = nowhere
set lift(out) = nowhere
set lift(south) = nowhere
set lift(out) = nowhere
set noun3(in) = nowhere
set noun3(north) = nowhere
}

{+lift_open
set lift_obj(turns_till_doors_close) = 3;
set noun3 = FLOOR_MAPPING[lift_obj(floor)]
set lift(south) = noun3
set lift(out) = noun3
set noun3(in) = lift
set noun3(north) = lift
}

{+lift_action
if here = lift
   move lift_obj to here
endif
if here has OUTSIDE_LIFT
   move stairs to here
   move lift_obj to here
endif
if lift_obj(turns_till_doors_close) > 0
   # THE LIFT DOORS ARE OPEN...
   set lift_obj(turns_till_doors_close) - 1
   if lift_obj(turns_till_doors_close) = 0
      if here = lift : here = FLOOR_MAPPING[lift_obj(floor)]
         write "<p>^The lift doors slide closed.^"
       endif
      execute "+lift_close"
   endif
   break # THE LIFT CAN'T DO ANYTHING ELSE UNTIL THE DOORS HAVE CLOSED
endif
if lift_obj(moving_to) != arrived
   if lift_obj(moving_to) != lift_obj(floor)
      # THE LIFT IS STILL ON ITS WAY TO SOMEWHERE
      set lift_obj(delta) = lift_obj(moving_to)
      set lift_obj(delta) - lift_obj(floor)
      if lift_obj(delta) < 0 
         set lift_obj(floor) - 1
      else
         set lift_obj(floor) + 1
      endif
      if here = lift
         write "<p>^The lift vibrates as it slowly "
         if lift_obj(delta) < 0
            write "descends.^"
         else
            write "ascends.^"
         endif
      endif
      break
   endif
   set lift_obj(moving_to) = arrived
   if here = FLOOR_MAPPING[lift_obj(floor)]
      write "<p>^A crisp-sounding bell rings briefly and the doors "
      write "to the lift slide open.^"
   endif
   if here = lift
      write "<p>^Having arrived at the selected floor, the lift "
      write "gently comes to a halt and the doors open.^" 
   endif
   execute "+lift_open"
   execute "+disable_call_buttons"
endif
}

{+press_call_button
if arg[0] has ON
   write "<p>The call button has already been pressed.^"
   set TIME = false
   break
endif
if lift_obj(floor) = arg[0](floor)
   if lift_obj(turns_till_doors_close) > 0
      write "<p>There is already a lift here.^"
      set TIME = false
      break
   endif
   write "<p>A crisp-sounding bell rings briefly and the doors "
   write "to the lift slide open.^"
   execute "+lift_open"
   ensure arg[0] hasnt ON
   break
endif   
write "<p>As you press the call button it lights up.^"
ensure arg[0] has ON
set lift_obj(moving_to) = arg[0](floor)
}

{+examine_call_button
write "<p>Pushing this button summons the elevator to this floor. It's "
write "currently "
if arg[0] has ON
   write "lit.^"
   break
endif
write "dark.^"
}

{+disable_call_buttons
ensure g_button hasnt ON
ensure 1_button hasnt ON
ensure 2_button hasnt ON
ensure call_button_g hasnt ON
ensure call_button_1 hasnt ON
ensure call_button_2 hasnt ON
}

{+door_warning
if COMPASS = north : COMPASS = in
   if destination = false
       write "<p>The lift doors are closed.^"
       break
   endif
endif
break false
}

object kryten: myself self me
 has		ANIMATE
 short		name "yourself"
 quantity	50
 parent		ground_floor
 player

location prologue : prologue
 short		name "Game Over"

{look
write "<p>The game is over. Type <b>restart</b> or <b>restore</b>. "
write "A <b>restore</b> command can optionally be followed by a filename.</p>^"
}

{+header
write "Content-type: text/html"
write "Expire: -1^^"
write "<HTML><HEAD>
write "<TITLE></TITLE>"
execute "+styles"
write "<script language=~JavaScript~>"
write "function putFocus(formInst, elementInst) {"
write "if (document.forms.length > 0) {"
write "document.forms[formInst].elements[elementInst].focus();}}"
write "</script>"
write "</HEAD><BODY bgcolor=~gray~ onLoad=~putFocus(0,0);~>"
write "<FORM NAME=~GameForm~ METHOD=get>"
}

{+footer
write "<hr><center>"
prompt
execute "+exits"
execute "+score"
write "</center>"
write "</FORM>"
write "</BODY></HTML>"
}

{+styles
write "<style>"
write "  <!--"
write "  P { 		font-family: Helvetica, Arial, Sanserif; "
write "			color: white; font-size: 14pt}"
write "  A { 		font-family: Helvetica, Arial, Sanserif; "
write "			color: yellow; font-size: 14pt}"
write "  H1 { 		font-family: Helvetica, Arial, Sanserif; "
write "			color: white; font-size: 24pt}"
write "  H2 { 		font-family: Helvetica, Arial, Sanserif; "
write "			color: white; font-size: 18pt}"
write "  -->"
write "</style>"
}

#include "verbs.library"