File: coord.C

package info (click to toggle)
xevil 1.5.1e-2.2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 4,580 kB
  • ctags: 2,655
  • sloc: cpp: 17,863; makefile: 223; sh: 1
file content (616 lines) | stat: -rw-r--r-- 9,487 bytes parent folder | download | duplicates (2)
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// "coord.C"

/*    Copyright (C) 1994,1995,1996  Steve Hardt

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    Steve Hardt 
    hardts@mit.edu (valid until Nov. 1996)
    hardts@netscape.com
    hardts@alum.mit.edu
    http://graphics.lcs.mit.edu/~hardts/xevil.html
*/

#ifndef NO_PRAGMAS
#pragma implementation "coord.h"
#endif


// Include Files
#include "utils.h"
#include "coord.h"
#include "area.h"



// Functions.
void Stats::add_death(time_t birthTime)
{
  if (enabled) {
    time_t lifespan = time(NULL) - birthTime;
    
    if (deaths > 0)
      aveLifespan = (aveLifespan * deaths + lifespan) / (deaths + 1);
    else
      aveLifespan = lifespan;
    
    deaths++;
  }
}



Boolean Stats::enabled = False;



int Pos::distance(const Pos &p) const
{
  Size diff = *this - p;
  assert (diff.abs_2() >= 0);
  
  return (int)sqrt(diff.width * diff.width + diff.height * diff.height);
}



int Pos::distance_2(const Pos &p) const
{
  Size diff = *this - p;
  
  return diff.width * diff.width + diff.height * diff.height;
}



Dir Size::get_dir()
{
  if (!width && !height)
    return CO_air;

  if (height < 0.5 * width)
    if (height > -2 * width)
      if (height < -0.5 * width)
	return CO_UP_R;
      else
	return CO_R;
    else
      if (height < 2 * width)
	return CO_UP;
      else
	return CO_UP_L;
  else
    if (height < -2 * width)
      if (height < -0.5 * width)
	return CO_L;
      else
	return CO_DN_L;
    else
      if (height < 2 * width)
	return CO_DN_R;
      else
	return CO_DN;
}



void Size::get_dirs_4(Dir &d1,Dir &d2)
{
  if (!width && !height)
    {
      d1 = d2 = CO_air;
      return;
    }

  if (width)
    d1 = (width > 0) ? CO_R : CO_L;
  
  if (height)
    d2 = (height > 0) ? CO_DN : CO_UP;
  else
    d2 = d1;

  if (!width)
    d1 = d2;
}



float Size::cross(const Vel &vel)
{
  return width * vel.dy - height * vel.dx;
}



Boolean Box::overlap(const Loc &l)
{
  if ((l.c >= loc.c) && (l.c < loc.c + dim.colMax) &&
      (l.r >= loc.r) && (l.r < loc.r + dim.rowMax))
    return True;
  else
    return False;
}



Vel Vel::shrink(float k) const
{
  Vel ret = *this;

  if (ret.dx > k)
    ret.dx -= k;
  else if (ret.dx < -k)
    ret.dx += k;
  else
    ret.dx = 0;

  if (ret.dy > k)
    ret.dy -= k;
  else if (ret.dy < -k)
    ret.dy += k;
  else
    ret.dy = 0;

  return ret;
}



void Vel::damp(float k)
{
  if (dx > k)
    dx -= k;
  else if (dx < -k)
    dx += k;
  else
    dx = 0;

  if (dy > k)
    dy -= k;
  else if (dy < -k)
    dy += k;
  else
    dy = 0;
}



Boolean Vel::is_zero() const
{
  Boolean ret;
  if ((dx == 0) && (dy == 0))
    ret = True;
  else
    ret = False;
  return ret;
}



Dir Vel::get_dir() const
{
  if (!dx && !dy)
    return CO_air;

  if (dy < 0.5 * dx)
    if (dy > -2 * dx)
      if (dy < -0.5 * dx)
	return CO_UP_R;
      else
	return CO_R;
    else
      if (dy < 2 * dx)
	return CO_UP;
      else
	return CO_UP_L;
  else
    if (dy < -2 * dx)
      if (dy < -0.5 * dx)
	return CO_L;
      else
	return CO_DN_L;
    else
      if (dy < 2 * dx)
	return CO_DN_R;
      else
	return CO_DN;
}



void Vel::limit(float k)
{
  assert (k >= 0);

  if (dx > k)
    dx = k;
  if (dx < -k)
    dx = -k;
  if (dy > k)
    dy = k;
  if (dy < -k)
    dy = -k;
}



void Vel::get_dirs_4(Dir in[4],Dir out[4],int &inNum,int &outNum)
{
  inNum = 0;
  outNum = 0;

  if (dx > 0)
    {
      in[inNum] = CO_R;
      inNum++;
    }
  else
    {
      out[outNum] = CO_R;
      outNum++;
    }

  if (dy > 0)
    {
      in[inNum] = CO_DN;
      inNum++;
    }
  else
    {
      out[outNum] = CO_DN;
      outNum++;
    }

  if (dx < 0)
    {
      in[inNum] = CO_L;
      inNum++;
    }
  else
    {
      out[outNum] = CO_L;
      outNum++;
    }

  if (dy < 0)
    {
      in[inNum] = CO_UP;
      inNum++;
    }
  else
    {
      out[outNum] = CO_UP;
      outNum++;
    }

  assert(inNum + outNum == 4);
}



Boolean operator == (const Loc &l1, const Loc &l2)
{
  return l1.r == l2.r && l1.c == l2.c;
}



Boolean operator == (const Pos &p1, const Pos &p2)
{
  return p1.x == p2.x && p1.y == p2.y;
}



Boolean operator == (const Vel &v1, const Vel &v2)
{
  return v1.dx == v2.dx && v1.dy == v2.dy;
}



Boolean operator == (const Size &s1, const Size &s2)
{
  if ((s1.width == s2.width) && (s1.height == s2.height))
    return True;
  else
    return False;
}



Boolean operator == (const GLoc &g1,const GLoc &g2)
{
  return (g1.horiz == g2.horiz) && (g1.vert == g2.vert);
}



Boolean operator != (const GLoc &g1,const GLoc &g2)
{
  return (g1.horiz != g2.horiz) || (g1.vert != g2.vert);
}



 Pos operator + (const Pos &pos,const Size &size)
{
  Pos ret;
  ret.x = pos.x + size.width;
  ret.y = pos.y + size.height;
  return ret;
}



 Pos operator - (const Pos &pos,const Size &size)
{
  Pos ret;
  ret.x = pos.x - size.width;
  ret.y = pos.y - size.height;
  return ret;
}



Size operator - (const Pos &p1,const Pos &p2)
{
  Size ret;
  ret.width = p1.x - p2.x;
  ret.height = p1.y - p2.y;
  return ret;
}



Pos operator + (const Pos &pos, const Vel &vel)
{
  Pos ret;
  
  /* We want it to round towards zero so that it does the same thing for 
     something going to the right as to the left. */
  ret.x = pos.x + (int)vel.dx;
  ret.y = pos.y + (int)vel.dy;
  /*  ret.x = pos.x + (int)trunc(vel.dx);
      ret.y = pos.y + (int)trunc(vel.dy);
      */
  return ret;
}

     

Size operator * (float k,const Size &size)
{
  Size ret;
  ret.width = (int)floor(k * size.width);
  ret.height = (int)floor(k * size.height);
  return ret;
}



Vel operator + (const Vel &v1,const Vel &v2)
{
  Vel ret(v1.dx + v2.dx,v1.dy + v2.dy);
  return ret;
}



Vel operator + (const Vel &vel, const Acc &acc)
{
  Vel ret(vel.dx + acc.ddx,vel.dy + acc.ddy);
  return ret;
}

     

Vel operator * (float k,const Vel &vel)
{
  Vel ret(k * vel.dx, k * vel.dy);
  return ret;
}



Vel operator / (float k,const Vel &vel)
{
  Vel ret(k / vel.dx, k / vel.dy);
  return ret;
}



Vel operator + (float k,const Vel &vel)
{
  Vel ret(k + vel.dx,k + vel.dy);
  return ret;
}



Acc operator * (int k,const Acc &acc)
{
  Acc ret;
  ret.ddx = k * acc.ddx;
  ret.ddy = k * acc.ddy;
  return ret;
}



Dir Coord::dir_opposite(Dir dir)
{
  if (dir == CO_air)
    return CO_air;

  assert(dir >= CO_R && dir < CO_DIR_MAX);

  return ((dir - CO_R + CO_DIR_PURE / 2) % CO_DIR_PURE) + CO_R;
}



Dir Coord::movement_dir_4(Dir dir)
{
  switch(dir) {
  case CO_center:
  case CO_air:
  case CO_climb:
  case CO_r:
  case CO_dn:
  case CO_l:
  case CO_up:
    return CO_air;

  case CO_center_R:
  case CO_dn_R:
  case CO_up_R:
  case CO_air_R:
  case CO_climb_R:
  case CO_UP_R:
  case CO_UP_R_R:
  case CO_R:
  case CO_DN_R_R:
  case CO_DN_R:
    return CO_R;

  case CO_air_DN:
  case CO_r_DN:
  case CO_l_DN:
  case CO_climb_DN:
  case CO_DN_DN_R:
  case CO_DN:
  case CO_DN_DN_L:
    return CO_DN;
    
  case CO_center_L:
  case CO_dn_L:
  case CO_up_L:
  case CO_air_L:
  case CO_climb_L:
  case CO_UP_L:
  case CO_UP_L_L:
  case CO_L:
  case CO_DN_L_L:
  case CO_DN_L:
    return CO_L;

  case CO_air_UP:
  case CO_r_UP:
  case CO_l_UP:
  case CO_climb_UP:
  case CO_UP_UP_R:
  case CO_UP:
  case CO_UP_UP_L:
    return CO_UP;

  default:
    assert(0);
    return CO_air;
  }
}



Pos Coord::shot_initial_pos(const Area &area,
			  const Size &shotSize,Dir shotDir)
{
  // Return value;
  Pos ret;

  Pos pos;
  Size size;
  area.get_rect(pos,size);
  
  // Set x coord.
  switch (shotDir) {
  case CO_UP_R:
  case CO_UP_R_R:
  case CO_R:
  case CO_DN_R_R:
  case CO_DN_R:
    if (shotSize.width <= size.width)
      ret.x = pos.x + size.width - shotSize.width;
    else
      ret.x = pos.x;
    break;

  case CO_UP_L:
  case CO_UP_L_L:
  case CO_L:
  case CO_DN_L_L:
  case CO_DN_L:
    if (shotSize.width <= size.width)
      ret.x = pos.x;
    else
      ret.x = pos.x + size.width - shotSize.width;
    break;
    
  case CO_UP_UP_L:
  case CO_UP:
  case CO_UP_UP_R:
  case CO_DN_DN_L:
  case CO_DN:
  case CO_DN_DN_R:
  case CO_air:
    ret.x = (int)(pos.x + size.width * .5 - shotSize.width * .5);
    break;
    
  default:
    assert(0);
  }

  // Set y coord.
  switch (shotDir) {
  case CO_DN_L:
  case CO_DN_DN_L:
  case CO_DN:
  case CO_DN_DN_R:
  case CO_DN_R:
    if (shotSize.height <= size.height)
      ret.y = pos.y + size.height - shotSize.height;
    else
      ret.y = pos.y;
    break;
    
  case CO_UP_L:
  case CO_UP_UP_L:
  case CO_UP:
  case CO_UP_UP_R:
  case CO_UP_R:
    if (shotSize.height <= size.height)
      ret.y = pos.y;
    else
      ret.y = pos.y + size.height - shotSize.height;
    break;

  case CO_UP_R_R:
  case CO_R:
  case CO_DN_R_R:
  case CO_UP_L_L:
  case CO_L:
  case CO_DN_L_L:
  case CO_air:
    ret.y = (int)(pos.y + size.height * .5 - shotSize.height * .5);
    break;
    
  default:
    assert(0);
  }

  return ret;
}