File: main.c

package info (click to toggle)
xtux 0.2-2.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,616 kB
  • ctags: 465
  • sloc: ansic: 2,921; makefile: 89
file content (1148 lines) | stat: -rw-r--r-- 26,405 bytes parent folder | download
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
/*
 * main.c: Main source file for XTux, including main game loop and game logic.
 * Copyright 1999 David Lawrence (philaw@camtech.net.au)
 * Last modified July 26.
 */

#include <math.h>
#include <signal.h>
#include "header.h"
#include "entity.h"
#include "main.h"
#include "win.h"
#include "map.h"
#include "input.h"
#include "timing.h"
#include "tile.h"
#include "weap.h"
#include "sound.h"
#include "image.h"

extern struct image_data_struct image_data[NUM_ENTITIES];

int fps = DEFAULT_FPS, frame_len = DEFAULT_FRAME_LEN;
/* Global Flags */
int trigger = 0, old_health = 0, old_weap = 0, change_weap = 0;
int status_dirty = 0, map_dirty = 0;
entity *root, *player, *focus;
weapon players_weapons;
struct timeval last_fired, ct, st, game_init;

int main(int argc, char *argv[])
{

  int i, lastfrlen;
  int game_state = INIT; /* menu, game etc.. */
  int screenx, screeny; /* view screen. */
  struct map_t map;

  for(i = 1; i < argc ; i++) {
    if(argv[i][0] == '-') {
      switch(argv[i][1]) {
      case 'f': /* set FPS */
	if( argc >= i + 1 ) {
	  fps = atoi(argv[++i]);
	  frame_len = 1000000 / fps;
	  fprintf(stderr, "fps = %d\n", fps);
	  break;
	} else {
	  fprintf(stderr, "Bad arguments!\n");
	  exit(1);
	}
	break;
      case 'v': /* Print version number */
	printf("Version: %s\n"
	       "Compiled with options: ", XTUXVERSION);
#if VERBOSE
	printf(" VERBOSE");
#endif
#if FOCUSFOLLOWSNUKE
	printf(" FOCUSFOLLOWSNUKE");
#endif
#if CHARS_RUN_IN_MENU
	printf(" CHARS_RUN_IN_MENU");
#endif
	printf(" Default FPS = %d\n", DEFAULT_FPS);
	exit(0);
      case 'h':
      default:
	printf("usage: %s [OPTIONS]\n\n"
	       "-f NUMBER,\tSet frames/second to NUMBER\n"
	       "-h,\t\tHelp (this screen)\n"
	       "-v,\t\tPrint version.\n\n"
	       "Report bugs to philaw@camtech.net.au\n", argv[0]);
	exit(0);
      }
    }
  }

  gettimeofday( &game_init, NULL );
  /* Seed random number generator */
  srand(game_init.tv_usec);

  if( signal(SIGSEGV, sig_handler) == SIG_ERR )
    die("Can't set signal handler\n", 1);

  /* Initialise root entity */
  if( (root = (entity *)malloc( sizeof(entity) )) == NULL)
    die("Malloc failed in creating root entity!\n", 1);

  player = create_entity(TUX);
  create_window();
  game_state = MENU;

  /* Master loop */
  while( 1 ) {

    switch( game_state ) {
    case MENU:

      menu(map.file_name, &player->type);

      /* Load map */
      if( !load_map(&map) )
	die("Error loading map\n", 1);
#if VERBOSE
      printf("\"%s\" Dimensions(%d,%d) Sucessfully loaded\n", map.name,
	     map.width, map.height);
#endif

      /* Init weapons */
      if( player->type == TUX || player->type == GOWN ) {
	players_weapons.has_cd = 1;
	player->weapon = WEAP_CD;
      }
      else
	players_weapons.has_cd = 0;
      
      if( player->type == BSD ) {
	players_weapons.has_fireball = 1;
	player->weapon = WEAP_FIREBALL;
      }
      else
	players_weapons.has_fireball = 0;
      
      player->health = 100;
      player->mode = ALIVE;
      players_weapons.num_nukes = 0;
      players_weapons.has_pitchfork = 0;
      players_weapons.has_bat = 0;
      player->dir = 4;

      gettimeofday( &last_fired, NULL );

      /* Initialsed so some things will be redrawn */
      map_dirty = 1;
      old_weap = 0;
      old_health = 0;
      focus = player;
      
      game_state = PLAYING;

    case PLAYING:
      /* main game loop */
      while( game_state == PLAYING ) {

	gettimeofday( &st, NULL); /* Time @ start of frame */
	
	if( old_weap != player->weapon ) {
	  draw_status_bar(&map, player->weapon);
	  /* Redraw the health bar */
	  update_caff_bar(player->health);
	  old_weap = player->weapon;
	  status_dirty = 1;
	}
	
	/* Health related things */
	if( old_health != player->health ) {
	  
	  if( player->health <= 0) {
	    fprintf(stderr,"All out of caffeine! You drift off to sleep...\n");
	    game_state = MENU;
	    break;
	  }
	  else if( player->health >= 100 ) {
	    player->health = 100;
	    player->speed = 16; /* May give speed bonus for 100 later */
	  }
	  else if( player->health >= 75 )
	    player->speed = 16;
	  else
	    player->speed = 12;
	  
	  /* Redraw the health bar */
	  update_caff_bar(player->health);
	  old_health = player->health;
	  status_dirty = 1;

	}

	handle_events(&game_state);
	
	update_entities(&map);
	check_entity_collisions(&map);
	update_weapons();
	
	calculate_view_window(&map, focus, &screenx, &screeny);
	update_screen(&map, screenx, screeny);
	draw_entities(screenx, screeny);
	update_window();
	
	gettimeofday( &ct, NULL); /* Current time */
	lastfrlen = time_diff( ct, st ); /* time it took to draw last frame */
	if( lastfrlen < frame_len )
	  delay( frame_len - lastfrlen );
      }
      
      remove_all_entities();
      /* Free map data */
      free(map.base);
      free(map.object);
      break;
    case QUIT:
      die("Quitting Xtux.\n", 0);

    } /* Switch */

  }

} /* main. */


/* Adjust screenx (sx) and screeny (sy) to keep it in view.
   Center on ent if possible (witout going out of bounds) */
void calculate_view_window(struct map_t *map, entity *ent, int *sx, int *sy)
{

  *sx = ent->x - VIEW_W/2 + TILE_W/2;
  *sy = ent->y - VIEW_H/2 + TILE_H/2;

  /* Check view area is inside map boundaries. */
  if( *sx >= map->width * TILE_W - VIEW_W )
    *sx = map->width * TILE_W - VIEW_W;
  else if( *sx < 0 )
    *sx = 0;
  
  if( *sy >= map->height * TILE_H - VIEW_H)
    *sy = map->height * TILE_H - VIEW_H;
  else if( *sy < 0 )
    *sy = 0;

}


void update_entities(struct map_t *map)
{

  /* BASE and OBJECT clip levels */
  int b_clip_level, o_clip_level;
  unsigned char *b_tile, *o_tile;
  entity *ent;

  ent = root->next;
  
  if( NULL == ent )
    die("update_entities: ent == NULL!\n", 1);

  for( ent = root->next ; ent != NULL ; ent=ent->next ) {

    if( ent->class == ITEM ) /* skip items */
      continue;

    /* Clean up gibbed entities */
    if( ent->mode == GIB
#if BLOOD_SPOTS_STAY
	&& ent->type != BLOOD_SPOT
#endif
#if BUNNY_HEADS_STAY
	&& ent->type != BUNNY_HEAD
#endif
#if BUNNY_BODIES_STAY
	&& ent->type != BUNNY_DEAD
#endif
	)
      if( time_diff(ct, ent->last_frame) >= GIB_EXPIRE_TIME ) {
	ent = remove_entity(ent->ent_id);
	continue;
      }

    /* Calculate image to be drawn */
    ent->img_no = calculate_frame(ent);

    /* Get rid of dead entities. After death animation is finished, entities
     will be set to dead in calculate frame */
    if( ent->mode == DEAD ) {
      ent = remove_entity(ent->ent_id);
      continue;
    }

    /* Stop dying entities */
    if( ent->mode == DYING ) {
      ent->x_v = ent->y_v = 0;
      continue;
    }

    /* Remove dead entities and make <= 0 health entities DYING or removed
     according to type */
    if( ent->health <= 0 && ent != player ) {
      if( image_data[ent->type].dying_ani == 1 ) {
	ent->frame = 0;
	ent->mode = DYING;
      } else
	ent = remove_entity(ent->ent_id);
      continue;
    }

    switch( ent->class ) {
    case GOODIE:
    case BADDIE:
      b_clip_level = map->tileset? TECH_BASE_SPECIAL : REAL_BASE_SPECIAL;
      o_clip_level = map->tileset? TECH_OBJECT_CLIP : REAL_OBJECT_CLIP;
      if( ent != player ) /* Don't move player */
	if( ent->type != MSCP || !(rand()%8) ) /* Peons act zombie like */
	  move_enemy(ent, HERO);
      break;
    case NEUTRAL:
      if( ent->mode == GIB ) { /* make bunnyhead fly off */
	if( time_diff(ct, ent->last_frame) >= GIB_DECEL_TIME && ent->speed>0){
	  ent->speed--;
	  ent->last_frame = ct;
	}
      } else if( ent != player )
        critter_ai(ent);
      b_clip_level = map->tileset? TECH_BASE_SPECIAL : REAL_BASE_SPECIAL;
      o_clip_level = map->tileset? TECH_OBJECT_CLIP : REAL_OBJECT_CLIP;
      break;
    case PROJECTILE:
      b_clip_level = map->tileset? TECH_BASE_WALL : REAL_BASE_WALL;
      o_clip_level = map->tileset? TECH_OBJECT_CLIP : REAL_OBJECT_CLIP;
      break;
    default:
      fprintf(stderr, "Tried to move unknown entity, of type %d\n", ent->type);
      break;
    }
    
    b_tile = world_collision(map, BASE, ent, b_clip_level);

    if( in_range(map,ent) && b_tile == NULL ) {
      
      o_tile = world_collision(map, OBJECT, ent, o_clip_level);

      if( o_tile == NULL ) {

	/* Check for hitting stuff like messages etc */
	if( ent == player )
	  special_objects(map, ent);
	ent->x += ent->x_v * ent->speed;
	ent->y += ent->y_v * ent->speed;
	
      } else { /* Hit a clipping object */
	
	switch( ent->class ) {
	case GOODIE:
	case BADDIE:
	case NEUTRAL:
	  ent->x_v = 0;
	  ent->y_v = 0;
	  break;
	case PROJECTILE:
	  if( ent->type == NUKE ) {
	    /* Remove object */
	    blowup(map, ent, (ent->x+ent->width/2)/TILE_W,
		   (ent->y+ent->height/2)/TILE_W, NUKE_RADIUS);
	    ent->speed = 0;
	    ent = remove_entity(ent->ent_id);
	    break;
	  }

#if USESOUND
	  if( ent->type == CD )
	    play_sound(S_CHINK);
#endif
	  /* In the real world, we can destroy some object tiles */
	  if( map->tileset == REAL_TILESET ) {
	    if(*o_tile >= REAL_OBJECT_DESTROY )
	      if( *o_tile < REAL_OBJECT_BROKEN ) /* Break it */
		*o_tile += REAL_OBJECT_BROKEN - REAL_OBJECT_DESTROY;
	    /* else
	        *o_tile = RO_RUBBLE; */
	    map_dirty = 1;
	  }

	  ent = remove_entity(ent->ent_id);
	  break;
	default:
	  fprintf(stderr, "Some unknown entity hit an object!!!\n");
	}

      } /* Hit an object */

      
    } else { /* Hit a wall */
      
      switch( ent->class ) {
      case GOODIE:
      case BADDIE:
      case NEUTRAL:
	ent->x_v = 0;
	ent->y_v = 0;
	break;
      case PROJECTILE:
	switch( ent->type ) {
	case NUKE:
	  blowup(map, ent, (ent->x+ent->width/2)/TILE_W,
		 (ent->y+ent->height/2)/TILE_W, NUKE_RADIUS);
	  ent = remove_entity(ent->ent_id);
	  break;
	case CD:
#if USESOUND
	  play_sound(S_CHINK);
#endif
	case FIREBALL:
	  ent = remove_entity(ent->ent_id);
	  break;
	default:
	  fprintf(stderr, "Some unknown entity hit a wall!!!\n");
	}
      }
      
    } /* hit a wall */
    
  }

} /* Move sprites */



void update_weapons(void)
{

  int fire_ok = 0;

  /* FIXME: Setup reload time so it's weapon dependant */
  gettimeofday(&ct, NULL);
  if( trigger && (time_diff(ct, last_fired) >= RELOAD_TIME)) {

    switch( player->weapon ) {
    case WEAP_CD:
      if( players_weapons.has_cd )
	fire_ok = 1;
      break;
    case WEAP_FIREBALL:
      if( players_weapons.has_fireball )
	fire_ok = 1;
      break;
    case WEAP_NUKE:
      if( players_weapons.num_nukes > 0 ) {
	fire_ok = 1;
	players_weapons.num_nukes--;
      }
    }

    if( fire_ok ) {
      fire_weapon(player, player->weapon );
      last_fired = ct;
    }

  }

  /* FIXME: Do this all properly!!!
     Perhaps we'll use the number keys ala quake/doom etc. */
  if( change_weap ) {

    if( player->type == TUX || player->type == GOWN ) {
      if( player->weapon == WEAP_CD )
	player->weapon = WEAP_NUKE;
      else player->weapon = WEAP_CD;
    } else if( player->type == BSD ) {
      if( player->weapon == WEAP_FIREBALL )
	player->weapon = WEAP_NUKE;
      else player->weapon = WEAP_FIREBALL;
    }
    change_weap = 0;

  }

}


void check_entity_collisions(struct map_t *map)
{

  entity *ent, *other;
  entity *first, *second;
  int entisfirst;

  ent = root;

  do {

    if( ent->next->next == NULL )
      break;
    else {
      ent = ent->next;
      other = ent;
    }
    
    if( ent->mode == DYING || ent->mode == GIB || ent->health <= 0 )
      continue; /* Skip him */

    do {

      other = other->next;

      /* One day we may worry about this, but at the moment
	 it saves a heap of cpu time when there are lots of peons (Dragula) */
      if( ent->type == MSCP && other->type == MSCP )
	continue;

      if( other->mode == DYING || other->mode == GIB || other->health <= 0)
	continue;
      
      /* If x is within range */
      if( abs(ent->x - other->x) <= ent->width + other->width ) {
	if( abs(ent->y - other->y) <= ent->height + other->height) {
	  /* Relies that entity height == width! */
	  if( entity_dist( ent, other ) <= ent->width/2 + other->width/2) {
	    
	    /* set the first type of entity to first, and keep track of what
	       was switched, so we can switch them back. */
	    if( ent->type <= other->type) {
	      first = ent;
	      second = other;
	      entisfirst = 1;
	    } else {
	      first = other;
	      second = ent;
	      entisfirst = 0;
	    }

	    /* Collision has occured, determine what happens via classes then
	     special entity type cases */
	    switch( first->class) {
	    case GOODIE:
	      
	      switch( second->class ) {
		/* Check for GOODIE'S??? */
	      case BADDIE:
		first->health -= 5;
		break;
	      case NEUTRAL:
		/* ??? */
		break;
	      case PROJECTILE:
		if( second->class == PROJECTILE )
		  if( second->owner_id == first->ent_id )
		    break; /* Dont' clip on your own Stuff */
		switch(second->type) {
		case NUKE:
		  blowup(map, second, (second->x+second->width/2)/TILE_W,
			 (second->y+second->height/2)/TILE_W, NUKE_RADIUS);
		  first->health -= NUKEHIT_DMG;
		  break;
		case CD:
		  first->health -= CD_DMG;
		  break;
		case FIREBALL:
		  first->health -= FIREBALL_DMG;
		  break;
		}
		/* Remove projectile */
		second = remove_entity(second->ent_id);
		break;
	      case ITEM:
		switch(second->type) {
		case NUKE_CRATE:
		  /* give player 1 more nuke */
#if USESOUND
		  play_sound(S_AMMO_PICKUP);
#endif
		  players_weapons.num_nukes++;
		  second = remove_entity(second->ent_id);
		  break;
		case CAN:
		  first->health += CAN_HEALTH;
		  second = remove_entity(second->ent_id);
		  break;
		case NODOZE:
		  first->health = 100;
		  second = remove_entity(second->ent_id);
		  break;
		case DISK:
		  /* Do something??? */
		  second = remove_entity(second->ent_id);
		  break;
		case SPOON:
		  second = remove_entity(second->ent_id);
		  draw_text_window("There is no spoon!");
		  break;
		default:
#if VERBOSE
		  fprintf(stderr, "Entity type %d hit entity %d\n",
			  first->type, second->type);
#endif
		  break;
		} /* ITEMS */
		break;
	      } /* Tux & BSD */
	      break;

	    case BADDIE:
	      switch( second->class ) {
	      case BADDIE:
		/* ???? */
		break;
	      case NEUTRAL:
		/* ??? */
		break;
	      case PROJECTILE:

		if( second->owner_id == first->ent_id )
		  break; /* Dont' clip on your own Stuff */

		switch( second->type ) {
		case CD:
		  first->health -= CD_DMG;
		  break;
		case FIREBALL:
		  first->health -= FIREBALL_DMG;
		  break;
		case NUKE:
		  first->health -= NUKEHIT_DMG;
		  blowup(map, second,(second->x+second->width/2)/TILE_W,
			 (second->y+second->height/2)/TILE_W, NUKE_RADIUS);
		  break;
		} /* Projectile */
		second = remove_entity(second->ent_id); /* Remove projectile */
		break;
	      case ITEM:
		/* ??? */
		break;
	      default:
#if VERBOSE
		fprintf(stderr, "Entity type %d hit entity %d\n",
			first->type, second->type);
#endif
		break;
	      } /* MSCP && CLIPPY */
	      break;
	    case NEUTRAL:
	      switch( first->type ) {
	      case BUNNY:
		switch( second->type ) {
		case CD:
		case FIREBALL:

		  first->mode = GIB;
		  first->last_frame = ct;		  
		  first->dir = getdir(first->x_v, first->y_v);
		  first->class = NEUTRAL;

		  second->mode = GIB;
		  second->last_frame = ct;
		  second->dir = getdir(second->x_v, second->y_v);
		  second->class = NEUTRAL;

		  if( (rand()%2) ) {
		    /* Make bunny DEAD */
		    first->type = BUNNY_DEAD;
		    first->img_no = 0; /* Headless */
		    first->speed = 4;
		    /* Turn proj. into head. */
		    second->x = first->x; /* start from body's posn */
		    second->y = first->y;
		    second->type = BUNNY_HEAD;
		    second->class = NEUTRAL;
		    second->speed = 12;

		  } else {
		    /* Make blood spot */
		    first->img_no = 0;
		    first->frame = 0;
		    first->x_v = 0;
		    first->y_v = 0;
		    first->type = BLOOD_SPOT;
		    /* Make bunny DEAD */
		    second->type = BUNNY_DEAD;
		    second->x = first->x;
		    second->y = first->y;
		    second->speed = 4;
		    /* Make remaining bit of bunny the smashed up body */
		    second->img_no = 1;
		  }
		  break;
		default:
		  break;
		}
		break; /* Bunny */
		/* Bunny head is not clipped */
	      }
	      break; /* NEUTRAL */
	    case PROJECTILE:
	      if( first->owner_id == second->ent_id )
		break; /* Don't clip on Owner */
	      switch( second->class ) {
	      case PROJECTILE:
		switch( second->type ) {
		case CD:
		  second->health -= CD_DMG;
		  first = remove_entity(first->ent_id);
		  break;
		case FIREBALL:
		  second->health -= FIREBALL_DMG;
		  first = remove_entity(first->ent_id); /* Remove proj. */
		  break;
		case NUKE:
		  switch( first->type ) {
		  case NUKE:
		    /* Twice the carnage! */
		    blowup(map, second,(second->x+second->width/2)/TILE_W,
			   (second->y+second->height/2)/TILE_W, NUKE_RADIUS*2);
		    second = remove_entity(second->ent_id);
		    break;
		  case CD:
		    second->health -= CD_DMG;
		    break;
		  case FIREBALL:
		    second->health -= FIREBALL_DMG;
		    break;
		  } /* switch( first->type ) */
		  first = remove_entity(first->ent_id);
		}
	      }
	    }
	   
	    /* Put ent and other back */
	    if( entisfirst ) {
	      ent = first;
	      other = second;
	    } else {
	      ent = second;
	      other = first;
	    }
 
	  } /* Collision occured */
	}
      }
      
    }while( other->next != NULL ); /* Checked entities collision with */
    
  } while( ent->next != NULL && ent->next->next != NULL );

  
}


/* returns pixels between the CENTER of ent0 & ent1 */
int entity_dist(entity *ent0, entity *ent1)
{

  int x_dist, y_dist;

  x_dist = (ent0->x+ent0->width/2) - (ent1->x+ent1->width/2);
  y_dist = (ent0->y+ent0->height/2) - (ent1->y+ent1->height/2);

  return (int)sqrt( x_dist * x_dist + y_dist * y_dist );

}


/* Returns true if entity is inside the map */
int in_range(struct map_t *map, entity *ent)
{

  int x,y;

  /* X & Y positions next frame. ent->[width|height] is there
     to allow you to go off the screen a little (enough to get
     through tight gaps with nearby walls) */
  x = ent->width + ent->x + ent->x_v * ent->speed;
  y = ent->height + ent->y + ent->y_v * ent->speed;

  /* Is x in range? */
  if( x <= 0 || x >= map->width * TILE_W )
    return 0;

  /* is y? */
  if( y <= 0 || y >= map->height * TILE_H )
    return 0;

  return 1; /* Still inside the map */

}


/* Returns what entity will hit in next turn ( if >= t ) */
unsigned char *world_collision(struct map_t *map, int layer, entity *ent, int t)
{

  int x,y, x_v, y_v;
  unsigned char *tile;
  unsigned char *ptr;

  if( layer == BASE )
    ptr = map->base;
  else if( layer == OBJECT )
    ptr = map->object;
  else 
    die("world_collision: layer is not BASE or OBJECT!!\n", 1);

  if( NULL == ent ) {
    fprintf(stderr, "valid move: Entity is NULL!\n");
    return 0;
  }

  x_v = ent->x_v * ent->speed;
  y_v = ent->y_v * ent->speed;

  x = ent->x + x_v; /* desired x position. */
  y = ent->y + y_v; /* desired y position. */

  /* top left. */
  tile = (ptr + (y+ent->height/2)/TILE_H * map->width + x/TILE_W);
  if( *tile >= t )
    return tile;
  /* top right. */
  tile = (ptr + (y+ent->height/2)/TILE_H * map->width + (x+ent->width)/TILE_W);
  if( *tile >= t )
    return tile;
  /* bottom left. */
  tile = (ptr + (y+3*ent->height/2)/TILE_H * map->width + x/TILE_W);
  if( *tile >= t )
    return tile;
  /* bottom right. */
  tile = (ptr + (y+3*ent->height/2)/TILE_H * map->width +
	  (x+ent->width)/TILE_W);
  if( *tile >=t)
    return tile;

  return NULL; /* All clear. */

}


/* Not in world_collisions because it only needs to be called for the player */
void special_objects(struct map_t *map, entity *ent)
{

  char msg_str[MAXMSGLEN];
  unsigned char *tile;
  int x,y, i, mesg_no;

  /* Work out positions next frame */
  x = ent->x + ent->x_v * ent->speed;
  y = ent->y + ent->y_v * ent->speed;

  for( i=0 ; i<4 ; i++ ) {

    switch( i ) {
    case 0: /* Top Left */
      tile = (map->object + (y+ent->height/2)/TILE_H * map->width + x/TILE_W);
      break;
    case 1: /* top right. */
      tile = (map->object + (y+ent->height/2)/TILE_H * map->width +
	      (x+ent->width)/TILE_W);
      break;
    case 2: /* bottom left. */
      tile = (map->object + (y+3*ent->height/2)/TILE_H*map->width + x/TILE_W);
      break;
    case 3: /* bottom right. */
      tile = (map->object + (y+3*ent->height/2)/TILE_H * map->width +
	      (x+ent->width)/TILE_W);
    }

    if( O_MSG1 <= *tile && *tile <= O_MSG9 ) {

#if VERBOSE
      printf("Hit message: %d\n", *tile);
#endif

      mesg_no = *tile - O_MSG0;
      get_message(map->file_name, mesg_no, msg_str);

      draw_text_window(msg_str);
      *tile = O_NULL; /* Remove read message */
      map_dirty = 1;
      
    }

  }

}


/* Do the ai for entity ent */
void move_enemy(entity *enemy, int bravery)
{

  /* Try and get close to the player */
  if( player->x > enemy->x )
    enemy->x_v = 1;
  else if( player->x < enemy->x )
    enemy->x_v = -1;
  else enemy->x_v = 0;
  
  if( player->y > enemy->y )
    enemy->y_v = 1;
  else if( player->y < enemy->y )
    enemy->y_v = -1;
  else enemy->y_v = 0;
  
  enemy->x_v *= bravery;
  enemy->y_v *= bravery;

  if( enemy->type == FLAMER )
    enemy_proj_attack(enemy);

  /* Melee attack??? */

}

void enemy_proj_attack(entity *enemy)
{

  fire_weapon(enemy, WEAP_FIREBALL);

}


void critter_ai(entity *critter)
{

  int modifier;

  /* Run away if player is near! */
  if( entity_dist(critter, player) <= SCARED_DIST ) {
    critter->mode = ALIVE; /* Stop grooming */
    if( critter->type == BUNNY )
      critter->speed = 8;
    else
      critter->speed = 6;
    move_enemy(critter, COWARD);
  } 
  else if( critter->mode == ALIVE && (critter->x_v || critter->y_v) ) {
    if( !(rand() % (2*fps)) ) {
      critter->x_v = critter->y_v = 0; /* Stop him after a bit of a run */
      critter->mode = GROOMING;
      if( critter->type == BUNNY )
	critter->speed = 4;
      else
	critter->speed = 3;
      critter->frame = 0;
    }
  } else if( critter->mode == ALIVE ) {
    
    critter->mode = GROOMING;
    critter->speed = 4;
    critter->x_v = 0;
    critter->y_v = 0;

  }

  if( critter->mode == GROOMING )
    /* Move around a bit */
    if( !(rand() % (3*fps)) ) {
      
      critter->mode = ALIVE;
      modifier = rand()%2? 1 : -1;
      critter->x_v = rand()%2 * modifier;
      critter->y_v = rand()%2 * modifier;

    }

}


void fire_weapon(entity *ent, int type)
{

  int x_v = 0, y_v = 0;
  entity *new_projectile;

  switch( ent->dir ) {
  case 0:
    y_v = -1;
    break;
  case 1:
    y_v = -1;
    x_v = 1;
    break;
  case 2:
    x_v = 1;
    break;
  case 3:
    x_v = 1;
    y_v = 1;
    break;
  case 4:
    y_v = 1;
    break;
  case 5:
    x_v = -1;
    y_v = 1;
    break;
  case 6:
    x_v = -1;
    break;
  case 7:
    x_v = -1;
    y_v = -1;
    break;
  default:
    die("Direction is > 7!\n", 1);
  }

  switch( type ) {
  case WEAP_CD:
    new_projectile = create_entity(CD);
    new_projectile->speed = (ent->x_v || ent->y_v)*ent->speed + 25;
    break;
  case WEAP_FIREBALL:
    new_projectile = create_entity(FIREBALL);
    new_projectile->speed = (ent->x_v || ent->y_v)*ent->speed + 25;
    break;
  case WEAP_NUKE:
    new_projectile = create_entity(NUKE);
    new_projectile->dir = ent->dir;
    new_projectile->speed = 8;
    new_projectile->health = 25;
#if FOCUSFOLLOWSNUKE
    /* Uncomment if you want to keep on the old nuke
       if( focus == player ) */
    focus = new_projectile;
#endif
    break;
  default:
    fprintf(stderr, "Tried to fire unknown weapon type %d!\n", type);
    die("Exiting\n", 1);
  }

  new_projectile->owner_id = ent->ent_id;
  new_projectile->x = ent->x + ent->width/3;
  new_projectile->y = ent->y + ent->width/3;
  new_projectile->x_v = x_v;
  new_projectile->y_v = y_v;

#if VERBOSE
    printf(" speed = %d, direction %d%d\n", new_projectile->speed,
	   new_projectile->x_v, new_projectile->y_v);
#endif

}


void blowup(struct map_t *map, entity *nuke, int gz_x, int gz_y, int blast_rad)
{

  entity *ent;
  int x,y,i, tile_rad;
  int num_killed = 0;

  flash_color(1); /* gives a quick WHITE flash. */
  update_window();

  for( y=gz_y-blast_rad, i=0 ; y <= gz_y+blast_rad ; y++, y-gz_y>0? i-- : i++)
    if( y > 0 && y < map->height-1)
      for( x=gz_x - i ; x<=gz_x + i; x++ )
	if( x > 0 && x < map->width-1) {
	  if( i && x > gz_x - i && x < gz_x + i) {
	    *(map->base + y*map->width + x) = RB_ASH;
	    *(map->object + y*map->width + x) = O_NULL;
	  } /* Only draw blastmarks on floor tiles */
	  else if( *(map->base + y*map->width + x) < REAL_BASE_SPECIAL )
	    *(map->object + y*map->width + x) = RO_BMARK;
	}

  tile_rad = blast_rad * TILE_W; /* Convert from tile squares -> pixels */

  for( ent = root ; ent->next != NULL ; ent = ent->next) {

    if( ent->type == ITEM )
      continue; /* Don't blow away items */

    if( ent->ent_id != nuke->ent_id && ent->mode )
      /* Relies that entity height == width */
      if( abs(nuke->x - ent->x) <= tile_rad + ent->width)
	if( abs(nuke->y - ent->y) <= tile_rad + ent->height)
	  if( entity_dist( nuke, ent ) <= tile_rad ) {
	    /* Give entity apropriate amount of damage, blast damage is
	       directly related to size of the blast radius */
	    ent->health -= (blast_rad/NUKE_RADIUS) * NUKERADIUS_DMG;
	    if( ent->health <= 0 )
	      num_killed++;
	  }
  }
  
#if VERBOSE
  printf("That blast killed %d entities!!\n", num_killed);
#endif

#if USESOUND
  play_sound(S_NUKE);
#endif

  map_dirty = 1;

}


int getdir(int x_v, int y_v)
{

  if( !x_v )

    if( y_v > 0 )
      return 4;
    else return 0;
  
  else if( x_v > 0 )
    
    if( !y_v )
      return 2;
    else if( y_v > 0 )
      return  3;
    else return 1;
  
  else
    
    if( !y_v )
      return 6;
    else if( y_v > 0 )
      return  5;
    else return 7;
  
}


char *dirtocompass(int dir)
{

  switch( dir ) {
  case 0: return "n";
  case 1: return "ne";
  case 2: return "e";
  case 3: return "se";
  case 4: return "s";
  case 5: return "sw";
  case 6: return "w";
  case 7: return "nw";
  case 8: return "die";
  default:
    fprintf(stderr, "Something went wrong in dirtocompass, dir=%d\n", dir);
    die("Bah!\n", 1);
  }

  return (char *)NULL; /* Never reached */

}


void sig_handler(int signo)
{

  char err_buf[32];

  sprintf(err_buf, "Got signal %d", signo);
  die(err_buf, signo);

}