[Script] Generic Pit Sequence Puzzle

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

[Script] Generic Pit Sequence Puzzle

Post by Grimwold »

Another generic script that you can add to your Dungeon if you want to include this type of puzzle. The idea here is that a room or path is covered in pits, which open and then close in sequence and the player must correctly navigate the pits to reach the other side. It is a puzzle that cropped up on a couple of occasions in the main game.

As with my other generic puzzle scripts, this can be used multiple times in the same dungeon, provided you change the counter and timer variables to unique names. You could even re-use the same pits and have multiple combinations.. the most obvious of which is a reversal for getting back across the room.

You will need to place pits in your dungeon, and add trapdoors to each one. you will also need to place a button that will be used to start the sequence off. Then you will need to place a script entity and paste in the following code. Finally you will edit the first few lines of the script to customise the puzzle to suit your needs.

Code: Select all

timer_name = "pit_sequence_timer_3"
counter_name = "pit_sequence_counter_3"
button_name = "pit_sequence_button_3"
delay = 2.5
pit_sequence = {"dungeon_pit_7","dungeon_pit_8","dungeon_pit_9","dungeon_pit_10","dungeon_pit_11","dungeon_pit_8","dungeon_pit_12","dungeon_pit_13","dungeon_pit_14","dungeon_pit_15","dungeon_pit_16","dungeon_pit_17","dungeon_pit_18","dungeon_pit_15","dungeon_pit_19"}

function runSequence()
  if initialisation == 1 then
    print("puzzle not initialised. Internal Script Error")
    return false
  else
    local counter_entity = findEntity(counter_name)
    if counter_entity == nil then
      print(counter_name .. " not spawned. Internal Script Error")
      return false
    end
    local cidx = #pit_sequence - counter_entity:getValue()
    local oidx = #pit_sequence - counter_entity:getValue() - 2
    if cidx > 0 and cidx <= #pit_sequence then
      local pit_to_close = findEntity(pit_sequence[cidx])
      if pit_to_close ~= nil then
        pit_to_close:close()
      end 
    end
    if oidx > 0 and oidx <= #pit_sequence then
      local pit_to_open = findEntity(pit_sequence[oidx])
      if pit_to_open ~= nil then
        pit_to_open:open()
      end 
    end
    if cidx > #pit_sequence+1 then
      stopSequence()
    end
    findEntity(counter_name):decrement()
  end
end

function stopSequence()
  local timer_entity = findEntity(timer_name)
  local counter_entity = findEntity(counter_name)
  if timer_entity ~= nil then
    timer_entity:deactivate()
  end
  if counter_entity ~= nil then
    counter_entity:setValue(#pit_sequence)
  end
end

function startSequence()
  for _,i in pairs(pit_sequence) do
    local pit_e = findEntity(i)
    if pit_e ~= nil then
      pit_e:open()
    end
  end
  local timer_entity = findEntity(timer_name)
  if timer_entity ~= nil then
    timer_entity:deactivate()
  else
    print(timer_name .. " is missing. Internal Script Error")
  end
  local counter_entity = findEntity(counter_name)
  if counter_entity ~= nil then
    counter_entity:setValue(#pit_sequence)
  else
    print(counter_name .. " is missing. Internal Script Error")
  end
  if timer_entity ~= nil then
    timer_entity:activate()
  else
    print(timer_name .. " is missing. Internal Script Error")
  end
end

function puzzleSetup()
  if initialisation == 1 then
    local button_entity = findEntity(button_name)
    if button_entity ~= nil then
      button_entity:addConnector("toggle", self.id, "startSequence")
    else
      print(button_name .. " is missing")
      return false
    end
    local timer_entity = findEntity(timer_name)
    if timer_entity ~= nil then
      print(timer_name .. " already exists")
      return false
    else
      spawn("timer",self.level,self.x,self.y,self.facing,timer_name)
        :addConnector("activate", self.id, "runSequence")
    end
    local counter_entity = findEntity(counter_name)
    if counter_entity ~= nil then
      print(counter_name .. " already exists")
      return false
    else
      spawn("counter",self.level,self.x,self.y,self.facing,counter_name)
        :setValue(#pit_sequence)
    end
  initialisation = 0
  end
end


initialisation = 1

puzzleSetup()
Customize the script as follows:

timer_name = "pit_sequence_timer_3"
replace the text pit_sequence_timer_3 with a unique ID for a timer. You do not need to place the timer in the dungeon, but you need to make sure that this ID is not used for anything else in your dungeon (and does not duplicate the ID of a timer in a subsequent copy of this script)

counter_name = "pit_sequence_counter_3"
replace the text pit_sequence_counter_3 with a unique ID for a counter. You do not need to place the counter in the dungeon, but you need to make sure that this ID is not used for anything else in your dungeon (and does not duplicate the ID of a counter in a subsequent copy of this script)

button_name = "pit_sequence_button_3"
replace the text pit_sequence_button_3 with the ID of the button you placed in the dungeon to activate this puzzle. You do need to place this button using the editor, but you do not need to create any connectors

delay = 2.5
The value here is the duration in seconds between one pit closing and the next. The bigger the number the more time the party will have to move between the trapdoors.

pit_sequence = {"dungeon_pit_7","dungeon_pit_8","dungeon_pit_9","dungeon_pit_10","dungeon_pit_11","dungeon_pit_8","dungeon_pit_12","dungeon_pit_13","dungeon_pit_14",
"dungeon_pit_15","dungeon_pit_16","dungeon_pit_17","dungeon_pit_18","dungeon_pit_15","dungeon_pit_19"}

This table refers to the pits in your dungeon and the sequence in which they will close. Replace the entries here, with the IDs of the pits you placed, in the exact sequence you want them to close.
jimMy
Posts: 1
Joined: Wed Mar 05, 2014 5:58 pm

Re: [Script] Generic Pit Sequence Puzzle

Post by jimMy »

Thank you ... really usefull !
Post Reply