From 9aa80d6d05f11afcb0682699c1a631ed9287341b Mon Sep 17 00:00:00 2001 From: uruk Date: Sun, 8 Jun 2014 07:30:31 +0200 Subject: CGE2: Implement helper functions connected to Hero. Also rework Hero::len() during the process. --- engines/cge2/cge2.h | 1 + engines/cge2/hero.cpp | 32 ++++++++++++++++++++------------ engines/cge2/hero.h | 2 +- 3 files changed, 22 insertions(+), 13 deletions(-) (limited to 'engines') diff --git a/engines/cge2/cge2.h b/engines/cge2/cge2.h index 0533e19bd8..be2bbd33e3 100644 --- a/engines/cge2/cge2.h +++ b/engines/cge2/cge2.h @@ -134,6 +134,7 @@ public: void setAutoColors(); bool cross(const V2D &a, const V2D &b, const V2D &c, const V2D &d); bool contain(const V2D &a, const V2D &b, const V2D &p); + long det(const V2D &a, const V2D &b, const V2D &c); int sgn(long n); int mapCross(const V2D &a, const V2D &b); diff --git a/engines/cge2/hero.cpp b/engines/cge2/hero.cpp index 0c46b827f0..5ebce6ef4f 100644 --- a/engines/cge2/hero.cpp +++ b/engines/cge2/hero.cpp @@ -306,9 +306,8 @@ void Hero::operator -- () { warning("STUB: Hero::operator --()"); } -uint32 Hero::len(V2D v) { - long x = v.x, y = v.y; - return (uint32)((x * x + y * y) * (x * x + y * y)); +int Hero::len(V2D v) { + return ((v.x * v.x + v.y * v.y) * (v.x * v.x + v.y * v.y)); } bool Hero::findWay(){ @@ -399,7 +398,6 @@ V3D Hero::screenToGround(V2D pos) { return V3D(V2D::round(x), 0, V2D::round(z)); } - int Hero::cross(const V2D &a, const V2D &b) { int x = V2D::trunc(_pos3D._x); int z = V2D::trunc(_pos3D._z); @@ -407,21 +405,31 @@ int Hero::cross(const V2D &a, const V2D &b) { return _vm->cross(a, b, V2D(_vm, x - r, z), V2D(_vm, x + r, z)) << 1; } - - bool CGE2Engine::cross(const V2D &a, const V2D &b, const V2D &c, const V2D &d) { - warning("STUB: CGE2Engine::cross()"); - return false; + if (contain(a, b, c)) + return true; + if (contain(a, b, d)) + return true; + if (contain(c, d, a)) + return true; + if (contain(c, d, b)) + return true; + return sgn(det(a, b, c)) != sgn(det(a, b, d)) && sgn(det(c, d, a)) != sgn(det(c, d, b)); } bool CGE2Engine::contain(const V2D &a, const V2D &b, const V2D &p) { - warning("STUB: CGE2Engine::contain()"); - return false; + if (det(a, b, p)) + return false; + return ((long)(a.x - p.x) * (p.x - b.x) >= 0 && (long)(a.y - p.y) * (p.y - b.y) >= 0); +} + +long CGE2Engine::det(const V2D &a, const V2D &b, const V2D &c) { + long n = ((long)a.x * b.y + (long)b.x * c.y + (long)c.x*a.y) - ((long)c.x*b.y + (long)b.x*a.y + (long)a.x*c.y); + return n; } int CGE2Engine::sgn(long n) { - warning("STUB: CGE2Engine::sgn()"); - return 0; + return (n == 0) ? 0 : ((n > 0) ? 1 : -1); } int Hero::mapCross(const V2D &a, const V2D &b) { diff --git a/engines/cge2/hero.h b/engines/cge2/hero.h index 8a8d400c26..c9933867a1 100644 --- a/engines/cge2/hero.h +++ b/engines/cge2/hero.h @@ -78,7 +78,7 @@ public: int distance(Sprite * spr); void turn(Dir d); void park(); - static uint32 len(V2D v); + int len(V2D v); bool findWay(); static int snap(int p, int q, int grid); void walkTo(V3D pos); -- cgit v1.2.3