1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2020, 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 /**
21  * \file script/script.h
22  * \brief CBot script runner
23  */
24 
25 #pragma once
26 
27 #include "CBot/CBot.h"
28 
29 #include <memory>
30 #include <limits>
31 #include <string>
32 #include <boost/optional.hpp>
33 
34 
35 class COldObject;
36 class CTaskExecutorObject;
37 class CRobotMain;
38 class CScriptFunctions;
39 
40 namespace Ui
41 {
42 class CEdit;
43 class CInterface;
44 class CList;
45 } /* Ui */
46 
47 namespace Gfx
48 {
49 class CEngine;
50 class CTerrain;
51 class CWater;
52 } /* Gfx */
53 
54 
55 const int ERM_CONT = 0;     // if error -> continue
56 const int ERM_STOP = 1;     // if error -> stop
57 
58 
59 class CScript
60 {
61 friend class CScriptFunctions;
62 public:
63     CScript(COldObject* object);
64     ~CScript();
65 
66     void        PutScript(Ui::CEdit* edit, const char* name);
67     bool        GetScript(Ui::CEdit* edit);
68     bool        GetCompile();
69 
70     const std::string& GetTitle();
71 
72     void        SetStepMode(bool bStep);
73     bool        GetStepMode();
74     bool        Run();
75     bool        Continue();
76     bool        Step();
77     void        Stop();
78     bool        IsRunning();
79     bool        IsContinue();
80     bool        GetCursor(int &cursor1, int &cursor2);
81     void        UpdateList(Ui::CList* list);
82     static void ColorizeScript(Ui::CEdit* edit, int rangeStart = 0, int rangeEnd = std::numeric_limits<int>::max());
83     bool        IntroduceVirus();
84 
85     int         GetError();
86     void        GetError(std::string& error);
87 
88     void        New(Ui::CEdit* edit, const char* name);
89     bool        SendScript(const char* text);
90     bool        ReadScript(const char* filename);
91     bool        WriteScript(const char* filename);
92     bool        ReadStack(std::istream &istr);
93     bool        WriteStack(std::ostream &ostr);
94     bool        Compare(CScript* other);
95 
96     void        SetFilename(const std::string &filename);
97     const std::string& GetFilename();
98 
99 protected:
100     bool        IsEmpty();
101     bool        CheckToken();
102     bool        Compile();
103 
104 protected:
105     COldObject*          m_object = nullptr;
106     CTaskExecutorObject* m_taskExecutor = nullptr;
107 
108     Gfx::CEngine*       m_engine = nullptr;
109     Ui::CInterface*     m_interface = nullptr;
110     std::unique_ptr<CBot::CBotProgram> m_botProg;
111     CRobotMain*         m_main = nullptr;
112     Gfx::CTerrain*      m_terrain = nullptr;
113     Gfx::CWater*        m_water = nullptr;
114 
115     int     m_ipf = 0;          // number of instructions/second
116     int     m_errMode = 0;      // what to do in case of error
117     int     m_len = 0;          // length of the script (without <0>)
118     std::unique_ptr<char[]> m_script;       // script ends with <0>
119     bool    m_bRun = false;         // program during execution?
120     bool    m_bStepMode = false;        // step by step
121     bool    m_bContinue = false;        // external function to continue
122     bool    m_bCompile = false;     // compilation ok?
123     std::string m_title = "";        // script title
124     std::string m_mainFunction = "";
125     std::string m_filename = "";     // file name
126     std::string m_token = "";        // missing instruction
127     int m_tokenUsed = 0, m_tokenAllowed = 0;
128     CBot::CBotError m_error = CBot::CBotNoErr;        // error (0=ok)
129     int     m_cursor1 = 0;
130     int     m_cursor2 = 0;
131     boost::optional<float> m_returnValue = boost::none;
132 };
133