Skip to content
User Avatar
#3777
Auto-translated

Thank you very much for your response. Yes, Osillian is a variable ("Osillian = GetDefenderHero()"), and 'Priest' is the name of the stack created by the AddCreature script. The random function is present. The only remaining issue is the hero's refusal to cast Heavenly Shield on a creature. The screenshot below shows the error that the console displays when attempting to cast it on a creature.

Another problem arose with the script you suggested. The error related to this is also present in the screenshot. Here is my random function:

Titlelocal default_state = {
    {1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0},
    {0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0},
    {1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0},
    {1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1},
}
function GetPRNGSeed(state)
    local col, row = 1, 4
    local seed = {0, 0, 0, 0, 0, 0}
    for i=1,6 do
        local p2 = 1
        for j=1,23 do
            seed= seed+ p2 * (%default_state[row][col] ~= state[row][col] or 0)
            p2 = p2 * 2
            row = row - 1
            if row < 1 then row, col = 4, col + 1 end
            if col > 32 then break end
        end
        if col > 32 then break end
    end
    return seed
end

function SetPRNGSeed(state, seeds)
    state = state or {}
    seeds = seeds or {}
    for i=1,4 do
        if not statethen
            state= {}
        end
        for j=1,32 do
            state[j] = %default_state[j]
        end
    end
    local col, row = 1, 4
    local si
    for i=1,6 do
        si = seedsor si
        if not si then break end
        local si = si
        for j=1,23 do
            local bit = mod(si, 2)
            si = (si - bit) / 2
            state[row][col] = state[row][col] ~= bit or 0
            row = row - 1
            if row < 1 then row, col = 4, col + 1 end
            if col > 32 then break end
        end
        if col > 32 then break end
    end
    return state
end

function NewPRNG(seeds)
    local state = SetPRNGSeed({}, seeds)
    local random = function(n, m)
        local state = %state
        local s
        local t = state[4]
        for i=1,21 do  -- t = t xor (t << 11)
            t= t~= t[i+11] or 0
        end
        for i=9,32 do  -- t = t xor (t >> 8)
            t= t~= t[i-8] or 0
        end
        state[4] = state[3]
        state[3] = state[2]
        s = state[1]
        state[2] = s
        for i=1,32 do  -- t = t xor s
            t= t~= sor 0
        end
        for i=20,32 do  -- t = t xor (s >> 19)
            t= t~= s[i-19] or 0
        end
        state[1] = t
        local r = 0
        local p = 1
        for i=24,1,-1 do
            r = r + p * t
            p = p * 2
        end
        r = r / 16777216
        if n then
            if not m then
                n, m = 1, n
            end
            return n + floor(r * (m - n + 1))
        else
            return r
        end
    end
    return random, state
end

function InitRandom()
    local seed_t = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    local seed = {}
    local shift = 0
    for side = 0, 1 do
        for j, unit in GetCreatures(side) do
            local type = GetCreatureType(unit)
            local num = GetCreatureNumber(unit)
            local x, y = pos(unit)
            local n = mod(type, 180) + 180 * (x + 14 * (y + 14 * mod(num, 200)))
            shift = shift + 7
            for i=1,24 do
                local bit = mod(n, 2)
                n = (n - bit) / 2
                local index = mod(shift + i, 24) + 1
                seed_t[index] = seed_t[index] ~= bit or 0
            end
        end
        local x = 0
        local p = 1
        for i=1,24 do
            x = x + p * seed_t
            p = p * 2
        end
        for i=1,12 do
            seed_t, seed_t[25 - i] = seed_t[25 - i], seed_t
        end
        seed[side+1] = x
        seed[side+3] = x
        seed[side+5] = x
    end
    print('PRNG seed = ' .. seed[1] .. ':' .. seed[2])
    random = NewPRNG(seed)
end

And here is the script in which I use it:

Titlefunction DefenderHeroMove (unit)
Timer = Timer+1
  for n, creature in GetDefenderCreatures() do
    local id = GetCreatureType(creature)
    if id == CREATURE_ARCHER then
     local targets = GetAttackerCreatures()
     chosen_target = targets[random(length(targets)+1)]
    end
  end
commandShot (creature, chosen_target, nil)

}{0TT@6bI4
Hello,
I have a couple of questions about your script: there cannot be a creature with the name "Priest" on the battlefield, just as there cannot be Osillian (if this is a variable that stores the correct name, then it's okay, but if you forgot the quotes, then read on)
All creatures have a composite script name in the form "coordinates+id+number", and this name is stored in the first argument of the hook. In your case, this is the variable unit. And "Priest" and "Osillian" are not on the battlefield.

Write a hook for the turn of the defending hero, and inside it, make a check:
for n, creature in GetDefenderCreatures() do
local id = GetCreatureType(creature)
if id == CREATURE_ARCHER then
local targets = GetAttackerCreatures()
chosen_target = targets[random(length(targets)+1)]
-- actions with the archer creature and the target chosen_target
end
end

The random function needs to be rewritten, as the standard one does not work. You can take the randomizer from Echoes of the Void by RedHeavenHero, if he doesn't mind, or write it yourself.

Attachments 1
1 file attached • Total size: 230.3 KB