/*
 * amiga.c
 *
 * This source herein may be modified and/or distributed by anybody who
 * so desires, with the following restrictions:
 *    1.)  No portion of this notice shall be removed.
 *    2.)  Credit shall not be taken for the creation of this source.
 *    3.)  This code is not to be traded, sold, or used for personal
 *         gain or profit.
 *
 */

#include <clib/alib_protos.h>
#include <clib/console_protos.h>
#include <clib/exec_protos.h>
#include <clib/icon_protos.h>
#include <clib/intuition_protos.h>
#include <graphics/display.h>
#include <workbench/startup.h>
#include <stdio.h>
#include "rogue.h"

#define QUALS (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT|IEQUALIFIER_CONTROL)

int LINES = DROWS, COLS = DCOLS;
WINDOW *curscr;
char terminal[DROWS][DCOLS];
int line_changed[DROWS];

struct Screen *Screen;
struct Window *Window;
struct RastPort *RastPort;
struct MsgPort *ConPort;
struct IOStdReq *ConReq;
struct Library *ConsoleDevice;
struct DiskObject *Icon;
int ConDevice = -1;

wbmain(struct WBStartup *wbmsg)
{
int argc = 1;
char *argv[2];
char *saved;

/*  if (wbmsg->sm_ArgList[0].wa_Lock)
    CurrentDir(wbmsg->sm_ArgList[0].wa_Lock);*/
  argv[0] = wbmsg->sm_ArgList[0].wa_Name;

  Icon = GetDiskObject(argv[0]);
  if (Icon) saved = FindToolType(Icon->do_ToolTypes,"SAVEGAME");
  if (saved)
  {
  BPTR lock;

    if (lock = Lock(saved,ACCESS_READ))
    {
      UnLock(lock);
      argc = 2;
      argv[1] = saved;
    }
  }

  main(argc,argv);
  exit(0);
}

initscr()
{
static struct TextAttr topaz8 = { "topaz.font",8,0,0 };
static struct NewScreen new_screen =
 { 0,0,640,STDSCREENHEIGHT,1,0,0,MODE_640,CUSTOMSCREEN,
   &topaz8,"Rogue",0,0 };
static struct NewWindow new_window =
{ 0,0,0,0,0,0,IDCMP_RAWKEY,WFLG_ACTIVATE|WFLG_BORDERLESS|WFLG_RMBTRAP,
  0,0,0,0,0,0,0,0,0,CUSTOMSCREEN };
static USHORT colours[2] = { 0x0000,0x0FFF };

  if ((Screen = OpenScreen(&new_screen)) == 0) exit(1);
  new_window.Width = Screen->Width;
  new_window.Height = Screen->Height;
  new_window.Screen = Screen;
  if ((Window = OpenWindow(&new_window)) == 0)
  {
    endwin();
    exit(1);
  }
  RastPort = Window->RPort;
  LoadRGB4(&Screen->ViewPort,colours,2);
  SetAPen(RastPort,1);
  SetBPen(RastPort,0);
  SetDrMd(RastPort,JAM2);

  if ((ConPort = CreatePort(0,0)) == 0)
  {
    endwin();
    exit(1);
  }
  if ((ConReq = CreateExtIO(ConPort,sizeof(struct IOStdReq))) == 0)
  {
    endwin();
    exit(1);
  }
  if (ConDevice = OpenDevice("console.device",-1,ConReq,0))
  {
    endwin();
    exit(1);
  }
  ConsoleDevice = ConReq->io_Device;
}

endwin()
{
  if (Icon) FreeDiskObject(Icon);
  if (ConDevice == 0) CloseDevice(ConReq);
  if (ConReq) DeleteExtIO(ConReq);
  if (ConPort) DeletePort(ConPort);
  if (Window) CloseWindow(Window);
  if (Screen) CloseScreen(Screen);

  Icon = 0;
  ConDevice = -1;
  ConReq = 0;
  ConPort = 0;
  Window = 0;
  Screen = 0;
}

getch()
{
static struct InputEvent ie;
struct IntuiMessage *imsg;
char buffer[1];
ULONG class;
UWORD code,qual;

  cursor();
  while(1)
  {
    while(imsg = (struct IntuiMessage *)GetMsg(Window->UserPort))
    {
      class = imsg->Class;
      code = imsg->Code;
      qual = imsg->Qualifier;
      ReplyMsg((struct Message *)imsg);
      switch(class)
      {
  case IDCMP_RAWKEY:
    switch(code)
    {
      case 0x4c:
      case 0x3e:
        return ((qual & QUALS) ? 'K' : 'k');
        break;
      case 0x4d:
      case 0x1e:
        return ((qual & QUALS) ? 'J' : 'j');
        break;
      case 0x4e:
      case 0x2f:
        return ((qual & QUALS) ? 'L' : 'l');
        break;
      case 0x4f:
      case 0x2d:
        return ((qual & QUALS) ? 'H' : 'h');
        break;
      case 0x3d:
        return ((qual & QUALS) ? 'Y' : 'y');
        break;
      case 0x3f:
        return ((qual & QUALS) ? 'U' : 'u');
        break;
      case 0x1d:
        return ((qual & QUALS) ? 'B' : 'b');
        break;
      case 0x1f:
        return ((qual & QUALS) ? 'N' : 'n');
        break;
      default:
        ie.ie_Class = IECLASS_RAWKEY;
        ie.ie_Code = code;
        ie.ie_Qualifier = qual;
        if (RawKeyConvert(&ie,buffer,1,0))
        {
    cursor();
    return (buffer[0]);
        }
        break;
    }
    break;
      }
    }
    WaitPort(Window->UserPort);
  }
}

cursor()
{
  SetDrMd(RastPort,COMPLEMENT);
  RectFill(RastPort,RastPort->cp_x,RastPort->cp_y,RastPort->cp_x+RastPort->TxWidth-1,RastPort->cp_y+RastPort->TxHeight-1);
  SetDrMd(RastPort,JAM2);
}

move(row, col)
short row, col;
{
  Move(RastPort,col*RastPort->TxWidth,row*RastPort->TxHeight);
}

mvaddstr(row, col, str)
short row, col;
char *str;
{
  move(row, col);
  addstr(str);
}

addstr(str)
char *str;
{
  while (*str) {
    addch((int) *str++);
  }
}

addch(ch)
register int ch;
{
short row, col;

  col = RastPort->cp_x/RastPort->TxWidth;
  row = RastPort->cp_y/RastPort->TxHeight;
  terminal[row][col] = (char) ch;
  line_changed[row] = 1;
  move(row,col+1);
}

mvaddch(row, col, ch)
short row, col;
int ch;
{
  move(row, col);
  addch(ch);
}

refresh()
{
int i;
short row,col;

  col = RastPort->cp_x/RastPort->TxWidth;
  row = RastPort->cp_y/RastPort->TxHeight;
  for (i = 0; i < DROWS; i++)
  {
    if (line_changed[i])
    {
      Move(RastPort,0,(i*RastPort->TxHeight)+RastPort->TxBaseline);
      Text(RastPort,terminal[i],DCOLS);
      line_changed[i] = 0;
    }
  }
  move(row,col);
}

wrefresh(scr)
WINDOW *scr;
{
  refresh();
}

mvinch(row, col)
short row, col;
{
  move(row, col);
  return((int) terminal[row][col]);
}

clear()
{
  register i, j;

  move(0, 0);
  for (i = 0; i < DROWS; i++)
  {
    for (j = 0; j < DCOLS; j++) terminal[i][j] = ' ';
  }
  SetAPen(RastPort,0);
  RectFill(RastPort,0,0,Window->Width,Window->Height);
  SetAPen(RastPort,1);
}

clrtoeol()
{
  register row, col;

  col = RastPort->cp_x/RastPort->TxWidth;
  row = RastPort->cp_y/RastPort->TxHeight;

  for (; col < DCOLS; col++) terminal[row][col] = ' ';

  SetAPen(RastPort,0);
  RectFill(RastPort,RastPort->cp_x,RastPort->cp_y,Window->Width,RastPort->cp_y+RastPort->TxHeight-1);
  SetAPen(RastPort,1);
}

standout()
{
}

standend()
{
}

crmode()
{
}

noecho()
{
}

nonl()
{
}

get_random_seed()
{
  return (VBeamPos()*(int)terminal);
}

get_player_name()
{
char *name = 0;

  if (Icon == 0) Icon = GetDiskObject("Rogue");
  if (Icon != 0) name = FindToolType(Icon->do_ToolTypes,"PLAYER");
  if (name == 0) name = "Player";
  return (name);
}
