#include "script/common.lua"

function init()
	tnt = FindBody("TNT")
	shapes = FindShapes("TNT")
	SetTag(tnt, "explosive", GetFloat("game.tool.tnt.damage"))
	tntT = GetShapeWorldTransform(shapes[1])
end

local activated = false
local explosionPower = 2.5
local explode = false
function tick()
	checkShock()
	if activated then
		return
	end

	if explode or IsShapeBroken(shapes[1]) then
		Explosion(tntT.pos, GetFloat("game.tool.tnt.damage"))
		activated = true
		Delete(tnt)
		Delete(shapes[1])
	end
end


function checkShock()
	local hasBreakage = GetBool("game.break")
	if hasBreakage then
		local breakPoint = Vec(GetFloat("game.break.x"), GetFloat("game.break.y"), GetFloat("game.break.z"))
		tntT = GetShapeWorldTransform(shapes[1])
			if VecLength(VecSub(breakPoint, tntT.pos)) < 1.1 then
				explode = true
			end
	end
end


function handleCommand(cmd)
    if not explode then
        words = splitString(cmd, " ")
        if #words == 5 then
            if words[1] == "explosion" then
                local strength = tonumber(words[2])
                local x = tonumber(words[3])
                local y = tonumber(words[4])
                local z = tonumber(words[5])
                tntT = GetShapeWorldTransform(shapes[1])
                pos = Vec(x, y, z)
                if VecLength(VecSub(pos, tntT.pos)) < 2 * strength then
                    explode = true
                end
            end
        end
    end
end
