tavli

diff src/board.cc @ 17:16a420432aa3

pieces on the board
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Jun 2015 23:04:37 +0300
parents b1a195c3ee16
children 986c0b76513f
line diff
     1.1 --- a/src/board.cc	Sun Jun 28 08:48:25 2015 +0300
     1.2 +++ b/src/board.cc	Sun Jun 28 23:04:37 2015 +0300
     1.3 @@ -5,10 +5,49 @@
     1.4  #include "meshgen.h"
     1.5  #include "pnoise.h"
     1.6  #include "revol.h"
     1.7 +#include "opt.h"
     1.8 +
     1.9 +
    1.10 +#define HSIZE			1.0
    1.11 +#define VSIZE			(2.0 * HSIZE)
    1.12 +#define BOT_THICKNESS	(HSIZE * 0.01)
    1.13 +#define WALL_THICKNESS	(HSIZE * 0.05)
    1.14 +#define WALL_HEIGHT		(HSIZE * 0.1)
    1.15 +#define GAP				(HSIZE * 0.025)
    1.16 +#define HINGE_RAD		(GAP * 0.5)
    1.17 +#define HINGE_HEIGHT	(VSIZE * 0.075)
    1.18 +#define PIECE_RAD		(0.45 * HSIZE / 5.0)
    1.19 +#define BOARD_OFFSET	(HSIZE / 2.0 + WALL_THICKNESS + HINGE_RAD * 0.25)
    1.20 +#define PIECES_PER_LAYER	5
    1.21 +
    1.22 +
    1.23 +Piece::Piece()
    1.24 +{
    1.25 +	owner = 0;
    1.26 +	slot = prev_slot = -1;
    1.27 +	level = 0;
    1.28 +	move_start = 0;
    1.29 +}
    1.30 +
    1.31 +void Piece::move_to(int slot, int level, bool anim)
    1.32 +{
    1.33 +	int prev_slot = this->slot;
    1.34 +	int prev_level = this->level;
    1.35 +
    1.36 +	this->slot = slot;
    1.37 +	this->level = level;
    1.38 +
    1.39 +	if(anim) {
    1.40 +		this->prev_slot = prev_slot;
    1.41 +		this->prev_level = prev_level;
    1.42 +		move_start = cur_time;
    1.43 +	}
    1.44 +}
    1.45 +
    1.46  
    1.47  Board::Board()
    1.48  {
    1.49 -	puck_obj = 0;
    1.50 +	piece_obj = 0;
    1.51  	clear();
    1.52  }
    1.53  
    1.54 @@ -36,13 +75,77 @@
    1.55  	}
    1.56  	obj.clear();
    1.57  
    1.58 -	delete puck_obj;
    1.59 -	puck_obj = 0;
    1.60 +	delete piece_obj;
    1.61 +	piece_obj = 0;
    1.62  }
    1.63  
    1.64  void Board::clear()
    1.65  {
    1.66 -	memset(slots, 0, sizeof slots);
    1.67 +	memset(hist, 0, sizeof hist);
    1.68 +
    1.69 +	for(int i=0; i<MAX_PIECES; i++) {
    1.70 +		pieces[i].owner = i < PLAYER_PIECES ? MINE : OTHER;
    1.71 +		move_piece(i, -1, false);
    1.72 +	}
    1.73 +}
    1.74 +
    1.75 +void Board::setup()
    1.76 +{
    1.77 +	static const int initial[] = { 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 2 };
    1.78 +
    1.79 +	clear();
    1.80 +
    1.81 +	int id = 0;
    1.82 +	for(int i=0; i<NUM_SLOTS; i++) {
    1.83 +		for(int j=0; j<initial[i]; j++) {
    1.84 +			move_piece(id, i, false);
    1.85 +			move_piece(PLAYER_PIECES + id, NUM_SLOTS - i - 1, false);
    1.86 +			++id;
    1.87 +		}
    1.88 +	}
    1.89 +}
    1.90 +
    1.91 +int Board::slot_pieces(int slot) const
    1.92 +{
    1.93 +	return hist[slot + 1];
    1.94 +}
    1.95 +
    1.96 +bool Board::move_piece(int id, int slot, bool anim)
    1.97 +{
    1.98 +	// TODO do validity checking first
    1.99 +	int prev_slot = pieces[id].slot;
   1.100 +
   1.101 +	pieces[id].move_to(slot, slot_pieces(slot), anim);
   1.102 +	--hist[prev_slot + 1];
   1.103 +	++hist[slot + 1];
   1.104 +	return true;
   1.105 +}
   1.106 +
   1.107 +Vector3 Board::piece_pos(int slot, int level) const
   1.108 +{
   1.109 +	int top_side = slot / 10;
   1.110 +	int sidx = (top_side ? (19 - slot) : slot) % 5;
   1.111 +	int left_side = (top_side ? (19 - slot) : slot) / 5;
   1.112 +
   1.113 +	Vector3 pos;
   1.114 +
   1.115 +	if(left_side) {
   1.116 +		pos.x = -(sidx * HSIZE / 5.0 + BOARD_OFFSET - HSIZE / 2.0) - PIECE_RAD;
   1.117 +	} else {
   1.118 +		pos.x = (4 - sidx) * HSIZE / 5.0 + BOARD_OFFSET - HSIZE / 2.0 + PIECE_RAD;
   1.119 +	}
   1.120 +
   1.121 +	int layer = level / PIECES_PER_LAYER;
   1.122 +	int layer_level = level % PIECES_PER_LAYER;
   1.123 +
   1.124 +	pos.y = (layer + 1) * 0.25 * PIECE_RAD;
   1.125 +
   1.126 +	pos.z = (-VSIZE * 0.5 + PIECE_RAD + PIECE_RAD * 2.0 * layer_level);
   1.127 +	if(top_side) {
   1.128 +		pos.z = -pos.z;
   1.129 +	}
   1.130 +
   1.131 +	return pos;
   1.132  }
   1.133  
   1.134  void Board::draw() const
   1.135 @@ -55,18 +158,15 @@
   1.136  			obj[i]->draw();
   1.137  		}
   1.138  	}
   1.139 +
   1.140 +	for(int i=0; i<MAX_PIECES; i++) {
   1.141 +		Vector3 pos = piece_pos(pieces[i].slot, pieces[i].level);
   1.142 +		piece_obj->xform().set_translation(pos);
   1.143 +		piece_obj->mtl.diffuse = opt.piece_color[pieces[i].owner];
   1.144 +		piece_obj->draw();
   1.145 +	}
   1.146  }
   1.147  
   1.148 -#define HSIZE	1.0
   1.149 -#define VSIZE	(2.0 * HSIZE)
   1.150 -#define BOT_THICKNESS	(HSIZE * 0.01)
   1.151 -#define WALL_THICKNESS	(HSIZE * 0.05)
   1.152 -#define WALL_HEIGHT		(HSIZE * 0.1)
   1.153 -#define GAP				(HSIZE * 0.025)
   1.154 -#define HINGE_RAD		(GAP * 0.5)
   1.155 -#define HINGE_HEIGHT	(VSIZE * 0.075)
   1.156 -#define PIECE_RAD		(0.45 * HSIZE / 5.0)
   1.157 -
   1.158  
   1.159  static const vec2_t piece_cp[] = {
   1.160  		{0, 0.25},
   1.161 @@ -106,7 +206,7 @@
   1.162  
   1.163  		Object *obottom = new Object;
   1.164  		obottom->set_mesh(bottom);
   1.165 -		obottom->xform().set_translation(Vector3(sign * (HSIZE / 2.0 + WALL_THICKNESS + HINGE_RAD * 0.25), 0, 0));
   1.166 +		obottom->xform().set_translation(Vector3(sign * BOARD_OFFSET, 0, 0));
   1.167  		obottom->set_texture(img_field.texture());
   1.168  		obj.push_back(obottom);
   1.169  
   1.170 @@ -222,7 +322,9 @@
   1.171  	opiece->mtl.specular = Vector3(0.8, 0.8, 0.8);
   1.172  	opiece->xform().set_translation(Vector3(0, 0.2, 0));
   1.173  	opiece->set_shader(sdr_phong_notex);
   1.174 -	obj.push_back(opiece);
   1.175 +	//obj.push_back(opiece);
   1.176 +
   1.177 +	piece_obj = opiece;
   1.178  
   1.179  	// meshgen stats
   1.180  	printf("Generated board:\n  %u meshes\n", (unsigned int)obj.size());