include, plotscr.hsd
include, scancode.hsi
include, test.hsi

define script(2,doortest,none)
define script(3,mapautorun,none)
define script(4,makesuseofretvals,none)
define script(5,loader,none)
define script(6,random battle,2,0,0)
define script(7,each step,3,0,0,0)
define script(8,key trigger,none)
define script(9,reference dance,none)
define script(10,use NPC test,none)
define script(autonumber,sum of some stuff,none)
define script(11,backdrop displayer,none)
define script(12,swap def wep,none)
define script(13,death stalks you at every turn,none)
define script(14,box within script,none)
define script(15,script within box,none)
define script(16,another map while backdroped,none)
define script(17,empty onkey,none)
define script(18,keywaiter,none)
define script(19,teachspelltest,none)
define script(20,forgetspelltest,none)
define script(21,writespelltest,none)
define script(22,erasespelltest,none)
define script(23,readspelltest,none)
define script(24,canlearnspelltest,none)
define script(25,heropositiontest,none)
define script(26,scriptmenutest,none)
define script(27,referencetest,none)
define script(28,mapchangerkeylistener,none)

# globals 0 to 4 are used
global variable (1024,highval)

global variable (5, mylayer)
global variable (6, tileset)

# strings 0-10 are used. 31-32 is for temporaries
define constant (9, zone watch string)
define constant (10, mouse string)

# Used timers:
define constant(0, timer: mouse handler)  # menu click handler
# 9: in "dead time"
# 199: in "killer timer"


script,makesuseofretvals,begin
  show value (sum of some stuff)
end

script,sum of some stuff,begin
  variable(a,b,c)
  set variable(a,hero X(me))
  set variable(b,hero direction(3))
  set variable(c,minutes of play)
  return(a+b+c)
end

plotscript,killer timer,begin
  variable (timer)
  show text box (9) # Oh no! Its sinking!
  set hero picture(0,1)
  set hero palette(0,1)
  set variable(timer,15)
  set tag(6,ON)

  allocate timers (200)
  show string at (4, 150, 180)
  set timer (199, 12, 18, @dead time, 4, timerflag:critical + timerflag:menu + timerflag:battle)
end

#-----------------------------------------------------------------------

script,dead time,begin
  if (not (check tag(6))) then (
    # no idea how you do this...
    set hero picture(0,0)
    set hero palette(0,0)
    show text box(11)# yipee!
    exit script
  )

  show text box(10)#ah! Its crumbling! we are dead!
  wait for text box

  #restart the other timer
  set timer (199, 10)

  wait(20)
  #test that it is deleted
  allocate timers (10)

  #test the gameover flag
  show string at (8, 150, 170)
  set timer (9, 20, 1, timer:gameover, 8, timerflag:critical + timerflag:menu + timerflag:battle)
end

#-----------------------------------------------------------------------

script,doortest,begin
  fight formation(1)
  usedoor(3) 
end

#-----------------------------------------------------------------------

script,map autorun,begin
  highval:=random(4,6)
  set hero palette(0,highval)
  set hero palette(0,highval,inside battle)
  show value(highval)
end

#-----------------------------------------------------------------------

plotscript,wrapping map autorun,begin
  map autorun
  while (current map == map:foolish wrapping map place) do (
    $31="Vehicle Z: "
    append number(31, NPC Z(2))
    show string(31)
    wait
  )
end

#-----------------------------------------------------------------------

# new game script
plotscript, new game, begin
  init mouse
  show mouse cursor
  menu click handler  # installs timer
end

# load game script
script,loader,begin
  init mouse
  show mouse cursor
  menu click handler  # installs timer
  suspend NPCs
  show value(random(-32000,32000))           
  show text box (13)
  wait for text box
  resume NPCs
  get hero name (1,find hero(hero by rank(leader)))
  string color (1,12,240)
  string style (1,string:flat)
  center string at (1,160,0)
  clear string (2)
  append number(2,string length(1))
  show string at (2,0,0)
end

#-----------------------------------------------------------------------

script,random battle,form,set,begin
  show string(string sprintf(31, $31="set=%d formation=%d", set, form))
  #greyscale palette
  fight formation(form)
  #reset palette
end

#-----------------------------------------------------------------------

script,each step,x,y,d,begin
  show value(x*100+y)
  variable(i)
  for (i,1,3) do(
    set hero direction(i,d)
  )
end

#-----------------------------------------------------------------------

# onkeypress on 'nameless other map' and 'foolish wrapping map place'
script,key trigger,begin
  if (current map == map:foolish wrapping map place) then (
    if (key is pressed(key:plus)) then (set npc z(2, npc z(2) + 1))
    if (key is pressed(key:minus)) then (set npc z(2, npc z(2) -- 1))
  )

  if (key is pressed(key:E)) then (
    # test hero elemental commands
    # element 2 is <lit>
    trace value (hero by rank (0), hero base elemental resist as int (hero by rank (0), 2))
    trace value (hero total elemental resist as int (hero by rank (0), 2))
    $ 31 = "Setting element 2 to -37%..."
    trace (31)
    set hero base elemental resist(hero by rank (0), 2, -37)
  )

  if (key is pressed(key:a)) then (
    # test NPC bits
    tracevalue (1)
    trace value(getnpcignoreswalls(99),getnpcobstructs(99),getnpcusable(99),getnpcmoves(99))
    set npc usable(99, false)
    suspend npcs
    wait for scancode(key:a)
    resume npcs
    wait for scancode(key:a)
    set npc moves(99, false)
    wait for scancode(key:a)
    set npc moves(99, true)
    set npc obstructs(99, false)
    wait for scancode(key:a)
    set npc ignores walls(99, true)
    wait for scancode(key:a)
    set npc moves(99, false)
    wait for scancode(key:a)
    set npc moves(99, true)
    show value (100000)
    tracevalue (2)
    trace value(getnpcignoreswalls(99),getnpcobstructs(99),getnpcusable(99),getnpcmoves(99))
    exit script
  )
  if (key is pressed(key:S))
     then (suspend overlay,show text box(14))
  if (key is pressed(key:R))
     then (resume overlay,show text box(15))
  if (key is pressed(key:N))
     then (rename hero by slot(pick hero))
  if (key is pressed(key:1))
     then (play sound(0))
  if (key is pressed(key:2))
     then (pause sound(0))
  if (key is pressed(key:3))
     then (stop sound(0))
  show string at(5,50,10)
  show string at(6,50,58)
  show string at(7,50,66)
  if (not(hero direction(me)==north))
    then($5="not north")
    else($5="north")
  clear string(6)
  append number(6,hero direction(me))
  clear string(7)
  append number(7,not(hero direction(me)==north))

end

#-----------------------------------------------------------------------

script,reference dance,begin
  variable(i,j)

  for(i,0,4) do(
    create NPC(1,6+i,1,random(0,3))
    wait (1)
  )
  wait (5)

  for(i,0,4) do(
    write global(i,NPC reference(1,i))
  )

  for(i,0,4) do(
    walk NPC(read global(i),south,2)
    wait(3)
  )

  wait for all

  for(j,1,20) do(
    for(i,0,4) do(
      set NPC direction(read global(i),(j,mod,4))
      wait(1)
    )
  )


end

#-----------------------------------------------------------------------

script,use NPC test,begin
  use NPC (0)
  show value(get hero level(0))
end

#-----------------------------------------------------------------------

script, backdrop displayer, begin
  show backdrop (random(0,4))
end

#-----------------------------------------------------------------------
script, swap def wep, begin
  if (get default weapon(0)==4),then(
    set default weapon(0,6)
  )else(
    set default weapon(0,4)
  )
end

#-----------------------------------------------------------------------

script, death stalks you at every turn, begin
  show text box(18)
  wait for text box
  game over
end

#-----------------------------------------------------------------------

script,box within script,begin
  walk hero(me,hero direction(me)+2,1)
  show backdrop(5)
  wait for key
  show text box(19)
  wait for text box
  wait for key
  show map
end

#-----------------------------------------------------------------------

script,script within box,begin
  walk hero(me,hero direction(me)+2,1)
  suspend box advance
  show text box(19)
  wait for key
  show backdrop(5)
  wait for key
  show map
  resume box advance
  wait for text box
end

#-----------------------------------------------------------------------

script,another map while backdroped,begin
  walk hero(me,hero direction(me)+2,1)
  show backdrop(5)
  wait for key
  teleport to map (0,0,0)
  wait for key
  show map
end

#-----------------------------------------------------------------------

script,empty onkey,begin
  show value(2000)
end

#-----------------------------------------------------------------------

script,keywaiter,begin
  show value (1000)
  wait for key
  show value (1001)
  wait for key
  show value (1002)
  wait for key
  show value (1003)
  wait for key
  show value (1004)
  wait for key
end

#-----------------------------------------------------------------------

script,teachspelltest,begin
  show text box (20)
  show value(teach spell(3,atk:learnable))
end

script,forgetspelltest,begin
  show text box (21)
  show value(forget spell(3,atk:learnable))
end

script,writespelltest,begin
  show text box (22)
  show value(write spell(3,1,2,atk:unlearnab))
end

script,erasespelltest,begin
  show text box (23)
  show value(write spell(3,1,2,none))
end

script,readspelltest,begin
  if (read spell(3,1,2)==atk:unlearnab) then(
    show text box(24)
  )else(
    show text box(25)
  )
  show no value
end

script,canlearnspelltest,begin
  if (can learn spell(3,atk:learnable)) then(
    show text box(26)
  )else(
    if (knows spell(3,atk:learnable)) then(
      show text box(28)
    )else(
      show text box(27)
    )
  )
  show no value
end

#-----------------------------------------------------------------------

script, heropositiontest, begin

  show text box(29)
  show value(hero by slot(0))
  wait for text box
  show no value

  show text box(30)
  show value(hero by rank(0))
  wait for text box
  show no value

  show text box(31)
  show value(hero by slot(1))
  wait for text box
  show no value

  show text box(32)
  show value(hero by rank(1))
  wait for text box
  show no value

end

#-----------------------------------------------------------------------

script, script menu test, begin
  variable(slot)
  slot := save menu (false)
  if (slot) then (
    save in slot (slot)
  )
  wait(2) # make sure exit-keypress will not be re-read
end

#-----------------------------------------------------------------------

script, reference test, begin
  run script by ID(@empty onkey) # empty onkey
  show value(read global(@highval)) # highval
end

#-----------------------------------------------------------------------

# Lets the player select and click on menu items, and select the current menu
plotscript, menu click handler, begin
  variable(mitem, menu)
  mitem := menu item at pixel(mouse pixel x, mouse pixel y)
  if (mitem) then (menu := parent menu(mitem))
  if (top menu) then (
    string sprintf(10, $10="topmenu:%d selected:%d menu:%d item:%d", top menu, selected menu item, menu, mitem)
    showstringat(10, 0, 0)
  ) else (
    hide string(10)
  )
  if (mitem) then (
    select menu item(mitem)
    if (mouse release(leftbutton)) then (
      if (menu == top menu) then (
        # Clicked on the current menu
        use menu item(mitem)
      ) else (
        bring menu forward(menu)
      )
    )
  )
  set timer (timer: mouse handler, 0, 1, @menu click handler)
end

#-----------------------------------------------------------------------

#Map 0 on-keypress
script, map changer key listener, begin
  if (key is pressed(key:s)) then (
    trace value (write map block (hero x, hero y, read map block (hero x, hero y) xor, 3))
  )
  if (key is pressed(key:a)) then (
    trace value (save map state)
  )
  if (key is pressed(key:g)) then (
    trace value (save map state (mapstate:mapsettings))
  )
  if (key is pressed(key:minus)) then (
    trace value (save map state(mapstate:npcs, 3))
  )
  if (key is pressed(key:plus)) then (
    trace value (load map state(mapstate:all, 3))
  )
  if (key is pressed(key:l)) then (
    trace value (load map state)
  )
  if (key is pressed(key:r)) then (
    trace value (reset map state)
  )
  if (key is pressed(key:d)) then (
    trace value (reset map state (mapstate:all))
  )

  if (key is pressed(key:numpad0)) then (mylayer := -1)
  if (key is pressed(key:numpad1)) then (mylayer := 0)
  if (key is pressed(key:numpad2)) then (mylayer := 1)
  if (key is pressed(key:numpad3)) then (mylayer := 2)

  #Nb: TEST.RPG comes with only 1 tileset
  if (key is pressed(key:tilde)) then (tileset := tileset:default)
  if (key is pressed(key:0)) then (tileset := 0)
  if (key is pressed(key:1)) then (tileset := 1)
  if (key is pressed(key:2)) then (tileset := 2)
  if (key is pressed(key:3)) then (tileset := 3)
  if (key is pressed(key:4)) then (tileset := 4)

  $31 = "layer: "
  append number (31, mylayer)
  $31 + " tileset: "  
  append number (31, tileset)
  show string (31)

  if (key is pressed(key:c)) then (
    change tileset (tileset, mylayer)
  )
  if (key is pressed(key:t)) then (
    load tileset (tileset, mylayer)
  )
  if (key is pressed(key:x)) then (
    $31 = "layer "
    append number (31, mylayer)
    $31 + " using tileset "
    if (mylayer == -1) then (
      append number (31, get map tileset)
    ) else (
      append number (31, layer tileset (mylayer))
    )
    show string (31)
  )
  if (keyval(key:z) >> 1) then (
    variable (temp)
    temp := read zone (17, herox, heroy)
    write zone (17, herox(0), heroy(0), (temp, xor, true))
    if (temp) then (
      $zone watch string = "Erased zone 17"
    ) else (
      $zone watch string = "Wrote zone 17"
    )
  )
  if(keyval(key:i) >> 1) then(
    # use the top right item in the inventory screen
    show value(use item in slot(2))
  )
end

#-----------------------------------------------------------------------

#Map 0 each-step
plotscript, zone watcher, begin
  variable(id, count)
  show string at (zone watch string, 0, 180)
  count := zone at spot(herox, heroy, get count)
  id := zone at spot(herox, heroy, 0)  #just the first
  if (id == 4) then (
    $zone watch string = "BYE!"
    wait (3)
    $zone watch string = ""
    exit script
  )
  if (id) then (
    $zone watch string = "Zone "
    append number (zone watch string, id)
    $zone watch string + " here ("
    get zone name (31, id)
    zone watch string $+ 31
    $zone watch string + ") extra 1="
    append number(zone watch string, get zone extra(id, extra1))
    if (read zone (id, herox, heroy) == 0) then (
      $zone watch string = "OHMIGOSH ERROR"
    )
  ) else (
    $zone watch string = "No Zone"
  )
end

#-----------------------------------------------------------------------

# In 'Testing scripts' menu
plotscript, dump stats, begin
  variable (hero, stat)
  for (hero, 0, 40) do (
    for (stat, 0, 11) do (
      trace value(hero, stat, getherostat(hero,stat,currentstat), getherostat(hero,stat,maximumstat))
    )
  )
end

#-----------------------------------------------------------------------

# In 'Testing scripts' menu
plotscript, show script error, begin
  script error($0="Scripterror called.")
end

# In 'Testing scripts' menu
plotscript, infinite loop, begin
  while(true) do ()
end

# In 'Testing scripts' menu
plotscript, remote vehicle, begin
  use NPC(2)
end

# In 'Testing scripts' menu
plotscript, kill leader with map cure, begin
  map cure(atk:KillAlly, leader)
end

plotscript, dummy death script, begin

end

# In 'Testing scripts' menu
plotscript, fight formation test, begin
  set death script(@dummy death script)
  variable (ret)
  ret := fight formation(0)
  $31 = "Fightformation = "
  appendnumber(31, ret)
  show string(31)
  set death script()
end

# In 'Testing scripts' menu
plotscript, suspend obstruction test, begin
  suspend obstruction
end

# In 'Testing scripts' menu
plotscript, resume obstruction test, begin
  resume obstruction
end

# In 'Testing scripts' menu
plotscript, suspend npc walls test, begin
  suspend npc walls
end

# In 'Testing scripts' menu
plotscript, resume npc walls test, begin
  resume npc walls
end

# In 'Testing scripts' menu
plotscript, suspend npcs test, begin
  suspend npcs
end

# In 'Testing scripts' menu
plotscript, resume npcs test, begin
  resume npcs
end

# In 'Testing scripts' menu
plotscript, send me save, begin
  save in slot(32)
  send email(32)
end

# In 'Testing scripts' menu
plotscript, send me mail, begin
  send email(false, $31="Honk!", $32="Achievement unlocked: sent email")
end

# In 'Testing scripts' menu
plotscript, do game over, begin
  game over
end

# In 'Testing scripts' menu
plotscript, do reset game, begin
  reset game
end

# In 'Testing scripts' menu
plotscript, set battle counter to 0, begin
  set battle countdown(0)
end

# In 'Testing scripts' menu
plotscript, run collider_rpg, begin
  run game($0="COLLIDER.RPG")
end

# In 'Testing scripts' menu
plotscript, swap slot 0 and 3, begin
  swap by position(0, 3)
end

# In 'Testing scripts' menu
# Tip: you can play a sound on Endless Wrapping and other maps by pressing '1'
plotscript, fade sound volume in out, begin
  variable(i, oldvol)
  oldvol := get global sound volume
  show value(oldvol)
  for (i, 0, 255, 20) do (set global sound volume(i), showvalue(get global sound volume), wait)
  for (i, 255, 0, -20) do (set global sound volume(i), showvalue(get global sound volume), wait)
  set global sound volume(oldvol)
end

plotscript, run vikings_rpgdir, begin
  run game($0="..\VIKINGS/vikings.rpgdir")
end

plotscript, make npc wander, arg, npc, begin
  Alter NPC (npc,NPCstat:move type,NPCmovetype:wander)
end

plotscript, add hero test, begin
  variable(slot)
  slot := add hero(0)
  $31="added hero at slot "
  append number(31, slot)
  trace(31)
  wait(2)
end

plotscript, delete hero test, begin
  delete hero(0)
  $31="deleted a yorgo"
  trace(31)
  wait(2)
end

plotscript, step on suspend, begin
  suspend player
  wait(36)
  resume player
end

