# HG changeset patch # User Michael Pavone # Date 1389595383 28800 # Node ID 51a0972fcf761701c9e5ae8b8be72cee872cb65b # Parent 08f2bcf3447f118ac1bdd09e6b420e6e1969997a Move some tilemap/distance stuff out of creep.c and main.c into a separate source file diff -r 08f2bcf3447f -r 51a0972fcf76 src/creep.c --- a/src/creep.c Sun Jan 12 22:05:12 2014 -0800 +++ b/src/creep.c Sun Jan 12 22:43:03 2014 -0800 @@ -1,11 +1,9 @@ #include #include "creep.h" +#include "map.h" creep creeps[MAX_CREEPS]; u16 cur_creeps; -extern u16 tilemap[40*28]; - -u16 distances[20*14]; const s16 speeds[NUM_SPECIES] = { 2 }; @@ -33,104 +31,6 @@ return cur_creeps++; } -typedef struct { - u16 index; - u16 x; - u16 y; -} mpoint; - -s16 explore(mpoint * points, s16 num_points, u16 src, u16 srcx, u16 srcy) -{ - u16 dist = distances[src]+1; - if (srcx < 19 && distances[src + 1] > dist && !tilemap[(srcx+1)*2+srcy*2*40]) - { - distances[src + 1] = dist; - points[num_points].index = src + 1; - points[num_points].x = srcx+1; - points[num_points++].y = srcy; - } - if (srcx && distances[src - 1] > dist && !tilemap[(srcx-1)*2 + srcy*2*40]) - { - distances[src - 1] = dist; - points[num_points].index = src - 1; - points[num_points].x = srcx-1; - points[num_points++].y = srcy; - } - if (srcy < 13 && distances[src + 20] > dist && !tilemap[srcx*2+(srcy+1)*2*40]) - { - distances[src + 20] = dist; - points[num_points].index = src + 20; - points[num_points].x = srcx; - points[num_points++].y = srcy+1; - } - if (srcy && distances[src - 20] > dist && !tilemap[srcx*2 + (srcy-1)*2*40]) - { - distances[src - 20] = dist; - points[num_points].index = src - 20; - points[num_points].x = srcx; - points[num_points++].y = srcy-1; - } - return num_points; -} - -void gen_distances(u16 x, u16 y) -{ - //TODO: Figure out the actual maximum number of candidate points that can exist - mpoint pointsa[20*14]; - mpoint pointsb[20*14]; - mpoint *points=pointsa; - mpoint *new_points; - s16 num_points = 0, old_points; - x /= 2; - y /= 2; - memset(distances, 0xFF, sizeof(distances)); - - distances[x + y*20] = 0; - num_points = explore(points, num_points, x + y*20, x, y); - - while (num_points) - { - new_points = points == pointsa ? pointsb : pointsa; - old_points = num_points; - for (num_points = 0, old_points--; old_points >= 0; old_points--) - { - num_points = explore(new_points, num_points, points[old_points].index, points[old_points].x, points[old_points].y); - } - points = new_points; - } -} - -void print_distances(void) -{ - u16 x,y,tindex,dindex,dist; - for (y = 0; y < 14; y++) - { - dindex = y * 20; - tindex = y * 2 * 40; - for (x = 0; x < 20; x++, dindex++, tindex += 2) - { - dist = distances[dindex]; - if (dist < 10000) - { - tilemap[tindex+41] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); - dist /= 10; - if (dist) - { - tilemap[tindex+40] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); - dist /= 10; - if (dist) - { - tilemap[tindex+1] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); - dist /= 10; - if (dist) - tilemap[tindex] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); - } - } - } - } - } -} - void update_creeps(void) { s16 i, disty, distx, mdist; diff -r 08f2bcf3447f -r 51a0972fcf76 src/creep.h --- a/src/creep.h Sun Jan 12 22:05:12 2014 -0800 +++ b/src/creep.h Sun Jan 12 22:43:03 2014 -0800 @@ -17,10 +17,8 @@ #define MAX_CREEPS 40 extern u16 cur_creeps; -extern u16 distances[20*14]; + u16 spawn_creep(u8 species, s16 x, s16 y); -void gen_distances(u16 x, u16 y); -void print_distances(void); void update_creeps(void); #endif //CREEP_H_ diff -r 08f2bcf3447f -r 51a0972fcf76 src/main.c --- a/src/main.c Sun Jan 12 22:05:12 2014 -0800 +++ b/src/main.c Sun Jan 12 22:43:03 2014 -0800 @@ -1,5 +1,6 @@ #include #include "creep.h" +#include "map.h" // I now realize this should be one tile that uses the flipping flags. oops. const u32 cursor_tiles[4*8] = { @@ -46,7 +47,6 @@ #define TOWER TILE_ATTR_FULL(1, 0, 0, 0, 'T'-32 + TILE_FONTINDEX) #define GOAL TILE_ATTR_FULL(1, 0, 0, 0, 'G'-32 + TILE_FONTINDEX) -u16 tilemap[40*28]; u16 countdown; int cursor_x = 0; // tiles diff -r 08f2bcf3447f -r 51a0972fcf76 src/map.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/map.c Sun Jan 12 22:43:03 2014 -0800 @@ -0,0 +1,103 @@ +#include +#include "map.h" + +u16 distances[20*14]; +u16 tilemap[40*28]; + +typedef struct { + u16 index; + u16 x; + u16 y; +} mpoint; + +s16 explore(mpoint * points, s16 num_points, u16 src, u16 srcx, u16 srcy) +{ + u16 dist = distances[src]+1; + if (srcx < 19 && distances[src + 1] > dist && !tilemap[(srcx+1)*2+srcy*2*40]) + { + distances[src + 1] = dist; + points[num_points].index = src + 1; + points[num_points].x = srcx+1; + points[num_points++].y = srcy; + } + if (srcx && distances[src - 1] > dist && !tilemap[(srcx-1)*2 + srcy*2*40]) + { + distances[src - 1] = dist; + points[num_points].index = src - 1; + points[num_points].x = srcx-1; + points[num_points++].y = srcy; + } + if (srcy < 13 && distances[src + 20] > dist && !tilemap[srcx*2+(srcy+1)*2*40]) + { + distances[src + 20] = dist; + points[num_points].index = src + 20; + points[num_points].x = srcx; + points[num_points++].y = srcy+1; + } + if (srcy && distances[src - 20] > dist && !tilemap[srcx*2 + (srcy-1)*2*40]) + { + distances[src - 20] = dist; + points[num_points].index = src - 20; + points[num_points].x = srcx; + points[num_points++].y = srcy-1; + } + return num_points; +} + +void gen_distances(u16 x, u16 y) +{ + //TODO: Figure out the actual maximum number of candidate points that can exist + mpoint pointsa[20*14]; + mpoint pointsb[20*14]; + mpoint *points=pointsa; + mpoint *new_points; + s16 num_points = 0, old_points; + x /= 2; + y /= 2; + memset(distances, 0xFF, sizeof(distances)); + + distances[x + y*20] = 0; + num_points = explore(points, num_points, x + y*20, x, y); + + while (num_points) + { + new_points = points == pointsa ? pointsb : pointsa; + old_points = num_points; + for (num_points = 0, old_points--; old_points >= 0; old_points--) + { + num_points = explore(new_points, num_points, points[old_points].index, points[old_points].x, points[old_points].y); + } + points = new_points; + } +} + +void print_distances(void) +{ + u16 x,y,tindex,dindex,dist; + for (y = 0; y < 14; y++) + { + dindex = y * 20; + tindex = y * 2 * 40; + for (x = 0; x < 20; x++, dindex++, tindex += 2) + { + dist = distances[dindex]; + if (dist < 10000) + { + tilemap[tindex+41] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); + dist /= 10; + if (dist) + { + tilemap[tindex+40] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); + dist /= 10; + if (dist) + { + tilemap[tindex+1] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); + dist /= 10; + if (dist) + tilemap[tindex] = TILE_ATTR_FULL(3, 0, 0, 0, '0' - 32 + dist % 10 + TILE_FONTINDEX); + } + } + } + } + } +} diff -r 08f2bcf3447f -r 51a0972fcf76 src/map.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/map.h Sun Jan 12 22:43:03 2014 -0800 @@ -0,0 +1,9 @@ +#ifndef MAP_H_ +#define MAP_H_ + +extern u16 distances[20*14]; +extern u16 tilemap[40*28]; +void gen_distances(u16 x, u16 y); +void print_distances(void); + +#endif //MAP_H_