/[cvs]/wurmi/board.h
ViewVC logotype

Contents of /wurmi/board.h

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations)
Fri Sep 7 16:36:35 2007 UTC (11 years, 1 month ago) by kralovic
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +6 -1 lines
File MIME type: text/plain
beta

1 /*
2 * *******************************************************
3 *
4 * data structures and functions for manipulating board
5 *
6 * board dimensions must be >10
7 * nplayers < 18
8 * player names cannot contain spaces!
9 *
10 * coordinate convention:
11 * 0<=x<=width
12 * 0<=y<=height
13 * [0,0] is upper left corner
14 *
15 *
16 * protocol:
17 * <command> <parameter_1> ... <parameter_n>
18 *
19 * code | meaning | parameters
20 * -----+-----------------+----------------------------
21 * 0 | dump board | <width> <height> <nplayers> <moves> <bitmap> <p1>...<pi>
22 * | | <bitmap> = 33+<board bitmap>
23 * | | <pi> - player i
24 * | | <pi> = name alive length head_x head_y dir moved
25 * | | dir = 0..3 - index into PDIR
26 * | | alive (bool)
27 * | | moved (bool) = already moved in this round
28 * -----+-----------------+-----------------------------
29 * 1 | turn | <player> <dir>
30 * | | <dir> = 0 left | 1 right | 2 no turn
31 * -----+-----------------+-----------------------------
32 * 2 | maintanance |
33 *
34 *
35 * board bitmap:
36 * 0 = empty
37 * 1 = food
38 * 2..4 = reserved
39 * >=5 5(i+1)+dir player i, dir to next part (index into PDIR)
40 * 0 - up
41 * 1 - right
42 * 2 - down
43 * 3 - left
44 * 4 - none
45 * *******************************************************
46 */
47 #ifndef __BOARD_H_
48 #define __BOARD_H_
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <strings.h>
53 #include <string.h>
54
55 #define BUFFERSIZE 2000001
56
57 #define B_DIR_NONE 4
58
59 /* dimensions of tiles */
60 #define TILESIZE 3
61
62 typedef char BOOL;
63
64 typedef struct {
65 int x,y;
66 } POS;
67
68 static const POS PDIR[4]={{0,-1},{1,0},{0,1},{-1,0}};
69 static const int NDIR[4]={2,3,0,1}; /* opposite direction */
70
71
72 /*
73 * player status
74 */
75 typedef struct {
76 char * name;
77 BOOL alive;
78 int length;
79 POS head;
80 int dir; /* where is heading */
81
82 /* ******************** */
83 /* dirty internal stuff */
84
85 unsigned long time_of_death;
86 BOOL moved;
87
88 /* used by arbiter ONLY (not initialized in viewer, excluded from serialization)*/
89 int move_of_death;
90 } PLR_STAT;
91
92
93 /*
94 * main data structure for the board
95 */
96 /* index of the tile [i,j] in the board bitmap */
97 #define BPosIdx(B,i,j) ((i)+(j)*(((B)->dim).x))
98
99 /* the [i,j] tile in the board bitmap */
100 #define BPos(B,x,y) ((B)->board[(BPosIdx((B),(x),(y)))])
101
102 /* give tile coordinates for a position in the board bitmap */
103 #define Bx(B,pos) ((pos)%(((B)->dim).x))
104 #define By(B,pos) ((pos)/(((B)->dim).x))
105
106 #define B_FOOD 1
107 #define B_EMPTY 0
108
109
110 /* create player tile from number and direction */
111 #define BMake(i,d) (((i)+1)*5+(d))
112 #define BNum(x) ((x)/5-1)
113 #define BDir(x) ((x)%5)
114
115 typedef struct {
116 /* number of moves */
117 int moves;
118
119 /* dimensions and bitmap of the board */
120 POS dim;
121 int *board;
122
123 /* players */
124 int nplayers;
125 int live_players;
126 PLR_STAT *plr;
127
128 } BRD;
129
130
131
132
133 #ifdef __cplusplus
134 extern "C" {
135 #endif
136
137
138
139 /*
140 * allocate and setup new board
141 * parameters:
142 * - board
143 * - width, height
144 * - number of foods
145 * - number of players
146 * - player names
147 * - random seed
148 */
149 extern BRD* NewBoard(int, int, int, int, char **, unsigned int);
150
151 /* serialize the board */
152 extern int WriteBoard(BRD *,char *);
153
154 /* allocate and setup board from command string (starting with command 0) */
155 extern BRD * ReadBoard(char *);
156
157 /* deallocate */
158 extern int FreeBoard(BRD *);
159
160 /* create a maintanance string */
161 extern int CreateMaintenance(BRD *, char *);
162
163 /* apply command
164 * command =
165 * move
166 * maintanance (advance move and move idle players)
167 */
168 extern int ApplyCommand(BRD *,char *);
169
170
171
172 #ifdef __cplusplus
173 }
174 #endif
175
176
177 #endif

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.26