Mercurial > repos > genjam1
comparison src/map.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 | |
children | bb7dfb42b320 |
comparison
equal
deleted
inserted
replaced
19:08f2bcf3447f | 20:51a0972fcf76 |
---|---|
1 #include <genesis.h> | |
2 #include "map.h" | |
3 | |
4 u16 distances[20*14]; | |
5 u16 tilemap[40*28]; | |
6 | |
7 typedef struct { | |
8 u16 index; | |
9 u16 x; | |
10 u16 y; | |
11 } mpoint; | |
12 | |
13 s16 explore(mpoint * points, s16 num_points, u16 src, u16 srcx, u16 srcy) | |
14 { | |
15 u16 dist = distances[src]+1; | |
16 if (srcx < 19 && distances[src + 1] > dist && !tilemap[(srcx+1)*2+srcy*2*40]) | |
17 { | |
18 distances[src + 1] = dist; | |
19 points[num_points].index = src + 1; | |
20 points[num_points].x = srcx+1; | |
21 points[num_points++].y = srcy; | |
22 } | |
23 if (srcx && distances[src - 1] > dist && !tilemap[(srcx-1)*2 + srcy*2*40]) | |
24 { | |
25 distances[src - 1] = dist; | |
26 points[num_points].index = src - 1; | |
27 points[num_points].x = srcx-1; | |
28 points[num_points++].y = srcy; | |
29 } | |
30 if (srcy < 13 && distances[src + 20] > dist && !tilemap[srcx*2+(srcy+1)*2*40]) | |
31 { | |
32 distances[src + 20] = dist; | |
33 points[num_points].index = src + 20; | |
34 points[num_points].x = srcx; | |
35 points[num_points++].y = srcy+1; | |
36 } | |
37 if (srcy && distances[src - 20] > dist && !tilemap[srcx*2 + (srcy-1)*2*40]) | |
38 { | |
39 distances[src - 20] = dist; | |
40 points[num_points].index = src - 20; | |
41 points[num_points].x = srcx; | |
42 points[num_points++].y = srcy-1; | |
43 } | |
44 return num_points; | |
45 } | |
46 | |
47 void gen_distances(u16 x, u16 y) | |
48 { | |
49 //TODO: Figure out the actual maximum number of candidate points that can exist | |
50 mpoint pointsa[20*14]; | |
51 mpoint pointsb[20*14]; | |
52 mpoint *points=pointsa; | |
53 mpoint *new_points; | |
54 s16 num_points = 0, old_points; | |
55 x /= 2; | |
56 y /= 2; | |
57 memset(distances, 0xFF, sizeof(distances)); | |
58 | |
59 distances[x + y*20] = 0; | |
60 num_points = explore(points, num_points, x + y*20, x, y); | |
61 | |
62 while (num_points) | |
63 { | |
64 new_points = points == pointsa ? pointsb : pointsa; | |
65 old_points = num_points; | |
66 for (num_points = 0, old_points--; old_points >= 0; old_points--) | |
67 { | |
68 num_points = explore(new_points, num_points, points[old_points].index, points[old_points].x, points[old_points].y); | |
69 } | |
70 points = new_points; | |
71 } | |
72 } | |
73 | |
74 void print_distances(void) | |
75 { | |
76 u16 x,y,tindex,dindex,dist; | |
77 for (y = 0; y < 14; y++) | |
78 { | |
79 dindex = y * 20; | |
80 tindex = y * 2 * 40; | |
81 for (x = 0; x < 20; x++, dindex++, tindex += 2) | |
82 { | |
83 dist = distances[dindex]; | |
84 if (dist < 10000) | |
85 { | |
86 tilemap[tindex+41] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); | |
87 dist /= 10; | |
88 if (dist) | |
89 { | |
90 tilemap[tindex+40] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); | |
91 dist /= 10; | |
92 if (dist) | |
93 { | |
94 tilemap[tindex+1] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); | |
95 dist /= 10; | |
96 if (dist) | |
97 tilemap[tindex] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); | |
98 } | |
99 } | |
100 } | |
101 } | |
102 } | |
103 } |