/*
--------------------------------------------------------------------------------
Duke Nukem 3D SplitScreen mod
Beta 1
By Bloodclaw

Modified from original for use with EDuke32 SVN.
Requires an EDuke32 binary built with SPLITSCREEN_MOD_HACKS preprocessor flag.
Usage: eduke32 -mx splitscr.con -q2
--------------------------------------------------------------------------------
*/

// Key name definitions
definegamefuncname 11 P2_Move_Forward
definegamefuncname 12 P2_Move_Backward
definegamefuncname 13 P2_Turn_Left
definegamefuncname 14 P2_Turn_Right
definegamefuncname 17 P2_Aim_Up
definegamefuncname 18 P2_Aim_Down
definegamefuncname 19 P2_Fire
definegamefuncname 20 P2_Open
definegamefuncname 21 P2_Jump
definegamefuncname 22 P2_Crouch
definegamefuncname 23 P2_Inventory
definegamefuncname 24 P2_Inventory_Left
definegamefuncname 25 P2_Inventory_Right
definegamefuncname 26 P2_Quick_Kick
definegamefuncname 27 P2_Previous_Weapon
definegamefuncname 28 P2_Next_Weapon


gamevar pid 1 0 // This hold player 2 id
gamevar pnum 0 0 // This is set to player 2 sprite number

// Temp variables
gamevar tmp 0 0
gamevar px 0 0 // Player (both 1 and 2) X coordinate
gamevar py 0 0 // Player Y coordinate
gamevar pz 0 0 // Player Z coordinate
gamevar psect 0 0 // Player sector number
gamevar pang 0 0 // Player angle
gamevar phoriz 0 0 // Player vertical angle
gamevar phorizoff 0 0 // Something similar
gamevar pangcos 0 0 // Cosinus of player angle (used for player 2 movements)
gamevar pangsin 0 0 // Sinus of player angle
gamevar pinput 0 0 // Set to input, used in the PROCESSINPUT event

gamevar pcrosshair 0 0 // Do you like crosshairs? Game options will tell.
gamevar crossscale 0 0 // crosshair scale
gamevar pweapon 0 0 // Player weapon

gamevar p1heat 0 0 // Common night vision googles
gamevar p2heat 0 0


// Crosshair need to be redone for two players
onevent EVENT_DISPLAYCROSSHAIR
  setvar RETURN -1
endevent


define KNEE_WEAPON 0
define PISTOL_WEAPON 1
define SHOTGUN_WEAPON 2
define CHAINGUN_WEAPON 3
define RPG_WEAPON 4
define HANDBOMB_WEAPON 5
define SHRINKER_WEAPON 6
define DEVISTATOR_WEAPON 7
define TRIPBOMB_WEAPON 8
define FREEZE_WEAPON 9
define HANDREMOTE_WEAPON 10
define GROW_WEAPON 11

// Next and previous weapon actions will never pick the expander.
onevent EVENT_NEXTWEAPON
  getplayer[THISACTOR].curr_weapon pweapon
  ifvare pweapon HANDBOMB_WEAPON  // XXX: HANDREMOTE_WEAPON?
  {
      getplayer[THISACTOR].subweapon tmp
      andvar tmp 0xfffff7ff
      setplayer[THISACTOR].subweapon tmp
  }
  else ifvare pweapon SHRINKER_WEAPON
  {
    ifvarg player[THISACTOR].ammo_amount GROW_WEAPON 0
      setvar RETURN SHRINKER_WEAPON  // select expander
    // else select next weapon
  }
endevent

onevent EVENT_PREVIOUSWEAPON
  getplayer[THISACTOR].curr_weapon pweapon

  ifvare pweapon DEVISTATOR_WEAPON
  {
    ifvarg player[THISACTOR].ammo_amount GROW_WEAPON 0
    {
      getplayer[THISACTOR].subweapon tmp
      orvar tmp 0x800
      setplayer[THISACTOR].subweapon tmp
      // select previous weapon
    }
  }
  ifvare pweapon GROW_WEAPON
  {
    ifvarg player[THISACTOR].ammo_amount SHRINKER_WEAPON 0
      setvar RETURN SHRINKER_WEAPON  // select shrinker
    // else select previous weapon
  }
endevent

// Disable all the following keys, need room for player 2
onevent EVENT_LOOKUP // Move Forward
  setvar RETURN -1
endevent

onevent EVENT_LOOKDOWN // Move Backward
  setvar RETURN -1
endevent

onevent EVENT_LOOKLEFT // Turn Left
  setvar RETURN -1
endevent

onevent EVENT_LOOKRIGHT // Turn Right
  setvar RETURN -1
endevent

onevent EVENT_AIMUP // Aim Up
  setvar RETURN -1
endevent

onevent EVENT_AIMDOWN // Aim Down
  setvar RETURN -1
endevent

onevent EVENT_WEAPKEY1 // Fire
  setvar RETURN -1
endevent

onevent EVENT_WEAPKEY2 // Open
  setvar RETURN -1
endevent

onevent EVENT_WEAPKEY3 // Jump
  setvar RETURN -1
endevent

onevent EVENT_WEAPKEY4 // Crouch
  setvar RETURN -1
endevent

onevent EVENT_WEAPKEY5 // Inventory
  setvar RETURN -1
endevent

onevent EVENT_WEAPKEY6 // Inventory Left
  setvar RETURN -1
endevent

onevent EVENT_WEAPKEY7 // Inventory Right
  setvar RETURN -1
endevent

onevent EVENT_WEAPKEY8 // Quick Kick
  setvar RETURN -1
endevent

onevent EVENT_WEAPKEY9 // Previous Weapon
  setvar RETURN -1
endevent

onevent EVENT_WEAPKEY10 // Next Weapon
  setvar RETURN -1
endevent


// Controls for player 2
onevent EVENT_PROCESSINPUT // I had to use this event in case player 1 die
  getinput[THISACTOR].bits pinput

  ifvarand pinput 8
  {
    setinput[pid].bits 8
    ifvarl phoriz 199 addvar phoriz 14
    setplayer[pid].horiz phoriz // For some reasons, only setting the bits won't work
  }
  ifvarand pinput 16
  {
    setinput[pid].bits 16
    ifvarg phoriz 1  subvar phoriz 14
    setplayer[pid].horiz phoriz
  }
  ifvarand pinput 64
  {
    setinput[pid].avel -20
  }
  ifvarand pinput 128
  {
    setinput[pid].avel 20
  }
  ifvarand pinput 8192 // This block and the following one are for moving forward and backward, with X and Y speeds set with help of sinus and cosinus
  {
    setvar RETURN -1
    getplayer[pid].i pnum
    getactor[pnum].ang pang
    cos pangcos pang
    mulvar pangcos 10
    divvar pangcos 64
    sin pangsin pang
    mulvar pangsin 10
    divvar pangsin 64
    setinput[pid].fvel pangcos
    setinput[pid].svel pangsin
  }
  ifvarand pinput 16384
  {
    setvar RETURN -1
    getplayer[pid].i pnum
    getactor[pnum].ang pang
    cos pangcos pang
    mulvar pangcos -10
    divvar pangcos 64
    sin pangsin pang
    mulvar pangsin -10
    divvar pangsin 64
    setinput[pid].fvel pangcos
    setinput[pid].svel pangsin
  }
  // Following code come from Eduke32 wiki
  shiftvarr pinput 8
  andvar pinput 0xF
  switch pinput
  case 1:
    setinput[pid].bits 4
    break
  case 2:
    setinput[pid].bits 536870912
    break
  case 3:
    setinput[pid].bits 1
    break
  case 4:
    setinput[pid].bits 2
    break
  case 5:
    setinput[pid].bits 1073741824
    break
  case 6:
    setinput[pid].bits 1048576
    break
  case 7:
    setinput[pid].bits 134217728
    break
  case 8:
    setinput[pid].bits 4194304
    break
  case 9:
    setinput[pid].bits 2816
    break
  case 10:
    setinput[pid].bits 3072
    break
  endswitch
endevent


// NOTE: status bar is displayed for both players (EDuke32 splitscreen hack)


// orientation bits for the crosshairs for left/right split
define ORIENT 1032  // 26

// gametext args
define P1X 4
define P2X 164

gamevar arg_player_id 0 0

state show_player_view
  getplayer[arg_player_id].posx px
  getplayer[arg_player_id].posy py
  getplayer[arg_player_id].posz pz
  getplayer[arg_player_id].ang pang
  getplayer[arg_player_id].horiz phoriz
  getplayer[arg_player_id].horizoff phorizoff
  addvarvar phoriz phorizoff

  // simulate corresponding part in G_DrawRooms (almost)
  ifvarvare player[arg_player_id].spritebridge 0
  {
    setvarvar tmp player[arg_player_id].truecz
    addvar tmp 1024
    ifvarvarl pz tmp
      setvarvar pz tmp
    else
    {
      setvarvar tmp player[arg_player_id].truefz
      subvar tmp 1024
      ifvarvarg pz tmp
        setvarvar pz tmp
    }
  }

  getplayer[arg_player_id].cursectnum psect

  ifvarg psect -1
  {
    getuserdef .screen_size tmp

    ifvare tmp 0
    {
        // above/below split: first player is above
        ifvare arg_player_id 0
            showviewunbiased px py pz pang phoriz psect 0 0 319 100
        else
            showviewunbiased px py pz pang phoriz psect 0 100 319 199
    }
    else
    {
        // side by side split: first player is left
        ifvare arg_player_id 0
            showviewunbiased px py pz pang phoriz psect 0 0 160 199
        else
            showviewunbiased px py pz pang phoriz psect 160 0 319 199
    }
  }
ends

onevent EVENT_DISPLAYROOMS
    setvar RETURN 1  // don't draw them

    // an EDuke32 hack now:
//    setplayer[THISACTOR].rotscrnang 0 // (almost) Prevent the screen rotation when player 1 die

  // DRAW THE PLAYER VIEWS!
  setvar arg_player_id 0
  state show_player_view
  setvar arg_player_id 1
  state show_player_view

// Draw crosshairs if they are enabled
  getuserdef[THISACTOR].crosshair pcrosshair
  ifvare pcrosshair 1
  {
      getuserdef .crosshairscale crossscale
      shiftvarl crossscale 16, divvar crossscale 100

      getuserdef .screen_size tmp
      ifvarn tmp 0
      {
          // left/right

          rotatesprite 80 100 crossscale 0 CROSSHAIR 0 0 ORIENT 0 0 xdim ydim
          rotatesprite 240 100 crossscale 0 CROSSHAIR 0 0 ORIENT 0 0 xdim ydim
      }
      else
      {
          // above/below
          setvar tmp 8
          rotatesprite 160 50 crossscale 0 CROSSHAIR 0 0 tmp 0 0 xdim ydim
          rotatesprite 160 150 crossscale 0 CROSSHAIR 0 0 tmp 0 0 xdim ydim
      }
  }


  // NOTE: weapons are drawn for both players now (EDuke32 hack)
endevent