Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
script.h
Go to the documentation of this file.
1 // * This file is part of the COLOBOT source code
2 // * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
3 // *
4 // * This program is free software: you can redistribute it and/or modify
5 // * it under the terms of the GNU General Public License as published by
6 // * the Free Software Foundation, either version 3 of the License, or
7 // * (at your option) any later version.
8 // *
9 // * This program is distributed in the hope that it will be useful,
10 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // * GNU General Public License for more details.
13 // *
14 // * You should have received a copy of the GNU General Public License
15 // * along with this program. If not, see http://www.gnu.org/licenses/.
16 
22 #pragma once
23 
24 
25 #include "common/event.h"
26 
27 #include "common/global.h"
28 
29 #include "app/pausemanager.h"
30 
31 #include "CBot/CBotDll.h"
32 
33 #include <stdio.h>
34 
35 
36 class CObject;
37 class CTaskManager;
38 class CRobotMain;
39 
40 namespace Ui{
41 class CEdit;
42 class CInterface;
43 class CList;
44 } /* Ui */
45 
46 namespace Gfx {
47 class CEngine;
48 class CTerrain;
49 class CWater;
50 } /* Gfx */
51 
52 
53 
54 class CScript
55 {
56 public:
57  CScript(CObject* object, CTaskManager** secondaryTask);
58  ~CScript();
59 
60  static void InitFonctions();
61 
62  void PutScript(Ui::CEdit* edit, const char* name);
63  bool GetScript(Ui::CEdit* edit);
64  bool GetCompile();
65 
66  void GetTitle(char* buffer);
67 
68  void SetStepMode(bool bStep);
69  bool Run();
70  bool Continue(const Event &event);
71  bool Step(const Event &event);
72  void Stop();
73  bool IsRunning();
74  bool IsContinue();
75  bool GetCursor(int &cursor1, int &cursor2);
76  void UpdateList(Ui::CList* list);
77  void ColorizeScript(Ui::CEdit* edit);
78  bool IntroduceVirus();
79 
80  int GetError();
81  void GetError(std::string& error);
82 
83  void New(Ui::CEdit* edit, const char* name);
84  bool SendScript(const char* text);
85  bool ReadScript(const char* filename);
86  bool WriteScript(const char* filename);
87  bool ReadStack(FILE *file);
88  bool WriteStack(FILE *file);
89  bool Compare(CScript* other);
90 
91  void SetFilename(char *filename);
92  char* GetFilename();
93 
94 protected:
95  bool IsEmpty();
96  bool CheckToken();
97  bool Compile();
98 
99 private:
100  static CBotTypResult cNull(CBotVar* &var, void* user);
101  static CBotTypResult cOneFloat(CBotVar* &var, void* user);
102  static CBotTypResult cTwoFloat(CBotVar* &var, void* user);
103  static CBotTypResult cString(CBotVar* &var, void* user);
104  static CBotTypResult cStringString(CBotVar* &var, void* user);
105  static CBotTypResult cEndMission(CBotVar* &var, void* user);
106  static CBotTypResult cPlayMusic(CBotVar* &var, void* user);
107  static CBotTypResult cGetObject(CBotVar* &var, void* user);
108  static CBotTypResult cDelete(CBotVar* &var, void* user);
109  static CBotTypResult cSearch(CBotVar* &var, void* user);
110  static CBotTypResult cRadar(CBotVar* &var, void* user);
111  static CBotTypResult cDetect(CBotVar* &var, void* user);
112  static CBotTypResult cDirection(CBotVar* &var, void* user);
113  static CBotTypResult cCanBuild(CBotVar* &var, void* user);
114  static CBotTypResult cProduce(CBotVar* &var, void* user);
115  static CBotTypResult cDistance(CBotVar* &var, void* user);
116  static CBotTypResult cSpace(CBotVar* &var, void* user);
117  static CBotTypResult cFlatGround(CBotVar* &var, void* user);
118  static CBotTypResult cGoto(CBotVar* &var, void* user);
119  static CBotTypResult cGrabDrop(CBotVar* &var, void* user);
120  static CBotTypResult cReceive(CBotVar* &var, void* user);
121  static CBotTypResult cSend(CBotVar* &var, void* user);
122  static CBotTypResult cDeleteInfo(CBotVar* &var, void* user);
123  static CBotTypResult cTestInfo(CBotVar* &var, void* user);
124  static CBotTypResult cShield(CBotVar* &var, void* user);
125  static CBotTypResult cFire(CBotVar* &var, void* user);
126  static CBotTypResult cAim(CBotVar* &var, void* user);
127  static CBotTypResult cMotor(CBotVar* &var, void* user);
128  static CBotTypResult cTopo(CBotVar* &var, void* user);
129  static CBotTypResult cMessage(CBotVar* &var, void* user);
130  static CBotTypResult cPenDown(CBotVar* &var, void* user);
131  static CBotTypResult cOnePoint(CBotVar* &var, void* user);
132  static CBotTypResult cPoint(CBotVar* &var, void* user);
133  static CBotTypResult cOneObject(CBotVar* &var, void* user);
134 
135 
136  static bool rSin(CBotVar* var, CBotVar* result, int& exception, void* user);
137  static bool rCos(CBotVar* var, CBotVar* result, int& exception, void* user);
138  static bool rTan(CBotVar* var, CBotVar* result, int& exception, void* user);
139  static bool rSqrt(CBotVar* var, CBotVar* result, int& exception, void* user);
140  static bool rPow(CBotVar* var, CBotVar* result, int& exception, void* user);
141  static bool rRand(CBotVar* var, CBotVar* result, int& exception, void* user);
142  static bool rAbs(CBotVar* var, CBotVar* result, int& exception, void* user);
143  static bool rEndMission(CBotVar* var, CBotVar* result, int& exception, void* user);
144  static bool rPlayMusic(CBotVar* var, CBotVar* result, int& exception, void* user);
145  static bool rStopMusic(CBotVar* var, CBotVar* result, int& exception, void* user);
146  static bool rGetBuild(CBotVar* var, CBotVar* result, int& exception, void* user);
147  static bool rGetResearchEnable(CBotVar* var, CBotVar* result, int& exception, void* user);
148  static bool rGetResearchDone(CBotVar* var, CBotVar* result, int& exception, void* user);
149  static bool rSetBuild(CBotVar* var, CBotVar* result, int& exception, void* user);
150  static bool rSetResearchEnable(CBotVar* var, CBotVar* result, int& exception, void* user);
151  static bool rSetResearchDone(CBotVar* var, CBotVar* result, int& exception, void* user);
152  static bool rGetObjectById(CBotVar* var, CBotVar* result, int& exception, void* user);
153  static bool rGetObject(CBotVar* var, CBotVar* result, int& exception, void* user);
154  static bool rProgFunc(CBotVar* var, CBotVar* result, int& exception, void* user);
155  static bool rDelete(CBotVar* var, CBotVar* result, int& exception, void* user);
156  static bool rSearch(CBotVar* var, CBotVar* result, int& exception, void* user);
157  static bool rRadar(CBotVar* var, CBotVar* result, int& exception, void* user);
158  static bool rDetect(CBotVar* var, CBotVar* result, int& exception, void* user);
159  static bool rDirection(CBotVar* var, CBotVar* result, int& exception, void* user);
160  static bool rCanBuild(CBotVar* var, CBotVar* result, int& exception, void* user);
161  static bool rBuild(CBotVar* var, CBotVar* result, int& exception, void* user);
162  static bool rProduce(CBotVar* var, CBotVar* result, int& exception, void* user);
163  static bool rDistance(CBotVar* var, CBotVar* result, int& exception, void* user);
164  static bool rDistance2d(CBotVar* var, CBotVar* result, int& exception, void* user);
165  static bool rSpace(CBotVar* var, CBotVar* result, int& exception, void* user);
166  static bool rFlatGround(CBotVar* var, CBotVar* result, int& exception, void* user);
167  static bool rWait(CBotVar* var, CBotVar* result, int& exception, void* user);
168  static bool rMove(CBotVar* var, CBotVar* result, int& exception, void* user);
169  static bool rTurn(CBotVar* var, CBotVar* result, int& exception, void* user);
170  static bool rGoto(CBotVar* var, CBotVar* result, int& exception, void* user);
171  static bool rFind(CBotVar* var, CBotVar* result, int& exception, void* user);
172  static bool rGrab(CBotVar* var, CBotVar* result, int& exception, void* user);
173  static bool rDrop(CBotVar* var, CBotVar* result, int& exception, void* user);
174  static bool rSniff(CBotVar* var, CBotVar* result, int& exception, void* user);
175  static bool rReceive(CBotVar* var, CBotVar* result, int& exception, void* user);
176  static bool rSend(CBotVar* var, CBotVar* result, int& exception, void* user);
177  static bool rDeleteInfo(CBotVar* var, CBotVar* result, int& exception, void* user);
178  static bool rTestInfo(CBotVar* var, CBotVar* result, int& exception, void* user);
179  static bool rThump(CBotVar* var, CBotVar* result, int& exception, void* user);
180  static bool rRecycle(CBotVar* var, CBotVar* result, int& exception, void* user);
181  static bool rShield(CBotVar* var, CBotVar* result, int& exception, void* user);
182  static bool rFire(CBotVar* var, CBotVar* result, int& exception, void* user);
183  static bool rAim(CBotVar* var, CBotVar* result, int& exception, void* user);
184  static bool rMotor(CBotVar* var, CBotVar* result, int& exception, void* user);
185  static bool rJet(CBotVar* var, CBotVar* result, int& exception, void* user);
186  static bool rTopo(CBotVar* var, CBotVar* result, int& exception, void* user);
187  static bool rMessage(CBotVar* var, CBotVar* result, int& exception, void* user);
188  static bool rCmdline(CBotVar* var, CBotVar* result, int& exception, void* user);
189  static bool rIsMovie(CBotVar* var, CBotVar* result, int& exception, void* user);
190  static bool rErrMode(CBotVar* var, CBotVar* result, int& exception, void* user);
191  static bool rIPF(CBotVar* var, CBotVar* result, int& exception, void* user);
192  static bool rAbsTime(CBotVar* var, CBotVar* result, int& exception, void* user);
193  static bool rDeleteFile(CBotVar* var, CBotVar* result, int& exception, void* user);
194  static bool rPenDown(CBotVar* var, CBotVar* result, int& exception, void* user);
195  static bool rPenUp(CBotVar* var, CBotVar* result, int& exception, void* user);
196  static bool rPenColor(CBotVar* var, CBotVar* result, int& exception, void* user);
197  static bool rPenWidth(CBotVar* var, CBotVar* result, int& exception, void* user);
198  static bool rCameraFocus(CBotVar* var, CBotVar* result, int& exception, void* user);
199 
200 public:
201  static CBotTypResult cBusy(CBotVar* thisclass, CBotVar* &var);
202  static CBotTypResult cFactory(CBotVar* thisclass, CBotVar* &var);
203  static CBotTypResult cClassNull(CBotVar* thisclass, CBotVar* &var);
204  static CBotTypResult cClassOneFloat(CBotVar* thisclass, CBotVar* &var);
205 
206  static bool rBusy(CBotVar* thisclass, CBotVar* var, CBotVar* result, int& exception);
207  static bool rFactory(CBotVar* thisclass, CBotVar* var, CBotVar* result, int& exception);
208  static bool rResearch(CBotVar* thisclass, CBotVar* var, CBotVar* result, int& exception);
209  static bool rTakeOff(CBotVar* thisclass, CBotVar* var, CBotVar* result, int& exception);
210  static bool rDestroy(CBotVar* thisclass, CBotVar* var, CBotVar* result, int& exception);
211 
212 private:
213  static bool Process(CScript* script, CBotVar* result, int &exception);
214  static bool ShouldProcessStop(Error err, int errMode);
215  static CObject* SearchInfo(CScript* script, CObject* object, float power);
216 
217 protected:
218  Gfx::CEngine* m_engine;
219  Ui::CInterface* m_interface;
220  CBotProgram* m_botProg;
221  CRobotMain* m_main;
222  Gfx::CTerrain* m_terrain;
223  Gfx::CWater* m_water;
224  CTaskManager* m_primaryTask;
225  CTaskManager** m_secondaryTask;
226  CObject* m_object;
227  CPauseManager* m_pause;
228 
229  int m_ipf; // number of instructions/second
230  int m_errMode; // what to do in case of error
231  int m_len; // length of the script (without <0>)
232  char* m_script; // script ends with <0>
233  bool m_bRun; // program during execution?
234  bool m_bStepMode; // step by step
235  bool m_bContinue; // external function to continue
236  bool m_bCompile; // compilation ok?
237  char m_title[50]; // script title
238  char m_filename[50]; // file name
239  char m_token[50]; // missing instruction
240  int m_error; // error (0=ok)
241  int m_cursor1;
242  int m_cursor2;
243  Event m_event;
244  float m_returnValue;
245 };
246