tavli
changeset 17:16a420432aa3
pieces on the board
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 28 Jun 2015 23:04:37 +0300 |
parents | d6209903454b |
children | 986c0b76513f |
files | src/board.cc src/board.h src/game.cc src/game.h src/main.cc src/opt.cc src/opt.h |
diffstat | 7 files changed, 199 insertions(+), 22 deletions(-) [+] |
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());
2.1 --- a/src/board.h Sun Jun 28 08:48:25 2015 +0300 2.2 +++ b/src/board.h Sun Jun 28 23:04:37 2015 +0300 2.3 @@ -5,16 +5,33 @@ 2.4 #include "object.h" 2.5 #include "image.h" 2.6 2.7 -#define NUM_SLOTS 24 2.8 -#define MAX_PUCKS 30 2.9 +#define NUM_SLOTS 20 2.10 +#define PLAYER_PIECES 15 2.11 +#define MAX_PIECES (PLAYER_PIECES * 2) 2.12 2.13 -enum { EMPTY = 0, MINE, OTHER }; 2.14 +enum { MINE, OTHER }; 2.15 + 2.16 +class Piece { 2.17 +private: 2.18 + int prev_slot, prev_level; 2.19 + unsigned long move_start; 2.20 + 2.21 +public: 2.22 + int owner, slot, level; 2.23 + 2.24 + Piece(); 2.25 + 2.26 + void move_to(int slot, int level, bool anim = true); 2.27 +}; 2.28 + 2.29 2.30 class Board { 2.31 private: 2.32 - int slots[NUM_SLOTS][MAX_PUCKS]; 2.33 + Piece pieces[MAX_PIECES]; 2.34 + int hist[NUM_SLOTS + 1]; 2.35 + 2.36 std::vector<Object*> obj; 2.37 - Object *puck_obj; 2.38 + Object *piece_obj; 2.39 2.40 Image img_wood, img_field, img_hinge; 2.41 2.42 @@ -29,6 +46,12 @@ 2.43 void destroy(); 2.44 2.45 void clear(); 2.46 + void setup(); 2.47 + 2.48 + int slot_pieces(int slot) const; 2.49 + bool move_piece(int id, int slot, bool anim = true); 2.50 + 2.51 + Vector3 piece_pos(int slot, int level = 0) const; 2.52 2.53 void draw() const; 2.54 };
3.1 --- a/src/game.cc Sun Jun 28 08:48:25 2015 +0300 3.2 +++ b/src/game.cc Sun Jun 28 23:04:37 2015 +0300 3.3 @@ -8,6 +8,7 @@ 3.4 static void draw_backdrop(); 3.5 3.6 int win_width, win_height; 3.7 +unsigned long cur_time; 3.8 unsigned int sdr_phong, sdr_phong_notex; 3.9 bool wireframe; 3.10 3.11 @@ -52,6 +53,7 @@ 3.12 if(!board.init()) { 3.13 return false; 3.14 } 3.15 + board.setup(); 3.16 3.17 if(!init_scenery()) { 3.18 return false; 3.19 @@ -68,6 +70,7 @@ 3.20 3.21 void game_update(unsigned long time_msec) 3.22 { 3.23 + cur_time = time_msec; 3.24 } 3.25 3.26 void game_display()
4.1 --- a/src/game.h Sun Jun 28 08:48:25 2015 +0300 4.2 +++ b/src/game.h Sun Jun 28 23:04:37 2015 +0300 4.3 @@ -3,6 +3,7 @@ 4.4 4.5 extern int win_width, win_height; 4.6 extern unsigned int sdr_phong, sdr_phong_notex; 4.7 +extern unsigned long cur_time; 4.8 4.9 extern bool wireframe; 4.10
5.1 --- a/src/main.cc Sun Jun 28 08:48:25 2015 +0300 5.2 +++ b/src/main.cc Sun Jun 28 23:04:37 2015 +0300 5.3 @@ -8,6 +8,7 @@ 5.4 #include <GL/glut.h> 5.5 #endif 5.6 #include "game.h" 5.7 +#include "opt.h" 5.8 5.9 static void display(); 5.10 static void reshape(int x, int y); 5.11 @@ -19,10 +20,19 @@ 5.12 int main(int argc, char **argv) 5.13 { 5.14 glutInit(&argc, argv); 5.15 - glutInitWindowSize(1280, 800); 5.16 + 5.17 + if(!init_options(argc, argv)) { 5.18 + return 1; 5.19 + } 5.20 + 5.21 + glutInitWindowSize(opt.xres, opt.yres); 5.22 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); 5.23 glutCreateWindow("Tavli"); 5.24 5.25 + if(opt.fullscreen) { 5.26 + glutFullScreen(); 5.27 + } 5.28 + 5.29 glutDisplayFunc(display); 5.30 glutReshapeFunc(reshape); 5.31 glutKeyboardFunc(keypress);
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/opt.cc Sun Jun 28 23:04:37 2015 +0300 6.3 @@ -0,0 +1,19 @@ 6.4 +#include <stdio.h> 6.5 +#include "opt.h" 6.6 + 6.7 +Options opt; 6.8 + 6.9 +bool init_options(int argc, char **argv) 6.10 +{ 6.11 + // TODO read config files, parse args, etc... 6.12 + 6.13 + opt.xres = 1280; 6.14 + opt.yres = 800; 6.15 + opt.fullscreen = false; 6.16 + opt.def_username = opt.saved_passwd = 0; 6.17 + 6.18 + opt.piece_color[0] = v3_cons(0.3, 0.35, 0.6); 6.19 + opt.piece_color[1] = v3_cons(1.0, 0.75, 0.5); 6.20 + 6.21 + return true; 6.22 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/src/opt.h Sun Jun 28 23:04:37 2015 +0300 7.3 @@ -0,0 +1,19 @@ 7.4 +#ifndef OPT_H_ 7.5 +#define OPT_H_ 7.6 + 7.7 +#include "vmath/vmath.h" 7.8 + 7.9 +struct Options { 7.10 + int xres, yres; 7.11 + bool fullscreen; 7.12 + 7.13 + char *def_username, *saved_passwd; 7.14 + 7.15 + vec3_t piece_color[2]; 7.16 +}; 7.17 + 7.18 +extern Options opt; 7.19 + 7.20 +bool init_options(int argc, char **argv); 7.21 + 7.22 +#endif /* OPT_H_ */