if isServer() then
    return
end
local FlashTable = {}
local function RemoveFlash()
    for i = 1, #FlashTable do
        if FlashTable[i] then
            local light = FlashTable[i][1]
            local tick = FlashTable[i][2]
            FlashTable[i][2] = tick - 1
            if light and tick <= 0 then
                table.remove(FlashTable, i)
                i = i - 1
                getCell():removeLamppost(light)
            end
        end
    end
end

Events.OnTick.Add(RemoveFlash)

ISPanzerAimingCursor = ISBuildingObject:derive("ISPanzerAimingCursor")
ISPanzerAimTime = 0
function ISPanzerAimingCursor:create(x, y, z, north, sprite)
    local sq = getWorld():getCell():getGridSquare(x, y, z)
    local PanzerType = GetPanzerType(self.character:getVehicle())
    self.character:getVehicle():playSound(PanzerList[PanzerType].FireSound)
    local vehicle = self.character:getVehicle()
    local modData = vehicle:getModData()
    modData.PanzerReadyToFire = false
    if ISPanzerAimTime > 0 then
        local Range = math.floor(ISPanzerAimTime)
        sq = getWorld():getCell():getGridSquare(x + ZombRand(-Range, Range), y + ZombRand(-Range, Range), z)
    end
    PanzerDetectTarget(sq)
    TurretBarrelOffset = -1
    GunMuzzleSet = true
    local newlight = IsoLightSource.new(self.character:getX(), self.character:getY(), 0, 1, 1, 1, 20)
    table.insert(FlashTable, {newlight, 20})
    getCell():addLamppost(newlight)
end

function ISPanzerAimingCursor:isValid(square) -- local distan = 10
    local vehicle = self.character:getVehicle()
    local modData = vehicle:getModData()
    if not modData.PanzerReadyToFire then
        return false
    end
    if isMouseButtonDown(1) and self.character:getModData().PanzerReadyToFire then
        local distan = ((square:getX() - self.character:getX()) ^ 2 + (square:getY() - self.character:getY()) ^ 2) ^ 0.5
        if distan > 5 then
            return true

        else
            return false
        end
    else
        return false
    end
end

-- function ISPanzerAimingCursor:walkTo(x, y, z)

--     return
-- end

function ISPanzerAimingCursor:render(x, y, z, square)
    if not self.floorSprite then
        self.floorSprite = IsoSprite.new()
        self.floorSprite:LoadFramesNoDirPageSimple('media/ui/FloorTileCursor.png')
    end
    local r, g, b, a = 0.0, 1.0, 0.0, 0.8
    if not self:isValid(square) then
        r = 1.0
        g = 0.0
    end
    if ISPanzerAimTime and ISPanzerAimTime > 0 then
        if self.RenderTick > 0 then
            self.RenderTick = self.RenderTick - 1
        else
            self.RenderTick = 25
            ISPanzerAimTime = ISPanzerAimTime - 1
        end
        if ISPanzerAimTime <= 0 then
            ISPanzerAimTime = 0
        end
        local Range = math.floor(ISPanzerAimTime)
        if Range > 0 then
            for i = -Range, Range, Range do
                for j = -Range, Range, Range do
                    self.floorSprite:RenderGhostTileColor(x + i, y + j, z, 1, 0, 1, a)
                end
            end
        end
    end
    local vehicle = self.character:getVehicle()
    local modData = vehicle:getModData()
    if modData.PanzerLoadNow == "HE" then
        for i = -2, 2, 4 do
            for j = -2, 2, 4 do
                self.floorSprite:RenderGhostTileColor(x + i, y + j, z, 1, 1, 0, a)
            end
        end
    elseif modData.PanzerLoadNow == "AP" then
        for i = -1, 1, 2 do
            for j = -1, 1, 2 do
                self.floorSprite:RenderGhostTileColor(x + i, y + j, z, 1, 1, 0, a)
            end
        end
    end
    self.floorSprite:RenderGhostTileColor(x, y, z, r, g, b, a)
    self.square = square
    self.Vaild = self:isValid(square)
end

function ISPanzerAimingCursor:new(sprite, northSprite, character, load)
    local o = {}
    setmetatable(o, self)
    self.__index = self
    o:init()
    o:setSprite(sprite)
    o:setNorthSprite(northSprite)
    o.player = 0
    o.character = character
    o.type = "Panzer"
    o.load = load
    o.RenderTick = 25
    o.noNeedHammer = true
    o.skipBuildAction = true
    o.maxrange = 100
    o.square = nil
    o.Vaild = false
    o.skipWalk2 = true
    o.canBeAlwaysPlaced = true
    return o
end

