File: shape_square.c

package info (click to toggle)
xbattle 5.4.1-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 736 kB
  • ctags: 1,086
  • sloc: ansic: 9,102; sh: 845; makefile: 493
file content (309 lines) | stat: -rw-r--r-- 7,791 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <stdio.h>

#include "constant.h"
  
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>

#include "extern.h"

/******************************************************************************
  square_set_dimensions (shape, cell_size, side)

  Set all internal values of <shape> of <side>, given canonical size of
  <cell_size>.  This includes the various dimensions and offsets, polygonal
  points, direction vectors, town and troop size mappings, and drawing
  method.
******************************************************************************/

square_set_dimensions (shape, cell_size, side, use_circle)
  shape_type *shape;
  int cell_size,
      side,
      use_circle;
{
  int i,
      troop_size,
      max_troop_size,
      width,
      full_side,
      half_side;

  /** Make all cells have odd dimension **/

  if (cell_size%2 == 0)
    cell_size -= 1;

  /** Set all the relevant dimensions and coordinates **/

  shape->side =			cell_size;

  shape->center_bound.x =	cell_size/2;
  shape->center_bound.y =	cell_size/2;
  shape->center_vertex.x =	cell_size/2;
  shape->center_vertex.y =	cell_size/2;

  if (Config->enable[OPTION_GRID][side])
  {
    shape->center_rectangle.x =	cell_size/2 - 1;
    shape->center_rectangle.y =	cell_size/2 - 1;
    shape->center_erase.x =	cell_size/2 - 1;
    shape->center_erase.y =	cell_size/2 - 1;
  }
  else
  {
    shape->center_erase.x =	cell_size/2;
    shape->center_erase.y =	cell_size/2;
    shape->center_rectangle.x =	cell_size/2;
    shape->center_rectangle.y =	cell_size/2;
  }

  if (Config->enable[OPTION_GRID][side])
  {
    shape->corner_erase.x =	1;
    shape->corner_erase.y =	1;
  }
  else
  {
    shape->corner_erase.x =	0;
    shape->corner_erase.y =	0;
  }
  shape->corner_vertex.x =	0;
  shape->corner_vertex.y =	0;

  shape->size_bound.x =		cell_size;
  shape->size_bound.y =		cell_size;
  if (Config->enable[OPTION_GRID][side])
  {
    shape->size_erase.x =	cell_size - 2;
    shape->size_erase.y =	cell_size - 2;
    shape->size_rectangle.x =	cell_size - 2;
    shape->size_rectangle.y =	cell_size - 2;
  }
  else
  {
    shape->size_erase.x =	cell_size;
    shape->size_erase.y =	cell_size;
    shape->size_rectangle.x =	cell_size;
    shape->size_rectangle.y =	cell_size;
  }

  shape->circle_bound =		shape->center_erase.y;
  shape->area =			cell_size*cell_size;

  shape->direction_count =	4;
  shape->direction_factor =	24/shape->direction_count;
  shape->angle_offset =		45;
  shape->use_secondary =	TRUE;

  /** Define polygon points **/

  shape->point_count =		5;
  shape->points[0].x =		0;
  shape->points[0].y =		0;
  shape->points[1].x =		shape->side-1;
  shape->points[1].y =		0;
  shape->points[2].x =		0;
  shape->points[2].y =		shape->side-1;
  shape->points[3].x =		-(shape->side-1);
  shape->points[3].y =		0;
  shape->points[4].x =		0;
  shape->points[4].y =		-(shape->side-1);

  if (use_circle)
  {
    shape->troop_shape =	SHAPE_CIRCLE;
    shape->erase_shape =	SHAPE_CIRCLE;
  }
  else
  {
    shape->troop_shape =	SHAPE_SQUARE;
    shape->erase_shape =	SHAPE_SQUARE;
  }

  shape_set_draw_method (shape, side, FALSE);

  shape_set_growth (shape);

  shape_set_troops (shape);

  shape_set_arrows (shape, 0);
}



/******************************************************************************
  square_set_center (cell, shape, side)

  Set the center position of <cell> of <side> with <shape>, taking into
  account any row- and column-based shifts.
******************************************************************************/

square_set_center (cell, shape, side)
  cell_type *cell;
  shape_type *shape;
  int side;
{
  if (Config->enable[OPTION_GRID][side])
  {
    cell->x_center[side] = cell->x * (shape->side-1) + shape->side/2;
    cell->y_center[side] = cell->y * (shape->side-1) + shape->side/2;
  }
  else
  {
    cell->x_center[side] = cell->x * shape->side + shape->side/2;
    cell->y_center[side] = cell->y * shape->side + shape->side/2;
  }
}



/******************************************************************************
  square_set_horizons (shape)

  Set the even and odd horizon arrays for <shape>.
******************************************************************************/

square_set_horizons (shape)
  shape_type *shape;
{
  int i, j,
      index,
      range;

  index = 0;

  /** For each possible horizon range **/

  for (range=1; range<=Config->view_range_max; range++)
  {
    /** For each point in a square of side 2*range+1 **/

    for (i = -range; i<=range; i++)
      for (j = -range; j<=range; j++)
      {
        /** If cell on edge of current square **/

        if (i == -range || i == range || j == -range || j == range)
        {
          shape->horizon_even[index][0] = i;
          shape->horizon_even[index][1] = j;
          shape->horizon_odd[index][0] = i;
          shape->horizon_odd[index][1] = j;

          index++;
        }
      }

    /** Set number of cells within given range **/

    shape->horizon_counts[range] = index;
  }

  /** Set 0 horizon, just in case **/

  shape->horizon_counts[0] = 0;
}



/******************************************************************************
  square_set_connections ()

  Set the intercell pointers for the given tiling.
******************************************************************************/

square_set_connections ()
{
  int i, j;

  /** For each cell, establish connections, making sure to exclude	**/
  /** connections across board edges.					**/

  for (j=0; j<Config->board_y_size; j++)
  {
    for (i=0; i<Config->board_x_size; i++)
    {
      if (j != 0)
        CELL2(i,j)->connect[SQUARE_UP] = CELL2(i,j-1);
      else
        CELL2(i,j)->connect[SQUARE_UP] = CELL2(i,j);

      if (j != Config->board_y_size-1)
        CELL2(i,j)->connect[SQUARE_DOWN] = CELL2(i,j+1);
      else
        CELL2(i,j)->connect[SQUARE_DOWN] = CELL2(i,j);

      if (i != 0)
        CELL2(i,j)->connect[SQUARE_LEFT] = CELL2(i-1,j);
      else
        CELL2(i,j)->connect[SQUARE_LEFT] = CELL2(i,j);

      if (i != Config->board_x_size-1)
        CELL2(i,j)->connect[SQUARE_RIGHT] = CELL2(i+1,j);
      else
        CELL2(i,j)->connect[SQUARE_RIGHT] = CELL2(i,j);
    }
  }

  /** If wrapping is allowed, set connections across board edges **/

  if (Config->enable_all[OPTION_WRAP])
  {
    for (i=0; i<Config->board_x_size; i++)
    {
      CELL2(i,0)->connect[SQUARE_UP] = CELL2(i,Config->board_y_size-1);
      CELL2(i,Config->board_y_size-1)->connect[SQUARE_DOWN] = CELL2(i,0);
    }

    for (j=0; j<Config->board_y_size; j++)
    {
      CELL2(0,j)->connect[SQUARE_LEFT] = CELL2(Config->board_x_size-1,j);
      CELL2(Config->board_x_size-1,j)->connect[SQUARE_RIGHT] = CELL2(0,j);
    }
  }
}



/******************************************************************************
  square_set_selects (shape, select, side)

  Set the selection chart which indicates the basic unit of tiling.  For each
  position in the chart, indicate the offset from the canonical cell location. 
  A trivial operation for square tiling.
******************************************************************************/

square_set_selects (shape, select, side)
  shape_type *shape;
  select_type *select;
  int side;
{
  int x, y;

  select->dimension.x =		shape->size_bound.x;
  select->dimension.y =		shape->size_bound.y;
  select->multiplier.x =	1;
  select->multiplier.y =	1;

  if (Config->enable[OPTION_GRID][side])
  {
    select->offset.x =		1;
    select->offset.y =		1;
  }
  else
  {
    select->offset.x =		0;
    select->offset.y =		0;
  }

  for (y=0; y<shape->size_bound.y; y++)
    for (x=0; x<shape->size_bound.x; x++)
    {
      select->matrix[y][x].x =	0;
      select->matrix[y][x].y =	0;
    }
}