Text Adventure Game

This script example is taken from a text adventure game. It determines the details of the current room the player has entered:

 var #adv = load(&root/Adventure[Name=#input/AdvName])
 if NotExists(#adv) then
     error "The adventure could not be found"
 end if

 var &advPath = GetPath(#adv)

 var #session = load(&root/Users/Adventure[AdventureName=#input/AdvName])
 if NotExists(#session) then
     set #session = new record for &root/Users/Adventure
     set #session/AdventureName = #input/AdvName
     set #session/CurrentRoomX = #adv/StartingRoom/X
     set #session/CurrentRoomY = #adv/StartingRoom/Y
     insert #session at &root/Users/Adventure
 end if

 // the input is AdvName only

 // output is Room, Desc, ExitsDesc, ExitsDetails, North, East, South, West, Person
 var #room = load(&advPath/Rooms[X=#session/CurrentRoomX, Y=#session/CurrentRoomY])
 var #output = NewRecord()

 if (NotExists(#room)) then
     set #output/Room = "[Room was not found]"
     exit script
 end if

 set #output/Room = #room/Title
 set #output/Desc = #room/Description

 var #roomX = #session/CurrentRoomX
 var #roomY = #session/CurrentRoomY

 set #output/RoomX = #roomX
 set #output/RoomY = #roomY

 var #north = load(&advPath/Rooms[Y=(#roomY - 1), X = #roomX])
 var #south = load(&advPath/Rooms[Y=(#roomY + 1), X = #roomX])
 var #east = load(&advPath/Rooms[Y = #roomY, X = (#roomX + 1)])
 var #west = load(&advPath/Rooms[Y = #roomY, X = (#roomX - 1)])

 if (#room/BlockNorth) then
     set #north = Nothing()
 end if

 if (#room/BlockEast) then
     set #east = Nothing()
 end if

 if (#room/BlockSouth) then
     set #south = Nothing()
 end if

 if (#room/BlockWest) then
     set #west = Nothing()
 end if

 set #output/ExitsDesc = ""
 if Exists(#room/NorthTitle) then
     set #output/ExitsDesc = #output/ExitsDesc + #room/NorthTitle + " "
 end if

 if Exists(#room/EastTitle) then
     set #output/ExitsDesc = #output/ExitsDesc + #room/EastTitle + " "
 end if

 if Exists(#room/SouthTitle) then
     set #output/ExitsDesc = #output/ExitsDesc + #room/SouthTitle + " "
 end if

 if Exists(#room/WestTitle) then
     set #output/ExitsDesc = #output/ExitsDesc + #room/WestTitle + " "
 end if

 set #output/ExitsDesc = #output/ExitsDesc + "You can go "
 var #dirList = NewList()
 if Exists(#north) then
   set #output/North = true
     add "north" to #dirList
 end if
 if Exists(#east) then
     set #output/East = true
     add "east" to #dirList
 end if
 if Exists(#south) then
     set #output/South = true
     add "south" to #dirList
 end if
 if Exists(#west) then
     set #output/West = true
     add "west" to #dirList
 end if

 set #output/ExitsDesc = #output/ExitsDesc + str:Join(#dirList, ",", " or ") + "."

 set #output/PersonList = load(GetPath(#room)/Person)
 set #output/ItemList = load(GetPath(#room)/ItemsInHere)

 var &sessionPath = GetPath(#session)
 // what are we holding?
 var #holding = load(&sessionPath/Holding)
 // remove any items that we have already, or that a person may be holding already
 var #newItemList = NewList()
 loop through #output/ItemList as #item
     var &itemPath = GetPath(#item/Item)
     var #found = false
     var #actualItem = load(&itemPath)
     if Not(#actualItem/CanPickUp) then
         set #found = true
     end if
     if Exists(#holding) then
         loop through #holding as #held
             log "Looking for: " + &itemPath + ": against " + GetPath(#held/Item)
             if GetPath(#held/Item)=&itemPath then
                 set #found = true
             end if
         end loop
     end if
     if #found =false then
         // also check nobody is holding it...
         var #personHolding = load(&sessionPath/PersonsHolding[Item=&itemPath])
         if NotExists(#personHolding) then
             set #item/ImagePath = URLSafe(str:ToString(&itemPath))
             add #item to #newItemList
         end if
     end if
 end loop
 // also add in stuff that's dropped in this room....
 loop through load(&sessionPath/Holding[Dropped=true, RoomX=#roomX, RoomY=#roomY]) as #dropped
    set #dropped/Description = "There is a " + GetDisplay(#dropped/Item) + " on the floor"
         set #dropped/ImagePath = URLSafe(str:ToString(GetPath(#dropped/Item)))
      add #dropped to #newItemList
 end loop

 set #output/ItemList = #newItemList
 set #holding = load(&sessionPath/Holding[Dropped=false])
 var #finalHoldingList = NewList()
 loop through #holding as #heldItem
     var #heldItemInventory = load(GetPath(#heldItem/Item))
     if #heldItemInventory/CanPickUp then
         add #heldItem to #finalHoldingList
     end if
 end loop
 set #output/Holding = #finalHoldingList
 // who are we talking to?
 if Exists(#session/TalkingTo) then
     set #output/TalkingTo = #session/TalkingTo/Name
     set #output/TalkingToInanimate = #session/TalkingTo/Inanimate
     var &personPath = GetPath(#session/TalkingTo)
     var &talkTextPath = GetPath(#session/Topic)
     var #talkTextItem = load(&talkTextPath)
     set #output/TalkText = #talkTextItem/Text
     if Exists(#session/TalkingTo/Image) then
         set #output/TalkingImage = URLSafe(str:ToString(GetPath(#session/TalkingTo)))
     end if
   // response options
     var &talkPath = GetPath(#talkTextItem)

     set #output/TalkOptions = RunPlugin("Functions:ChooseTalkOptions", &path, record ( Options = (load(&talkPath/Options)), AdvName=#input/AdvName))
 end if
 // look for a picture to add...
 if Exists(#room/Picture) then
     set #output/ImagePath = URLSafe(str:ToString(GetPath(#room)))
 else
     set #output/ImagePath = URLSafe(str:ToString(GetPath(#room/Region)))
 end if
 return #output
 


Next Topic:
v4.2.0.956 (beta)
Up Since 2/29/2024 12:02:23 AM