Skip to content

Commit

Permalink
Fix bug in AssemblyScript version of the baker
Browse files Browse the repository at this point in the history
  • Loading branch information
Tugcga committed Nov 1, 2022
1 parent 224b7c9 commit b0462f4
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ assemblyscript/grid_graph_test.js
assemblyscript/bvh_test.js
*.js
*.nm
*.obj
python/issues
python/map_benchmark.py
python/navmeshdata.txt
Expand Down
18 changes: 12 additions & 6 deletions assemblyscript/assembly/baker/rc_area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ function box_blur(chf: CompactHeightfield,
}
}
}
dst[i] = (d + 5) // 9
dst[i] = (d + 5) / 9;
}
}
}
Expand Down Expand Up @@ -599,7 +599,7 @@ function expand_regions(max_iter: int,
let dirty_entries: List<DirtyEntry> = new List<DirtyEntry>();
let iter: int = 0;
while(stack.length > 0){
let failed: int = 0;
let failed: int = 0;
dirty_entries.reset();

for(let j = 0; j < stack.length; j++){
Expand Down Expand Up @@ -645,10 +645,10 @@ function expand_regions(max_iter: int,
failed += 1;
}
}
for(let i = 0; i < dirty_entries.length; i++){
const idx: int = dirty_entries[i].index;
buf[src_reg + idx] = dirty_entries[i].region;
buf[src_dist + idx] = dirty_entries[i].distance2;
for(let i_for = 0; i_for < dirty_entries.length; i_for++){
const idx: int = dirty_entries[i_for].index;
buf[src_reg + idx] = dirty_entries[i_for].region;
buf[src_dist + idx] = dirty_entries[i_for].distance2;
}

if(failed == stack.length){
Expand Down Expand Up @@ -1261,8 +1261,14 @@ export function build_regions(chf: CompactHeightfield,
let lvl_stacks: StaticArray<List<LevelStackEntry>> = new StaticArray<List<LevelStackEntry>>(NB_STACKS);
for(let i = 0; i < NB_STACKS; i++){
lvl_stacks[i] = new List<LevelStackEntry>();
for(let j = 0; j < 256; j++) {
lvl_stacks[i].push(new LevelStackEntry());
}
}
let stack: List<LevelStackEntry> = new List<LevelStackEntry>();
for(let i = 0; i < 256; i++) {
stack.push(new LevelStackEntry());
}

let src_reg: int = 0;
let src_dist: int = chf.span_count;
Expand Down
4 changes: 4 additions & 0 deletions assemblyscript/assembly/baker/rc_classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export class LevelStackEntry{
this.y = _y;
this.index = _index;
}

toString(): string {
return "LS[" + this.x.toString() + ", " + this.y.toString() + ", " + this.index.toString() + "]";
}
}

export class DirtyEntry{
Expand Down
18 changes: 11 additions & 7 deletions assemblyscript/assembly/baker/rc_contour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,14 @@ function distance_pt_seg(x: int,
pz: int,
qx: int,
qz: int): float{
const pqx: float = <f64>(qx - px);
const pqz: float = <f64>(qz - pz);
let dx: float = <f64>(x - px);
let dz: float = <f64>(z - pz);
const pqx_int = qx - px;
const pqx: float = <f64>pqx_int;
const pqz_int = qz - pz;
const pqz: float = <f64>pqz_int;
const dx_int = x - px;
let dx: float = <f64>dx_int;
const dz_int = z - pz;
let dz: float = <f64>dz_int;
const d: float = pqx*pqx + pqz*pqz;
let t: float = pqx*dx + pqz*dz;
if(d > 0.0){
Expand All @@ -230,8 +234,8 @@ function distance_pt_seg(x: int,
t = 1.0;
}

dx = <f64>(px) + t*pqx - <f64>(x);
dz = <f64>(pz) + t*pqz - <f64>(z);
dx = <f64>px + t*pqx - <f64>x;
dz = <f64>pz + t*pqz - <f64>z;

return dx*dx + dz*dz;
}
Expand Down Expand Up @@ -812,7 +816,7 @@ function merge_region_holes(region: ContourRegion, holes: StaticArray<ContourHol
}
bestVertex = (bestVertex + 1) % hole.nverts;
}

if(index == -1){
let region_outline = region.outline;
if(region_outline){
Expand Down
1 change: 0 additions & 1 deletion assemblyscript/assembly/common/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//import "wasi";
import { Vector2, abs_sq, dot } from "./vector2";
import { List } from "./list";
import { Serializable, SD_TYPE } from "./binary_io";
Expand Down
10 changes: 5 additions & 5 deletions python/pathfinder/navmesh_baker/rc_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ def expand_regions(max_iter: int,
ai: int = c.index + get_con(s, dir)
if chf.areas[ai] != area:
continue
if (buf[src_reg + ai] > 0) and (buf[src_reg + ai] & RC_BORDER_REG == 0):
if (buf[src_reg + ai] > 0) and ((buf[src_reg + ai] & RC_BORDER_REG) == 0):
if buf[src_dist + ai] + 2 < d2:
r = buf[src_reg + ai]
d2 = buf[src_dist + ai] + 2
Expand All @@ -510,10 +510,10 @@ def expand_regions(max_iter: int,
else:
failed += 1
# Copy entries that differ between src and dst to keep them in sync
for i in range(len(dirty_entries)):
idx: int = dirty_entries[i].index
buf[src_reg + idx] = dirty_entries[i].region
buf[src_dist + idx] = dirty_entries[i].distance2
for i_for in range(len(dirty_entries)):
idx: int = dirty_entries[i_for].index
buf[src_reg + idx] = dirty_entries[i_for].region
buf[src_dist + idx] = dirty_entries[i_for].distance2

if failed == len(stack):
break
Expand Down
3 changes: 3 additions & 0 deletions python/pathfinder/navmesh_baker/rc_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def __init__(self, _x: int = 0, _y: int = 0, _index: int = 0):
self.y = _y
self.index = _index

def __repr__(self):
return "LS[" + str(self.x) + ", " + str(self.y) + ", " + str(self.index) + "]"

class DirtyEntry:
def __init__(self, _index: int = 0,
_region: int = 0, # 2 bytes
Expand Down
2 changes: 1 addition & 1 deletion python/pathfinder/navmesh_baker/rc_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ def merge_region_holes(region: ContourRegion, holes: List[ContourHole]):
break
# All the potential diagonals for the current vertex were intersecting, try next vertex
bestVertex = (bestVertex + 1) % hole.nverts

if index == -1:
print("[Navmesh Baker] merge_holes: Failed to find merge points for " + str(region.outline) + " and " + str(hole) +".")
continue
Expand Down

0 comments on commit b0462f4

Please sign in to comment.