From 4502b69638ecaf9dcc64e679492a6332dd5a6fc0 Mon Sep 17 00:00:00 2001 From: Elronnd Date: Sun, 31 Dec 2017 21:15:43 -0800 Subject: [PATCH] Clean ups --- src/fov.d | 62 +++++++++++++++++++++++++++++--------------------------------- src/game.d | 2 +- src/item.d | 2 +- src/main.d | 1 - src/map.d | 20 +++++++------------- 5 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/fov.d b/src/fov.d index b309d6b..27e6520 100644 --- a/src/fov.d +++ b/src/fov.d @@ -50,7 +50,7 @@ private immutable int[][] mult = [ void cast_light(ref Map map, uint cx, uint cy, uint row, float start, float end, int radius, uint r2, uint xx, uint xy, uint yx, - uint yy, int id, bool light_walls) { + uint yy, bool light_walls) { float new_start=0.0f; if (start < end) return; @@ -65,35 +65,34 @@ void cast_light(ref Map map, uint cx, uint cy, uint row, float start, X=cx+dx*xx+dy*xy; Y=cy+dx*yx+dy*yy; - if (X < map_x && Y < map_y) { - float l_slope, r_slope; + float l_slope, r_slope; - l_slope=(dx-0.5f)/(dy+0.5f); - r_slope=(dx+0.5f)/(dy-0.5f); + l_slope=(dx-0.5f)/(dy+0.5f); + r_slope=(dx+0.5f)/(dy-0.5f); - if (start < r_slope) - continue; - else if (end > l_slope) - break; + if (start < r_slope) { + continue; + } else if (end > l_slope) { + break; + } - if (dx*dx+dy*dy <= r2 && (light_walls || !(map[Y, X].blocks_light))) { - map[Y, X].visible = true; - } - if (blocked) { - if (map[Y, X].blocks_light) { - new_start=r_slope; - continue; - } else { - blocked=false; - start=new_start; - } + if (dx*dx+dy*dy <= r2 && (light_walls || !(map[Y, X].blocks_light))) { + map[Y, X].visible = true; + } + if (blocked) { + if (map[Y, X].blocks_light) { + new_start=r_slope; + continue; } else { - if ((map[Y, X].blocks_light) && j < radius ) { - blocked=true; - cast_light(map,cx,cy,j+1,start,l_slope,radius,r2,xx,xy,yx, - yy,id+1,light_walls); - new_start=r_slope; - } + blocked=false; + start=new_start; + } + } else { + if ((map[Y, X].blocks_light) && j < radius ) { + blocked=true; + cast_light(map,cx,cy,j+1,start,l_slope,radius,r2,xx,xy,yx, + yy,light_walls); + new_start=r_slope; } } } @@ -107,12 +106,9 @@ void do_fov(ref Map map, uint x, uint y, uint radius, bool light_walls) { map.for_all((ref Tile x) => x.visible = false); if (radius == 0) { - int radius_x=map_x-x; - int radius_y=map_y-y; - radius_x=radius_x > x ? radius_x : x; - radius_y=radius_y > y ? radius_y : y; - radius = cast(uint)(sqrt(cast(float)(radius_x*radius_x) - +(radius_y*radius_y))) +1; + //int radius_x=map_x; + //int radius_y=map_y; + radius = cast(uint)(sqrt(cast(float)(map_x*map_x) + (map_y * map_y))) + 1; } r2=radius*radius; @@ -120,7 +116,7 @@ void do_fov(ref Map map, uint x, uint y, uint radius, bool light_walls) { // recursive shadow casting foreach (oct; 0 .. 8) { cast_light(map,x,y,1,1.0,0.0,radius,r2, - mult[0][oct],mult[1][oct],mult[2][oct],mult[3][oct],0,light_walls); + mult[0][oct],mult[1][oct],mult[2][oct],mult[3][oct],light_walls); } map[y, x].visible = true; } diff --git a/src/game.d b/src/game.d index 6c5dbe3..d361ea8 100644 --- a/src/game.d +++ b/src/game.d @@ -331,7 +331,7 @@ class Game { deltay = deltax = 0; } refresh(); - do_fov(this.map, u.loc.x, u.loc.y, 100, true); + do_fov(map, u.loc.x, u.loc.y, 100, true); } graphics.close(); } diff --git a/src/item.d b/src/item.d index 5d0df38..a5043a3 100644 --- a/src/item.d +++ b/src/item.d @@ -21,9 +21,9 @@ struct Item { mixin funcwrapper!_on_pick_up; mixin funcwrapper!_on_use; -// mixin funcwrappers!(_on_pick_up, _on_use); } + Item[] items; shared static this() { diff --git a/src/main.d b/src/main.d index 43b5a8d..d196ff5 100644 --- a/src/main.d +++ b/src/main.d @@ -23,6 +23,5 @@ void main(string[] args) { set_logger_target(stderr); Game g = new Game(args); - log("Made a game!"); g.mainloop(); } diff --git a/src/map.d b/src/map.d index 1f40b0c..40ed8e4 100644 --- a/src/map.d +++ b/src/map.d @@ -37,15 +37,14 @@ struct Vector2n { return _i = normalized(_i, max); } uint opBinary(string op)(int other) { - int i = mixin("_i" ~ op ~ "other"); - return normalized(i, max); + return normalized(mixin("_i" ~ op ~ "other"), max); } @property uint i() { // we don't have to normalize i. Because assignment and inc/dec already normalize it return _i; } - @property int i(int newi) { + @property uint i(int newi) { return _i = normalized(newi, max); } alias i this; @@ -57,14 +56,6 @@ struct Vector2n { y.i = newy; x.i = newx; } - - /* - @property uint y() { return _y; } - @property uint y(int newy) { return _y = normalized(newy, maxy); } - - @property uint x() { return _x; } - @property uint x(int newx) { return _x = normalized(newx, maxx); } - */ } pragma(inline, true) uint normalized(int i, int max) { i %= max; @@ -83,8 +74,11 @@ pragma(inline, true) uint normalized(int i, int max) { class Map { private Tile[][] tiles; - this(Tile[][] tiles = null) { - this.tiles = tiles ? tiles : new Tile[][](map_x + 2, map_y + 2); + this(Tile[][] tiles) { + this.tiles = tiles; + } + this() { + tiles = new Tile[][](map_x, map_y); } Tile[][] get_tiles() { -- 2.11.4.GIT