From 0d7723492be564b7819186b2b1ec646c7e543572 Mon Sep 17 00:00:00 2001 From: Mario Date: Fri, 26 May 2017 18:58:20 +1000 Subject: [PATCH] Minor code cleanup in pathlib --- qcsrc/common/turrets/turret/ewheel.qc | 28 +++---- qcsrc/server/pathlib/costs.qc | 59 ++++++-------- qcsrc/server/pathlib/expandnode.qc | 38 ++++----- qcsrc/server/pathlib/main.qc | 109 ++++++++++---------------- qcsrc/server/pathlib/main.qh | 2 +- qcsrc/server/pathlib/movenode.qc | 34 ++++---- qcsrc/server/pathlib/pathlib.qh | 14 ++-- qcsrc/server/pathlib/utility.qc | 79 +++++++++---------- 8 files changed, 159 insertions(+), 204 deletions(-) diff --git a/qcsrc/common/turrets/turret/ewheel.qc b/qcsrc/common/turrets/turret/ewheel.qc index 0a633c784..d2feb0823 100644 --- a/qcsrc/common/turrets/turret/ewheel.qc +++ b/qcsrc/common/turrets/turret/ewheel.qc @@ -10,17 +10,18 @@ float autocvar_g_turrets_unit_ewheel_speed_slower; float autocvar_g_turrets_unit_ewheel_speed_stop; float autocvar_g_turrets_unit_ewheel_turnrate; -const float ewheel_anim_stop = 0; -const float ewheel_anim_fwd_slow = 1; -const float ewheel_anim_fwd_fast = 2; -const float ewheel_anim_bck_slow = 3; -const float ewheel_anim_bck_fast = 4; +const int ewheel_anim_stop = 0; +const int ewheel_anim_fwd_slow = 1; +const int ewheel_anim_fwd_fast = 2; +const int ewheel_anim_bck_slow = 3; +const int ewheel_anim_bck_fast = 4; void ewheel_move_path(entity this) { -#ifdef EWHEEL_FANCYPATH - // Are we close enougth to a path node to switch to the next? + // Are we close enough to a path node to switch to the next? if(vdist(this.origin - this.pathcurrent.origin, <, 64)) + { +#ifdef EWHEEL_FANCYPATH if (this.pathcurrent.path_next == NULL) { // Path endpoint reached @@ -43,11 +44,10 @@ void ewheel_move_path(entity this) } else this.pathcurrent = this.pathcurrent.path_next; - #else - if(vdist(this.origin - this.pathcurrent.origin, <, 64)) this.pathcurrent = this.pathcurrent.enemy; #endif + } if (this.pathcurrent) { @@ -61,11 +61,11 @@ void ewheel_move_path(entity this) void ewheel_move_enemy(entity this) { - float newframe; + int newframe; this.steerto = steerlib_arrive(this, this.enemy.origin,this.target_range_optimal); - this.moveto = this.origin + this.steerto * 128; + this.moveto = this.origin + this.steerto * 128; if (this.tur_dist_enemy > this.target_range_optimal) { @@ -176,17 +176,17 @@ METHOD(EWheel, tr_setup, void(EWheel this, entity it)) e = find(NULL, targetname, it.target); if (!e) { - LOG_TRACE("Initital waypoint for ewheel does NOT exsist, fix your map!"); + LOG_TRACE("Initital waypoint for ewheel does NOT exist, fix your map!"); it.target = ""; } if (e.classname != "turret_checkpoint") - LOG_TRACE("Warning: not a turrret path"); + LOG_TRACE("Warning: not a turret path"); else { #ifdef EWHEEL_FANCYPATH - it.pathcurrent = WALKER_PATH(it, it.origin, e.origin); + it.pathcurrent = pathlib_astar(it, it.origin, e.origin); it.pathgoal = e; #else it.pathcurrent = e; diff --git a/qcsrc/server/pathlib/costs.qc b/qcsrc/server/pathlib/costs.qc index fdb95d2f6..1449e382a 100644 --- a/qcsrc/server/pathlib/costs.qc +++ b/qcsrc/server/pathlib/costs.qc @@ -35,8 +35,7 @@ float pathlib_h_manhattan(vector a,vector b) { //h(n) = D * (abs(n.x-goal.x) + abs(n.y-goal.y)) - float h; - h = fabs(a.x - b.x); + float h = fabs(a.x - b.x); h += fabs(a.y - b.y); h *= pathlib_gridsize; @@ -50,11 +49,10 @@ float pathlib_h_manhattan(vector a,vector b) float pathlib_h_diagonal(vector a,vector b) { //h(n) = D * max(abs(n.x-goal.x), abs(n.y-goal.y)) - float h,x,y; - x = fabs(a.x - b.x); - y = fabs(a.y - b.y); - h = pathlib_movecost * max(x,y); + float hx = fabs(a.x - b.x); + float hy = fabs(a.y - b.y); + float h = pathlib_movecost * max(hx,hy); return h; } @@ -75,21 +73,19 @@ float pathlib_h_euclidean(vector a,vector b) **/ float pathlib_h_diagonal2(vector a,vector b) { - float h_diag,h_str,h,x,y; - /* h_diagonal(n) = min(abs(n.x-goal.x), abs(n.y-goal.y)) h_straight(n) = (abs(n.x-goal.x) + abs(n.y-goal.y)) h(n) = D2 * h_diagonal(n) + D * (h_straight(n) - 2*h_diagonal(n))) */ - x = fabs(a.x - b.x); - y = fabs(a.y - b.y); + float hx = fabs(a.x - b.x); + float hy = fabs(a.y - b.y); - h_diag = min(x,y); - h_str = x + y; + float h_diag = min(hx,hy); + float h_str = hx + hy; - h = pathlib_movecost_diag * h_diag; + float h = pathlib_movecost_diag * h_diag; h += pathlib_movecost * (h_str - 2 * h_diag); return h; @@ -101,28 +97,23 @@ float pathlib_h_diagonal2(vector a,vector b) **/ float pathlib_h_diagonal2sdp(vector preprev,vector prev,vector point,vector end) { - float h_diag,h_str,h,x,y,z; - //h_diagonal(n) = min(abs(n.x-goal.x), abs(n.y-goal.y)) //h_straight(n) = (abs(n.x-goal.x) + abs(n.y-goal.y)) //h(n) = D2 * h_diagonal(n) + D * (h_straight(n) - 2*h_diagonal(n))) - x = fabs(point.x - end.x); - y = fabs(point.y - end.y); - z = fabs(point.z - end.z); + float hx = fabs(point.x - end.x); + float hy = fabs(point.y - end.y); + float hz = fabs(point.z - end.z); - h_diag = min3(x,y,z); - h_str = x + y + z; + float h_diag = min3(hx,hy,hz); + float h_str = hx + hy + hz; - h = pathlib_movecost_diag * h_diag; + float h = pathlib_movecost_diag * h_diag; h += pathlib_movecost * (h_str - 2 * h_diag); - float m; - vector d1,d2; - - d1 = normalize(preprev - point); - d2 = normalize(prev - point); - m = vlen(d1-d2); + vector d1 = normalize(preprev - point); + vector d2 = normalize(prev - point); + float m = vlen(d1-d2); return h * m; } @@ -130,16 +121,14 @@ float pathlib_h_diagonal2sdp(vector preprev,vector prev,vector point,vector end) float pathlib_h_diagonal3(vector a,vector b) { - float h_diag,h_str,h,x,y,z; - - x = fabs(a.x - b.x); - y = fabs(a.y - b.y); - z = fabs(a.z - b.z); + float hx = fabs(a.x - b.x); + float hy = fabs(a.y - b.y); + float hz = fabs(a.z - b.z); - h_diag = min3(x,y,z); - h_str = x + y + z; + float h_diag = min3(hx,hy,hz); + float h_str = hx + hy + hz; - h = pathlib_movecost_diag * h_diag; + float h = pathlib_movecost_diag * h_diag; h += pathlib_movecost * (h_str - 2 * h_diag); return h; diff --git a/qcsrc/server/pathlib/expandnode.qc b/qcsrc/server/pathlib/expandnode.qc index 700ba41c0..b77736b19 100644 --- a/qcsrc/server/pathlib/expandnode.qc +++ b/qcsrc/server/pathlib/expandnode.qc @@ -8,14 +8,12 @@ float plib_fvals[8]; float pathlib_expandnode_starf(entity node, vector start, vector goal) { - vector where,f,r,t; - float fc,c; - entity nap; + float fc; - where = node.origin; + vector where = node.origin; - f = PLIB_FORWARD * pathlib_gridsize; - r = PLIB_RIGHT * pathlib_gridsize; + vector f = PLIB_FORWARD * pathlib_gridsize; + vector r = PLIB_RIGHT * pathlib_gridsize; // Forward plib_points[0] = where + f; @@ -43,7 +41,7 @@ float pathlib_expandnode_starf(entity node, vector start, vector goal) for(int i=0;i < 8; ++i) { - t = plib_points[i]; + vector t = plib_points[i]; fc = pathlib_heuristic(t,goal) + pathlib_cost(node, t, pathlib_gridsize); plib_fvals[i] = fc; @@ -56,13 +54,15 @@ float pathlib_expandnode_starf(entity node, vector start, vector goal) int fc2 = 0; for(int i = 0; i < 8; ++i) { - c = 0; - nap = pathlib_nodeatpoint(plib_points[i]); + bool c = false; + entity nap = pathlib_nodeatpoint(plib_points[i]); if(nap) + { if(nap.owner == openlist) - c = 1; + c = true; + } else - c = 1; + c = true; if(c) if(plib_fvals[i] < fc) @@ -94,12 +94,12 @@ float pathlib_expandnode_starf(entity node, vector start, vector goal) float pathlib_expandnode_star(entity node, vector start, vector goal) { - vector point, where, f, r; + vector point; - where = node.origin; + vector where = node.origin; - f = PLIB_FORWARD * pathlib_gridsize; - r = PLIB_RIGHT * pathlib_gridsize; + vector f = PLIB_FORWARD * pathlib_gridsize; + vector r = PLIB_RIGHT * pathlib_gridsize; if (node.pathlib_node_edgeflags == pathlib_node_edgeflag_unknown) node.pathlib_node_edgeflags = tile_check_plus2(node, node.origin); @@ -173,12 +173,12 @@ float pathlib_expandnode_star(entity node, vector start, vector goal) float pathlib_expandnode_octagon(entity node, vector start, vector goal) { - vector point,where,f,r; + vector point; - where = node.origin; + vector where = node.origin; - f = PLIB_FORWARD * pathlib_gridsize; - r = PLIB_RIGHT * pathlib_gridsize; + vector f = PLIB_FORWARD * pathlib_gridsize; + vector r = PLIB_RIGHT * pathlib_gridsize; // Forward point = where + f; diff --git a/qcsrc/server/pathlib/main.qc b/qcsrc/server/pathlib/main.qc index 36c7c1d31..833ad9b3f 100644 --- a/qcsrc/server/pathlib/main.qc +++ b/qcsrc/server/pathlib/main.qc @@ -13,8 +13,7 @@ void pathlib_deletepath(entity start) }); } -//#define PATHLIB_NODEEXPIRE 0.05 -const float PATHLIB_NODEEXPIRE = 20; +const float PATHLIB_NODEEXPIRE = 20; // 0.05 void dumpnode(entity n) { @@ -32,9 +31,7 @@ void pathlib_showsquare(vector where,float goodsquare,float _lifetime); entity pathlib_mknode(vector where,entity parent) { - entity node; - - node = pathlib_nodeatpoint(where); + entity node = pathlib_nodeatpoint(where); if(node) { #ifdef TURRET_DEBUG @@ -51,7 +48,6 @@ entity pathlib_mknode(vector where,entity parent) node.owner = openlist; node.path_prev = parent; - setsize(node, '0 0 0', '0 0 0'); setorigin(node, where); @@ -64,11 +60,10 @@ entity pathlib_mknode(vector where,entity parent) return node; } -float pathlib_makenode_adaptive(entity parent,vector start, vector to, vector goal,float cost) +bool pathlib_makenode_adaptive(entity parent,vector start, vector to, vector goal,float cost) { - entity node; - float h,g,f,doedge = 0; - vector where; + bool dodge = false; + float f, h, g; ++pathlib_searched_cnt; @@ -94,11 +89,11 @@ float pathlib_makenode_adaptive(entity parent,vector start, vector to, vector go pathlib_expandnode = pathlib_expandnode_star; pathlib_movenode = pathlib_walknode; - doedge = 1; + dodge = true; } } - node = pathlib_nodeatpoint(to); + entity node = pathlib_nodeatpoint(to); if(node) { LOG_TRACE("NodeAtPoint"); @@ -107,7 +102,7 @@ float pathlib_makenode_adaptive(entity parent,vector start, vector to, vector go if(node.owner == openlist) { h = pathlib_heuristic(node.origin,goal); - g = pathlib_cost(parent,node.origin,cost); + float g = pathlib_cost(parent,node.origin,cost); f = g + h; if(node.pathlib_node_g > g) @@ -125,17 +120,17 @@ float pathlib_makenode_adaptive(entity parent,vector start, vector to, vector go best_open_node = node; } - return 1; + return true; } - where = pathlib_movenode(parent, parent.origin, to, 0); + vector where = pathlib_movenode(parent, parent.origin, to, 0); if (!pathlib_movenode_goodnode) { //pathlib_showsquare(where, 0 ,30); //pathlib_showsquare(parent.origin, 1 ,30); LOG_TRACE("pathlib_movenode_goodnode = 0"); - return 0; + return false; } //pathlib_showsquare(where, 1 ,30); @@ -145,18 +140,18 @@ float pathlib_makenode_adaptive(entity parent,vector start, vector to, vector go LOG_TRACE("NAP WHERE :",vtos(where)); LOG_TRACE("not NAP TO:",vtos(to)); LOG_TRACE("NAP-NNAP:",ftos(vlen(to-where))); - return 0; + return false; } - if(doedge) + if(dodge) if (!tile_check(parent, where)) { LOG_TRACE("tile_check fail"); #if DEBUGPATHING pathlib_showsquare(where, 0 ,30); #endif - return 0; + return false; } @@ -208,7 +203,7 @@ float pathlib_makenode_adaptive(entity parent,vector start, vector to, vector go else if(best_open_node.pathlib_node_f > node.pathlib_node_f) best_open_node = node; - return 1; + return true; } entity pathlib_getbestopen() @@ -235,7 +230,6 @@ entity pathlib_getbestopen() void pathlib_close_node(entity node,vector goal) { - if(node.owner == closedlist) { LOG_TRACE("Pathlib: Tried to close a closed node!"); @@ -269,22 +263,10 @@ void pathlib_cleanup() //return; - entity node; - - node = findfloat(NULL,is_path_node, true); - while(node) + FOREACH_ENTITY_FLOAT(is_path_node, true, { - /* - node.owner = openlist; - node.pathlib_node_g = 0; - node.pathlib_node_h = 0; - node.pathlib_node_f = 0; - node.path_prev = NULL; - */ - - dumpnode(node); - node = findfloat(node,is_path_node, true); - } + dumpnode(it); + }); if(openlist) delete(openlist); @@ -294,60 +276,53 @@ void pathlib_cleanup() openlist = NULL; closedlist = NULL; - } float Cosine_Interpolate(float a, float b, float c) { - float ft,f; - - ft = c * 3.1415927; - f = (1 - cos(ft)) * 0.5; + float ft = c * 3.1415927; + float f = (1 - cos(ft)) * 0.5; return a*(1-f) + b*f; } -float buildpath_nodefilter_directional(vector n,vector c,vector p) +bool buildpath_nodefilter_directional(vector n,vector c,vector p) { - vector d1,d2; - - d2 = normalize(p - c); - d1 = normalize(c - n); + vector d2 = normalize(p - c); + vector d1 = normalize(c - n); if(vdist(d1 - d2, <, 0.25)) { //mark_error(c,30); - return 1; + return true; } //mark_info(c,30); - return 0; + return false; } -float buildpath_nodefilter_moveskip(entity this, vector n,vector c,vector p) +bool buildpath_nodefilter_moveskip(entity this, vector n,vector c,vector p) { pathlib_walknode(this, p, n, 1); if(pathlib_movenode_goodnode) - return 1; + return true; - return 0; + return false; } -float buildpath_nodefilter_none(vector n,vector c,vector p) +bool buildpath_nodefilter_none(vector n,vector c,vector p) { - return 0; + return false; } entity path_build(entity next, vector where, entity prev, entity start) { - entity path; - if(prev && next) if(buildpath_nodefilter) if(buildpath_nodefilter(next.origin,where,prev.origin)) return next; - path = spawn(); + entity path = spawn(); path.owner = start; path.path_next = next; @@ -366,12 +341,10 @@ entity path_build(entity next, vector where, entity prev, entity start) return path; } -entity pathlib_astar(entity this, vector from,vector to) +entity pathlib_astar(entity this, vector from, vector to) { - entity path, start, end, open, n, ln; - float ptime, ftime, ctime; - - ptime = gettime(GETTIME_REALTIME); + entity open; + float ptime = gettime(GETTIME_REALTIME); pathlib_starttime = ptime; pathlib_cleanup(); @@ -455,7 +428,7 @@ entity pathlib_astar(entity this, vector from,vector to) //to_z += 32; LOG_TRACE("AStar init"); - path = pathlib_mknode(from, NULL); + entity path = pathlib_mknode(from, NULL); pathlib_close_node(path, to); if(pathlib_foundgoal) { @@ -479,7 +452,7 @@ entity pathlib_astar(entity this, vector from,vector to) } best_open_node = pathlib_getbestopen(); - n = best_open_node; + entity n = best_open_node; pathlib_close_node(best_open_node, to); if(inwater(n.origin)) pathlib_expandnode_box(n, from, to); @@ -512,12 +485,12 @@ entity pathlib_astar(entity this, vector from,vector to) if(pathlib_foundgoal) { LOG_TRACE("Target found. Rebuilding and filtering path..."); - ftime = gettime(GETTIME_REALTIME); + float ftime = gettime(GETTIME_REALTIME); ptime = ftime - ptime; - start = path_build(NULL,path.origin,NULL,NULL); - end = path_build(NULL,goal_node.origin,NULL,start); - ln = end; + entity start = path_build(NULL,path.origin,NULL,NULL); + entity end = path_build(NULL,goal_node.origin,NULL,start); + entity ln = end; open = goal_node; for(open = goal_node; open.path_prev != path; open = open.path_prev) @@ -530,7 +503,7 @@ entity pathlib_astar(entity this, vector from,vector to) n.path_prev = start; ftime = gettime(GETTIME_REALTIME) - ftime; - ctime = gettime(GETTIME_REALTIME); + float ctime = gettime(GETTIME_REALTIME); pathlib_cleanup(); ctime = gettime(GETTIME_REALTIME) - ctime; diff --git a/qcsrc/server/pathlib/main.qh b/qcsrc/server/pathlib/main.qh index 6c8e4dc76..c867080dd 100644 --- a/qcsrc/server/pathlib/main.qh +++ b/qcsrc/server/pathlib/main.qh @@ -1,4 +1,4 @@ #pragma once -float buildpath_nodefilter_none(vector n,vector c,vector p); +bool buildpath_nodefilter_none(vector n,vector c,vector p); entity path_build(entity next, vector where, entity prev, entity start); diff --git a/qcsrc/server/pathlib/movenode.qc b/qcsrc/server/pathlib/movenode.qc index 4a3bfe35c..ca9180eef 100644 --- a/qcsrc/server/pathlib/movenode.qc +++ b/qcsrc/server/pathlib/movenode.qc @@ -9,7 +9,7 @@ vector pathlib_wateroutnode(entity this, vector start, vector end, float doedge) { vector surface; - pathlib_movenode_goodnode = 0; + pathlib_movenode_goodnode = false; end.x = fsnap(end.x, pathlib_gridsize); end.y = fsnap(end.y, pathlib_gridsize); @@ -31,17 +31,17 @@ vector pathlib_wateroutnode(entity this, vector start, vector end, float doedge) tracebox(start + '0 0 64', movenode_boxmin,movenode_boxmax, end + '0 0 64', MOVE_WORLDONLY, this); if(trace_fraction == 1) - pathlib_movenode_goodnode = 1; + pathlib_movenode_goodnode = true; if(fabs(surface.z - end.z) > 32) - pathlib_movenode_goodnode = 0; + pathlib_movenode_goodnode = false; return end; } vector pathlib_swimnode(entity this, vector start, vector end, float doedge) { - pathlib_movenode_goodnode = 0; + pathlib_movenode_goodnode = false; if(pointcontents(start) != CONTENT_WATER) return end; @@ -54,21 +54,21 @@ vector pathlib_swimnode(entity this, vector start, vector end, float doedge) tracebox(start, movenode_boxmin,movenode_boxmax, end, MOVE_WORLDONLY, this); if(trace_fraction == 1) - pathlib_movenode_goodnode = 1; + pathlib_movenode_goodnode = true; return end; } vector pathlib_flynode(entity this, vector start, vector end, float doedge) { - pathlib_movenode_goodnode = 0; + pathlib_movenode_goodnode = false; end.x = fsnap(end.x, pathlib_gridsize); end.y = fsnap(end.y, pathlib_gridsize); tracebox(start, movenode_boxmin,movenode_boxmax, end, MOVE_WORLDONLY, this); if(trace_fraction == 1) - pathlib_movenode_goodnode = 1; + pathlib_movenode_goodnode = true; return end; } @@ -84,12 +84,11 @@ void a_think(entity this) vector pathlib_walknode(entity this, vector start, vector end, float doedge) { - vector direction,point,last_point,s,e; - float steps, distance, i; + vector point; LOG_TRACE("Walking node from ", vtos(start), " to ", vtos(end)); - pathlib_movenode_goodnode = 0; + pathlib_movenode_goodnode = false; end.x = fsnap(end.x,pathlib_gridsize); end.y = fsnap(end.y,pathlib_gridsize); @@ -116,15 +115,16 @@ vector pathlib_walknode(entity this, vector start, vector end, float doedge) start = trace_endpos; // Find the direcion, without Z - s = start; e = end; + vector s = start; + vector e = end; //e_z = 0; s_z = 0; - direction = normalize(e - s); + vector direction = normalize(e - s); - distance = vlen(start - end); - steps = rint(distance / movenode_stepsize); + float distance = vlen(start - end); + int steps = rint(distance / movenode_stepsize); - last_point = start; - for(i = 1; i < steps; ++i) + vector last_point = start; + for(int i = 1; i < steps; ++i) { point = last_point + (direction * movenode_stepsize); traceline(point + movenode_stepup,point - movenode_maxdrop,MOVE_WORLDONLY,this); @@ -151,6 +151,6 @@ vector pathlib_walknode(entity this, vector start, vector end, float doedge) if(trace_fraction != 1.0) return trace_endpos; - pathlib_movenode_goodnode = 1; + pathlib_movenode_goodnode = true; return last_point; } diff --git a/qcsrc/server/pathlib/pathlib.qh b/qcsrc/server/pathlib/pathlib.qh index 028a2c7e6..a014c7ce2 100644 --- a/qcsrc/server/pathlib/pathlib.qh +++ b/qcsrc/server/pathlib/pathlib.qh @@ -66,10 +66,10 @@ entity best_open_node; vector tile_check_up; vector tile_check_down; float tile_check_size; -float tile_check_cross(entity this, vector where); -float tile_check_plus(entity this, vector where); -float tile_check_star(entity this, vector where); -var float tile_check(entity this, vector where); +bool tile_check_cross(entity this, vector where); +bool tile_check_plus(entity this, vector where); +bool tile_check_star(entity this, vector where); +var bool tile_check(entity this, vector where); float movenode_stepsize; vector movenode_stepup; @@ -77,7 +77,7 @@ vector movenode_maxdrop; vector movenode_boxup; vector movenode_boxmax; vector movenode_boxmin; -float pathlib_movenode_goodnode; +bool pathlib_movenode_goodnode; vector pathlib_wateroutnode(entity this, vector start, vector end, float doedge); vector pathlib_swimnode(entity this, vector start, vector end, float doedge); @@ -105,7 +105,7 @@ float pathlib_h_diagonal2sdp(vector preprev, vector prev, vector point, vec float pathlib_h_none(vector preprev, vector prev) { return 0; } var float pathlib_heuristic(vector from, vector to); -var float pathlib_makenode(entity parent,vector start, vector to, vector goal,float cost); -var float buildpath_nodefilter(vector n,vector c,vector p); +var bool pathlib_makenode(entity parent,vector start, vector to, vector goal,float cost); +var bool buildpath_nodefilter(vector n,vector c,vector p); var float pathlib_wpp_waypointcallback(entity wp, entity wp_prev); diff --git a/qcsrc/server/pathlib/utility.qc b/qcsrc/server/pathlib/utility.qc index be901f886..9ebaac9f2 100644 --- a/qcsrc/server/pathlib/utility.qc +++ b/qcsrc/server/pathlib/utility.qc @@ -23,102 +23,95 @@ bool location_isok(vector point, bool waterok, bool air_isok) entity pathlib_nodeatpoint(vector where) { - entity node; - ++pathlib_searched_cnt; where.x = fsnap(where.x,pathlib_gridsize); where.y = fsnap(where.y,pathlib_gridsize); - node = findradius(where,pathlib_gridsize * 0.5); - while(node) + FOREACH_ENTITY_RADIUS(where, pathlib_gridsize * 0.5, it.is_path_node, { - if(node.is_path_node == true) - return node; - - node = node.chain; - } + return it; + }); return NULL; } -float tile_check_cross(entity this, vector where) +bool tile_check_cross(entity this, vector where) { - vector p,f,r; - - f = PLIB_FORWARD * tile_check_size; - r = PLIB_RIGHT * tile_check_size; + vector p; + vector f = PLIB_FORWARD * tile_check_size; + vector r = PLIB_RIGHT * tile_check_size; // forward-right p = where + f + r; traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this); if (!location_isok(trace_endpos, 1, 0)) - return 0; + return false; // Forward-left p = where + f - r; traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this); if (!location_isok(trace_endpos, 1, 0)) - return 0; + return false; // Back-right p = where - f + r; traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this); if (!location_isok(trace_endpos, 1 ,0)) - return 0; + return false; //Back-left p = where - f - r; traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this); if (!location_isok(trace_endpos, 1, 0)) - return 0; + return false; - return 1; + return true; } -float tile_check_plus(entity this, vector where) +bool tile_check_plus(entity this, vector where) { - vector p,f,r; + vector p; - f = PLIB_FORWARD * tile_check_size; - r = PLIB_RIGHT * tile_check_size; + vector f = PLIB_FORWARD * tile_check_size; + vector r = PLIB_RIGHT * tile_check_size; // forward p = where + f; traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this); if (!location_isok(trace_endpos,1,0)) - return 0; + return false; //left p = where - r; traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this); if (!location_isok(trace_endpos,1,0)) - return 0; + return false; // Right p = where + r; traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this); if (!location_isok(trace_endpos,1,0)) - return 0; + return false; //Back p = where - f; traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this); if (!location_isok(trace_endpos,1,0)) - return 0; + return false; - return 1; + return true; } float tile_check_plus2(entity this, vector where) { - vector p,f,r; - float i = 0, e = 0; + vector p; + int j = 0, e = 0; - f = PLIB_FORWARD * pathlib_gridsize; - r = PLIB_RIGHT * pathlib_gridsize; + vector f = PLIB_FORWARD * pathlib_gridsize; + vector r = PLIB_RIGHT * pathlib_gridsize; //#define pathlib_node_edgeflag_left 2 //#define pathlib_node_edgeflag_right 4 @@ -130,7 +123,7 @@ float tile_check_plus2(entity this, vector where) traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this); if (location_isok(trace_endpos,1,0)) { - ++i; + ++j; e |= pathlib_node_edgeflag_forward; } @@ -140,7 +133,7 @@ float tile_check_plus2(entity this, vector where) traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this); if (location_isok(trace_endpos,1,0)) { - ++i; + ++j; e |= pathlib_node_edgeflag_left; } @@ -150,7 +143,7 @@ float tile_check_plus2(entity this, vector where) traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this); if (location_isok(trace_endpos,1,0)) { - ++i; + ++j; e |= pathlib_node_edgeflag_right; } @@ -159,7 +152,7 @@ float tile_check_plus2(entity this, vector where) traceline(p+tile_check_up,p-tile_check_down,MOVE_WORLDONLY,this); if (location_isok(trace_endpos,1,0)) { - ++i; + ++j; e |= pathlib_node_edgeflag_back; } @@ -168,7 +161,7 @@ float tile_check_plus2(entity this, vector where) traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this); if (location_isok(trace_endpos, 1, 0)) { - ++i; + ++j; e |= pathlib_node_edgeflag_forwardright; } @@ -177,7 +170,7 @@ float tile_check_plus2(entity this, vector where) traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this); if (location_isok(trace_endpos, 1, 0)) { - ++i; + ++j; e |= pathlib_node_edgeflag_forwardleft; } @@ -186,7 +179,7 @@ float tile_check_plus2(entity this, vector where) traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this); if (location_isok(trace_endpos, 1 ,0)) { - ++i; + ++j; e |= pathlib_node_edgeflag_backright; } @@ -195,22 +188,22 @@ float tile_check_plus2(entity this, vector where) traceline(p + tile_check_up, p - tile_check_down, MOVE_WORLDONLY, this); if (location_isok(trace_endpos, 1, 0)) { - ++i; + ++j; e |= pathlib_node_edgeflag_backleft; } - if(i == 0) + if(j == 0) e = pathlib_node_edgeflag_none; return e; } -float tile_check_star(entity this, vector where) +bool tile_check_star(entity this, vector where) { if(tile_check_plus(this, where)) return tile_check_cross(this, where); - return 0; + return false; } -- 2.39.2