En:Random Trigger Targets

Aus EnigmaWiki

Wechseln zu: Navigation, Suche
Available languages: Deutsch, English, Русский

Inhaltsverzeichnis

Random targets for switches

Problem

There are a certain number of switches in the level and the same number of targets. Each switch has to operate exactly one target, but which one should be determined randomly during the level start.

Solution

We store the names of the targets in an array. One randomly selected name from this array is then assigned to each of the switches as target.

Example solution

-- Array of coordinates, at which switches are to be set
pos_trigger = {{2,2},{5,5},{3,7},{2,8}}
-- Array of coordinates, at which the target stones (e.g. bridges) are to be set
pos_targets = {{5,2},{2,4},{5,6},{6,8}}
 
-- Produces an array from stone names in the style of {"stone1","stone2","stone3",...}
-- and also sets «gleich die so» designated stones to in pos_targets indicated
-- positions.
stone_name = {}
for i=1 , # pos_targets do
   stone_name[i] = "stone"..i
   set_stone("st-door_a" , pos_targets[i][1] , pos_targets[i][2] , {name = stone_name[i]})
end
 
-- The switches are set to the desired positions and assigned to a random stone.
for i=1 , # pos_targets do
   zufall = random(1 , # stone_name)
   set_item("it-trigger" , pos_trigger[i][1] , pos_trigger[i][2] ,
      {action = "openclose", target = stone_name[zufall]})
   table.remove(stone_name,zufall)
end

Analysis

First, as the Lua comments explains, the positions for switches and stone targets are indicated:

pos_trigger = {{x1,y1}, {x2,y2}, ... } -- Switch coordinates
pos_targets = {{x1,y1}, {x2,y2}, ... }  -- Target stone coordinates

In the next step the target objects are set (in this example it concerns st-door_a). At the same time a name is assigned to each stone, by which it is later accessible. In this code the stones are called "stone1", "stone2" etc. The names are stored in the array stone_name.

for i=1 , #pos_targets do
   stone_name[i] = "stone"..i
   set_stone("st-door_a" , pos_targets[i][1] , pos_targets[i][2] , {name = stone_name[i]})
end

In a second loop the switches (Here it-trigger) are finally set. A random entry from the array stone_name is chosen and assigned the attribute target. As we have to take care that no second switch addresses the same target stone, the mentioned entry is afterwards deleted.

for i=1 , #pos_targets do
   zufall = random(1 , # stone_name)
   set_item("it-trigger" , pos_trigger[i][1] , pos_trigger[i][2] ,
      {action = "openclose", target = stone_name[zufall]})
   table.remove(stone_name,zufall)
end
Persönliche Werkzeuge