From ec2e2a84a488106f1803236e77741bf8cc19d796 Mon Sep 17 00:00:00 2001 From: Itms Date: Tue, 5 Jul 2016 20:23:12 +0000 Subject: [PATCH] Fix and improve 001c411cc2. This was SVN commit r18489. --- source/simulation2/components/tests/test_Pathfinder.h | 11 ++++++++++- source/simulation2/helpers/Pathfinding.h | 10 ++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/source/simulation2/components/tests/test_Pathfinder.h b/source/simulation2/components/tests/test_Pathfinder.h index 814ff0ab25..5424ca2939 100644 --- a/source/simulation2/components/tests/test_Pathfinder.h +++ b/source/simulation2/components/tests/test_Pathfinder.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2015 Wildfire Games. +/* Copyright (C) 2016 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -56,6 +56,15 @@ public: CXeromyces::Terminate(); } + void test_namespace() + { + // Check that Pathfinding::NAVCELL_SIZE is actually an integer and that the definitions + // of Pathfinding::NAVCELL_SIZE_INT and Pathfinding::NAVCELL_SIZE_LOG2 match + TS_ASSERT_EQUALS(Pathfinding::NAVCELL_SIZE.ToInt_RoundToNegInfinity(), Pathfinding::NAVCELL_SIZE.ToInt_RoundToInfinity()); + TS_ASSERT_EQUALS(Pathfinding::NAVCELL_SIZE.ToInt_RoundToNearest(), Pathfinding::NAVCELL_SIZE_INT); + TS_ASSERT_EQUALS((Pathfinding::NAVCELL_SIZE >> 1).ToInt_RoundToZero(), Pathfinding::NAVCELL_SIZE_LOG2); + } + void test_performance_DISABLED() { CTerrain terrain; diff --git a/source/simulation2/helpers/Pathfinding.h b/source/simulation2/helpers/Pathfinding.h index d3178910cd..c72ea46f37 100644 --- a/source/simulation2/helpers/Pathfinding.h +++ b/source/simulation2/helpers/Pathfinding.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2015 Wildfire Games. +/* Copyright (C) 2016 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -144,11 +144,9 @@ namespace Pathfinding */ inline void NearestNavcell(entity_pos_t x, entity_pos_t z, u16& i, u16& j, u16 w, u16 h) { - // x, z should be divided by NAVCELL_SIZE but that value happens to be 1 for now - // Since this is an i64 division and rather slow (10% of ComputeShortPath), cut it out - cassert(NAVCELL_SIZE_INT == 1); - i = (u16)clamp(x.ToInt_RoundToNegInfinity(), 0, w - 1); - j = (u16)clamp(z.ToInt_RoundToNegInfinity(), 0, h - 1); + // Use NAVCELL_SIZE_INT to save the cost of dividing by a fixed + i = (u16)clamp((x / NAVCELL_SIZE_INT).ToInt_RoundToNegInfinity(), 0, w - 1); + j = (u16)clamp((z / NAVCELL_SIZE_INT).ToInt_RoundToNegInfinity(), 0, h - 1); } /**