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 #pragma once
21 
22 #include "object/interface/programmable_object.h"
23 
24 #include "math/vector.h"
25 
26 #include "object/interface/interactive_object.h"
27 #include "object/interface/trace_drawing_object.h"
28 
29 #include <sstream>
30 
31 class CObject;
32 
33 enum TraceOper
34 {
35     TO_STOP         = 0,    // stop
36     TO_ADVANCE      = 1,    // advance
37     TO_RECEDE       = 2,    // back
38     TO_TURN         = 3,    // rotate
39     TO_PEN          = 4,    // color change
40 };
41 
42 struct TraceRecord
43 {
44     TraceOper   oper = TO_STOP;
45     float       param = 0.0f;
46 };
47 
48 class CProgrammableObjectImpl : public CProgrammableObject
49 {
50 public:
51     explicit CProgrammableObjectImpl(ObjectInterfaceTypes& types, CObject* object);
52     virtual ~CProgrammableObjectImpl();
53 
54     bool EventProcess(const Event& event);
55 
56     bool IsProgram() override;
57     void RunProgram(Program* program) override;
58     Program* GetCurrentProgram() override;
59     void StopProgram() override;
60 
61     bool ReadStack(std::istream &istr) override;
62     bool WriteStack(std::ostream &ostr) override;
63 
64     void TraceRecordStart() override;
65     void TraceRecordStop() override;
66     bool IsTraceRecord() override;
67 
68     void SetActivity(bool activity) override;
69     bool GetActivity() override;
70 
71     void SetCmdLine(unsigned int rank, float value);
72     float GetCmdLine(unsigned int rank) override;
73     std::vector<float>& GetCmdLine();
74 
75 private:
76     //! Save current status to recording buffer
77     void        TraceRecordFrame();
78     //! Save this operation to recording buffer
79     bool        TraceRecordOper(TraceOper oper, float param);
80     //! Convert this recording operation to CBot instruction
81     bool        TraceRecordPut(std::stringstream& buffer, TraceOper oper, float param);
82 
83 private:
84     CObject* m_object;
85 
86 private:
87     bool                m_activity;
88 
89     std::vector<float>  m_cmdLine;
90 
91     Program*            m_currentProgram;
92 
93     bool                m_traceRecord;
94     TraceOper           m_traceOper;
95     Math::Vector        m_tracePos;
96     float               m_traceAngle;
97     TraceColor          m_traceColor;
98     int                 m_traceRecordIndex;
99     std::unique_ptr<TraceRecord[]> m_traceRecordBuffer;
100 };
101