summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source
diff options
context:
space:
mode:
authorRobert Vollmert <rvollmert@gmx.net>2009-10-11 17:47:32 +0200
committerRobert Vollmert <rvollmert@gmx.net>2009-10-11 18:05:00 +0200
commit4b84fa4805af04d59d98eba0425be894831e7383 (patch)
tree7b40540fe59309afa2f97a9fba77d84f9f5efa55 /crawl-ref/source
parent0f2a6c57d47cc0e5e55198914a3a67f25f2af380 (diff)
downloadcrawl-ref-4b84fa4805af04d59d98eba0425be894831e7383.tar.gz
crawl-ref-4b84fa4805af04d59d98eba0425be894831e7383.zip
Use floor() instead of static_cast<int> for rounding.
This way the code is less likely to fall over with negative coordinates.
Diffstat (limited to 'crawl-ref/source')
-rw-r--r--crawl-ref/source/ray.cc30
-rw-r--r--crawl-ref/source/ray.h6
2 files changed, 28 insertions, 8 deletions
diff --git a/crawl-ref/source/ray.cc b/crawl-ref/source/ray.cc
index d57a8e4058..604d448c80 100644
--- a/crawl-ref/source/ray.cc
+++ b/crawl-ref/source/ray.cc
@@ -8,16 +8,36 @@ REVISION("$Rev$");
#include "ray.h"
-#include <cmath>
+#include <math.h>
#include "los.h"
#include "terrain.h"
+int ifloor(double d)
+{
+ return static_cast<int>(floor(d));
+}
+
ray_def::ray_def(double ax, double ay, double s, int q, int idx)
: accx(ax), accy(ay), slope(s), quadrant(q), fullray_idx(idx)
{
}
+int ray_def::x() const
+{
+ return ifloor(accx);
+}
+
+int ray_def::y() const
+{
+ return ifloor(accy);
+}
+
+coord_def ray_def::pos() const
+{
+ return coord_def(x(), y());
+}
+
double ray_def::reflect(double p, double c) const
{
return (c + c - p);
@@ -195,7 +215,7 @@ int ray_def::advance(bool shortest_possible, const coord_def *target)
// If we want to minimise the number of moves on the ray, look one
// step ahead and see if we can get a diagonal.
- const coord_def old(static_cast<int>(accx), static_cast<int>(accy));
+ const coord_def old = pos();
const int ret = raw_advance();
if (ret == 2 || (target && pos() == *target))
@@ -204,7 +224,7 @@ int ray_def::advance(bool shortest_possible, const coord_def *target)
const double maccx = accx, maccy = accy;
if (raw_advance() != 2)
{
- const coord_def second(static_cast<int>(accx), static_cast<int>(accy));
+ const coord_def second = pos();
// If we can convert to a diagonal, do so.
if ((second - old).abs() == 2)
return (2);
@@ -235,8 +255,8 @@ int ray_def::_find_next_intercept()
return 1;
}
- const double xtarget = static_cast<int>(accx) + 1;
- const double ytarget = static_cast<int>(accy) + 1;
+ const double xtarget = ifloor(accx) + 1;
+ const double ytarget = ifloor(accy) + 1;
const double xdistance = xtarget - accx;
const double ydistance = ytarget - accy;
double distdiff = xdistance * slope - ydistance;
diff --git a/crawl-ref/source/ray.h b/crawl-ref/source/ray.h
index 03e8fc4749..1500f39cfc 100644
--- a/crawl-ref/source/ray.h
+++ b/crawl-ref/source/ray.h
@@ -22,9 +22,9 @@ public:
public:
ray_def(double accx = 0.0, double accy = 0.0, double slope = 0.0,
int quadrant = 0, int fullray_idx = -1);
- int x() const { return static_cast<int>(accx); }
- int y() const { return static_cast<int>(accy); }
- coord_def pos() const { return coord_def(x(), y()); }
+ int x() const;
+ int y() const;
+ coord_def pos() const;
// returns the direction taken (0,1,2)
int advance(bool shorten = false, const coord_def *p = NULL);