#ingame!

def toList(l):
    a = []
    b = eval(l)
    for i in b:
        a.append(int(i))
    return a

#def load(map=""):
HashTable.Clear()
HashTable.Load(System.AppPath +"Data/Mod/" + System.globalvar('mod') + "/maps/" + System.globalvar("map") + ".kbaf")

###################################################
#Rules
###################################################
if HashTable.KeyExists("GameMode") == 1:
    System.SetGlobalVar("gamemode", HashTable("GameMode"))
else:
    System.SetGlobalVar("gamemode", "")


if HashTable.KeyExists("LineCountLimit") == 1:
    if HashTable("LineCountLimit") >= 0:
        System.SetGlobalVar("line_limit", HashTable("LineCountLimit"))
    else:
         System.SetGlobalVar("line_limit", 1000)
else:
    System.SetGlobalVar("line_limit", 1000)

if HashTable.KeyExists("LineWidthLimit") == 1:
    if HashTable("LineWidthLimit") > 0:
        System.SetGlobalVar("max_width", HashTable("LineWidthLimit"))
    else:
         System.SetGlobalVar("max_width", 2000)
else:
    System.SetGlobalVar("max_width", 2000)

###################################################
#
###################################################

#gradient
Gradient.SetColour1(100,HashTable("gradC1"))
Gradient.SetColour2(100,HashTable("gradC2"))
Gradient.SetGradientDirection(HashTable("gradDir"))

#gradient color fade
gradient_color = 1
grad_fade = 1000

#Gradient.ToLayer("grad")
Gradient.ToBack()
System.SetGlobalVar('gradSat', HashTable("gradSat"))

loadSpriteList = str(HashTable("SpriteList"))
loadSpriteList = toList(loadSpriteList)

createBg(int(HashTable("SpriteCount")))

    
progress_gradient.SetColour1(100,HashTable("gradC1"))
progress_gradient.SetColour2(100,HashTable("gradC2"))
    
#music System.lerp(1,2,t)
if HashTable("music") != "":
    System.SetGlobalVar('music', HashTable("music"))
    Audiere.LoadMusic("Data/audio/" + str(System.globalvar('music')), "music", 0, System.lerp(0,System.globalvar('volume_main'), float(System.globalvar('volume_music'))/100.0 ) , 0.0, 1.0,1)

    
Particles2.SetColor(HashTable("gradC1"))
Particles2.SetFadeToColor(HashTable("gradC2"))
    
bg_effect.SetWidth(bg_effect.OriginalWidth)
bg_effect.SetHeight(bg_effect.OriginalHeight)
bg_effect.LoadFrameFromFile(System.AppPath +"Data/Tile/" + str(HashTable("bg")))
bg_effect.SetWidth(System.LayoutWidth * 2)
bg_effect.SetHeight(System.LayoutHeight * 2)
#bg_effect.SetOpacity(HashTable("bg_opacity"))
bg_effect.ToBack()

################################################
# TiledBackground - tb
################################################

tb.SetWidth(64)
tb.SetHeight(64)
tb.LoadFromFile(System.AppPath +"Data/Tile/" + str(HashTable("bg")))
tb.SetWidth(4096)
tb.SetHeight(4096)
tb.SetOpacity(int(HashTable("bg_opacity")) - 5)
#tb.ToLayer("grad")
tb.ToFront()

################################################
#   gravity
################################################
if HashTable.KeyExists("gravity") == 1:
    if HashTable("gravity") < 0:
        p_obstaclePhysics[0].SetGravityForce(int(HashTable("gravity")))

if HashTable.KeyExists("GravityStartDirection"):
    if HashTable("GravityStartDirection") == 1:
        gravity_var = -9.8
    else:
        gravity_var = 9.8

if HashTable.KeyExists("GravityChangeTime"):
    if HashTable("GravityChangeTime") > 0:
        txtgrav.SetVisible(1)
    else:
        txtgrav.SetVisible(0)
else:
    txtgrav.SetVisible(0)

################################################
#   
################################################

for i in cursor:
    i.SetCornerFilter(0,HashTable("gradC1"), 100)
    i.SetCornerFilter(1,HashTable("gradC1"), 100)
    i.SetCornerFilter(2,HashTable("gradC2"), 100)
    i.SetCornerFilter(3,HashTable("gradC2"), 100)
    i.ToFront()

txt = ""
for i in range(HashTable("csapCount")):
    l = eval(HashTable("csapPos" + str(i)))
    l2 = eval(HashTable("uvegPos" + str(i)))
    System.CreateByName("csap", "palackok", l[0], l[1])
        
    SOL.csap.SetAngle(l[2])
    SOL.csap.SetFilter(int(l[3]))
    SOL.csap.ToLayer("physics")
        
    SOL.s.SetValue('unlocked', int(l[4]))
    SOL.s.SetPositionToObject("csap", "1")
    SOL.s.SetFilter(int(l[3]))
    SOL.s.SetAngle(SOL.csap.Angle + 90)
    
    SOL.s.SetValue('id', i)
    if l[4] != 0:
        System.CreateByName("lakat1", "palackok", l[0], l[1])
        SOL.lakat1.SetValue('id', i)
        SOL.lakat1.SetValue('locked', 1)
        SOL.csap_1.ToFront()
        
    SOL.csap_1.SetPositionToObject("csap", "point")
    SOL.csap_1.SetFilter(int(l[3]))
    
    SOL.uveg.SetAngle(l2[2])
    SOL.uveg.SetPosition(l2[0], l2[1])
    SOL.uveg.SetFilter(int(l[3]))
        
    SOL.Box.SetPosition(l2[0], l2[1])
    SOL.Box.SetFilter(int(l[3]))
    SOL.Box.SetVisible(0)

    SOL.uveg2.SetPosition(SOL.uveg.X, SOL.uveg.Y)
    SOL.uveg2.SetAngle(SOL.uveg.Angle)
    SOL.uveg2.SetFilter(int(l[3]))
    
for i in range(HashTable("liftCount")):
    l = eval(HashTable("liftPos" + str(i)))
    l2 = eval(HashTable("lift_ePos" + str(i)))
    System.CreateByName("lift", "physics", l[0], l[1])
    
    SOL.lift.SetAngle(l[2])
    SOL.lift_e.SetPosition(l2[0], l2[1])
    if l[2] < 0:
        SOL.lift.SetValue('rotate', -1)
    else:
        SOL.lift.SetValue('rotate', 1)
    
for i in range(HashTable("hingeCount")):
    l = eval(HashTable("hingePos" + str(i)))
    l2 = eval(HashTable("hinge_start" + str(i)))
    System.CreateByName("hinge", "physics", l[0], l[1])
    
    SOL.hinge.SetAngle(l[2])
    SOL.hinge_start.SetPosition(l2[0], l2[1])

Function.Call("hinge_setup",1)
    
for i in range(HashTable("rotatingCount")):
    l = eval(HashTable("rotating" + str(i)))
    System.CreateByName("rotating", "physics", l[0], l[1])
    
    SOL.rotating.SetAngle(l[2])
    SOL.rotating.SetWidth(72)
    SOL.rotating.SetHeight(8)

    Function.Call("create_rotating", 1)
        
for i in range(HashTable("platCount")):
    l = eval(HashTable("p_obstacle" + str(i)))
    System.CreateByName("p_obstacle", "physics", l[0], l[1])
    
    SOL.p_obstacle.SetAngle(l[2])
    SOL.p_obstacle.SetWidth(l[3])
    
    size = 17

    SOL.p_obstacle.SetHeight(size)

    SOL.plat2.SetPosition(SOL.p_obstacle.ImagePointX("1"), SOL.p_obstacle.ImagePointY("1"))
    SOL.plat3.SetPosition(SOL.p_obstacle.ImagePointX("2"), SOL.p_obstacle.ImagePointY("2"))

    #circles at the end of the platforms
    SOL.plat2.SetWidth(size)
    SOL.plat2.SetHeight(size)
    SOL.plat2.SetAngle(SOL.p_obstacle.Angle)
    SOL.plat3.SetWidth(size)
    SOL.plat3.SetHeight(size)
    SOL.plat3.SetAngle(SOL.p_obstacle.Angle)
        
    SOL.p_obstacle.ToFront()
        
for i in range(HashTable("obj_textCount")):
    l = eval(HashTable("obj_text" + str(i)))
    System.CreateByName("obj_text", 1, l[0], l[1])
    
    #SOL.obj_text.SetText(l[2])
    SOL.obj_text.WriteText(l[2],40)
    SOL.obj_text.SetTextColour(l[3])
    SOL.obj_text.SetWidth(l[4])
    SOL.obj_text.SetHeight(l[5])
    SOL.obj_text.SetValue('t', 100)
    SOL.obj_text.SetValue('opacitydelay', 100)
        
for i in range(HashTable("arrowCount")):
    l = eval(HashTable("arrow" + str(i)))
    System.CreateByName("arrow", "physics", 0, 0)
    
    SOL.Arrow.SetPosition(l[0], l[1])
    SOL.Arrow.SetAngle(System.angle(l[0], l[1], l[2], l[3]))
    
    #path movement setup
    SOL.ArrowPathMovement.ClearNodes()
    SOL.ArrowPathMovement.AddNode(l[0], l[1])
    SOL.ArrowPathMovement.AddNode(l[2], l[3])
        
for i in range(HashTable("pushbtnCount")):
    l = eval(HashTable("pushbtn" + str(i)))
    System.CreateByName("pushbtn", "physics", l[0], l[1])
    
    SOL.pushbtn.SetFilter(int(l[2]))
    
for i in range(HashTable("unlockerCount")):
    l = eval(HashTable("unlocker" + str(i)))
    System.CreateByName("unlocker", "physics", l[0], l[1])
    
    SOL.unlocker.SetFilter(int(l[2]))

##########################################################  
#portals
##########################################################
for i in range(HashTable("portalRedCount")):
    l = eval(HashTable("portalRed" + str(i)))
    System.CreateByName("portalRed2", "palackok", l[0], l[1])

    SOL.portalRed2.SetAngle(l[2])
    SOL.portalRed2.SetFilter(l[3])

for i in range(HashTable("portalBlueCount")):
    l = eval(HashTable("portalBlue" + str(i)))
    System.CreateByName("portalBlue2", "palackok", l[0], l[1])

    SOL.portalBlue2.SetAngle(l[2])
    SOL.portalBlue2.SetFilter(l[3])
    
Function.Call("create_rotating", 1)

for i in p_obstacle:
    i.ToLayer('palackok')
    i.ToFront()


#########################################################

#electric in
for i in range(HashTable("electric1Count")):
    l = eval(HashTable("p_elect" + str(i)))
    System.CreateByName("p_elect", "palackok", l[0], l[1])

    SOL.p_elect.SetAngle(l[2])
    SOL.p_elect.SetAnimFrame(2)
    #SOL.Particles_elect.SetAcc(0-SOL.p_electhitbox.Width)
    #SOL.Particles_elect.SetSpeed(SOL.p_electhitbox.Width)

#electric out
for i in range(HashTable("electric2Count")):
    l = eval(HashTable("p_elect2" + str(i)))
    System.CreateByName("p_elect", "physics", l[0], l[1])

    SOL.p_elect.SetAngle(l[2])
    SOL.p_elect.SetAnimFrame(1)

#saw
for i in range(HashTable("sawCount")):
    l = eval(HashTable("p_saw" + str(i)))
    System.CreateByName("p_saw", "physics", l[0], l[1])

    SOL.p_saweye.SetPosition(SOL.p_saw.X, SOL.p_saw.Y)

    SOL.p_saw.SetAngle(randrange(360))

    #path movement setup
    SOL.p_sawPathMovement.ClearNodes()
  
    if len(l)> 6:

        if l[6]<0:
            SOL.p_sawPathMovement.AddNode(l[2],l[3])
            SOL.p_sawPathMovement.AddNode(l[4],l[5])

#trapdoor
for i in range(HashTable("trapdoorCount")):
    l = eval(HashTable("trapdoor" + str(i)))
    System.CreateByName("p_trapdoor", "physics", l[0], l[1])

    SOL.BoxTrapdoor.SetVisible(0)

    SOL.p_trapdoor.SetAngle(l[2])
    SOL.BoxTrapdoor.SetPosition(l[3], l[4])

    #hinge to
    SOL.p_trapdoorPhysics.HingeToXY(SOL.p_trapdoor.ImagePointX("point"),SOL.p_trapdoor.ImagePointY("point"),90)

    #eyecand
    SOL.p_trapdoor_gear.SetPosition(SOL.p_trapdoor.ImagePointX("point"),SOL.p_trapdoor.ImagePointY("point"))
    SOL.p_trapdoor_gearPhysics.DisableCollision("p_trapdoor")

###############################################################################################################################################
#                                                   REACTOR
#reactor zone
###############################################################################################################################################
for i in range(HashTable("reactorCount")):
    l = eval(HashTable("reactor" + str(i)))
    System.CreateByName("reactor", "physics", l[0], l[1])

    SOL.reactor.SetAngle(l[2])
    SOL.reactor.SetWidth(l[3])
    SOL.reactor.SetHeight(l[4])