Re: Useful scripts repository
Posted: Wed Oct 10, 2012 10:10 pm
Teleport to a Random Location (on the same floor)
My method works around the logic of a monster spawner won't spawn a monster if the space is blocked. This is a way to detect if a space is solid rock or not, even if there are no entities there.
Options
allowTeleFrag = true | false Whether to teleport on top of a monster, killing it (no XP gained this way), or reroll for a new target location.
allowRocked = true | false Whether to teleport into rock, killing the whole party! OUCH! (1000 damage to all split 50% physical, 50% earth), or reroll for a new target location.
dodgeRockLuck = 0 to 1 The probability to reroll instead of rocking the party. This parameter is to reduce the chance of landing in rock, because it can be a little too likely for dungeons with many rock spaces.
Setup
We need three pieces to get the random teleport working:
My method works around the logic of a monster spawner won't spawn a monster if the space is blocked. This is a way to detect if a space is solid rock or not, even if there are no entities there.
Options
allowTeleFrag = true | false Whether to teleport on top of a monster, killing it (no XP gained this way), or reroll for a new target location.
allowRocked = true | false Whether to teleport into rock, killing the whole party! OUCH! (1000 damage to all split 50% physical, 50% earth), or reroll for a new target location.
dodgeRockLuck = 0 to 1 The probability to reroll instead of rocking the party. This parameter is to reduce the chance of landing in rock, because it can be a little too likely for dungeons with many rock spaces.
Setup
We need three pieces to get the random teleport working:
- A custom monster type definition. (The "probe".) This is used for a "valid location" check where a monster spawner is placed which then attempts to place the monster probe.
Code: Select all
cloneObject{ name = "teleport_probe", baseObject = "snail", }
- A timer helper object. To teleport the party we must place a teleporter on top of it, and then delete the teleporter after a tiny delay.
- The timer's ID must be "teleport_timer" to work with the script.
- The timer interval can be short. I used 0.1 seconds.
- The timer must call the tpCleanup() function, as well as deactivate itself.
- The teleport script itself! Call the teleportRandom() function to trigger a random teleport! You can use a button, pressure plate, item, spell, whatever can call it!
Code: Select all
function teleportRandom() ---- Define teleporter behavior ---- local allowTeleFrag = true ---- local allowRocked = true ---- local dodgeRockLuck = 0.8 ---- ------------------------------------ local rockedMessage = "Teleported into Rock! A gruesome death!" local allMonsters = {"crab", "crowern", "goromorg", "green_slime", "herder", "herder_big", "herder_small", "herder_swarm", "ice_lizard", "ogre", "scavenger", "scavenger_swarm", "shrakk_torr", "skeleton_archer", "skeleton_archer_patrol", "skeleton_warrior", "snail", "spider", "tentacles", "uggardian", "warden", "wyvern"} local targetX = math.floor(math.random()*32) local targetY = math.floor(math.random()*32) local targetFacing = math.floor(math.random()*4) local teleportSuccess = false spawn("spawner", party.level, targetX, targetY, 0, "probe"):setSpawnedEntity("teleport_probe"):activate() for i in entitiesAt(party.level, targetX, targetY) do if i.name == "teleport_probe" then teleportSuccess = true i:destroy() spawn("teleporter", party.level, party.x, party.y, 0, "teleport_random"):setTeleportTarget(targetX, targetY, targetFacing) teleport_timer:activate() break end end probe:destroy() if teleportSuccess == false then local rockTarget = false local monsterTarget = false -- Test for reason of teleport fail -- if allowRocked or allowTeleFrag then local tempBlockage = entitiesAt(party.level, targetX, targetY) if tempBlockage() == nil then rockTarget = true end for i in entitiesAt(party.level, targetX, targetY) do for j=1,# allMonsters do if i.name == allMonsters[j] then monsterTarget = true break end end if monsterTarget == true then break end end end -- Should we rock the party? -- if allowRocked and rockTarget and math.random() > dodgeRockLuck then spawn("teleporter", party.level, party.x, party.y, 0, "teleport_random"):setTeleportTarget(targetX, targetY, targetFacing) teleport_timer:activate() hudPrint(rockedMessage) -- Everyone takes 1000 damage, 50% physical, 50% earth -- for i=1,4 do party:getChampion(i):damage(500, "physical") party:getChampion(i):damage(500, "poison") party:getChampion(i):playDamageSound() end return end -- Should we telefrag a monster? -- if allowTeleFrag and monsterTarget then spawn("teleporter", party.level, party.x, party.y, 0, "teleport_random"):setTeleportTarget(targetX, targetY, targetFacing) teleport_timer:activate() return end -- No suitable location found, try again -- teleportRandom() end end function tpCleanup() findEntity("teleport_random"):destroy() end