Colobot
robotmain.h
Go to the documentation of this file.
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 
25 #pragma once
26 
27 #include "app/pausemanager.h"
28 
29 #include "common/error.h"
30 #include "common/event.h"
31 #include "common/singleton.h"
32 
33 #include "graphics/engine/camera.h"
35 
36 #include "level/build_type.h"
37 #include "level/level_category.h"
38 #include "level/mainmovie.h"
39 #include "level/research_type.h"
40 
41 #include "object/drive_type.h"
42 #include "object/mission_type.h"
43 #include "object/object_type.h"
44 #include "object/tool_type.h"
45 
46 #include <deque>
47 #include <stdexcept>
48 
49 enum Phase
50 {
51  PHASE_WELCOME1,
52  PHASE_WELCOME2,
53  PHASE_WELCOME3,
54  PHASE_PLAYER_SELECT,
55  PHASE_APPERANCE,
56  PHASE_MAIN_MENU,
57  PHASE_LEVEL_LIST,
58  PHASE_SIMUL,
59  PHASE_SETUPd,
60  PHASE_SETUPg,
61  PHASE_SETUPp,
62  PHASE_SETUPc,
63  PHASE_SETUPs,
64  PHASE_SETUPds,
65  PHASE_SETUPgs,
66  PHASE_SETUPps,
67  PHASE_SETUPcs,
68  PHASE_SETUPss,
69  PHASE_WRITEs,
70  PHASE_READ,
71  PHASE_READs,
72  PHASE_WIN,
73  PHASE_LOST,
74  PHASE_QUIT_SCREEN,
75  PHASE_SATCOM,
76 };
77 std::string PhaseToString(Phase phase);
78 bool IsInSimulationConfigPhase(Phase phase);
79 bool IsPhaseWithWorld(Phase phase);
80 bool IsMainMenuPhase(Phase phase);
81 
82 
83 class CEventQueue;
84 class CSoundInterface;
85 class CLevelParserLine;
86 class CInput;
87 class CObjectManager;
88 class CSceneEndCondition;
90 class CPlayerProfile;
91 class CSettings;
92 class COldObject;
93 class CPauseManager;
94 struct ActivePause;
95 
96 namespace Gfx
97 {
98 class CEngine;
99 class CLightManager;
100 class CWater;
101 class CCloud;
102 class CLightning;
103 class CPlanet;
104 class CTerrain;
105 class CModelManager;
106 }
107 
108 namespace Ui
109 {
110 class CMainUserInterface;
111 class CMainShort;
112 class CMainMap;
113 class CInterface;
114 class CDisplayText;
115 class CDisplayInfo;
116 class CDebugMenu;
117 }
118 
120 {
121  ObjectType type = OBJECT_NULL;
122  std::string name = "";
123 };
124 
125 
126 const int MAXSHOWLIMIT = 5;
127 const int MAXSHOWPARTI = 200;
128 const float SHOWLIMITTIME = 20.0f;
129 
130 const int MAXSCENE = 999;
131 
132 struct ShowLimit
133 {
134  bool used = false;
135  Math::Vector pos;
136  float radius = 0.0f;
137  int total = 0;
138  int parti[MAXSHOWPARTI] = {};
139  CObject* link = nullptr;
140  float duration = 0.0f;
141  float time = 0.0f;
142 };
143 
144 struct MinMax
145 {
146  int min = -1;
147  int max = -1;
148 };
149 
150 
151 const int SATCOM_HUSTON = 0;
152 const int SATCOM_SAT = 1;
153 const int SATCOM_OBJECT = 2;
154 const int SATCOM_LOADING = 3;
155 const int SATCOM_PROG = 4;
156 const int SATCOM_SOLUCE = 5;
157 const int SATCOM_MAX = 6;
158 
159 class CRobotMain : public CSingleton<CRobotMain>
160 {
161 public:
162  CRobotMain();
163  virtual ~CRobotMain();
164 
165  Gfx::CCamera* GetCamera();
166  Gfx::CTerrain* GetTerrain();
167  Ui::CInterface* GetInterface();
168  Ui::CDisplayText* GetDisplayText();
169  CPauseManager* GetPauseManager();
170 
171  void ChangePhase(Phase phase);
172  bool ProcessEvent(Event &event);
173  Phase GetPhase();
174 
175  bool CreateShortcuts();
176  void ScenePerso();
177 
178  void SetMovieLock(bool lock);
179  bool GetMovieLock();
180  bool GetInfoLock();
181  void SetSatComLock(bool lock);
182  bool GetSatComLock();
183  void SetEditLock(bool lock, bool edit);
184  bool GetEditLock();
185  void SetEditFull(bool full);
186  bool GetEditFull();
187  void SetFriendAim(bool friendAim);
188  bool GetFriendAim();
189 
190  void SetSpeed(float speed);
191  float GetSpeed();
192 
193  void UpdateShortcuts();
194  void SelectHuman();
195  CObject* SearchHuman();
196  CObject* SearchToto();
197  CObject* SearchNearest(Math::Vector pos, CObject* exclu);
198  bool SelectObject(CObject* obj, bool displayError=true);
199  CObject* GetSelectObject();
200  CObject* DeselectAll();
201 
202  void ResetObject();
203  void UpdateAudio(bool frame);
204  void SetMissionResultFromScript(Error result, float delay);
205  Error CheckEndMission(bool frame);
206  Error ProcessEndMissionTake();
207  Error ProcessEndMissionTakeForGroup(std::vector<CSceneEndCondition*>& endTakes);
208  const std::map<std::string, MinMax>& GetObligatoryTokenList();
209  void UpdateMap();
210  bool GetShowMap();
211 
212  MainMovieType GetMainMovie();
213 
214  void FlushDisplayInfo();
215  void StartDisplayInfo(int index, bool movie);
216  void StartDisplayInfo(const std::string& filename, int index);
217  void StopDisplayInfo();
218  char* GetDisplayInfoName(int index);
219 
220  void StartSuspend();
221  void StopSuspend();
222 
223  float GetGameTime();
224 
225  const std::string& GetScriptName();
226  const std::string& GetScriptFile();
227  bool GetTrainerPilot();
228  bool GetFixScene();
229  bool GetShowSoluce();
230  bool GetSceneSoluce();
231  bool GetShowAll();
232  bool GetRadar();
233  MissionType GetMissionType();
234 
235  int GetGamerFace();
236  int GetGamerGlasses();
237  bool GetGamerOnlyHead();
238  float GetPersoAngle();
239 
240  void SetLevel(LevelCategory cat, int chap, int rank);
241  LevelCategory GetLevelCategory();
242  int GetLevelChap();
243  int GetLevelRank();
244  std::string GetCustomLevelDir();
245  void SetReadScene(std::string path);
246  void UpdateChapterPassed();
247 
248  void StartMusic();
249  void UpdatePause(PauseType pause);
250  void UpdatePauseMusic(PauseMusic music);
251  void ClearInterface();
252  void ChangeColor();
253 
254  float SearchNearestObject(Math::Vector center, CObject *exclu);
255  bool FreeSpace(Math::Vector &center, float minRadius, float maxRadius, float space, CObject *exclu);
256  bool FlatFreeSpace(Math::Vector &center, float minFlat, float minRadius, float maxRadius, float space, CObject *exclu);
257  float GetFlatZoneRadius(Math::Vector center, float maxRadius, CObject *exclu);
258  void HideDropZone(CObject* metal);
259  void ShowDropZone(CObject* metal, CObject* transporter);
260  void FlushShowLimit(int i);
261  void SetShowLimit(int i, Gfx::ParticleType parti, CObject *obj, Math::Vector pos,
262  float radius, float duration=SHOWLIMITTIME);
263  void StartShowLimit();
264  void FrameShowLimit(float rTime);
265 
266  void SaveAllScript();
267  void SaveOneScript(CObject *obj);
268  bool SaveFileStack(CObject *obj, FILE *file, int objRank);
269  bool ReadFileStack(CObject *obj, FILE *file, int objRank);
270 
271  void FlushNewScriptName();
272  void AddNewScriptName(ObjectType type, const std::string& name);
273  std::string GetNewScriptName(ObjectType type, int rank);
274 
275  void SelectPlayer(std::string playerName);
276  CPlayerProfile* GetPlayerProfile();
277 
278  bool IOIsBusy();
279  bool IOWriteScene(std::string filename, std::string filecbot, std::string filescreenshot, const std::string& info, bool emergencySave = false);
280  void IOWriteSceneFinished();
281  CObject* IOReadScene(std::string filename, std::string filecbot);
282  void IOWriteObject(CLevelParserLine *line, CObject* obj, const std::string& programDir, int objRank);
283  CObject* IOReadObject(CLevelParserLine *line, const std::string& programDir, const std::string& objCounterText, float objectProgress, int objRank = -1);
284 
285  int CreateSpot(Math::Vector pos, Gfx::Color color);
286 
287  CObject* GetSelect();
288 
289  void DisplayError(Error err, CObject* pObj, float time=10.0f);
290  void DisplayError(Error err, Math::Vector goal, float height=15.0f, float dist=60.0f, float time=10.0f);
291 
292  void UpdateCustomLevelList();
293  std::string GetCustomLevelName(int id);
294  const std::vector<std::string>& GetCustomLevelList();
295 
297  bool IsLoading();
298 
299  void StartMissionTimer();
300 
301  void SetAutosave(bool enable);
302  bool GetAutosave();
303  void SetAutosaveInterval(int interval);
304  int GetAutosaveInterval();
305  void SetAutosaveSlots(int slots);
306  int GetAutosaveSlots();
307 
309  void SetExitAfterMission(bool exit);
310 
312  bool CanPlayerInteract();
313 
315  const std::string& GetTeamName(int id);
316 
318  bool IsTeamColorDefined(int id);
319 
321 
322  int GetEnableBuild();
323  void SetEnableBuild(int enableBuild);
325 
327  int GetEnableResearch();
328  void SetEnableResearch(int enableResearch);
330 
332  int GetDoneResearch(int team);
333  void SetDoneResearch(int doneResearch, int team);
335 
337 
338  bool IsBuildingEnabled(BuildType type);
339  bool IsBuildingEnabled(ObjectType type);
341  bool IsResearchEnabled(ResearchType type);
344  bool IsResearchDone(ResearchType type, int team);
346  void MarkResearchDone(ResearchType type, int team);
347 
349 
350  bool CanBuild(ObjectType type, int team);
351  Error CanBuildError(ObjectType type, int team);
353 
355 
356  bool CanFactory(ObjectType type, int team);
357  Error CanFactoryError(ObjectType type, int team);
359 
360  void RemoveFromSelectionHistory(CObject* object);
361 
363  float GetGlobalMagnifyDamage();
364 
365  void StartDetectEffect(COldObject* object, CObject* target);
366 
367  bool IsSelectable(CObject* obj);
368 
369  void SetDebugCrashSpheres(bool draw);
370 
371  bool GetDebugCrashSpheres();
372 
373 protected:
374  bool EventFrame(const Event &event);
375  bool EventObject(const Event &event);
376  void InitEye();
377 
378  void ShowSaveIndicator(bool show);
379 
380  void CreateScene(bool soluce, bool fixScene, bool resetObject);
381  void ResetCreate();
382 
383  void LevelLoadingError(const std::string& error, const std::runtime_error& exception, Phase exitPhase = PHASE_LEVEL_LIST);
384 
385  int CreateLight(Math::Vector direction, Gfx::Color color);
386  void HiliteClear();
387  void HiliteObject(Math::Point pos);
388  void HiliteFrame(float rTime);
389  void CreateTooltip(Math::Point pos, const std::string& text);
390  void ClearTooltip();
391  CObject* DetectObject(Math::Point pos);
392  void ChangeCamera();
393  void AbortMovie();
394  void SelectOneObject(CObject* obj, bool displayError=true);
395  void HelpObject();
396  bool DeselectObject();
397  void DeleteAllObjects();
398  void UpdateInfoText();
399  void StartDisplayVisit(EventType event);
400  void FrameVisit(float rTime);
401  void StopDisplayVisit();
402  void ExecuteCmd(const std::string& cmd);
403  void UpdateSpeedLabel();
404 
405  void AutosaveRotate();
406  void Autosave();
407  bool DestroySelectedObject();
408  void PushToSelectionHistory(CObject* obj);
409  CObject* PopFromSelectionHistory();
410 
411  void CreateCodeBattleInterface();
412  void DestroyCodeBattleInterface();
413  void SetCodeBattleSpectatorMode(bool mode);
414  void UpdateDebugCrashSpheres();
415 
416 
417 protected:
418  CApplication* m_app = nullptr;
419  CEventQueue* m_eventQueue = nullptr;
420  Gfx::CEngine* m_engine = nullptr;
421  Gfx::CParticle* m_particle = nullptr;
422  Gfx::CWater* m_water = nullptr;
423  Gfx::CCloud* m_cloud = nullptr;
424  Gfx::CLightning* m_lightning = nullptr;
425  Gfx::CPlanet* m_planet = nullptr;
426  Gfx::COldModelManager* m_oldModelManager = nullptr;
427  Gfx::CLightManager* m_lightMan = nullptr;
428  CSoundInterface* m_sound = nullptr;
429  CInput* m_input = nullptr;
430  std::unique_ptr<CObjectManager> m_objMan;
431  std::unique_ptr<CMainMovie> m_movie;
432  std::unique_ptr<CPauseManager> m_pause;
433  std::unique_ptr<Gfx::CModelManager> m_modelManager;
434  std::unique_ptr<Gfx::CTerrain> m_terrain;
435  std::unique_ptr<Gfx::CCamera> m_camera;
436  std::unique_ptr<Ui::CMainUserInterface> m_ui;
437  std::unique_ptr<Ui::CMainShort> m_short;
438  std::unique_ptr<Ui::CMainMap> m_map;
439  std::unique_ptr<Ui::CInterface> m_interface;
440  std::unique_ptr<Ui::CDisplayInfo> m_displayInfo;
441  std::unique_ptr<Ui::CDisplayText> m_displayText;
442  std::unique_ptr<Ui::CDebugMenu> m_debugMenu;
443  std::unique_ptr<CSettings> m_settings;
444 
446  std::unique_ptr<CPlayerProfile> m_playerProfile;
447 
448 
450  float m_time = 0.0f;
452  float m_gameTime = 0.0f;
454  float m_gameTimeAbsolute = 0.0f;
455 
456  LevelCategory m_levelCategory;
457  int m_levelChap = 0;
458  int m_levelRank = 0;
460  std::string m_levelFile = "";
461  std::string m_sceneReadPath;
462 
463  float m_winDelay = 0.0f;
464  float m_lostDelay = 0.0f;
465  bool m_fixScene = false; // scene fixed, no interraction
466  CObject* m_base = nullptr; // OBJECT_BASE exists in mission
467  CObject* m_selectObject = nullptr;
468 
469  Phase m_phase = PHASE_WELCOME1;
470  ActivePause* m_userPause = nullptr;
471  ActivePause* m_focusPause = nullptr;
472  ActivePause* m_freePhotoPause = nullptr;
473  bool m_cmdEdit = false;
474  ActivePause* m_cmdEditPause = nullptr;
475  bool m_selectInsect = false;
476  bool m_showSoluce = false;
477  bool m_showAll = false;
478  bool m_cheatRadar = false;
479  bool m_shortCut = false;
480  std::string m_audioTrack;
481  bool m_audioRepeat = false;
482  std::string m_satcomTrack;
483  bool m_satcomRepeat = false;
484  std::string m_editorTrack;
485  bool m_editorRepeat = false;
486  int m_movieInfoIndex = 0;
487 
488  CObject* m_controller = nullptr;
489 
490  MissionType m_missionType = MISSION_NORMAL;
491  bool m_immediatSatCom = false; // SatCom immediately?
492  bool m_beginSatCom = false; // messages SatCom poster?
493  bool m_lockedSatCom = false; // SatCom locked?
494  bool m_movieLock = false; // movie in progress?
495  bool m_satComLock = false; // call of SatCom is possible?
496  bool m_editLock = false; // edition in progress?
497  bool m_editFull = false; // edition in full screen?
498  bool m_hilite = false;
499  bool m_trainerPilot = false; // remote trainer?
500  bool m_friendAim = false;
501  bool m_resetCreate = false;
502  bool m_mapShow = false;
503  bool m_mapImage = false;
504  char m_mapFilename[100] = {};
505 
506  ActivePause* m_suspend = nullptr;
507 
508  Math::Point m_tooltipPos;
509  std::string m_tooltipName;
510  float m_tooltipTime = 0.0f;
511 
512  char m_infoFilename[SATCOM_MAX][100] = {}; // names of text files
513  CObject* m_infoObject = nullptr;
514  int m_infoUsed = 0;
515  ActivePause* m_satcomMoviePause = nullptr;
516 
517  std::string m_scriptName = "";
518  std::string m_scriptFile = "";
519  std::string m_endingWin = "";
520  std::string m_endingLost = "";
521  bool m_winTerminate = false;
522 
523  float m_globalMagnifyDamage = 0.0f;
524 
525  bool m_exitAfterMission = false;
526 
527  bool m_codeBattleInit = false;
528  bool m_codeBattleStarted = false;
530  bool m_codeBattleSpectator = true;
531 
532  std::map<int, std::string> m_teamNames;
533 
534  std::vector<NewScriptName> m_newScriptName;
535 
536  EventType m_visitLast = EVENT_NULL;
537  CObject* m_visitObject = nullptr;
538  CObject* m_visitArrow = nullptr;
539  float m_visitTime = 0.0f;
540  float m_visitParticle = 0.0f;
541  Math::Vector m_visitPos;
542  Math::Vector m_visitPosArrow;
543  ActivePause* m_visitPause = nullptr;
544 
545  std::vector<std::unique_ptr<CSceneEndCondition>> m_endTake;
547  bool m_endTakeImmediat = false;
548  long m_endTakeResearch = 0;
549  float m_endTakeWinDelay = 0.0f;
550  float m_endTakeLostDelay = 0.0f;
551 
552  std::vector<std::unique_ptr<CAudioChangeCondition>> m_audioChange;
553 
554  std::map<std::string, MinMax> m_obligatoryTokens;
555 
557  int m_build = 0;
559  long m_researchEnable = 0;
561  std::map<int, int> m_researchDone;
562 
563  Error m_missionResult = ERR_OK;
565  bool m_missionResultFromScript = false;
566 
567  ShowLimit m_showLimit[MAXSHOWLIMIT];
568 
569  std::map<int, Gfx::Color> m_colorNewBot;
570  Gfx::Color m_colorNewAlien;
571  Gfx::Color m_colorNewGreen;
572  Gfx::Color m_colorNewWater;
573  float m_colorShiftWater = 0.0f;
574 
575  bool m_missionTimerEnabled = false;
576  bool m_missionTimerStarted = false;
577  float m_missionTimer = 0.0f;
578 
579  bool m_autosave = false;
580  int m_autosaveInterval = 0;
581  int m_autosaveSlots = 0;
582  float m_autosaveLast = 0.0f;
583 
584  int m_shotSaving = 0;
585 
586  std::deque<CObject*> m_selectionHistory;
587  bool m_debugCrashSpheres;
588 };
Definition: pausemanager.cpp:31
CSingleton base class for singletons.
ParticleType
Definition: particle.h:63
Definition: displaytext.h:51
Definition: robotmain.h:132
Definition: robotmain.h:108
Camera handling - CCamera class.
Audio change condition.
Definition: scene_conditions.h:97
Definition: singleton.h:30
Manager for static models.
Definition: oldmodelmanager.h:54
Definition: old_object.h:79
Global event queue.
Definition: event.h:866
Manager for dynamic lights in 3D scene.
Definition: lightman.h:146
PauseType
Definition: pausemanager.h:35
ObjectType enum.
Definition: parserline.h:37
Definition: robotmain.h:159
Cloud layer renderer.
Definition: cloud.h:54
Lightning effect renderer.
Definition: lightning.h:55
Particle engine.
Definition: particle.h:223
Definition: settings.h:32
Planet manager.
Definition: planet.h:49
CMainMovie - control over movie sequences.
Scene end condition.
Definition: scene_conditions.h:73
2D point
Definition: point.h:50
Main application.
Definition: app.h:180
Camera moving in 3D scene.
Definition: camera.h:128
Management of pause modes.
Terrain loader/generator and manager.
Definition: terrain.h:147
Namespace for (new) graphics code.
Definition: app.h:49
Definition: player_profile.h:58
The graphics engine.
Definition: engine.h:585
std::unique_ptr< CPlayerProfile > m_playerProfile
Progress of loaded player.
Definition: robotmain.h:446
Event types, structs and event queue.
ObjectType
Type of game object.
Definition: object_type.h:33
Water manager/renderer.
Definition: water.h:72
Definition: robotmain.h:144
Invalid event / no event.
Definition: event.h:47
Particle rendering - CParticle class (aka particle)
3D (3x1) vector
Definition: vector.h:53
RGBA color.
Definition: color.h:39
Manages CObject instances.
Definition: object_manager.h:148
EventType
Type of event message.
Definition: event.h:41
Event sent by system, interface or game.
Definition: event.h:735
Definition: pausemanager.h:71
Definition: interface.h:59
Base class for all 3D in-game objects.
Definition: object.h:63
Sound plugin interface.
Definition: sound.h:60
Definition: robotmain.h:119
std::map< int, int > m_researchDone
Done researches for each team.
Definition: robotmain.h:561
Management of mouse, keyboard and joystick.
Definition: input.h:71