--tweakables for player bounties

-- ATI Parameter indices for bounty display
--

PrintfCharacters = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}


function KATANA_DYNAMIC_MAP_SETUP()
	local bounds = {0,0,0}
	local bounds=Universe_GetWorldBounds()
	local radius = bounds[1] -500
	local idxer = 0
	Volume_AddSphere("TheUniverse", {0,0,0}, radius)
	while idxer < (ROUTES*2) do
		NAV_points=idxer
		SobGroup_Create("BUOY"..NAV_points)
		Volume_AddSphere("NAV_"..NAV_points, {RandomRange(-1*radius,radius),RandomRange(-1*radius,radius),RandomRange(-1*radius,radius)}, 3000)
		idxer=idxer+1
	end
	local idexer2 = 0
	while NAV_points < (ROUTES*2) do
		NAV_points=idexer2
		NewKatanaStarts()
		idexer2=idexer2+1
	end
	Rule_Remove("KATANA_DYNAMIC_MAP_SETUP")
end  


function Dyn_KatanaCheck()

  -- any Dreads captured ? => give Points
  local playerIndex = 0
  local playerCount = Universe_PlayerCount()
  local captured = 0
  local currentRU = 0
  SobGroup_Create("captured")
  SobGroup_Create("TmpGroup")
  while playerIndex < playerCount do
    if Player_IsAlive(playerIndex) == 1 then
     SobGroup_FillCompare("captured","Player_Ships"..playerIndex,"allbounty")
     captured = SobGroup_Count("captured")
     if captured>0 then
       currentPoints = Player_PTS[playerIndex] + captured
       SobGroup_FillSubstract("TmpGroup","allbounty","captured")
       SobGroup_Clear("allbounty")
       SobGroup_SobGroupAdd("allbounty","TmpGroup")
       SubGroup_Clear("TmpGroup")
      end
      SobGroup_Clear("captured")
    end
    playerIndex = playerIndex + 1
  end     
  SobGroup_SetSwitchOwnerFlag("allbounty",0)
  
end -- Dyn_KatanaCheck

function Dyn_KatanaEnter()
  -- Dread left Universe ? => Kill it
  SobGroup_Create("InVolume")
  SobGroup_FillSobGroupInVolume("InVolume","allbounty","TheUniverse")
  SobGroup_Create("OutVolume")
  SobGroup_FillSubstract("OutVolume","allbounty","InVolume")
  if SobGroup_Count("OutVolume")>0 then
    print("PRIVATEER: "..SobGroup_Count("OutVolume").." have left the Universe")
    SobGroup_Despawn("OutVolume")
  end
  SobGroup_Clear("InVolume")
  SobGroup_Clear("OutVolume")
  
  NewDyn_KatanaStarts()
end -- Dyn_KatanaEnter

function NewDyn_KatanaStarts()
  iKatana=iKatana+1
  local sgbEntry=""
  -- Add a new Dread
  SobGroup_Create("sgbounty")
if bStartPoint==-1 then
	temp = RandomIntMax(ROUTES*2)
	  sgbEntry="NAV_"..temp
	  sgBuoy = "BUOY"..temp 
  else
	  sgbEntry="NAV_"..NAV_points
   	   sgBuoy = "BUOY"..NAV_points
  end  
 
 local EntryPosition = {0,0,0}
	local positionBuoy = {0,0,0}
  local sgbExit="NAV_"..RandomIntMax(ROUTES*2)

  if sgbEntry == sgbExit then
    sgbExit="NAV_"..RandomIntMax(ROUTES*2)
  end

 

  positionBuoy = SobGroup_GetPosition(sgBuoy)
  local howMany = RandomIntMax(convoy_size)+1
  print("PRIVATEER: Sending "..howMany.." Dreadnaughts (guardtype "..guarded..") from "..sgbEntry.." to "..sgbExit)
  local idx = 0
  while idx<howMany do   
		local OMG=RandomIntMax(30000)		
   		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Imp_Dreadnaught","NewKatana","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgbounty","sgtemp")
		Volume_Delete("EntryPoint"..OMG)
		idx=idx+1


  end	  

  
  
  SobGroup_Move(0,"sgbounty",sgbExit)
  
  SobGroup_SobGroupAdd("allbounty","sgbounty")
  
  SobGroup_Clear("sgbounty")
end



function UpdateBountyStats()
       --display bounty stats for each player using ATI
    --display bounty stats for each player using ATI
    ATI_Clear()
    playerCount = Universe_PlayerCount();
    playerIndex = playerCount - 1
    local ATIRect = {0.01, 0.1725, 0.3, 0.02}                                   --rect to left just above taskbar
    while (playerIndex >= 0) do
        --setup parameters
		if (Player_IsAlive(playerIndex)==1) then
            ATI_CreateParameters(HCPN_Parameters)

            local playerName = Player_GetName(playerIndex)
            ATI_AddWString(HCPI_PlayerName, playerName)

            --...Do our own printf here
            local formatted = " "
            local number = Player_PTS[playerIndex + 1]
            if (number < 1) then
                formatted = " 0"
            else
                while (number >= 1) do
                    local index = floor(mod(number, 10))
                    formatted = PrintfCharacters[index + 1]..formatted
                    number = number / 10
                end
            end
            local bountyString = "B: "..formatted
            ATI_AddString(HCPI_PlayerBounty, bountyString)

            local playerColour = {1.0, 1.0, 1.0, 1.0}
            ATI_AddColour(HCPI_PlayerColour, playerColour)
            --actual display
            ATI_Display2D("playerProgress", ATIRect, 0)

    		-- go to next player
            ATIRect[2] = ATIRect[2] + 0.025
        end
		playerIndex = playerIndex - 1
    end

end













