Skip to content
User Avatar
#3830
Auto-translated
}{0TT@6bI4

Still, I didn't understand anything from your task, but oh well. My code goes through all k creatures (with i not needed) and generates a random index k until it's no longer in the array of indices. Once it exits — it works with the creature at the generated index k

Why such a check with a loop is needed — because simple random (like yours) can produce duplicate values, and as I understood, that shouldn't be the case. Maybe I misunderstood.

And overall, it's very strange that this doesn't work for you and generates in two places. I ran idle prints for k=100 and it generated a bunch of values, all different (well, I didn't scroll through them all).
Honestly, I understood even less) If I had understood, I wouldn't have asked for help 😅 Anyway, here's the final version of this spawn script, just finished writing it and moved on to other things (I haven't touched coordinate saving anymore):
function RespawnUnitsByLuck()
if ( GetDate(WEEK) == 2 ) and ( GetDate(DAY_OF_WEEK) == random(6) + 1 ) then
local WR_luck = GetHeroStat(WR, STAT_LUCK)
local respawns_types = { CREATURE_AIR_ELEMENTAL, CREATURE_PHOENIX, CREATURE_AIR_ELEMENTAL, CREATURE_PHOENIX }
local respawns_quantities = { 100 * diff, 10 * diff, 100 * diff, 4 * diff }
local respawns_mood = {3, 1, 3, 0}
local respawns_courage = {1, 2, 1, 0}
local respawns_num = 0
local previous_pos = {}
local respawns_id = 0

--Памятка по сложностям чтоб посчитать множители от сложностей: diff = 0.5(easy), 2(normal), 4(veteran), 6(heroic), 10(hardcore)
local CanRespawnUnitsByLuck = random(100) + ((10*(WR_luck+1)) * mod((WR_luck+1),diff))
if CanRespawnUnitsByLuck >= mod(70,(WR_luck+2)) + (60+diff) then
print("<color=blue>Рандом выдал число <color=yellow>"..CanRespawnUnitsByLuck, "<color=blue>, которое выше или равно порогу. <color=yellow>Респавним юнитов...")
elseif CanRespawnUnitsByLuck < mod(70,(WR_luck+2)) + (60+diff) then --( 50 + (10+diff) ) then
print("<color=blue>Рандом выдал число <color=red>"..CanRespawnUnitsByLuck, "<color=blue>, которое ниже порога. <color=red>Никого не спавним, выходим из функции...")
return
end

for i = 1, 20 + diff do
local n = random(length(creatures_positions))+1
local x = creatures_positions[n].x
local y = creatures_positions[n].y
local floor = 0;
local badpos = 0;

if IsObjectExists("m"..n) or IsObjectExists("respawns"..n) then
badpos = 1
end

if ( badpos == 0 ) and ( IsTilePassable( x, y, floor ) ) then
respawns_num = respawns_num + 1
if respawns_num == 20 + diff then
print("<color=yellow>Наспавнено максимальное количество юнитов. Выходим из респавнера.")
return --если наспавнили больше 20+сложность юнитов то выходим, иначе дичайший переспавн
end

local type = random( 3 ) + 1
if ( WR_luck < 3 ) and ( type == 4 ) then
type = random( 2 ) + 1
end
if ( WR_luck >= 5 ) then
respawns_quantities = { 200 * diff, 4 * diff, 200 * diff, 4 * diff }
respawns_courage = {1, 0, 1, 0}
type = random( 2 ) + 2
end
local creaturetype = respawns_types[ type ]
local quantity = respawns_quantities[ type ]
local mood = respawns_mood[ type ]
local courage = respawns_courage[ type ]
local respawnsname = 'respawns' .. n;
CreateMonster( respawnsname, creaturetype, quantity, x, y, floor, mood, courage, random( 360 ) );
end;
end;
if respawns_num == 0 then
print("<color=brown>Ни одно существо не было заспавнено. Вероятно, все точки спавна заняты.")
else
print("<color=blue>Респавн завершен. Заспавнено существ: <color=yellow>"..respawns_num)
end;
end;
end;