function Initialise()   
    MyPositions = 1
    WinnerIs = 0

	lib_SetupMultiplayer()
	SendMessage("WormManager.Reinitialise")

        SetData("Land.Indestructable", 1)

	SetData("TurnTime", 45000)

	StartFirstTurn()
    lock, scheme = EditContainer("GM.SchemeData")
    scheme.WeaponChance = 0
    scheme.UtilityChance = 0
    scheme.MysteryChance = 0
    scheme.HealthChance = 0
    CloseContainer(lock)

    lib_SpawnCrate("cr1")
    lib_SpawnCrate("cr2")
    lib_SpawnCrate("cr3")
    lib_SpawnCrate("cr4")

	SendMessage("GameLogic.ClearInventories")
	Index = 0
	InventoryName = lib_GetAllianceInventoryName(Index)
	while InventoryName ~= "" do
	 local Lock,Container = EditContainer(InventoryName)
	 Container.NinjaRope = -1
	 Container.ChangeWorm = -1
	 CloseContainer(Lock) 
	 Index = Index + 1
	 InventoryName = lib_GetAllianceInventoryName(Index)
        end

	SetData("Ninja.NumShots", -1)
        SetData("Jetpack.InitFuel", 75000)
        SetData("Jetpack.MaxAltitude", 50000)

end


function Crate_Collected()
    local CrateIndex = GetData("Crate.Index")
    ActWorm = GetData("ActiveWormIndex")
    local worm = lib_QueryWormContainer(ActWorm)
    ActTeam = worm.TeamIndex 
    if CrateIndex == 1 then
     Check1()
    end
    if CrateIndex == 2 then
     Check2()
    end
    if CrateIndex == 3 then
     Check3()
    end
    if CrateIndex == 4 then
     Check4()
    end
end


function Check1()
    if ActTeam == 0 and WinnerIs == 0 then
     WinnerIs = 1
     RedWin()
     local lock,Inventory = EditContainer("Inventory.Alliance00")
       Inventory.Jetpack = drj1 + drj1 + drj1
       Inventory.NinjaRope = 0
     CloseContainer(lock)
     SendMessage("Weapon.Delete")
     SendMessage("Utility.Delete")
     SendMessage("Timer.EndTurn") 
     SendMessage("Weapon.DisableWeaponChange")
     EndTurn()
    elseif ActTeam == 0 and WinnerIs == 1 then
     WeapRed()
     local lock,Inventory = EditContainer("Inventory.Alliance00")
       Inventory.Jetpack = drj1 + drj1 + drj1
       Inventory.NinjaRope = 0
     CloseContainer(lock)
     SendMessage("Weapon.Delete")
     SendMessage("Utility.Delete")
     SendMessage("Timer.EndTurn") 
     SendMessage("Weapon.DisableWeaponChange")
     EndTurn()
    end
    if ActTeam == 1 then
     lib_SpawnCrate("cr1")
    end
    if ActTeam == 2 then
     lib_SpawnCrate("cr1")
    end
    if ActTeam == 3 then
     lib_SpawnCrate("cr1")
    end
end


function Check2()
    if ActTeam == 1 and WinnerIs == 0 then
     WinnerIs = 1
     BlueWin()
     local lock,Inventory = EditContainer("Inventory.Alliance01")
       Inventory.Jetpack = drj2 + drj2 + drj2
       Inventory.NinjaRope = 0
     CloseContainer(lock)
     SendMessage("Weapon.Delete")
     SendMessage("Utility.Delete")
     SendMessage("Timer.EndTurn") 
     SendMessage("Weapon.DisableWeaponChange")
     EndTurn()
    elseif ActTeam == 1 and WinnerIs == 1 then
     WeapBlue()
     local lock,Inventory = EditContainer("Inventory.Alliance01")
       Inventory.Jetpack = drj2 + drj2 + drj2
       Inventory.NinjaRope = 0
     CloseContainer(lock)
     SendMessage("Weapon.Delete")
     SendMessage("Utility.Delete")
     SendMessage("Timer.EndTurn") 
     SendMessage("Weapon.DisableWeaponChange")
     EndTurn()
    end
    if ActTeam == 0 then
     lib_SpawnCrate("cr2")
    end
    if ActTeam == 2 then
     lib_SpawnCrate("cr2")
    end
    if ActTeam == 3 then
     lib_SpawnCrate("cr2")
    end
end


function Check3()
    if ActTeam == 2 and WinnerIs == 0 then
     WinnerIs = 1
     GreenWin()
     local lock,Inventory = EditContainer("Inventory.Alliance02")
       Inventory.Jetpack = drj3 + drj3 + drj3
       Inventory.NinjaRope = 0
     CloseContainer(lock)
     SendMessage("Weapon.Delete")
     SendMessage("Utility.Delete")
     SendMessage("Timer.EndTurn") 
     SendMessage("Weapon.DisableWeaponChange")
     EndTurn()
    elseif ActTeam == 2 and WinnerIs == 1 then
     WeapGreen()
     local lock,Inventory = EditContainer("Inventory.Alliance02")
       Inventory.Jetpack = drj3 + drj3 + drj3
       Inventory.NinjaRope = 0
     CloseContainer(lock)
     SendMessage("Weapon.Delete")
     SendMessage("Utility.Delete")
     SendMessage("Timer.EndTurn") 
     SendMessage("Weapon.DisableWeaponChange")
     EndTurn()
    end
    if ActTeam == 1 then
     lib_SpawnCrate("cr3")
    end
    if ActTeam == 0 then
     lib_SpawnCrate("cr3")
    end
    if ActTeam == 3 then
     lib_SpawnCrate("cr3")
    end
end


function Check4()
    if ActTeam == 3 and WinnerIs == 0 then
     WinnerIs = 1
     YellowWin()
     local lock,Inventory = EditContainer("Inventory.Alliance03")
       Inventory.Jetpack = drj4 + drj4 + drj4
       Inventory.NinjaRope = 0
     CloseContainer(lock)
     SendMessage("Weapon.Delete")
     SendMessage("Utility.Delete")
     SendMessage("Timer.EndTurn") 
     SendMessage("Weapon.DisableWeaponChange")
     EndTurn()
    elseif ActTeam == 3 and WinnerIs == 1 then
     WeapYellow()
     local lock,Inventory = EditContainer("Inventory.Alliance03")
       Inventory.Jetpack = drj4 + drj4 + drj4
       Inventory.NinjaRope = 0
     CloseContainer(lock)
     SendMessage("Weapon.Delete")
     SendMessage("Utility.Delete")
     SendMessage("Timer.EndTurn") 
     SendMessage("Weapon.DisableWeaponChange")
     EndTurn()
    end
    if ActTeam == 1 then
     lib_SpawnCrate("cr4")
    end
    if ActTeam == 2 then
     lib_SpawnCrate("cr4")
    end
    if ActTeam == 0 then
     lib_SpawnCrate("cr4")
    end
end


function Crate_Destroyed()
    local CrateIndex = GetData("Crate.Index")
    if CrateIndex == 1 then
     lib_SpawnCrate("cr1")
    end
    if CrateIndex == 2 then
     lib_SpawnCrate("cr2")
    end
    if CrateIndex == 3 then
     lib_SpawnCrate("cr3")
    end
    if CrateIndex == 4 then
     lib_SpawnCrate("cr4")
    end
end


function Crate_Sunk()
    local CrateIndex = GetData("Crate.Index")
    if CrateIndex == 1 then
     lib_SpawnCrate("cr1")
    end
    if CrateIndex == 2 then
     lib_SpawnCrate("cr2")
    end
    if CrateIndex == 3 then
     lib_SpawnCrate("cr3")
    end
    if CrateIndex == 4 then
     lib_SpawnCrate("cr4")
    end
end


function RedWin()
    n=3
    x=math.random(n)
    a=math.random(1,10)
    b=math.random(1,10)
    c=math.random(1,5)
    if x==1 then
     local lock,Inventory = EditContainer("Inventory.Alliance00")
       Inventory.Shotgun = -1
       Inventory.Bazooka = a
       Inventory.Dynamite = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
    if x==2 then
     local lock,Inventory = EditContainer("Inventory.Alliance00")
       Inventory.Bazooka = -1
       Inventory.Shotgun = a
       Inventory.Dynamite = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
    if x==3 then
     local lock,Inventory = EditContainer("Inventory.Alliance00")
       Inventory.Dynamite = -1
       Inventory.Bazooka = a
       Inventory.Shotgun = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
end



function BlueWin()
    n=3
    x=math.random(n)
    a=math.random(1,10)
    b=math.random(1,10)
    c=math.random(1,5)
    if x==1 then
     local lock,Inventory = EditContainer("Inventory.Alliance01")
       Inventory.Shotgun = -1
       Inventory.Bazooka = a
       Inventory.Dynamite = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
    if x==2 then
     local lock,Inventory = EditContainer("Inventory.Alliance01")
       Inventory.Bazooka = -1
       Inventory.Shotgun = a
       Inventory.Dynamite = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
    if x==3 then
     local lock,Inventory = EditContainer("Inventory.Alliance01")
       Inventory.Dynamite = -1
       Inventory.Bazooka = a
       Inventory.Shotgun = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
end



function GreenWin()
    n=3
    x=math.random(n)
    a=math.random(1,10)
    b=math.random(1,10)
    c=math.random(1,5)
    if x==1 then
     local lock,Inventory = EditContainer("Inventory.Alliance02")
       Inventory.Shotgun = -1
       Inventory.Bazooka = a
       Inventory.Dynamite = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
    if x==2 then
     local lock,Inventory = EditContainer("Inventory.Alliance02")
       Inventory.Bazooka = -1
       Inventory.Shotgun = a
       Inventory.Dynamite = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
    if x==3 then
     local lock,Inventory = EditContainer("Inventory.Alliance02")
       Inventory.Dynamite = -1
       Inventory.Bazooka = a
       Inventory.Shotgun = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
end



function YellowWin()
    n=3
    x=math.random(n)
    a=math.random(1,10)
    b=math.random(1,10)
    c=math.random(1,5)
    if x==1 then
     local lock,Inventory = EditContainer("Inventory.Alliance03")
       Inventory.Shotgun = -1
       Inventory.Bazooka = a
       Inventory.Dynamite = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
    if x==2 then
     local lock,Inventory = EditContainer("Inventory.Alliance03")
       Inventory.Bazooka = -1
       Inventory.Shotgun = a
       Inventory.Dynamite = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
    if x==3 then
     local lock,Inventory = EditContainer("Inventory.Alliance03")
       Inventory.Dynamite = -1
       Inventory.Bazooka = a
       Inventory.Shotgun = b
       Inventory.BubbleTrouble = c
     CloseContainer(lock)
    end
end


function WeapRed()
    n=4
    x=math.random(n)
    a=math.random(1,10)
    b=math.random(1,10)
    c=math.random(1,10)
    if x==1 then
     local lock,Inventory = EditContainer("Inventory.Alliance00")
       Inventory.BaseballBat = -1
       Inventory.SniperRifle = a
       Inventory.Grenade = b
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==2 then
     local lock,Inventory = EditContainer("Inventory.Alliance00")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = -1
       Inventory.Grenade = b
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==3 then
     local lock,Inventory = EditContainer("Inventory.Alliance00")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = b
       Inventory.Grenade = -1
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==4 then
     local lock,Inventory = EditContainer("Inventory.Alliance00")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = b
       Inventory.Grenade = c
       Inventory.ClusterGrenade = -1
     CloseContainer(lock)
    end
end



function WeapBlue()
    n=4
    x=math.random(n)
    a=math.random(1,10)
    b=math.random(1,10)
    c=math.random(1,10)
    if x==1 then
     local lock,Inventory = EditContainer("Inventory.Alliance01")
       Inventory.BaseballBat = -1
       Inventory.SniperRifle = a
       Inventory.Grenade = b
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==2 then
     local lock,Inventory = EditContainer("Inventory.Alliance01")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = -1
       Inventory.Grenade = b
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==3 then
     local lock,Inventory = EditContainer("Inventory.Alliance01")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = b
       Inventory.Grenade = -1
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==4 then
     local lock,Inventory = EditContainer("Inventory.Alliance01")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = b
       Inventory.Grenade = c
       Inventory.ClusterGrenade = -1
     CloseContainer(lock)
    end
end



function WeapGreen()
    n=4
    x=math.random(n)
    a=math.random(1,10)
    b=math.random(1,10)
    c=math.random(1,10)
    if x==1 then
     local lock,Inventory = EditContainer("Inventory.Alliance02")
       Inventory.BaseballBat = -1
       Inventory.SniperRifle = a
       Inventory.Grenade = b
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==2 then
     local lock,Inventory = EditContainer("Inventory.Alliance02")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = -1
       Inventory.Grenade = b
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==3 then
     local lock,Inventory = EditContainer("Inventory.Alliance02")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = b
       Inventory.Grenade = -1
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==4 then
     local lock,Inventory = EditContainer("Inventory.Alliance02")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = b
       Inventory.Grenade = c
       Inventory.ClusterGrenade = -1
     CloseContainer(lock)
    end
end



function WeapYellow()
    n=4
    x=math.random(n)
    a=math.random(1,10)
    b=math.random(1,10)
    c=math.random(1,10)
    if x==1 then
     local lock,Inventory = EditContainer("Inventory.Alliance03")
       Inventory.BaseballBat = -1
       Inventory.SniperRifle = a
       Inventory.Grenade = b
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==2 then
     local lock,Inventory = EditContainer("Inventory.Alliance03")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = -1
       Inventory.Grenade = b
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==3 then
     local lock,Inventory = EditContainer("Inventory.Alliance03")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = b
       Inventory.Grenade = -1
       Inventory.ClusterGrenade = c
     CloseContainer(lock)
    end
    if x==4 then
     local lock,Inventory = EditContainer("Inventory.Alliance03")
       Inventory.BaseballBat = a
       Inventory.SniperRifle = b
       Inventory.Grenade = c
       Inventory.ClusterGrenade = -1
     CloseContainer(lock)
    end
end