/*
    This is a manual translation from IMP9 to C of Ken Chisholm's "Draft4" program,
    as originally used for students on EMAS in the 70's.  Ken wrote a learning version
    of Draft4 in the style of Samuel's Checkers, but this version forgoes the learning
    code, while using the weights calculated by that code after years of play.
    It has very low dynamic memory requirements and runs within the 1K of Vectrex
    RAM

    The original code is at:
        http://history.dcs.ed.ac.uk/archive/apps/emas-games/gamespd_drafts.txt

    Ken's documentation and other versions are at http://gtoal.com/vectrex/chess/
    (sorry about the 'chess' directory name; I started on a chess project first but
     switched to draughts after a colleague on VFU said he was already working on
     a chess engine. By them I'd posted the URL and didn't want to change it...)

    Graham Toal, 12/17/2020

    TO DO: optimise graphics
    Bugs: when pressing B3 for menu, selecting something like offering a draw justs
    returns immediately to the game; but doing it again handles it properly.  I'm
    not sure if it is a logic bug or just a problem reading the buttons.  I added
    some code to display what buttons are pressed and also what button presses we
    are reading (though the latter may only flash up from one frame so may be hard
    to spot.)  There are other related bugs concerning display of notifications
    that will probably be down to the same cause.  There may have been related issues
    caused by dimming the text out of existence (!) but I think I fixed those.
 */

//#define DEBUG_SET_POSITION // set this to use a hard-coded initial test position

// On plain Vectrex, drawing a grid is flickery at first, until enough pieces
// have been removed from the board that the vector count is down.  (Not a
// problem on the PiTrex)

/* baseline 33.4K cycles */
#define DRAWGRID 1 /* 6.5K cycles with interlace on, 13K with interlace off */

#ifdef PIZERO
// Must only ask for diagnostics on PiTrex ...
//#define DIAGNOSTICS 2           // 1: silent checks  2: loud checks
#define DRAWHATCH 1 /* 5.5K cycles */
#define DRAWHATCHBACK 1 /* 5.5K cycles */
// total 45.6K cycles when both enabled and interlace on, 52.1K with interlace off */
#else
#define INTERLACE 1
#endif

#include "headers.h"
#include "decls.h"
#include "draft4.h"
#include "boardgfx.h"
#include "intro.h"

void Execute_state_machine(void) {
  int8_t I;
  switch (STATE) {            // if this slows us down too much, try non-std jump table... 
#include "actions.h"
#include "messages.h"
  } // switch
}

#include "init.h"

int main (void) {
#ifdef PIZERO
  logfile = NULL; //fopen("/tmp/draft4.log", "w");

  vectrexinit(1);
  v_setName("draft4");
  v_init();
  
  usePipeline = 1;
  bufferType = 2; // 0 = none, 1 = double buffer, 2 = auto buffer (if pipeline is empty -> use previous
  optimizationON = 1; // 0: don't optimise, ie reset to 0 for every line...

  v_setRefresh(60);
#endif

  intro(); // to do: handle diamonds, and fade in board as well...

  Reinitialise_nearly_everything();
  enable_controller_1_x (); enable_controller_1_y ();

  for (;;) { // MAIN LOOP
    Wait_Recal (); Flash++;
    Joy_Digital ();
    Read_Btns ();
    Reset0Ref ();
    Intensity_7F ();
    set_scale (BIG_SCALE);

    Execute_state_machine();

#if defined(DIAGNOSTICS) && DIAGNOSTICS == 2
{   char num[8];
    // show the strength of the computer's position...
    Intensity_7F();
    SHOW_NUM(num, MAX(1));
    Print_Str_d(-120,64, num);
    SHOW_NUM(num, Dim);
    Print_Str_d(-120,-120, num);
}
#endif

  }

  return 0;
} // main