include "inc/cmdsys.plh"
include "inc/conio.plh"

import roguemap
  predef moveplayer
end

struc t_pos
  byte xpos
  byte ypos
end

//
// Player
//

struc t_player
  byte[t_pos]
  byte     angle
  byte     lamp
  byte     fov
  byte     skill
  byte     health
  byte     energy
  word     oil
  byte     gold
  byte     key
  byte     raft
  byte[32] name[32]
end

//
// ASCII Warrior
//

byte[] ascii_warrior
byte = "   |      "
byte = "   T      "
byte = "   \\ ()   "
byte = "    \\^    "
byte = "     #\\_] "
byte = "     #  ] "
byte = "    /\\    "
byte = "   /  \\   "
byte = "   /  /   "
byte = "   =  =   "

//
// Other monsters
//

struc t_other
  byte[t_pos]
  byte kind
  byte tileid
  byte life
  byte power
  word prev_other
  word next_other
end

//
// ASCII Enemy Art
//
byte[] ascii_thief
byte = "          "
byte = "          "
byte = "   ____   "
byte = "  /o_o\\\\  "
byte = "| \\ = //  "
byte = "T /###|\\\\ "
byte = "@==###||| "
byte = "   xxx@// "
byte = "   #|#|\\\\ "
byte = "   #|#| \\\\"

byte[] ascii_ogre
byte = "    :^;   "
byte = " @  }\"{   "
byte = " #  /'\\   "
byte = " ##=<#>=##"
byte = "   ===== #"
byte = "   \\===/ ?"
byte = "   ===== :"
byte = "   ()^() :"
byte = "   () () *"
byte = "  {{} {}} "

byte[] ascii_zombie
byte = "   \\\\/    "
byte = "  {o|0}   "
byte = "  { ^ }   "
byte = "   ###    "
byte = "   /:\\    "
byte = " /%%%%%\\  "
byte = "|%%%//%%|\\"
byte = "|%%%\\%%%||"
byte = "|%%//%%%/|"
byte = "<===|@___/"

byte[] ascii_rogue
byte = "          "
byte = "   \\ /    "
byte = " < - - >  "
byte = " (  \"  )  "
byte = "  \\VVV/   "
byte = "   \\M/    "
byte = "  ^^^^^^  "
byte = " /      \\ "
byte = "( ) ^^>--="
byte = "( ) WW>--//"

word ascii_entity = @ascii_thief, @ascii_ogre, @ascii_zombie, @ascii_rogue
//
// Monster types
//
byte thief    = "Thief",  5
byte ogre     = "Ogre",   20
byte zombie   = "Zombie", 40
byte rogue    = "Rogue",  80
export word entity   = @thief, @ogre, @zombie, @rogue
export word entities = 0

//
// Combat  Return 1 if running away, 0 if end of fight
//

def win#0
  conio:tone(30, 15)
  conio:tone(5, 15)
  conio:tone(5, 15)
  conio:tone(30, 5)
end

export def fight(player, enemy)
  word p_atck, e_atck

  conio:textctrl(ctrlecho, ON)
  repeat
    conio:clear(cls)
    conio:gotoxy(0, 0)
    puts(player+name)
    conio:gotoxy(1, 2)
    puts("Skill  :"); puti(player->skill)
    conio:gotoxy(1, 3)
    puts("Health :"); puti(player->health)
    conio:gotoxy(1, 4)
    puts("Energy :"); puti(player->energy)
    conio:gotoxy(20, 0)
    puts(entity[enemy->kind])
    conio:gotoxy(21, 2)
    puts("Power  :"); puti(enemy->power)
    conio:gotoxy(21, 3)
    puts("Life   :");  puti(enemy->life)
    for e_atck = 0 to 9
      conio:gotoxy(0, 10 + e_atck)
      puts(@ascii_warrior + e_atck * 11)
      conio:gotoxy(20, 10 + e_atck)
      puts(ascii_entity[enemy->kind] + e_atck * 11)
    next
    conio:gotoxy(12, 8); puts("F)ight or R)un?")
    if toupper(conio:getkey()) == 'R'
      conio:textctrl(ctrlecho, OFF)
      return 1
    fin
    //
    // Turn player in random direction
    //
    player->angle = conio:rnd() & 7
    //
    // Calculate attack (with a little random variation)
    //
    p_atck = player->skill + player->energy / 10 - enemy->power / 25  + (conio:rnd() & 7)
    e_atck = enemy->power  - player->skill / 5   - player->energy / 20 + (conio:rnd() & 7)
    if enemy->life > p_atck
      enemy->life = enemy->life - p_atck
    else
      win
      enemy->life = 0
      p_atck      = player->skill + enemy->power / 3
      if p_atck > 100 // Limit skill
        p_atck = 100
      fin
      player->skill = p_atck
      //
      // Unlink dead enemy from entities list
      //
      if enemy == entities
        entities = enemy=>next_other
      fin
      if enemy=>next_other
        enemy=>next_other=>prev_other = enemy=>prev_other
      fin
      if enemy=>prev_other
        enemy=>prev_other=>next_other = enemy=>next_other
      fin
    fin
    if player->health > e_atck
      player->health = player->health - e_atck
    else
      player->energy = 0
      player->health = 0
    fin
    if player->energy >= 4
      player->energy = player->energy - 4
    fin
  until player->health == 0 or enemy->life == 0
  conio:textctrl(ctrlecho, OFF)
  return 0
end

done
