Mercurial > repos > genjam1
comparison src/creep.c @ 11:c0bb53eaa6f4
Calculate distances from goal for all passible points for pathfinding purposes
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 12 Jan 2014 17:05:53 -0800 |
parents | 5ec4707a3fd1 |
children | d118fe8fb1db |
comparison
equal
deleted
inserted
replaced
9:5ec4707a3fd1 | 11:c0bb53eaa6f4 |
---|---|
2 #include "creep.h" | 2 #include "creep.h" |
3 | 3 |
4 creep creeps[MAX_CREEPS]; | 4 creep creeps[MAX_CREEPS]; |
5 u16 cur_creeps; | 5 u16 cur_creeps; |
6 extern u16 tilemap[40*28]; | 6 extern u16 tilemap[40*28]; |
7 | |
8 u16 distances[20*14]; | |
7 | 9 |
8 u16 spawn_creep(u8 species, s16 x, s16 y) | 10 u16 spawn_creep(u8 species, s16 x, s16 y) |
9 { | 11 { |
10 u16 index; | 12 u16 index; |
11 for (index = 0; index < MAX_SPRITE; index++) | 13 for (index = 0; index < MAX_SPRITE; index++) |
24 creeps[cur_creeps].health = 1000; | 26 creeps[cur_creeps].health = 1000; |
25 creeps[cur_creeps].species = species; | 27 creeps[cur_creeps].species = species; |
26 creeps[cur_creeps].direction = 0; | 28 creeps[cur_creeps].direction = 0; |
27 return cur_creeps++; | 29 return cur_creeps++; |
28 } | 30 } |
31 | |
32 typedef struct { | |
33 u16 index; | |
34 u16 x; | |
35 u16 y; | |
36 } mpoint; | |
37 | |
38 s16 explore(mpoint * points, s16 num_points, u16 src, u16 srcx, u16 srcy) | |
39 { | |
40 u16 x,y,index,cindex,xmax,ymax,ystrt,dist; | |
41 dist = distances[src]+1; | |
42 xmax = srcx < 19 ? srcx+1 : srcx; | |
43 ymax = srcy < 13 ? srcy+1 : srcy; | |
44 if (srcy) | |
45 { | |
46 ystrt = srcy-1; | |
47 cindex = srcx ? src - 21 : src - 20; | |
48 } else { | |
49 ystrt = srcy; | |
50 cindex = srcx ? src - 1 : src; | |
51 } | |
52 for (x = srcx ? srcx-1 : srcx; x <= xmax; x++, cindex++) | |
53 { | |
54 for (y = ystrt, index=cindex; y<=ymax; y++,index+=20) | |
55 { | |
56 if (distances[index] > dist && !tilemap[x*2+y*2*40]) | |
57 { | |
58 distances[index] = dist; | |
59 if (dist == 0xFFFF) | |
60 { | |
61 tilemap[x*2 + y*2*40] = TILE_ATTR_FULL(2, 0, 0, 0, 'E' - 32 + TILE_FONTINDEX); | |
62 } | |
63 points[num_points].index = index; | |
64 points[num_points].x = x; | |
65 points[num_points++].y = y; | |
66 } | |
67 } | |
68 } | |
69 return num_points; | |
70 } | |
71 | |
72 void gen_distances(u16 x, u16 y) | |
73 { | |
74 //TODO: Figure out the actual maximum number of candidate points that can exist | |
75 mpoint pointsa[20*14]; | |
76 mpoint pointsb[20*14]; | |
77 mpoint *points=pointsa; | |
78 mpoint *new_points; | |
79 s16 num_points = 0, old_points; | |
80 x /= 2; | |
81 y /= 2; | |
82 memset(distances, 0xFF, sizeof(distances)); | |
83 | |
84 distances[x + y*20] = 0; | |
85 num_points = explore(points, num_points, x + y*20, x, y); | |
86 | |
87 while (num_points) | |
88 { | |
89 new_points = points == pointsa ? pointsb : pointsa; | |
90 old_points = num_points; | |
91 for (num_points = 0, old_points--; old_points >= 0; old_points--) | |
92 { | |
93 num_points = explore(new_points, num_points, points[old_points].index, points[old_points].x, points[old_points].y); | |
94 } | |
95 points = new_points; | |
96 } | |
97 } | |
98 | |
99 void print_distances() | |
100 { | |
101 u16 x,y,tindex,dindex,dist; | |
102 for (y = 0; y < 14; y++) | |
103 { | |
104 dindex = y * 20; | |
105 tindex = y * 2 * 40; | |
106 for (x = 0; x < 20; x++, dindex++, tindex += 2) | |
107 { | |
108 dist = distances[dindex]; | |
109 if (dist < 10000) | |
110 { | |
111 tilemap[tindex+41] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); | |
112 dist /= 10; | |
113 if (dist) | |
114 { | |
115 tilemap[tindex+40] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); | |
116 dist /= 10; | |
117 if (dist) | |
118 { | |
119 tilemap[tindex+1] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); | |
120 dist /= 10; | |
121 if (dist) | |
122 tilemap[tindex] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); | |
123 } | |
124 } | |
125 } | |
126 } | |
127 } | |
128 } |