File: shape_triangle.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 (455 lines) | stat: -rw-r--r-- 11,294 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#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"

#define TRIANGLE_AREA_FACTOR	1.5197

/******************************************************************************
  triangle_set_dimensions (shape, cell_size, side, point_up)

  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.  If <point_up>, triangle should ... point up.
******************************************************************************/

triangle_set_dimensions (shape, cell_size, side, point_up)
  shape_type *shape;
  int cell_size,
      side,
      point_up;
{
  int i,
      troop_size,
      max_troop_size,
      width, height, center,
      half_cell_size,
      full_side,
      half_side;

  /** Set cell side to normalize cell area to cell_size*cell_size **/
 
  cell_size = (TRIANGLE_AREA_FACTOR * cell_size + 0.5); 
 
  /** Make all cells have odd dimension **/

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

  height = (int)(CONST_SQ3D2 * cell_size + 0.5);
  center = (int)(CONST_SQ3D6 * cell_size + 0.5);

  half_cell_size = cell_size/2;

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

  shape->side =			cell_size;

  shape->center_bound.x =	half_cell_size;
  if (point_up)
    shape->center_bound.y =	height - center;
  else
    shape->center_bound.y =	center;
  shape->size_bound.x =		cell_size;
  shape->size_bound.y =		height;

  shape->center_erase.x =	center - 2;
  shape->center_erase.y =	center - 2;
  shape->corner_erase.x =	shape->center_bound.x - shape->center_erase.x;
  shape->corner_erase.y =	shape->center_bound.y - shape->center_erase.y;
  shape->size_erase.x =		2*shape->center_erase.x + 1;
  shape->size_erase.y =		2*shape->center_erase.y + 1;

  if (point_up)
  {
    shape->center_vertex.x =	0;
    shape->center_vertex.y =	cell_size - center;
    shape->corner_vertex.x =	half_cell_size;
    shape->corner_vertex.y =	0;
  }
  else
  {
    shape->center_vertex.x =	shape->center_bound.x;
    shape->center_vertex.y =	shape->center_bound.y;
    shape->corner_vertex.x =	0;
    shape->corner_vertex.y =	0;
  }

  shape->center_rectangle.x =	(int)(CONST_SQ2D2*shape->center_erase.x);
  shape->center_rectangle.y =	(int)(CONST_SQ2D2*shape->center_erase.y);
  shape->size_rectangle.x =	2*shape->center_rectangle.x;
  shape->size_rectangle.y =	2*shape->center_rectangle.y;

  shape->circle_bound =		shape->center_erase.x;
  shape->area =			cell_size*cell_size/2;

  shape->direction_count =	3;
  shape->direction_factor =	24/shape->direction_count;
  if (point_up)
    shape->angle_offset =	90;
  else
    shape->angle_offset =	30;
  shape->use_secondary =	FALSE;

  /** Define polygon points **/

  if (point_up)
  {
    shape->point_count =	4;
    shape->points[0].x =	0;
    shape->points[0].y =	0;
    shape->points[1].x =	half_cell_size;
    shape->points[1].y =	height;
    shape->points[2].x =	-cell_size+1;
    shape->points[2].y =	0;
    shape->points[3].x =	half_cell_size;
    shape->points[3].y =	-height;
  }
  else
  {
    shape->point_count =	4;
    shape->points[0].x =	0;
    shape->points[0].y =	0;
    shape->points[1].x =	cell_size-1;
    shape->points[1].y =	0;
    shape->points[2].x =	-half_cell_size;
    shape->points[2].y =	height;
    shape->points[3].x =	-half_cell_size;
    shape->points[3].y =	-height;
  }


  shape->troop_shape =		SHAPE_CIRCLE;
  shape->erase_shape =		SHAPE_CIRCLE;

  shape_set_draw_method (shape, side, TRUE);

  shape_set_growth (shape);

  shape_set_troops (shape);

  shape_set_arrows (shape, 0);
}



/******************************************************************************
  triangle_set_center (cell, shape1, shape2, side)

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

triangle_set_center (cell, shape1, shape2, side)
  cell_type *cell;
  shape_type *shape1, *shape2;
  int side;
{
  cell->y_center[side] = cell->y * shape1->size_bound.y;
  cell->x_center[side] = (cell->x/2) * (shape1->size_bound.x-1) +
		shape1->center_bound.x;

  cell->shape_index = (cell->y%2 + cell->x%2)%2;

  if (cell->x%2)
    cell->x_center[side] += shape1->center_bound.x;

  if (cell->shape_index == 0)
    cell->y_center[side] += shape1->center_bound.y;
  else
    cell->y_center[side] += shape2->center_bound.y;
}



/******************************************************************************
  triangle_set_horizons (shape, point_up)

  Set the even and odd horizon arrays for <shape>.  If <point_up>,  triangle
  points up, else it points down.
******************************************************************************/

triangle_set_horizons (shape, point_up)
  shape_type *shape;
  int point_up;
{
  int i, j,
      range, count,
      index;

  index = 0;

  /** Step through each distance less than the view_range **/

  for (range=1; range<=Config->view_range_max; range++)
  {
    if (point_up)
      count = range;
    else
      count = range + 1;

    for (j = -range; j<=range; j++)
    {
      for (i = -count; i<=count; i++)
      {
        if (i == -count || i == count || j == -range || j == range ||
			i == -count+1 || i == count-1)
        {
          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++;
        }
      }

      if (point_up)
      {
        if (j < 0)
          count += 1;
        else if (j > 0)
          count -= 1;
      }
      else
      {
        if (j < -1)
          count += 1;
        else if (j >= 0)
          count -= 1;
      }
    }

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

    shape->horizon_counts[range] = index;
  }

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

  shape->horizon_counts[0] = 0;
}



/******************************************************************************
  triangle_set_connections ()

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

triangle_set_connections ()
{
  int i, j;

  cell_type *cell;

  /** For each cell, establish connections without crossing edges.  The **/
  /** problem with triangle connections is that depending on the row,	**/
  /** 2-D indexing changes.						**/

  for (j=0; j<Config->board_y_size; j++)
  {
    for (i=0; i<Config->board_x_size; i++)
    {
      cell = CELL2(i,j);

      if (cell->shape_index == 0)
      {
        if (i == Config->board_x_size-1)
          cell->connect[TRI_RIGHT_UP] = cell;
        else
          cell->connect[TRI_RIGHT_UP] = CELL2(i+1,j);

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

        if (i == 0)
          cell->connect[TRI_LEFT_UP] = cell;
        else
          cell->connect[TRI_LEFT_UP] = CELL2(i-1,j);
      }
      else
      {
        if (i == Config->board_x_size-1)
          cell->connect[TRI_RIGHT_DOWN] = cell;
        else
          cell->connect[TRI_RIGHT_DOWN] = CELL2(i+1,j);

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

        if (i == 0)
          cell->connect[TRI_LEFT_DOWN] = cell;
        else
          cell->connect[TRI_LEFT_DOWN] = CELL2(i-1,j);
      }
    }
  }  

  /** If wrapping is allowed, set connections across board edges **/
 
  if (Config->enable_all[OPTION_WRAP])
  {
    /** Only do vertical wrapping if even board height **/

    if (Config->board_y_size%2 == 0)
    {
      for (i=0; i<Config->board_x_size; i++)
      {
        cell = CELL2(i,0);

        if (cell->shape_index == 1)
          cell->connect[TRI_UP] = CELL2(i,Config->board_y_size-1);

        cell = CELL2(i,Config->board_y_size-1);

        if (cell->shape_index == 0)
          cell->connect[TRI_DOWN] = CELL2(i,0);
      }
    }

    /** Only do horizontal wrapping if even board width **/

    if (Config->board_x_size%2 == 0)
    {
      for (j=0; j<Config->board_y_size; j++)
      {
        cell = CELL2(0,j);

        if (cell->shape_index == 0)
          cell->connect[TRI_LEFT_UP] = CELL2(Config->board_x_size-1,j);
        else
          cell->connect[TRI_LEFT_DOWN] = CELL2(Config->board_x_size-1,j);

        cell = CELL2(Config->board_x_size-1,j);

        if (cell->shape_index == 0)
          cell->connect[TRI_RIGHT_UP] = CELL2(0,j);
        else
          cell->connect[TRI_RIGHT_DOWN] = CELL2(0,j);
      }
    }
  }  
}



/******************************************************************************
  triangle_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. 
		__________
		|   /\   |
 	 (-1,0)	|  /  \  | (1,0)
		| /0,0 \ |
		|/______\|
		|\      /|
		| \0,1 / |
 	 (-1,1)	|  \  /  | (1,1)
		|___\/___|
******************************************************************************/

triangle_set_selects (shape, select, side)
  shape_type *shape;
  select_type *select;
  int side;
{
  int x, y,
      x_offset, y_offset,
      x_limit, y_limit,
      half_side;
  double fraction;

  select->dimension.x =		shape->size_bound.x;
  select->dimension.y =		2*shape->size_bound.y + 1;
  select->multiplier.x =	2;
  select->multiplier.y =	2;
  select->offset.x =		1;
  select->offset.y =		0;

  /** Set the default values **/

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

  fraction = 1.0/shape->size_bound.y;

  /** Progressing from top to bottom, set the various offsets **/

  for (y=0; y<shape->size_bound.y; y++)
  {
    x_limit = shape->corner_vertex.x -
		(int)(fraction * y * shape->corner_vertex.x);

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

    x_limit = shape->corner_vertex.x +
		(int)(fraction * y * shape->corner_vertex.x);

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

    x_limit = select->dimension.x;

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

  for (; y<select->dimension.y; y++)
  {
    y_offset = y - shape->size_bound.y;

    x_limit = (int)(fraction * y_offset * shape->corner_vertex.x);

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

    x_limit = select->dimension.x - 1 -
		(int)(fraction * y_offset * shape->corner_vertex.x);

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

    x_limit = select->dimension.x;

    for (; x<x_limit; x++)
    {
      select->matrix[y][x].x = 1;
      select->matrix[y][x].y = 1;
    }
  }
}