--[NEW] This is how we tell the game we've created a new prefab.
PrefabFiles = {
	--[NEW] This is the name of our prefab file in scripts/prefabs
	"ghostplayer",
	"summonclone",
	"abigail_3",
	"books_player_3",
	"nightsword_player_3",
	"balloons_empty_player_3",
	"lighter_player_3",
	"axe_player_3",
	"camera_prefab",
	"deftorch",
	"deftorch2",	
}
local require = GLOBAL.require
local GetWorld = GLOBAL.GetWorld
local GetPlayer = GLOBAL.GetPlayer
local table = require("table")
local math = require("math")
local Point = GLOBAL.Point -- note: Point is an alias of Vector3
local distsq = GLOBAL.distsq
local TILE_SCALE = GLOBAL.TILE_SCALE
local TUNING = GLOBAL.TUNING
--------------------------------------------------------------------------

local FollowCamera = GLOBAL.require("cameras/followcamera")
local default = FollowCamera.SetDefault

FollowCamera.SetDefault = function (self)
	default(self)
    if self.headingtarget == nil then
        self.headingtarget = 45
    end

    self.fov = 35
    self.pangain = 4
    self.headinggain = 20
    self.distancegain = 1

    self.zoomstep = 4
    self.distancetarget = 30

    self.mindist = 25
    self.maxdist = 125 --40
   
    self.mindistpitch = 30
    self.maxdistpitch = 60--60 
    self.paused = false
    self.shake = nil
    self.controllable = true
    self.cutscene = false

end
--------------------------------------------------------------------------

local Recipe = GLOBAL.Recipe
local Ingredient = GLOBAL.Ingredient
local RECIPETABS = GLOBAL.RECIPETABS
local STRINGS = GLOBAL.STRINGS
local TUNING = GLOBAL.TUNING
local ACTIONS = GLOBAL.ACTIONS
---//////////////////////////////////////////////////////////////////////----------------
TUNING.PLAYER_KEYS = {}
TUNING.PLAYER_KEYS[2] = {}
TUNING.PLAYER_KEYS[3] = {}


local function addcontrollkeys(num)
TUNING.PLAYER_KEYS[num] = GetModConfigData("KEYBOARDTOGGLEKEY")


end

---waves fix
local function WavesFix(inst)


	local waves = inst.entity:AddWaveComponent()
	waves:SetRegionSize( 50, 27 )
	waves:SetRegionNumWaves( 8 )
	waves:SetWaveTexture( "images/wave.tex" )

	waves:SetWaveEffect( "shaders/waves.ksh" )
	waves:SetWaveSize( 2048, 512 )

end
AddPrefabPostInit("forest", WavesFix)
-----NOTES

STRINGS.NAMES.SUMMONCLONE = " "
STRINGS.CHARACTERS.GENERIC.DESCRIBE.SUMMONCLONE = {}
STRINGS.CHARACTERS.GENERIC.DESCRIBE.SUMMONCLONE.GENERIC = "Hello!"

STRINGS.NAMES.GHOSTPLAYER = " "
STRINGS.CHARACTERS.GENERIC.DESCRIBE.GHOSTPLAYER = {}
STRINGS.CHARACTERS.GENERIC.DESCRIBE.GHOSTPLAYER.GENERIC = "Rise from your grave!"

STRINGS.NAMES.ABIGAIL_3 = "Abigail"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.ABIGAIL_3 = {}
STRINGS.CHARACTERS.GENERIC.DESCRIBE.ABIGAIL_3.GENERIC = "Abigail, twin sister of Wendy!"


---------/////////// PLAYER 3 SPECIAL ITEMS///////////////---------
STRINGS.NAMES.BOOKPLAYER3 = "The End is Nigh!"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.BOOKPLAYER3 = {}
STRINGS.CHARACTERS.GENERIC.DESCRIBE.BOOKPLAYER3.GENERIC = "The world will end in fire and tribulation!"


STRINGS.NAMES.AXE_PLAYER_3 = "Lucy the Axe"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.AXE_PLAYER_3 = {}
STRINGS.CHARACTERS.GENERIC.DESCRIBE.AXE_PLAYER_3.GENERIC = "Lucy the Axe"


STRINGS.NAMES.NIGHTSWORD_PLAYER3 = "Dark Sword"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.NIGHTSWORD_PLAYER3 = {}
STRINGS.CHARACTERS.GENERIC.DESCRIBE.NIGHTSWORD_PLAYER3.GENERIC = "Dark Sword"


STRINGS.NAMES.LIGHTER_PLAYER_3 = "Lucky Lighter"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.LIGHTER_PLAYER_3 = {}
STRINGS.CHARACTERS.GENERIC.DESCRIBE.LIGHTER_PLAYER_3.GENERIC = "Lucky Lighter"

STRINGS.NAMES.balloons_empty_player_3 = "Empty Balloons"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.BALLOONS_EMPTY = {}
STRINGS.CHARACTERS.GENERIC.DESCRIBE.BALLOONS_EMPTY.GENERIC = "Empty Balloons"
---------/////////// PLAYER 3 SPECIAL ITEMS end/////////////-------

--[[
function Combat:CanBeAttacked(attacker)
	if self.inst:HasTag("player_coop_team") and attacker:HasTag("player_coop_team") then return false end
	local can_be_attacked = true
	if self.canbeattackedfn then
		can_be_attacked = self.canbeattackedfn(self.inst, attacker)
	end
	return can_be_attacked
end
--]]



TUNING.MAXDIST = 1700
TUNING.COOPMAXCAMHEIGHT = 60


TUNING.COOP_OFFEST_X = 100
TUNING.COOP_OFFEST_Y = 50
--TUNING.COOP_CUSTOM_HUD = GetModConfigData("CustomeInvPos")
TUNING.COOP_BADGE_POSITION1 = GetModConfigData("Inv_Position1")
TUNING.COOP_BADGE_POSITION2 = GetModConfigData("Inv_Position2")

TUNING.PLAYER_CHARACTER_ENABLED = {}


local playerconfigs2 = GetModConfigData("Player 2")
local playerconfigs3 = GetModConfigData("Player 3")

if playerconfigs2=="Pick_in_World" or playerconfigs3=="Pick_in_World" then
	TUNING.PICK_CHAR_IN_WORLD = 1
	else
	TUNING.PICK_CHAR_IN_WORLD = 0
	
	if playerconfigs2~="DISABLED" then
		TUNING.PLAYER_2_CHARACTER = playerconfigs2
	else
		TUNING.PLAYER_2_CHARACTER = "wilson"
	end
	if playerconfigs3~="DISABLED" then
		TUNING.PLAYER_3_CHARACTER = playerconfigs3
	else
		TUNING.PLAYER_3_CHARACTER = "wilson"
	end
	
	
end

if playerconfigs2~="DISABLED" then
	TUNING.PLAYER_CHARACTER_ENABLED[2] = true
else
	TUNING.PLAYER_CHARACTER_ENABLED[2] = false
end
if playerconfigs3~="DISABLED" then
	TUNING.PLAYER_CHARACTER_ENABLED[3] = true
else
	TUNING.PLAYER_CHARACTER_ENABLED[3] = false
end
--if 


--TUNING.PLAYER_3_CHARACTER = GetModConfigData("Char Name 3")
--TUNING.PLAYER_2_CHARACTER = GetModConfigData("Char Name 2")

--TUNING.PICK_CHAR_IN_WORLD = GetModConfigData("Character")

---end





TUNING.NUMPADCONROLLS =2-- GetModConfigData("NUMPAD")
TUNING.GHOSTPLAYERON = 1--GetModConfigData("Ghost_player")

TUNING.DEFENCEOFF = 1---GetModConfigData("Coop_Defence")

TUNING.CONTROLLER = GetModConfigData("CONTROLLER")


TUNING.COOP_MOD_RESPAWNS = GetModConfigData("Respawns")




AddPrefabPostInit("world", function(inst)
	GLOBAL.assert( GLOBAL.GetPlayer() == nil )
	local player_prefab = GLOBAL.SaveGameIndex:GetSlotCharacter()

	-- Unfortunately, we can't add new postinits by now. So we have to do
	-- it the hard way...

	GLOBAL.TheSim:LoadPrefabs( {player_prefab} )
	local oldfn = GLOBAL.Prefabs[player_prefab].fn
	GLOBAL.Prefabs[player_prefab].fn = function()
		local inst = oldfn()

--//////////////////////////////////////////////////////////////////--
	if TUNING.CONTROLLER == 2 then
		TUNING.NUMPADCONROLLS = 2
		
		
		if	TUNING.PICK_CHAR_IN_WORLD ==1	then	--GetModConfigData("Character")
			TUNING.PLAYER_3_CHARACTER = "wilson"
			print("===DEFAULT 2nd player")
		end
		
		print("===DEFAULT CONTROLLER CONROLS")

	end
--//////////////////////////////////////////////////////////////////--

		inst:AddComponent("playercontroller_spawner")
		inst:AddComponent("p3stats_saver")
		inst:AddTag("player_coop_team")
		inst.plnum = 1
		
		
		if inst.components.poisonable then 
			inst.components.poisonable.duration = TUNING.POISON_DURATION*3
		end
		local function addlife(inst)
			if GLOBAL.GetClock():IsDay() then
				GetPlayer().components.p3stats_saver:RespawnDelta(1,GetPlayer())
			end
		end
		inst:ListenForEvent( "daytime", function() addlife(inst) end , GetWorld())

		return inst
	end
end)




local function moon_defuser(component)
	local moonphases = 
			{
				"new",
				"quarter",
				"half",
				"threequarter",
				"new", 
			}
	local original_GetMoonPhase = component.GetMoonPhase
	component.GetMoonPhase = function(self)
		if GetPlayer().components.moisture and not GetPlayer().components.driver then --no full moon in RoG
			moonphases = 
			{
				"new",
				"quarter",
				"half",
				"threequarter",
				"new", 
			}
		end


		local phaselength = 2
		local n = #moonphases-1
		
		local idx = math.floor(self.numcycles/phaselength) % (2*n)
		
		if idx >= n then
			idx = n*2 - idx
		end
		
		return moonphases[idx+1]
		
	end
end
AddComponentPostInit("clock", moon_defuser)--- defuses fool moon in RoG



--[[
local function AddEnableMouseWithController( inst )

  if GLOBAL.TheInput.ControllerAttached then
    local input_enablemouse_base = GLOBAL.TheInput.EnableMouse
    GLOBAL.TheInput.EnableMouse = function(self, enable)
      self.mouse_enabled = true
      return true
    end
  else
    return
  end

  local ismouse = false

  local playercontroller = inst.components.playercontroller

  local playercontroller_usingmouse_base = playercontroller.UsingMouse
  playercontroller.UsingMouse = function(self)
    return true
  end

  local input_onmousemove_base = GLOBAL.TheInput.OnMouseMove
  GLOBAL.TheInput.OnMouseMove = function(self, x,y)
    input_onmousemove_base(self, x,y)

    local new_highlight = nil
    playercontroller.LMBaction, playercontroller.RMBaction = playercontroller.inst.components.playeractionpicker:DoGetMouseActions()
    new_highlight = (playercontroller.LMBaction and playercontroller.LMBaction.target) or (playercontroller.RMBaction and playercontroller.RMBaction.target)

    if new_highlight ~= playercontroller.highlight_guy_mouse then
      if playercontroller.highlight_guy_mouse and playercontroller.highlight_guy_mouse:IsValid() then
        if playercontroller.highlight_guy_mouse.components.highlight then
          playercontroller.highlight_guy_mouse.components.highlight:UnHighlight()
        end
      end
      playercontroller.highlight_guy_mouse = new_highlight
    end

    if playercontroller.highlight_guy_mouse and playercontroller.highlight_guy_mouse:IsValid() then
      if not playercontroller.highlight_guy_mouse.components.highlight then
        playercontroller.highlight_guy_mouse:AddComponent("highlight")
      end


      local override = playercontroller.highlight_guy_mouse.highlight_override
      if override then
        playercontroller.highlight_guy_mouse.components.highlight:Highlight(override[1], override[2], override[3])
      else
        playercontroller.highlight_guy_mouse.components.highlight:Highlight()
      end
    else
      playercontroller.highlight_guy_mouse = nil
    end

  end

  local input_controllerattached = GLOBAL.TheInput.ControllerAttached
  GLOBAL.TheInput.ControllerAttached = function(self)
    if ismouse then
      return false
    end
    return input_controllerattached(self)
  end

  local theinput_oncontrol_base = GLOBAL.TheInput.OnControl
  GLOBAL.TheInput.OnControl = function(self, control, digitalvalue, analogvalue)
    if control == GLOBAL.CONTROL_PRIMARY then
      ismouse = true
    end
    if control == GLOBAL.CONTROL_SECONDARY then
      ismouse = true
    end
    theinput_oncontrol_base(self, control, digitalvalue, analogvalue)
  end



  local function OnUpdate(dt)
    if inst.sg.currentstate == inst.sg.sg.states["idle"]
       and ismouse
       and not playercontroller.inst.components.inventory.activeitem
       and not playercontroller.placer
    then
      --print("IDLE !!!")
      ismouse = false
      playercontroller:CancelDeployPlacement()
    end
  end

  local task

  local function StopUpdating()
    if task then
      task:Cancel()
      task = nil
    end
  end

  local function StartUpdating()
    if not task then
        local dt = .5
        task = inst:DoPeriodicTask(dt, function() OnUpdate(dt) end, dt + math.random()*.67)
    end
  end

  StartUpdating()

end

AddSimPostInit( AddEnableMouseWithController )

--]]








