/*
 * This file is part of OpenKropki.
 *
 * Copyright (C) 2014-2022 Mateusz Viste
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef KROPKI_CORE_H_SENTINEL
  #define KROPKI_CORE_H_SENTINEL

  #define kropki_EMPTY 0     /* These values */
  #define kropki_PL1_DOT 1   /* must all fit */
  #define kropki_PL2_DOT 2   /* in 2 bits!   */

  #define kropki_NO_WALLS 1

  /* a linked list node used to store walls within a kropki_game struct */
  struct kropki_wallelement {
    unsigned char x1, y1, x2, y2;
    unsigned char player; /* 1 or 2 */
    struct kropki_wallelement *next;
  };

  /* dots are stored as 1 byte per dot in the play field, where:
      bits ......XX = Indicates whether it's a dot for player 1, player 2 or an empty space.
      bits ....XX.. = Indicates whether the field is in captivity (captured by who)
      bits ...X.... = Indicates whether the dot is part of a wall
      bits XXX..... = flags used for internal processing (mostly for applying
                      the flooding algorithm), these bits are not returned by
                      neither kropki_getpos() nor KROPKI_GETPOS() */

  #define KROPKI_DOT1   (1)
  #define KROPKI_DOT2   (2)
  #define KROPKI_DOTX   (3)
  #define KROPKI_CAP1   (4)
  #define KROPKI_CAP2   (8)
  #define KROPKI_CAPX  (12)
  #define KROPKI_WALL  (16)
  #define KROPKI_FLGA  (32)
  #define KROPKI_FLGB  (64)
  #define KROPKI_FLGAB (96)
  #define KROPKI_FLGC (128)
  #define KROPKI_FLGX (224)

  struct kropki_game {
    struct kropki_wallelement *walls;
    unsigned char lastplayer; /* who played last (0, 1 or 2) - 0 = nobody played yet */
    unsigned char lastmovex;  /* lastmove X coordinate */
    unsigned char lastmovey;  /* lastmove Y coordinate */
    unsigned char chgrectx1;  /* coordinates [x1,y1]-[x2,y2] of a rectangle  */
    unsigned char chgrecty1;  /* containing changes that occured after last  */
    unsigned char chgrectx2;  /* move, can be used to selectively redraw the */
    unsigned char chgrecty2;  /* field. updated only after kropki_setpos()   */
    unsigned char nextplayer; /* who plays next (0, 1 or 2) - 0 = anyone can play */
    unsigned char field_width;
    unsigned char field_height;
    unsigned char playablefieldx;  /* x,y coordinates of one possible playable field, */
    unsigned char playablefieldy;  /*     ONLY valid if playablefields > 0 */
    unsigned short playablefields; /* amount of playable fields left (where players can put dots) */
    unsigned short score[2];/* points of player a and b, respectively */
    unsigned char field[1]; /* This element MUST be the last in the struct. The struct */
  };                        /* will be allocated at runtime to accomodate field's size */
                            /* yes, this is safe - see ISO 9899-1990, 6.5.2.1.         */

  /* the inline definition below is meant to be a FAST alternative to kropki_getpos(),
   * where we take care not to ask for out-of-playfield positions, since there is NO
   * boundary check performed and NO NULL check on the game pointer */
  #define KROPKI_GETPOS(g,x,y) (g->field[(g->field_width + 2) * ((y)+1) + ((x)+1)] & (0xff ^ KROPKI_FLGX))

  /* this translates (x,y) coordinates into an offset of the 1-dimensional data array.
   * used for internal computations, you should never need it. */
  #define KROPKI_GOFFS(g, x, y) ((g->field_width + 2) * (y) + (x))

  /* whoplaysfirst can be 1 (player a), 2 (player b) or 0 (anybody can play first) */
  struct kropki_game *kropki_newgame(unsigned char width, unsigned char height, unsigned char whoplaysfirst);
  void kropki_freegame(struct kropki_game *game);
  int kropki_getpos(const struct kropki_game *game, unsigned char x, unsigned char y);
  int kropki_setpos(struct kropki_game *game, unsigned char x, unsigned char y, unsigned char player, unsigned char flags);
  int kropki_pass(struct kropki_game *game, unsigned char player);
  struct kropki_game *kropki_copygame(const struct kropki_game *game, unsigned char flags);
  int kropki_findfirstfreefield(const struct kropki_game *game, unsigned char *resx, unsigned char *resy);

#endif
