comparison src/creep.c @ 20:51a0972fcf76

Move some tilemap/distance stuff out of creep.c and main.c into a separate source file
author Michael Pavone <pavone@retrodev.com>
date Sun, 12 Jan 2014 22:43:03 -0800
parents 08f2bcf3447f
children
comparison
equal deleted inserted replaced
19:08f2bcf3447f 20:51a0972fcf76
1 #include <genesis.h> 1 #include <genesis.h>
2 #include "creep.h" 2 #include "creep.h"
3 #include "map.h"
3 4
4 creep creeps[MAX_CREEPS]; 5 creep creeps[MAX_CREEPS];
5 u16 cur_creeps; 6 u16 cur_creeps;
6 extern u16 tilemap[40*28];
7
8 u16 distances[20*14];
9 7
10 const s16 speeds[NUM_SPECIES] = { 2 }; 8 const s16 speeds[NUM_SPECIES] = { 2 };
11 9
12 u16 spawn_creep(u8 species, s16 x, s16 y) 10 u16 spawn_creep(u8 species, s16 x, s16 y)
13 { 11 {
29 creeps[cur_creeps].species = species; 27 creeps[cur_creeps].species = species;
30 creeps[cur_creeps].direction = 0; 28 creeps[cur_creeps].direction = 0;
31 creeps[cur_creeps].targetx = x; 29 creeps[cur_creeps].targetx = x;
32 creeps[cur_creeps].targety = y; 30 creeps[cur_creeps].targety = y;
33 return cur_creeps++; 31 return cur_creeps++;
34 }
35
36 typedef struct {
37 u16 index;
38 u16 x;
39 u16 y;
40 } mpoint;
41
42 s16 explore(mpoint * points, s16 num_points, u16 src, u16 srcx, u16 srcy)
43 {
44 u16 dist = distances[src]+1;
45 if (srcx < 19 && distances[src + 1] > dist && !tilemap[(srcx+1)*2+srcy*2*40])
46 {
47 distances[src + 1] = dist;
48 points[num_points].index = src + 1;
49 points[num_points].x = srcx+1;
50 points[num_points++].y = srcy;
51 }
52 if (srcx && distances[src - 1] > dist && !tilemap[(srcx-1)*2 + srcy*2*40])
53 {
54 distances[src - 1] = dist;
55 points[num_points].index = src - 1;
56 points[num_points].x = srcx-1;
57 points[num_points++].y = srcy;
58 }
59 if (srcy < 13 && distances[src + 20] > dist && !tilemap[srcx*2+(srcy+1)*2*40])
60 {
61 distances[src + 20] = dist;
62 points[num_points].index = src + 20;
63 points[num_points].x = srcx;
64 points[num_points++].y = srcy+1;
65 }
66 if (srcy && distances[src - 20] > dist && !tilemap[srcx*2 + (srcy-1)*2*40])
67 {
68 distances[src - 20] = dist;
69 points[num_points].index = src - 20;
70 points[num_points].x = srcx;
71 points[num_points++].y = srcy-1;
72 }
73 return num_points;
74 }
75
76 void gen_distances(u16 x, u16 y)
77 {
78 //TODO: Figure out the actual maximum number of candidate points that can exist
79 mpoint pointsa[20*14];
80 mpoint pointsb[20*14];
81 mpoint *points=pointsa;
82 mpoint *new_points;
83 s16 num_points = 0, old_points;
84 x /= 2;
85 y /= 2;
86 memset(distances, 0xFF, sizeof(distances));
87
88 distances[x + y*20] = 0;
89 num_points = explore(points, num_points, x + y*20, x, y);
90
91 while (num_points)
92 {
93 new_points = points == pointsa ? pointsb : pointsa;
94 old_points = num_points;
95 for (num_points = 0, old_points--; old_points >= 0; old_points--)
96 {
97 num_points = explore(new_points, num_points, points[old_points].index, points[old_points].x, points[old_points].y);
98 }
99 points = new_points;
100 }
101 }
102
103 void print_distances(void)
104 {
105 u16 x,y,tindex,dindex,dist;
106 for (y = 0; y < 14; y++)
107 {
108 dindex = y * 20;
109 tindex = y * 2 * 40;
110 for (x = 0; x < 20; x++, dindex++, tindex += 2)
111 {
112 dist = distances[dindex];
113 if (dist < 10000)
114 {
115 tilemap[tindex+41] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX);
116 dist /= 10;
117 if (dist)
118 {
119 tilemap[tindex+40] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX);
120 dist /= 10;
121 if (dist)
122 {
123 tilemap[tindex+1] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX);
124 dist /= 10;
125 if (dist)
126 tilemap[tindex] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX);
127 }
128 }
129 }
130 }
131 }
132 } 32 }
133 33
134 void update_creeps(void) 34 void update_creeps(void)
135 { 35 {
136 s16 i, disty, distx, mdist; 36 s16 i, disty, distx, mdist;