/* MegaZeux
 *
 * idops.h - The new set of board manipulation primitives.
 *
 * Copyright (c) 2008 Patrick Walton <pcwalton@uchicago.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef IDOPS_H
#define IDOPS_H

#include "compat.h"

__M_BEGIN_DECLS

#include "board.h"
#include "data.h"
#include "hashtable.h"
#include "robot.h"
#include "world.h"

#define BOARD_OFFSET(b, x, y)   ((x) + (y) * ((b)->board_width))
#define BOARD_OFFSET_X(b, off)  ((off) % (b)->board_width)
#define BOARD_OFFSET_Y(b, off)  ((off) / (b)->board_width)
#define OVERLAY_OFFSET(b, x, y) ((x) + (y) * ((b)->board_width))
#define VLAYER_OFFSET(w, x, y)  ((x) + (y) * ((w)->vlayer_width))

#define BOARD_COLOR(b, off)       (((uint8_t *)((b)->level_color))[off])
#define BOARD_THING(b, off)       (((mzx_thing *)((b)->level_id))[off])
#define BOARD_PARAM(b, off)       (((uint8_t *)((b)->level_param))[off])
#define BOARD_UNDER_COLOR(b, off)                                       \
  (((uint8_t *)((b)->level_under_color))[off])
#define BOARD_UNDER_THING(b, off)                                       \
  (((mzx_thing *)((b)->level_under_id))[off])
#define BOARD_UNDER_PARAM(b, off)                                       \
  (((uint8_t *)((b)->level_under_param))[off])
#define PLAYER_UNDER_COLOR(w)     ((w)->under_player_color)
#define PLAYER_UNDER_THING(w)     ((w)->under_player_id)
#define PLAYER_UNDER_PARAM(w)     ((w)->under_player_param)

#define IS_SPECIAL(t)           !is_storageless(t)

#define RECORD_KEY(ctp)         (((int)(ctp).thing) << 8 | (ctp).param)

typedef struct {
  unsigned int color;
  mzx_thing thing;
  unsigned int param;
} Ctp;

typedef enum {
  LAYER_BOARD,
  LAYER_OVERLAY,
  LAYER_VLAYER
} Layer;

typedef struct {
  int x;
  int y;
  int z;
} Point;

typedef struct {
  int w;
  int h;
} Size;

typedef struct
{
  Ctp ctp;
  int deleted;         /* Were we deleted? */
  Point deleted_at;    /* Where did we get deleted? */
  int inserted;        /* Were we inserted? */
  Point inserted_at;   /* Where were we inserted? */
} SpecialRecord;

typedef HashTable SpecialRecordTable;

Ctp yank(World *world, Board *src_board, Layer layer, Point pt);
void put(World *world, Board *src_board, Layer layer, Point pt, Ctp ctp,
 SpecialRecordTable *records, int unsafe);
void delete_from(World *world, Board *src_board, Layer layer, Point pt,
 SpecialRecordTable *records, int unsafe);
void move_from(World *world, Board *src_board, Layer layer, Point src_pt,
 Point dest_pt);
int copy_block(World *world, Board *src_board, Layer src_layer, Point src_pt,
 Size size, Layer dest_layer, Point dest_pt);
int erase_block(World *world, Board *src_board, Layer dest_layer,
 Point dest_pt, Size size);
void copy_clipped_block(World *world, Board *src_board, Layer src_layer,
 Point src_pt, Size size, Layer dest_layer, Point dest_pt);

#endif
