Useful scripts repository

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Useful scripts repository

Post by Komag »

Carefully place an object in a square
(ex: you want to hide a note under a helmet)

If you want to have precise control over the placement of an object, you can just place a Lua script nearby and control the X,Y of the item within the square:

Code: Select all

mybrasskey:setSubtileOffset(-0.4, 1.3)
This will move the key a little to the west and nearly all the way south on the square.
The range is something like 1.5 to -1.5
0, 0 puts the item in the exact center of the square
1, 1 NE
1, -1 SE
-1, -1 SW
-1, 1 NW
Last edited by Komag on Wed Sep 19, 2012 1:46 pm, edited 1 time in total.
Finished Dungeons - complete mods to play
User avatar
Montis
Posts: 340
Joined: Sun Apr 15, 2012 1:25 am
Location: Grimrock II 2nd playthrough (hard/oldschool)

Re: Useful scripts repository

Post by Montis »

Code by Scorcher24 and me:

How to teleport your party with a consumable item:

1.) Open items.lua in mod_assets/scripts and paste the following:

Code: Select all


cloneObject{
   name = "rock_teleporter",   
   baseObject = "rock",
   uiName= "Weird looking rock",
   throwingWeapon = false,
   rangedWeapon = false,
   reachWeapon = false,
   stackable = true,
   consumable = true,
   
   onUseItem = function(item, champion)
		-- spawn the teleporter
		rockTeleporter = spawn("teleporter", 1, party.x, party.y, party.facing, "rockTeleporter")
		
		-- Set it invisible
		rockTeleporter:setInvisible(true)
		
		-- Let it be triggered by the party, items on the ground will be left behind
		rockTeleporter:setTriggeredByParty(true)
		rockTeleporter:setTriggeredByItems(false)
		
		-- Set the target - alter this to your needs!
		rockTeleporter:setTeleportTarget(15, 11, party.facing, 1)
		
		-- activate it
		rockTeleporter:activate()
		
		-- Print a message
		hudPrint("Weeeeee!")
		
		-- Consume item
		return true
   end,
}

2.) Reload your project and place the rock somewhere
3.) Place a hidden pressure plate where the party gets teleported at (location in the code above) and a script_entity next to it.
4.) Select the script entity and enter the following:

Code: Select all

function stopTeleporter()
	findEntity("rockTeleporter"):deactivate()
end
5.) Add the script and the function to the connector property of the hidden pressure plate

Here is a showcase to download:
http://www27.zippyshare.com/v/85374530/file.html
Open it in the editor, pick up the rock and rightclick it in the inventory.

This is pretty straight forward and easy to do.
I personally would still like a party:teleport function because it always leaves a hidden teleporter behind, since there is afaik no destroy or despawn function.
And it only works one time with this specific setup, since the id of the spawned teleporter must be unique.

-----
original thread for discussion/questions:
viewtopic.php?f=14&t=3132
Last edited by Montis on Fri Sep 21, 2012 1:24 am, edited 2 times in total.
When destiny calls, the chosen have no choice.

My completed dungeon (LoG1): Hypercube
User avatar
Shroom
Posts: 98
Joined: Tue Mar 27, 2012 6:37 pm
Location: UK

Re: Useful scripts repository

Post by Shroom »

Very simple bit of code to look for things in an area

I used this to see if a room had been cleared of snails

Code: Select all

function checkSnails()
	if checkForThings("snail",28,31,3,7) then
		dungeon_door_portcullis_41:open()
		timer_7:deactivate()
		teleporter_33:activate()
	end
end

function checkForThings(thing,xMin,xMax,yMin,yMax)
	for x = xMin,xMax do
		for y = yMin,yMax do
			for object in entitiesAt(party.level,x,y) do
				if object.name == thing then
					return false
				end
			end
		end
	end
	return true
end
checkForThings("snail",28,31,3,7)

is the call that sets up whats needed - so check for snail starting at co-ord 28,3 and check every square until you get to 31,7 - if it finds an object it returns false and stops checking. I had this script tied to a timer and the timer to a hidden plate when they entered the area. When a true value was returned (you do not have to add == true for boolean operations), I opened a portcullis (for the reward), deactivated the timer (we dont want to keep doing things after we have finished) and activated a teleport to allow them to leave the area.

So to summarise
:- hidden_pressure_plate connects to timer (make sure to set the plate to activate once only)
:- timer connects to script_entity (I left the interval at 1 second)

Hope this helps :)

----------

Komag - here is an example for maintaining a single monster presence in a room:

Code: Select all

function checkPatrol1()
	if checkForThings("snail",26,29,8,11) then
		spawn("snail", party.level, 27, 9, 1)
	end
end

function checkForThings(thing,xMin,xMax,yMin,yMax)
	for x = xMin,xMax do
		for y = yMin,yMax do
			for object in entitiesAt(party.level,x,y) do
				if object.name == thing then
					return false
				end
			end
		end
	end
	return true
end
-----
original thread for discussion/questions:
www.grimrock.net/forum/viewtopic.php?f=14&t=3194
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Useful scripts repository

Post by Lmaoboat »

Two digit password entry:
This works best by putting a dungeon_secret_button_large and a wall_button on the same wall, since they don't clash with each other, and both sit neatly in the middle. I'm using four of them, two on each side, each corresponding into one of the min/sub x/y functions. A fifth button can be used to check for the correct password. It can also easily be adapted to have more digits, or possibly cycle through word-based passwords, which I'm working on next. I'm pretty new to scripting, so there's probably a much more efficient way to code those.

Code: Select all

z = ""
a = "Enter Password\n"

if x == nil then
	x = 0
	text1:setWallText(a.."00")
end
if y == nil then
	y = 0
end



function addx()
	if x == 9 then
		x = 9
	else
		x = x + 1
	end
	text1:setWallText(a..x..z..y)
end


function minx()
	if x == 0 then
		x = 0
	else
		x = x - 1
	end
	text1:setWallText(a..x..z..y)
end


function addy()
	if y == 9 then
		y = 9
	else
		y = y + 1
	end
	text1:setWallText(a..x..z..y)
end


function miny()
	if y == 0 then
		y = 0
	else
		y = y - 1
	end
	text1:setWallText(a..x..z..y)
end

function answercheck()
	if x == 4 and y == 2 then
		passdoor:open()
	end
end
		


Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Useful scripts repository

Post by Lmaoboat »

Two word password entry:
Similar to the previous one. More words can be added similarly. I spent way too much time looking up synonyms for "opem" and types of seeds on Wikipedia making this:

Code: Select all

a = "Enter Password\n"

if x == nil then
	x = 0
	b = "-"
	text1:setWallText(a.."--")
end
if y == nil then
	y = 0
	c = "-"
end

function xword()
	if x == 0 then
		b = "-"
	end
	if x == 1 then
		b = "Display"
	end
	if x == 2 then
		b = "Dispell"
	end
	if x == 3 then
		b = "Uncover"
	end
	if x == 4 then
		b = "Open"
	end
	if x == 5 then
		b = "Expose"
	end
	if x == 6 then
		b = "Show"
	end
	if x == 7 then
		b = "Unveil"
	end
	if x == 8 then
		b = "Unfurl"
	end
	if x == 9 then
		b = "Reveal"
	end
	text1:setWallText(a..b.." "..c)
	hudPrint(b)
end

function yword()
	if y == 0 then
		c = "-"
	end
	if y == 1 then
		c = "Lotus"
	end
	if y == 2 then
		c = "Sesame"
	end
	if y == 3 then
		c = "Rye"
	end
	if y == 4 then
		c = "Hemp"
	end
	if y == 5 then
		c = "Millet"
	end
	if y == 6 then
		c = "Flax"
	end
	if y == 7 then
		c = "Barley"
	end
	if y == 8 then
		c = "Pumpkin"
	end
	if y == 9 then
		c = "Poppy"
	end
	text1:setWallText(a..b.." "..c)
	hudPrint(c)
end


function addx()
	if x == 9 then
		x = 9
	else
		x = x + 1
	end
	xword()
end


function minx()
	if x == 0 then
		x = 0
	else
		x = x - 1
	end
	xword()
end


function addy()
	if y == 9 then
		y = 9
	else
		y = y + 1
	end
	yword()
end


function miny()
	if y == 0 then
		y = 0
	else
		y = y - 1
	end
	yword()
end

function answercheck()
	if x == 4 and y == 2 then
		passdoor:open()
	end
end
		

User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Useful scripts repository

Post by Komag »

To turn on a colored light when the map starts:

- create an FX item and name it (such as fxlight1). I just put it in the middle of a room
- create a script nearby (which will run at mapstart):

Code: Select all

fxlight1:setLight(0, 1, 0, 10, 20, 100, true)
fxlight1:translate(0, 1, 0)
This will make a fully green (RGB) light that is 10 bright, 20 range, 100 duration, true shadows, and that is 1 off the ground (the translate)

-----
original threads for discussion/questions:
viewtopic.php?f=14&t=3212
viewtopic.php?f=14&t=3165
Finished Dungeons - complete mods to play
User avatar
Montis
Posts: 340
Joined: Sun Apr 15, 2012 1:25 am
Location: Grimrock II 2nd playthrough (hard/oldschool)

Re: Useful scripts repository

Post by Montis »

Teleporting your party to a certain location, using an item with charges:

1) Define the item in the items.lua
SpoilerShow

Code: Select all

cloneObject{
	name = "teleport_wand",
	baseObject = "lightning_rod",
	uiName = "Staff of Teleportation",
	description = "This staff crackles with energy and can teleport\nyou to the only safe location you know:\nout of this f---ing dungeon!",
	coolDownTime = 10, -- 10 seconds cooldown
	weight = 2.5, -- make it a bit heavier, why not
	charges = 10, -- 10 charges should be okay. you can alter this if you want
	skill = "spellcraft", -- makes it so only mages can use this
	requiredLevel = 1, -- makes it so only mages can use this
	wandPower = 0, -- it will cast a lightning bolt, so make it weak
	emptyItem = "teleport_wand_empty", -- when it's empty, it gets replaced by the item below
}

cloneObject{
	name = "teleport_wand_empty",
	baseObject = "lightning_rod_empty",
	uiName = "Staff of Teleportation (Empty)",
	description = "Could be used to teleport you to safety,\nnow only serves as an awkward club.",
	weight = 2.5, -- make it a bit heavier
	skill = "maces", -- why not let people with maces hit a bit harder with it? it's a staff anyway :P
}
2) Now it gets tricky: define the teleport function in init.lua
SpoilerShow

Code: Select all

cloneObject{
	name = "party",
	baseObject = "party",
	onUseItem = function(champ,item,slot)
		-- first I'll check if the used item is my custom teleport item
		if item.name == "teleport_wand" then
			-- then I'll check if the item is equipped in hand (slot 7 or 8) since this
			-- hook also triggers when you're right clicking on an item in your inventory
			if (slot ~= 7 and slot ~= 8) then	
				hudPrint("You have to equip the staff to use it.") -- you can just delete this line if you want
				return false
			end
			
			-- I'm assuming intelligent adventurers so I'll check here if it makes sense to use the item.
			-- this is also needed because we later will have a hidden pressure plate that deactivates
			-- any teleporters we created with this script, so at least confine this to your teleport target.
			-- "safe zone" here is defined as row (y coordinate) 3 and 4 on level 1
			if (party.y == 3 or party.y == 4) and (party.level == 1) then	
				hudPrint("Why would you use this here? You're already safe!")
				return false
			else
				-- ok all checks done, we're in for a teleport when the code reaches this point!
				
				-- spawn the teleporter.
				-- note that I use a teleportCounter, we have to place that in the dungeon later
				spawn("teleporter", party.level, party.x, party.y, party.facing, "wandTeleporter_"..teleportCounter:getValue())
					-- it's nice you can define spawned things this way, saves a bit of space and a lot of hassle
					:setTeleportTarget(7, 3, 2, 1) -- edit this to your teleport target in format x,y,facing,level!
					:setInvisible(true)
					:setTriggeredByParty(true)
					:setTriggeredByItem(false)
					:setScreenFlash(true)
					:setSilent(false)
					:setChangeFacing(true)
					:activate()
				
				-- confirmation always makes the player feel good (not that they wouldn't notice teleporting...)
				hudPrint("You teleport yourself to safety!")
			end
		end
	end,
}
3) We're teleported, now we have to clean up a bit...
This part was a big mess until I figured it out. you can't instantly deactivate the teleporter with the code above, because it will just not teleport you at all.

3.1) First we need the counter mentioned in the code above. Place one counter, at best near your teleport target and name it teleportCounter. You can give it an initial value of 1. No connectors needed.

3.2) Then we need a script_entity that contains a function to deactivate the teleporter we spawned just before
SpoilerShow

Code: Select all

function teleportDeactivate()
	--checks if the teleporter with the number inside the counter exists
	if findEntity("wandTeleporter_"..teleportCounter:getValue()) then
		-- if found, the following will deactivate the teleporter
		findEntity("wandTeleporter_"..teleportCounter:getValue()):deactivate()
		-- incrementing the counter number is vitel so we don't get any duplicates (would cause crash)
		teleportCounter:increment() 
	end
end
3.3) Finally we need a pressure_plate_hiddon on your teleport target location that will only be Triggered by Party (use the checkboxes). Add a connector to the script_entity we defined above on the activate event and use the action teleportDeactivate.


If you find anything not working, give me a shout. This code is still a bit confusing, even for me and I probably messed something up or forgot something. :D
Even if you don't use this code, I learned a lot while figuring this out, so you might derive some knowledge here as well :)
Last edited by Montis on Fri Sep 21, 2012 1:24 am, edited 1 time in total.
When destiny calls, the chosen have no choice.

My completed dungeon (LoG1): Hypercube
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Useful scripts repository

Post by Lmaoboat »

Dot matrix number display:

Creates a number display on a northern wall. Adjust the the spawn(object, level, x, y, facing, [id]) near the beginning depending on where you want to place it. Use functions make0() through make9() to display a number, and removelight() before adding another.

Code: Select all

function makelight()
	for i=1,25 do
		spawn("fx", 1, 5, 11, 3, "lcd"..i)
		local lcd = findEntity("lcd"..i)
		lcd:setLight(1,0,0,500,0.3, 360000, false)
	end
end

function removelight()
	for i=1,25 do
		local lcd = findEntity("lcd"..i)
		lcd:setLight(1,0,0,500,0.3, .1, false)
	end
end

function num0()
	lcd1:translate(-1,2.5,1.3)
	lcd2:translate(-0.5,2.5,1.3)
	lcd3:translate(0,2.5,1.3)
	lcd4:translate(0.5,2.5,1.3)
	lcd5:translate(1,2.5,1.3)
	lcd6:translate(-1,2,1.3)
	lcd10:translate(1,2,1.3)
	lcd11:translate(-1,1.5,1.3)
	lcd15:translate(1,1.5,1.3)
	lcd16:translate(-1,1,1.3)
	lcd20:translate(1,1,1.3)
	lcd21:translate(-1,0.5,1.3)
	lcd22:translate(-0.5,0.5,1.3)
	lcd23:translate(0,0.5,1.3)
	lcd24:translate(0.5,0.5,1.3)
	lcd25:translate(1,0.5,1.3)
end



function num1()
	lcd1:translate(-1,2.5,1.4)
	lcd2:translate(-0.5,2.5,1.3)
	lcd3:translate(0,2.5,1.3)
	lcd8:translate(0,2,1.3)
	lcd13:translate(0,1.5,1.3)
	lcd18:translate(0,1,1.3)
	lcd21:translate(-1,0.5,1.3)
	lcd22:translate(-0.5,0.5,1.3)
	lcd23:translate(0,0.5,1.3)
	lcd24:translate(0.5,0.5,1.3)
	lcd25:translate(1,0.5,1.3)
end


function num2()
	lcd1:translate(-1,2.5,1.3)
	lcd2:translate(-0.5,2.5,1.3)
	lcd3:translate(0,2.5,1.3)
	lcd4:translate(0.5,2.5,1.3)
	lcd5:translate(1,2.5,1.3)
	lcd10:translate(1,2,1.3)
	lcd11:translate(-1,1.5,1.3)
	lcd12:translate(-0.5,1.5,1.3)
	lcd13:translate(0,1.5,1.3)
	lcd14:translate(0.5,1.5,1.3)
	lcd15:translate(1,1.5,1.3)
	lcd16:translate(-1,1,1.3)
	lcd21:translate(-1,0.5,1.3)
	lcd22:translate(-0.5,0.5,1.3)
	lcd23:translate(0,0.5,1.3)
	lcd24:translate(0.5,0.5,1.3)
	lcd25:translate(1,0.5,1.3)
end


function num3()
	lcd1:translate(-1,2.5,1.3)
	lcd2:translate(-0.5,2.5,1.3)
	lcd3:translate(0,2.5,1.3)
	lcd4:translate(0.5,2.5,1.3)
	lcd5:translate(1,2.5,1.3)
	lcd10:translate(1,2,1.3)
	lcd11:translate(-1,1.5,1.3)
	lcd12:translate(-0.5,1.5,1.3)
	lcd13:translate(0,1.5,1.3)
	lcd14:translate(0.5,1.5,1.3)
	lcd15:translate(1,1.5,1.3)
	lcd20:translate(1,1,1.3)
	lcd21:translate(-1,0.5,1.3)
	lcd22:translate(-0.5,0.5,1.3)
	lcd23:translate(0,0.5,1.3)
	lcd24:translate(0.5,0.5,1.3)
	lcd25:translate(1,0.5,1.3)
end



function num4()
	lcd1:translate(-1,2.5,1.3)
	lcd5:translate(1,2.5,1.3)
	lcd6:translate(-1,2,1.3)
	lcd10:translate(1,2,1.3)
	lcd11:translate(-1,1.5,1.3)
	lcd12:translate(-0.5,1.5,1.3)
	lcd13:translate(0,1.5,1.3)
	lcd14:translate(0.5,1.5,1.3)
	lcd15:translate(1,1.5,1.3)
	lcd20:translate(1,1,1.3)
	lcd25:translate(1,0.5,1.3)
end


function num5()
	lcd1:translate(-1,2.5,1.3)
	lcd2:translate(-0.5,2.5,1.3)
	lcd3:translate(0,2.5,1.3)
	lcd4:translate(0.5,2.5,1.3)
	lcd5:translate(1,2.5,1.3)
	lcd6:translate(-1,2,1.3)
	lcd11:translate(-1,1.5,1.3)
	lcd12:translate(-0.5,1.5,1.3)
	lcd13:translate(0,1.5,1.3)
	lcd14:translate(0.5,1.5,1.3)
	lcd15:translate(1,1.5,1.3)
	lcd20:translate(1,1,1.3)
	lcd21:translate(-1,0.5,1.3)
	lcd22:translate(-0.5,0.5,1.3)
	lcd23:translate(0,0.5,1.3)
	lcd24:translate(0.5,0.5,1.3)
	lcd25:translate(1,0.5,1.3)
end

function num6()
	lcd1:translate(-1,2.5,1.3)
	lcd2:translate(-0.5,2.5,1.3)
	lcd3:translate(0,2.5,1.3)
	lcd4:translate(0.5,2.5,1.3)
	lcd5:translate(1,2.5,1.3)
	lcd6:translate(-1,2,1.3)
	lcd11:translate(-1,1.5,1.3)
	lcd12:translate(-0.5,1.5,1.3)
	lcd13:translate(0,1.5,1.3)
	lcd14:translate(0.5,1.5,1.3)
	lcd15:translate(1,1.5,1.3)
	lcd16:translate(-1,1,1.3)
	lcd20:translate(1,1,1.3)
	lcd21:translate(-1,0.5,1.3)
	lcd22:translate(-0.5,0.5,1.3)
	lcd23:translate(0,0.5,1.3)
	lcd24:translate(0.5,0.5,1.3)
	lcd25:translate(1,0.5,1.3)
end


function num7()
	lcd1:translate(-1,2.5,1.3)
	lcd2:translate(-0.5,2.5,1.3)
	lcd3:translate(0,2.5,1.3)
	lcd4:translate(0.5,2.5,1.3)
	lcd5:translate(1,2.5,1.3)
	lcd6:translate(-1,2,1.3)
	lcd10:translate(1,2,1.3)
	lcd15:translate(1,1.5,1.3)
	lcd20:translate(1,1,1.3)
	lcd25:translate(1,0.5,1.3)
end


function num8()
	lcd1:translate(-1,2.5,1.3)
	lcd2:translate(-0.5,2.5,1.3)
	lcd3:translate(0,2.5,1.3)
	lcd4:translate(0.5,2.5,1.3)
	lcd5:translate(1,2.5,1.3)
	lcd6:translate(-1,2,1.3)
	lcd10:translate(1,2,1.3)
	lcd11:translate(-1,1.5,1.3)
	lcd12:translate(-0.5,1.5,1.3)
	lcd13:translate(0,1.5,1.3)
	lcd14:translate(0.5,1.5,1.3)
	lcd15:translate(1,1.5,1.3)
	lcd16:translate(-1,1,1.3)
	lcd20:translate(1,1,1.3)
	lcd21:translate(-1,0.5,1.3)
	lcd22:translate(-0.5,0.5,1.3)
	lcd23:translate(0,0.5,1.3)
	lcd24:translate(0.5,0.5,1.3)
	lcd25:translate(1,0.5,1.3)
end

function num9()
	lcd1:translate(-1,2.5,1.3)
	lcd2:translate(-0.5,2.5,1.3)
	lcd3:translate(0,2.5,1.3)
	lcd4:translate(0.5,2.5,1.3)
	lcd5:translate(1,2.5,1.3)
	lcd6:translate(-1,2,1.3)
	lcd10:translate(1,2,1.3)
	lcd11:translate(-1,1.5,1.3)
	lcd12:translate(-0.5,1.5,1.3)
	lcd13:translate(0,1.5,1.3)
	lcd14:translate(0.5,1.5,1.3)
	lcd15:translate(1,1.5,1.3)
	lcd20:translate(1,1,1.3)
	lcd21:translate(-1,0.5,1.3)
	lcd22:translate(-0.5,0.5,1.3)
	lcd23:translate(0,0.5,1.3)
	lcd24:translate(0.5,0.5,1.3)
	lcd25:translate(1,0.5,1.3)
end



function make0()
	makelight()
	num0()
end

function make1()
	makelight()
	num1()
end


function make2()
	makelight()
	num2()
end

function make3()
	makelight()
	num3()
end

function make4()
	makelight()
	num4()
end

function make5()
	makelight()
	num5()
end

function make6()
	makelight()
	num6()
end

function make7()
	makelight()
	num7()
end

function make8()
	makelight()
	num8()
end


function make9()
	makelight()
	num9()
end
-----
original threads for discussion/questions:
viewtopic.php?f=14&t=3309
viewtopic.php?f=14&t=3165
User avatar
Montis
Posts: 340
Joined: Sun Apr 15, 2012 1:25 am
Location: Grimrock II 2nd playthrough (hard/oldschool)

Re: Useful scripts repository

Post by Montis »

Creating a sequence lock in 8 easy steps
a sequence lock consisting of multiple activation entities of the same type that have to be activated in a specific order

We need:
  • a door to be opened (e.g. dungeon_door_iron)
  • an unspecific amount of activation entities (e.g levers or buttons)
  • a counter
  • a script_entity
Steps:
  1. Place all the entities mentioned above to your liking. Important: no more than one activation entity per square, else the code won't work!
  2. Name the activation entities to the same kind of pattern e.g. sequenceLock_1 to sequenceLock_4.
  3. Name the counter properly so it's the same as in the code below e.g. myCustomSequenceCounter.
  4. Add a connector from the counter to the door, and set its event to activate and action to open.
  5. Add a connector from the counter to itself, and set its event to activate and action to reset.
  6. Set the initial value of the counter to the number of inputs you want the player to make.
  7. Paste the code below (inside the spoiler) into the script_entity and alter the variables (marked by local) so they fit your riddle. See also the comments in the code, marked by two dashes.
  8. Add connectors from all activation entities to the script_entity, and for each set the event to activate and the action to sequenceRiddle.
SpoilerShow

Code: Select all

function sequenceRiddle()
   -- change the following to what entity type you want to use for your riddle
   local triggerEntity = "lever"
   -- all entity IDs of your activation entities should start with the below, followed by a number
   local namingPattern = "sequenceLock_"
   -- define the sequence in which you want your entities to be activated
   local sequenceOfThings = {"1","2","4","2","3"}
   -- define the ID of your sequenceCounter
   -- important: this sequence counter needs to have the same initial value
   -- as the number of sequences you defined above
   local sequenceCounterID = "myCustomSequenceCounter"
   -- from here on, it shouldn't be needed to alter the rest of the code

   local curSeq = #sequenceOfThings+1-findEntity(sequenceCounterID):getValue()
   for i in entitiesAt(party.level, party.x, party.y) do
      if i.name == triggerEntity then
         if i.id == namingPattern..sequenceOfThings[curSeq] then
            findEntity(sequenceCounterID):decrement()
         else
            findEntity(sequenceCounterID):reset()
         end
      end
   end
end
-----
original thread for discussion/questions:
viewtopic.php?f=14&t=3199
Last edited by Montis on Mon Oct 22, 2012 11:39 am, edited 1 time in total.
When destiny calls, the chosen have no choice.

My completed dungeon (LoG1): Hypercube
User avatar
Xzalander
Posts: 62
Joined: Thu Sep 13, 2012 1:27 pm
Location: Yorkshire, United Kingdom

Re: Useful scripts repository

Post by Xzalander »

Combination Torch Lock

This Combination Torch Lock is repeatable to as many torches as you want and if you use multiple scripts you can even have the same set of torches create different combinations.
This template is preset for 6 Torches and one door.
The Script will also check for Door State and will only attempt to open, close and play the appropriate sound effect when needed. This prevents repeated "Opening" or "Closing" noises while moving torches around. Only once the last torch is put into place will it trigger the door and sound.

By setting any of the torches to false signifies that if the torch is lit, the combination will not take effect, thus preventing players from just putting torches on every holder.

Let me know if there is any errors although its running fine on my editor at the moment.

Edit:
Montis wrote:I think the code looks good for closing the door again when you remove a torch. You probably just need to set the connectors from the torch holders to the script entity to the "any" event instead of only "activate".
So thanks to Montis; set the connectors from the torch holders to Any instead of Activate and the puzzle will reset if any correct torches are taken out of place.

Code: Select all

function torchpuzzle()
   if torch_holder:hasTorch() == true and
    torch_holder:hasTorch() == true and
    torch_holder:hasTorch() == true and
    torch_holder:hasTorch() == true and
    torch_holder:hasTorch() == true and
    torch_holder:hasTorch() == true then
        if torch_puzzle_door:isClosed() then
    	torch_puzzle_door:open()
    	
		playSoundAt("gate_open", 2, 8, 12)
	end
   else
	
		if torch_puzzle_door:isOpen() then
    	torch_puzzle_door:close()
		playSoundAt("gate_close", 2, 8, 12)
		end
	
   
end end
To utilise this code in your own map, rename the torch_holder, torch_puzzle_door and the appropriate Soundat information. PlaySoundat won't be needed if the door/object being affected is nearby. I used soundat in this example to play a noise distantly from the player because the affected door could not be heard.

-----
original thread for discussion/questions:
viewtopic.php?f=14&t=3245
Last edited by Xzalander on Wed Sep 19, 2012 2:36 pm, edited 4 times in total.
Post Reply