Colobot
taskgoto.h
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2015, 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 
23 #include "object/task/task.h"
24 
25 #include "math/vector.h"
26 
27 #include <memory>
28 
29 
30 class CObject;
31 
32 const int MAXPOINTS = 500;
33 
34 
35 enum TaskGotoGoal
36 {
37  TGG_DEFAULT = -1, // default mode
38  TGG_STOP = 0, // goes to destination pausing with precision
39  TGG_EXPRESS = 1, // goes to destination without stopping
40 };
41 
42 enum TaskGotoCrash
43 {
44  TGC_DEFAULT = -1, // default mode
45  TGC_HALT = 0, // stops if collision
46  TGC_RIGHTLEFT = 1, // right-left
47  TGC_LEFTRIGHT = 2, // left-right
48  TGC_LEFT = 3, // left
49  TGC_RIGHT = 4, // right
50  TGC_BEAM = 5, // algorithm "sunlight"
51 };
52 
53 
54 enum TaskGotoPhase
55 {
56  TGP_ADVANCE = 1, // advance
57  TGP_LAND = 2, // landed
58  TGP_TURN = 3, // turns to finish
59  TGP_MOVE = 4, // advance to finish
60  TGP_CRWAIT = 5, // waits after collision
61  TGP_CRTURN = 6, // turns right after collision
62  TGP_CRADVANCE = 7, // advance to the right after collision
63  TGP_CLWAIT = 8, // waits after collision
64  TGP_CLTURN = 9, // turns left after collision
65  TGP_CLADVANCE = 10, // advance to the left after collision
66  TGP_BEAMLEAK = 11, // beam: leak (leaking)
67  TGP_BEAMSEARCH = 12, // beam: search
68  TGP_BEAMWCOLD = 13, // beam: expects cool reactor
69  TGP_BEAMUP = 14, // beam: off
70  TGP_BEAMGOTO = 15, // beam: goto dot list
71  TGP_BEAMDOWN = 16, // beam: landed
72 };
73 
74 
75 
76 class CTaskGoto : public CForegroundTask
77 {
78 public:
79  CTaskGoto(COldObject* object);
80  ~CTaskGoto();
81 
82  bool EventProcess(const Event &event) override;
83 
84  Error Start(Math::Vector goal, float altitude, TaskGotoGoal goalMode, TaskGotoCrash crashMode);
85  Error IsEnded() override;
86 
87 protected:
88  CObject* WormSearch(Math::Vector &impact);
89  void WormFrame(float rTime);
90  CObject* SearchTarget(Math::Vector pos, float margin);
91  bool AdjustTarget(CObject* pObj, Math::Vector &pos, float &distance);
92  bool AdjustBuilding(Math::Vector &pos, float margin, float &distance);
93  bool GetHotPoint(CObject *pObj, Math::Vector &pos, bool bTake, float distance, float &suppl);
94  bool LeakSearch(Math::Vector &pos, float &delay);
95  void ComputeRepulse(Math::Point &dir);
96  void ComputeFlyingRepulse(float &dir);
97 
98  int BeamShortcut();
99  void BeamStart();
100  void BeamInit();
101  Error BeamSearch(const Math::Vector &start, const Math::Vector &goal, float goalRadius);
102  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);
103  Math::Vector BeamPoint(const Math::Vector &startPoint, const Math::Vector &goalPoint, float angle, float step);
104 
105  void BitmapDebug(const Math::Vector &min, const Math::Vector &max, const Math::Vector &start, const Math::Vector &goal);
106  bool BitmapTestLine(const Math::Vector &start, const Math::Vector &goal, float stepAngle, bool bSecond);
107  void BitmapObject();
108  void BitmapTerrain(const Math::Vector &min, const Math::Vector &max);
109  void BitmapTerrain(int minx, int miny, int maxx, int maxy);
110  bool BitmapOpen();
111  bool BitmapClose();
112  void BitmapSetCircle(const Math::Vector &pos, float radius);
113  void BitmapClearCircle(const Math::Vector &pos, float radius);
114  void BitmapSetDot(int rank, int x, int y);
115  void BitmapClearDot(int rank, int x, int y);
116  bool BitmapTestDot(int rank, int x, int y);
117 
118 protected:
119  Math::Vector m_goal;
120  Math::Vector m_goalObject;
121  float m_angle = 0.0f;
122  float m_altitude = 0.0f;
123  TaskGotoCrash m_crashMode = TGC_DEFAULT;
124  TaskGotoGoal m_goalMode = TGG_DEFAULT;
125  TaskGotoPhase m_phase = TGP_ADVANCE;
126  int m_try = 0;
127  Error m_error = ERR_OK;
128  bool m_bTake = false;
129  float m_stopLength = 0.0f; // braking distance
130  float m_time = 0.0f;
131  Math::Vector m_pos;
132  bool m_bWorm = false;
133  bool m_bApprox = false;
134  float m_wormLastTime = 0.0f;
135  float m_lastDistance = 0.0f;
136 
137  int m_bmSize = 0; // width or height of the table
138  int m_bmOffset = 0; // m_bmSize/2
139  int m_bmLine = 0; // increment line m_bmSize/8
140  std::unique_ptr<unsigned char[]> m_bmArray; // bit table
141  int m_bmMinX = 0, m_bmMinY = 0;
142  int m_bmMaxX = 0, m_bmMaxY = 0;
143  int m_bmTotal = 0; // number of points in m_bmPoints
144  int m_bmIndex = 0; // index in m_bmPoints
145  Math::Vector m_bmPoints[MAXPOINTS+2];
146  char m_bmIter[MAXPOINTS+2] = {};
147  int m_bmIterCounter = 0;
148  CObject* m_bmCargoObject = nullptr;
149  float m_bmFinalMove = 0.0f; // final advance distance
150  float m_bmFinalDist = 0.0f; // effective distance to advance
151  Math::Vector m_bmFinalPos; // initial position before advance
152  float m_bmTimeLimit = 0.0f;
153  int m_bmStep = 0;
154  Math::Vector m_bmWatchDogPos;
155  float m_bmWatchDogTime = 0.0f;
156  Math::Vector m_leakPos; // initial position leak
157  float m_leakDelay = 0.0f;
158  float m_leakTime = 0.0f;
159  bool m_bLeakRecede = false;
160 };
Definition: old_object.h:77
Definition: task.h:96
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:709
Definition: taskgoto.h:76
Base class for all 3D in-game objects.
Definition: object.h:59