#include "../../script/helpers/entity.lua"

function init()
    pillar = FindShape("pillar")
    del_locations = FindLocations("del")
    for i = 1, #del_locations do
        local loc = del_locations[i]
        del_locations[i] = GetLocationTransform(loc).pos
        Delete(loc)
    end
    pigeon_locations = FindLocations("pigeon")
    for i = 1, #pigeon_locations do
        local loc = pigeon_locations[i]
        pigeon_locations[i] = GetLocationTransform(loc)
        Delete(loc)
    end
    local dovecote_sound = FindLocation("dovecote_sound")
    dovecote_sound_pos = GetLocationTransform(dovecote_sound).pos
    Delete(dovecote_sound)

    is_pillar_broken = false
    pigeon_script_file = "MOD/m_village/prefab/pigeon.xml"

    snd = LoadSound("MOD/snd/pigeons.ogg", 10.0)
    vol = 1.0
end

function update(dt)
    if is_pillar_broken then return end

    if not IsHandleValid(pillar) or IsShapeBroken(pillar) then
        is_pillar_broken = cEntity:EnqueueDeletion()
        DovecoteWasBroken()
    end
end

function DovecoteWasBroken()
    PlaySound(snd, dovecote_sound_pos, vol)

    -- Break other pillars
    for i = 1, #del_locations do
        MakeHole(del_locations[i], 1.8, 1.8)
    end

    -- Spawn pigeons
    for i = 1, #pigeon_locations do
        Spawn(pigeon_script_file, pigeon_locations[i])
    end
end
