changeset 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 bb7dfb42b320
files src/creep.c src/creep.h src/main.c src/map.c src/map.h
diffstat 5 files changed, 115 insertions(+), 105 deletions(-) [+]
line wrap: on
line diff
--- 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 <genesis.h>
 #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;
--- 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_
--- 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 <genesis.h>
 #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
--- /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 <genesis.h>
+#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);
+					}
+				}
+			}
+		}
+	}
+}
--- /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_