Colobot
taskgoto.h
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 #pragma once
21 
22 #include "object/task/task.h"
23 
24 #include "math/vector.h"
25 
26 #include <memory>
27 
28 namespace Math
29 {
30 struct Point;
31 } // namespace Math;
32 
33 
34 class CObject;
35 
36 const int MAXPOINTS = 500;
37 
38 
39 enum TaskGotoGoal
40 {
41  TGG_DEFAULT = -1, // default mode
42  TGG_STOP = 0, // goes to destination pausing with precision
43  TGG_EXPRESS = 1, // goes to destination without stopping
44 };
45 
46 enum TaskGotoCrash
47 {
48  TGC_DEFAULT = -1, // default mode
49  TGC_HALT = 0, // stops if collision
50  TGC_RIGHTLEFT = 1, // right-left
51  TGC_LEFTRIGHT = 2, // left-right
52  TGC_LEFT = 3, // left
53  TGC_RIGHT = 4, // right
54  TGC_BEAM = 5, // algorithm "sunlight"
55 };
56 
57 
58 enum TaskGotoPhase
59 {
60  TGP_ADVANCE = 1, // advance
61  TGP_LAND = 2, // landed
62  TGP_TURN = 3, // turns to finish
63  TGP_MOVE = 4, // advance to finish
64  TGP_CRWAIT = 5, // waits after collision
65  TGP_CRTURN = 6, // turns right after collision
66  TGP_CRADVANCE = 7, // advance to the right after collision
67  TGP_CLWAIT = 8, // waits after collision
68  TGP_CLTURN = 9, // turns left after collision
69  TGP_CLADVANCE = 10, // advance to the left after collision
70  TGP_BEAMLEAK = 11, // beam: leak (leaking)
71  TGP_BEAMSEARCH = 12, // beam: search
72  TGP_BEAMWCOLD = 13, // beam: expects cool reactor
73  TGP_BEAMUP = 14, // beam: off
74  TGP_BEAMGOTO = 15, // beam: goto dot list
75  TGP_BEAMDOWN = 16, // beam: landed
76 };
77 
78 
79 
80 class CTaskGoto : public CForegroundTask
81 {
82 public:
83  CTaskGoto(COldObject* object);
84  ~CTaskGoto();
85 
86  bool EventProcess(const Event &event) override;
87 
88  Error Start(Math::Vector goal, float altitude, TaskGotoGoal goalMode, TaskGotoCrash crashMode);
89  Error IsEnded() override;
90 
91 protected:
92  CObject* WormSearch(Math::Vector &impact);
93  void WormFrame(float rTime);
94  CObject* SearchTarget(Math::Vector pos, float margin);
95  bool AdjustTarget(CObject* pObj, Math::Vector &pos, float &distance);
96  bool AdjustBuilding(Math::Vector &pos, float margin, float &distance);
97  bool GetHotPoint(CObject *pObj, Math::Vector &pos, bool bTake, float distance, float &suppl);
98  bool LeakSearch(Math::Vector &pos, float &delay);
99  void ComputeRepulse(Math::Point &dir);
100  void ComputeFlyingRepulse(float &dir);
101 
102  int BeamShortcut();
103  void BeamStart();
104  void BeamInit();
105  Error BeamSearch(const Math::Vector &start, const Math::Vector &goal, float goalRadius);
106  Error BeamExplore(const Math::Vector &prevPos, const Math::Vector &curPos, const Math::Vector &goalPos, float goalRadius, float angle, int nbDiv, float step, int i, int nbIter);
107  Math::Vector BeamPoint(const Math::Vector &startPoint, const Math::Vector &goalPoint, float angle, float step);
108 
109  bool BitmapTestLine(const Math::Vector &start, const Math::Vector &goal, float stepAngle, bool bSecond);
110  void BitmapObject();
111  void BitmapTerrain(const Math::Vector &min, const Math::Vector &max);
112  void BitmapTerrain(int minx, int miny, int maxx, int maxy);
113  bool BitmapOpen();
114  bool BitmapClose();
115  void BitmapSetCircle(const Math::Vector &pos, float radius);
116  void BitmapClearCircle(const Math::Vector &pos, float radius);
117  void BitmapSetDot(int rank, int x, int y);
118  void BitmapClearDot(int rank, int x, int y);
119  bool BitmapTestDot(int rank, int x, int y);
120 
121 protected:
122  Math::Vector m_goal;
123  Math::Vector m_goalObject;
124  float m_angle = 0.0f;
125  float m_altitude = 0.0f;
126  TaskGotoCrash m_crashMode = TGC_DEFAULT;
127  TaskGotoGoal m_goalMode = TGG_DEFAULT;
128  TaskGotoPhase m_phase = TGP_ADVANCE;
129  int m_try = 0;
130  Error m_error = ERR_OK;
131  bool m_bTake = false;
132  float m_stopLength = 0.0f; // braking distance
133  float m_time = 0.0f;
134  Math::Vector m_pos;
135  bool m_bWorm = false;
136  bool m_bApprox = false;
137  float m_wormLastTime = 0.0f;
138  float m_lastDistance = 0.0f;
139 
140  bool m_bmChanged = true;
141  int m_bmSize = 0; // width or height of the table
142  int m_bmOffset = 0; // m_bmSize/2
143  int m_bmLine = 0; // increment line m_bmSize/8
144  std::unique_ptr<unsigned char[]> m_bmArray; // bit table
145  int m_bmMinX = 0, m_bmMinY = 0;
146  int m_bmMaxX = 0, m_bmMaxY = 0;
147  int m_bmTotal = 0; // number of points in m_bmPoints
148  int m_bmIndex = 0; // index in m_bmPoints
149  Math::Vector m_bmPoints[MAXPOINTS+2];
150  char m_bmIter[MAXPOINTS+2] = {};
151  int m_bmIterCounter = 0;
152  CObject* m_bmCargoObject = nullptr;
153  float m_bmFinalMove = 0.0f; // final advance distance
154  float m_bmFinalDist = 0.0f; // effective distance to advance
155  Math::Vector m_bmFinalPos; // initial position before advance
156  float m_bmTimeLimit = 0.0f;
157  int m_bmStep = 0;
158  Math::Vector m_bmWatchDogPos;
159  float m_bmWatchDogTime = 0.0f;
160  Math::Vector m_leakPos; // initial position leak
161  float m_leakDelay = 0.0f;
162  float m_leakTime = 0.0f;
163  bool m_bLeakRecede = false;
164 };
Definition: old_object.h:79
Namespace for (new) math code.
Definition: device.h:39
Definition: task.h:94
2D point
Definition: point.h:50
Vector struct and related functions.
3D (3x1) vector
Definition: vector.h:53
Event sent by system, interface or game.
Definition: event.h:735
Definition: taskgoto.h:80
Base class for all 3D in-game objects.
Definition: object.h:63