1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef BLADERUNNER_OBSTACLES_H
24 #define BLADERUNNER_OBSTACLES_H
25 
26 #include "bladerunner/rect_float.h"
27 #include "bladerunner/vector.h"
28 
29 namespace BladeRunner {
30 
31 class BladeRunnerEngine;
32 class SaveFileReadStream;
33 class SaveFileWriteStream;
34 
35 class Obstacles {
36 	static const int kVertexCount        = 150;
37 	static const int kPolygonCount       =  50;
38 	static const int kPolygonVertexCount = 160;
39 	static const int kMaxPathSize        = 500;
40 
41 	enum VertexType {
42 		BOTTOM_LEFT,
43 		TOP_LEFT,
44 		TOP_RIGHT,
45 		BOTTOM_RIGHT
46 	};
47 
48 	struct LineSegment {
49 		Vector2 start;
50 		Vector2 end;
51 	};
52 
53 	struct Polygon {
54 		bool       isPresent;
55 		int        verticeCount;
56 		RectFloat  rect;
57 		Vector2    vertices[kPolygonVertexCount];
58 		VertexType vertexType[kPolygonVertexCount];
59 
PolygonPolygon60 		Polygon() : isPresent(false), verticeCount(0), vertexType()
61 		{}
62 	};
63 
64 	BladeRunnerEngine *_vm;
65 
66 	Polygon *_polygons;
67 	Polygon *_polygonsBackup;
68 	Vector2 *_path;
69 	int      _pathSize;
70 	int      _count;
71 	bool     _backup;
72 
73 	static bool lineLineIntersection(LineSegment a, LineSegment b, Vector2 *intersectionPoint);
74 	static bool linePolygonIntersection(LineSegment lineA, VertexType lineAType, Polygon *polyB, Vector2 *intersectionPoint, int *intersectionIndex, int pathLengthSinceLastIntersection);
75 
76 	bool mergePolygons(Polygon &polyA, Polygon &PolyB);
77 
78 public:
79 	Obstacles(BladeRunnerEngine *vm);
80 	~Obstacles();
81 
82 	void clear();
83 	void add(RectFloat rect);
add(float x0,float z0,float x1,float z1)84 	void add(float x0, float z0, float x1, float z1) { add(RectFloat(x0, z0, x1, z1)); }
85 	int findEmptyPolygon() const;
86 	static float getLength(float x0, float z0, float x1, float z1);
87 	bool findNextWaypoint(const Vector3 &from, const Vector3 &to, Vector3 *next);
88 
89 	bool findIntersectionNearest(int polygonIndex, Vector2 from, Vector2 to,
90 	                             int *outVertexIndex, float *outDistance, Vector2 *out) const;
91 	bool findIntersectionFarthest(int polygonIndex, Vector2 from, Vector2 to,
92 	                              int *outVertexIndex, float *outDistance, Vector2 *out) const;
93 
94 	float pathTotalDistance(const Vector2 *path, int pathSize, Vector2 from, Vector2 to) const;
95 	bool findPolygonVerticeByXZ(int *polygonIndex, int *verticeIndex, int *verticeCount, float x, float z) const;
96 	bool findPolygonVerticeByXZWithinTolerance(float x, float z, int *polygonIndex, int *verticeIndex, int startSearchFromPolygonIdx) const;
97 
98 	void clearPath();
99 	int  buildNegativePath(int polyIndex, int vertStartIndex, Vector2 startPos, int vertEndIndex, Vector2 endPos, Vector2 *path, int pathCapacity, bool *pathBlocked);
100 	int  buildPositivePath(int polyIndex, int vertStartIndex, Vector2 startPos, int vertEndIndex, Vector2 endPos, Vector2 *path, int pathCapacity, bool *pathBlocked);
101 
102 	bool verticesCanIntersect(int lineType0, int lineType1, float x0, float y0, float x1, float y1) const;
103 	bool findFarthestAvailablePathVertex(Vector2 *path, int pathSize, Vector3 start, Vector3 *next) const;
104 
105 	void backup();
106 	void restore();
107 	void reset();
108 
109 	void draw();
110 	void save(SaveFileWriteStream &f);
111 	void load(SaveFileReadStream &f);
112 };
113 
114 } // End of namespace BladeRunner
115 
116 #endif
117