Elevator and stairs (JACL example): Difference between revisions

From IFWiki

m (Changed code to use new 1.14 feature.)
No edit summary
Line 1: Line 1:
(This sample can be [http://jacl.game-host.org:8080/fastcgi-bin/elevator.jacl played online].)
The guy who did the Inform example wasn't wrong...
The guy who did the Inform example wasn't wrong...


Line 15: Line 13:
{+intro
{+intro
clear
clear
write "^^<h2>Elevator and stairs example</h2>^^"
write "^^Elevator and stairs example^^"
look
look
}
}
Line 29: Line 27:


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


Line 49: Line 49:
{examine
{examine
if here = ground_floor
if here = ground_floor
   write "<p>The stairs go up.^"
   write "The stairs go up.^"
endif
endif
if here = first_floor
if here = first_floor
   write "<p>The go stairs up and down.^"
   write "The go stairs up and down.^"
endif
endif
if here = second_floor
if here = second_floor
   write "<p>The go stairs down.^"
   write "The go stairs down.^"
endif
endif
}
}
Line 61: Line 61:
{take
{take
if here = first_floor
if here = first_floor
   write "<p>Do you want to take the stairs up, or take the stairs down?^"
   write "Do you want to take the stairs up, or take the stairs down?^"
endif
endif
if here = ground_floor
if here = ground_floor
Line 82: Line 82:


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


Line 88: Line 88:


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


Line 96: Line 96:


{examine : *examine_callbutton1 : *examine_callbutton2
{examine : *examine_callbutton1 : *examine_callbutton2
write "<p>Pushing this button summons the elevator to this floor. It's "
write "Pushing this button summons the elevator to this floor. It's currently "
write "currently "
if self has ON
if self has ON
   write "lit.^"
   write "lit.^"
Line 107: Line 106:
{press : *press_callbutton1 : *press_callbutton2
{press : *press_callbutton1 : *press_callbutton2
if lift_obj(moving_to) != arrived
if lift_obj(moving_to) != arrived
   write "<p>The button lights as you press it, only to go off again when you "
   write "The button lights as you press it, only to go off again when you release it.^"
  write "release it.^"
   break
   break
endif
endif
if lift_obj(floor) = self(floor)
if lift_obj(floor) = self(floor)
   if here(out) = nowhere
   if here(out) = nowhere
       write "<p>The lift doors slide open.^"
       write "The lift doors slide open.^"
       execute "+lift_open"
       execute "+lift_open"
       break
       break
   endif
   endif
   write "<p>The button lights as you press it, only to go off again when you "
   write "The button lights as you press it, only to go off again when you "
   write "release it.^"
   write "release it.^"
   break
   break
Line 123: Line 121:
set lift_obj(moving_to) = self(floor)
set lift_obj(moving_to) = self(floor)
ensure self has ON
ensure self has ON
write "<p>The button lights up as you press it.^"
write "The button lights up as you press it.^"
}
}


Line 130: Line 128:


{look
{look
write "<p>You are inside the elevator. The door to the south is "
write "You are inside the elevator. The door to the south is "
if here(south) != nowhere
if here(south) != nowhere
   write "open, "
   write "open, "
Line 142: Line 140:
if COMPASS = south : COMPASS = out
if COMPASS = south : COMPASS = out
   if destination = nowhere
   if destination = nowhere
       write "<p>The lift doors are currently closed.^"
       write "The lift doors are currently closed.^"
       break
       break
endall
endall
if destination = false
if destination = false
   write "<p>The only way out of the lift is to the west.^"
   write "The only way out of the lift is to the west.^"
   break
   break
endif
endif
Line 157: Line 155:
{press : move
{press : move
if lift_obj(turns_till_doors_close) = 0
if lift_obj(turns_till_doors_close) = 0
   write "<p>The doors are already closed.^"
   write "The doors are already closed.^"
   set TIME = false
   set TIME = false
   break
   break
endif
endif
write "<p>The lift doors slide closed.^"
write "The lift doors slide closed.^"
execute "+lift_close"
execute "+lift_close"
}
}
Line 170: Line 168:
{press : move
{press : move
if lift_obj(turns_till_doors_close) > 0
if lift_obj(turns_till_doors_close) > 0
   write "<p>The doors are already open.^"
   write "The doors are already open.^"
   set TIME = false
   set TIME = false
   break
   break
endif
endif
if lift_obj(moving_to) != arrived
if lift_obj(moving_to) != arrived
   write "<p>The lift doors refuse to open while the lift is in motion.^"
   write "The lift doors refuse to open while the lift is in motion.^"
   break
   break
endif
endif
write "<p>The lift doors slide open.^"
write "The lift doors slide open.^"
execute "+lift_open"
execute "+lift_open"
}
}
Line 210: Line 208:


{examine
{examine
write "<p>The panel consists of five buttons labelled ground, one, two, "
write "The panel consists of five buttons labelled ground, one, two, "
write "open and close. "
write "open and close. "
if gbutton has ON
if gbutton has ON
Line 231: Line 229:


{examine
{examine
write "<p>Above the panel is an LED display with a large red '"
write "Above the panel is an LED display with a large red '"
write led_display[lift_obj(floor)] "'.^"
write led_display[lift_obj(floor)] "'.^"
}
}
Line 238: Line 236:
constant floor 5
constant floor 5
constant moving_to 6
constant moving_to 6
constant arrived 42; POSSIBLE VALUE OF moving_to
constant arrived         42; POSSIBLE VALUE OF moving_to
constant turns_till_doors_close 7
constant turns_till_doors_close                 7
constant delta 8
constant delta 8


Line 248: Line 246:
{examine
{examine
if lift_obj(turns_till_doors_close) > 0;
if lift_obj(turns_till_doors_close) > 0;
   write "<p>The lift doors are open.^"
   write "The lift doors are open.^"
else
else
   write "<p>The lift doors are closed.^"
   write "The lift doors are closed.^"
endif
endif
}
}
Line 256: Line 254:
{enter
{enter
if here = lift
if here = lift
   write "<p>You are already in the lift.^"
   write "You are already in the lift.^"
   set TIME = false
   set TIME = false
   break
   break
Line 265: Line 263:
{open
{open
if lift_obj(turns_till_doors_close) > 0;
if lift_obj(turns_till_doors_close) > 0;
   write "<p>The lift doors are already open.^"
   write "The lift doors are already open.^"
   break
   break
endif
endif
if here = lift
if here = lift
   write "<p>(by pressing the open doors button)^"
   write "(by pressing the open doors button)^"
   proxy "press open doors button"
   proxy "press open doors button"
   break
   break
endif
endif
write "<p>(by pressing the call button)^"
write "(by pressing the call button)^"
proxy "press call button"
proxy "press call button"
}
}
Line 279: Line 277:
{close
{close
if lift_obj(turns_till_doors_close) = 0;
if lift_obj(turns_till_doors_close) = 0;
   write "<p>The lift doors are already closed.^"
   write "The lift doors are already closed.^"
   break
   break
endif
endif
if here = lift
if here = lift
   write "<p>(by pressing the close doors button)^"
   write "(by pressing the close doors button)^"
   proxy "press close doors button"
   proxy "press close doors button"
   break
   break
endif
endif
write "<p>There is no way to close the lift doors from the outside.^"
write "There is no way to close the lift doors from the outside.^"
}
}


Line 297: Line 295:


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


Line 323: Line 322:


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


Line 375: Line 376:
   if lift_obj(turns_till_doors_close) = 0
   if lift_obj(turns_till_doors_close) = 0
       if here = lift : here = FLOOR_MAPPING[lift_obj(floor)]
       if here = lift : here = FLOOR_MAPPING[lift_obj(floor)]
         write "<p>^The lift doors slide closed.^"
         write "^The lift doors slide closed.^"
       endif
       endif
       execute "+lift_close"
       execute "+lift_close"
Line 392: Line 393:
       endif
       endif
       if here = lift
       if here = lift
         write "<p>^The lift vibrates as it slowly "
         write "^The lift vibrates as it slowly "
         if lift_obj(delta) < 0
         if lift_obj(delta) < 0
             write "descends.^"
             write "descends.^"
Line 403: Line 404:
   set lift_obj(moving_to) = arrived
   set lift_obj(moving_to) = arrived
   if here = FLOOR_MAPPING[lift_obj(floor)]
   if here = FLOOR_MAPPING[lift_obj(floor)]
       write "<p>^A crisp-sounding bell rings briefly and the doors "
       write "^A crisp-sounding bell rings briefly and the doors "
       write "to the lift slide open.^"
       write "to the lift slide open.^"
   endif
   endif
   if here = lift
   if here = lift
       write "<p>^Having arrived at the selected floor, the lift "
       write "^Having arrived at the selected floor, the lift "
       write "gently comes to a halt and the doors open.^"  
       write "gently comes to a halt and the doors open.^"  
   endif
   endif
Line 417: Line 418:
{+press_callbutton
{+press_callbutton
if arg[0] has ON
if arg[0] has ON
   write "<p>The call button has already been pressed.^"
   write "The call button has already been pressed.^"
   set TIME = false
   set TIME = false
   break
   break
Line 423: Line 424:
if lift_obj(floor) = arg[0](floor)
if lift_obj(floor) = arg[0](floor)
   if lift_obj(turns_till_doors_close) > 0
   if lift_obj(turns_till_doors_close) > 0
       write "<p>There is already a lift here.^"
       write "There is already a lift here.^"
       set TIME = false
       set TIME = false
       break
       break
   endif
   endif
   write "<p>A crisp-sounding bell rings briefly and the doors "
   write "A crisp-sounding bell rings briefly and the doors "
   write "to the lift slide open.^"
   write "to the lift slide open.^"
   execute "+lift_open"
   execute "+lift_open"
Line 463: Line 464:
  parent ground_floor
  parent ground_floor
  player
  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>" GAME_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 "<h1>" GAME_TITLE "</h1>"
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"
#include "verbs.library"

Revision as of 09:18, 11 July 2008

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

#!../bin/jacl

variable	GAME_VERSION	2
string		GAME_TITLE	"Smell Restrictions"

attribute	OUTSIDE_LIFT

variable FLOOR_MAPPING ground_floor first_floor second_floor

{+intro
clear
write "^^Elevator and stairs example^^"
look
}

{+eachturn
execute "+lift_action"
}

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

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

{movement
break +door_warning
break false
}

object stairs : stairs
 mass		scenery

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

{take
if here = first_floor
   write "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 "You can't take " noun1{the} " up.^"
}

grammar take *here down >take_down

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

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

{examine : *examine_callbutton1 : *examine_callbutton2
write "Pushing this button summons the elevator to this floor. It's currently "
if self has ON
   write "lit.^"
   break
endif
write "dark.^"
}

{press : *press_callbutton1 : *press_callbutton2
if lift_obj(moving_to) != arrived
   write "The button lights as you press it, only to go off again when you release it.^"
   break
endif
if lift_obj(floor) = self(floor)
   if here(out) = nowhere
      write "The lift doors slide open.^"
      execute "+lift_open"
      break
   endif
   write "The button lights as you press it, only to go off again when you "
   write "release it.^"
   break
endall
set lift_obj(moving_to) = self(floor)
ensure self has ON
write "The button lights up as you press it.^"
}

location lift : lift
 short		the "elevator"

{look
write "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 "The lift doors are currently closed.^"
      break
endall
if destination = false
   write "The only way out of the lift is to the west.^"
   break
endif
break false
}
 
object cbutton: east eastern close door button control 1
 short		a "close doors button"

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

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

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

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

{press : move
execute +floorbuttonpress<self
}

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

{press : move
execute +floorbuttonpress<self
}

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

{press : move
execute +floorbuttonpress<self
}

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

{examine
write "The panel consists of five buttons labelled ground, one, two, "
write "open and close. "
if gbutton has ON
   write "The button for the ground floor is currently lit."
endif
if 1button has ON
   write "The button for the first floor is currently lit."
endif
if 2button 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 "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 "The lift doors are open.^"
else
   write "The lift doors are closed.^"
endif
}

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

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

{close
if lift_obj(turns_till_doors_close) = 0;
   write "The lift doors are already closed.^"
   break
endif
if here = lift
   write "(by pressing the close doors button)^"
   proxy "press close doors button"
   break
endif
write "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 "<You are on the first floor. The "
if here(north) != nowhere
   write "open "
else
   write "closed "
endif
print:
   door door to an elevator is to the north and a set of stairs lead up
   and down from here. Beside the door is a call button.^
.
}

{movement
break +door_warning
break false
}

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

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

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

{movement
break +door_warning
break false
}

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

{+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 "^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 "^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 "^A crisp-sounding bell rings briefly and the doors "
      write "to the lift slide open.^"
   endif
   if here = lift
      write "^Having arrived at the selected floor, the lift "
      write "gently comes to a halt and the doors open.^" 
   endif
   execute "+lift_open"
   execute "+disable_callbuttons"
endif
}

{+press_callbutton
if arg[0] has ON
   write "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 "There is already a lift here.^"
      set TIME = false
      break
   endif
   write "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)
}

{+disable_callbuttons
ensure gbutton hasnt ON
ensure 1button hasnt ON
ensure 2button hasnt ON
ensure callbuttong hasnt ON
ensure callbutton1 hasnt ON
ensure callbutton2 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

#include "verbs.library"