Welcome to mirror list, hosted at ThFree Co, Russian Federation.

object.h « Objects « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f69b9028a012928dd85dfe4aa9734ba43670a86a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//-----------------------------------------------------------------------------
//           Name: object.h
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#pragma once

#include <Editors/entity_type.h>
#include <Editors/object_sanity_state.h>

#include <Math/overgrowth_geometry.h>
#include <Math/quaternions.h>

#include <Objects/object_msg.h>
#include <Graphics/palette.h>
#include <Scripting/scriptparams.h>
#include <Asset/Asset/material.h>

#include <list>
#include <cstdarg>
#include <string>
#include <map>
//-----------------------------------------------------------------------------
// Class Definition
//-----------------------------------------------------------------------------

class SceneGraph;
class Collision;
class Prefab;
struct LineSegment;
struct MaterialEvent;
struct MaterialDecal;
struct MaterialParticle;
class Material;
class BulletObject;
struct CollideInfo;
struct EntityDescription;
struct SavedChunk;
class TiXmlNode;

typedef int32_t ObjectID;

struct SeparatedTransform {
    vec3 translation;
    vec3 scale;
    quaternion rotation;
};

class Object {
   public:
    enum MoveType {
        kTranslate = (1 << 0),
        kRotate = (1 << 1),
        kScale = (1 << 2),
        kAll = kTranslate | kRotate | kScale
    };
    enum DrawType {
        kUnknown,
        kDrawDepthOnly,
        kDrawAllShadowCascades,
        kDrawDepthNoAA,
        kFullDraw,
        kWireframe,
        kDecal
    };
    enum ConnectionType {
        kCTNone,
        kCTMovementObjects,
        kCTItemObjects,
        kCTEnvObjectsAndGroups,
        kCTPathPoints,
        kCTPlaceholderObjects,
        kCTNavmeshConnections,
        kCTHotspots
    };

    // Transforms
    const vec3& GetTranslation() const { return translation_; }
    const vec3& GetScale() const { return scale_; }
    const quaternion& GetRotation() const { return rotation_; }
    const mat4& GetTransform() const { return transform_; }
    vec3 GetRotationEuler();

    virtual void SetTranslation(const vec3& trans);
    virtual void SetScale(const vec3& trans);
    virtual void SetRotation(const quaternion& trans);
    virtual void SetTranslationRotationFast(const vec3& trans, const quaternion& rotation);
    void SetRotationEuler(const vec3& trans);
    virtual void SetTransformationMatrix(const mat4& transform);
    virtual void GetDisplayName(char* buf, int buf_size);

    enum PermissionFlags {
        CAN_ROTATE = (1 << 0),
        CAN_TRANSLATE = (1 << 1),
        CAN_SCALE = (1 << 2),
        CAN_SELECT = (1 << 3),
        CAN_DELETE = (1 << 4),
        CAN_COPY = (1 << 5)
    };

    std::string name;
    int permission_flags;
    bool selectable_;
    bool enabled_;
    vec4 box_color;
    Box box_;
    bool editor_visible;
    SeparatedTransform start_transform;  // transform of object when editor starts to move it

    bool online_transform_dirty = false;
    bool interp_started = false;
    bool interp = false;
    bool overshot = false;
    bool behind = false;

    float walltime_last_update = 0.0f;

    std::string obj_file;  // generic xml data for my entity type
    std::string editor_label;
    vec3 editor_label_offset;
    float editor_label_scale;
    bool collidable;
    bool created_on_the_fly;
    bool transparent;
    bool exclude_from_undo;
    bool exclude_from_save;
    int update_list_entry;
    Object* parent;
    std::vector<int> unfinalized_connected_from;
    std::vector<int> unfinalized_connected_to;
    // These need to be here so copying an object correctly connects
    // it to and from other objects.
    // Currently they are only used when connecting to and from hotspots,
    // since hotspots should be able to connect to all objects, and all
    // objects should be able to connect to hotspots.
    std::vector<int> connected_from;  // Other objects connected to this object
    std::vector<int> connected_to;    // This object connected to other objects

    SceneGraph* scenegraph_;

    Object(SceneGraph* parent_scenegraph = 0)
        : permission_flags(CAN_ROTATE | CAN_TRANSLATE | CAN_SCALE | CAN_SELECT | CAN_COPY | CAN_DELETE),
          selected_(0),
          box_color(1.0f),
          editor_visible(true),
          collidable(false),
          transparent(false),
          exclude_from_undo(false),
          exclude_from_save(false),
          update_list_entry(-1),
          enabled_(true),
          parent(NULL),
          scenegraph_(parent_scenegraph),
          scale_(1.0f),
          rotation_(),
          translation_(0.0f),
          loaded_rotation_(),
          loaded_translation_(0.0f),
          loaded_scale_(0.0f),
          id(-1),
          editor_label(""),
          editor_label_offset(0.0f),
          editor_label_scale(10),
          selectable_(true),
          receive_depth(0),
          rotation_updated(true)

    {
        UpdateTransform();
    }

    virtual EntityType GetType() const = 0;
    virtual ~Object();
    virtual void SetParent(Object* new_parent);
    virtual bool HasParent();

    ObjectID GetID() const { return id; }
    void SetID(const int _id) {
        id = _id;
        sp.SetObjectID(_id);
    }
    std::string GetName() const { return name; }
    void SetName(const std::string& _name) { name = _name; }

    virtual void Collided(const vec3& pos, float impulse, const CollideInfo& collide_info, BulletObject* obj) {}

    virtual void SetEnabled(bool val) { enabled_ = val; }
    virtual void SetCollisionEnabled(bool val){};
    virtual void HandleTransformationOccurred();
    virtual void UpdateParentHierarchy(){};
    virtual void PropagateTransformsDown(bool deep) {}
    virtual void ChildMoved(Object::MoveType type) {}
    virtual const MaterialEvent& GetMaterialEvent(const std::string& the_event, const vec3& event_pos, int* tri = NULL);
    virtual const MaterialEvent& GetMaterialEvent(const std::string& the_event, const vec3& event_pos, const std::string& mod, int* tri = NULL);
    virtual const MaterialDecal& GetMaterialDecal(const std::string& type, const vec3& pos, int* tri = NULL);
    virtual const MaterialParticle& GetMaterialParticle(const std::string& type, const vec3& pos, int* tri = NULL);
    virtual MaterialRef GetMaterial(const vec3& pos, int* tri = NULL);
    virtual vec3 GetColorAtPoint(const vec3& pos, int* tri = NULL);
    virtual void HandleMaterialEvent(const std::string& the_event, const vec3& event_pos, int* tri = NULL) {}

    virtual void Update(float timestep) {}  // Is only called on certain objects that are registered as needing a frequent update.
    virtual void InfrequentUpdate();        // Is called on all objects, but there is no guarantee on how often.
                                      //  Interpolation boilerplate for all objects in multiplayer

    virtual bool Initialize() = 0;
    virtual void SetImposter(bool set) {}
    virtual void drawShadow(vec3 origin, float distance) {}
    virtual int lineCheck(const vec3& start, const vec3& end, vec3* point, vec3* normal = 0);
    virtual bool AcceptConnectionsFrom(ConnectionType type, Object& object) { return false; }
    virtual bool ConnectTo(Object& other, bool checking_other = false);
    virtual bool Disconnect(Object& other, bool from_socket = false, bool checking_other = false);
    virtual void ConnectedFrom(Object& object);
    virtual void DisconnectedFrom(Object& object);
    virtual void Dispose() {}
    virtual void SaveToXML(TiXmlElement* parent);
    virtual void GetDesc(EntityDescription& desc) const;
    virtual void NotifyDeleted(Object* o);
    void ReceiveObjectMessage(OBJECT_MSG::Type type, ...);
    void ReceiveScriptMessage(const std::string& msg);
    void QueueScriptMessage(const std::string& msg);
    virtual void ReceiveObjectMessageVAList(OBJECT_MSG::Type type, va_list args);
    virtual void GetChildren(std::vector<Object*>* children) {}
    virtual void GetBottomUpCompleteChildren(std::vector<Object*>* ret_children) {}
    virtual void GetTopDownCompleteChildren(std::vector<Object*>* ret_children) {}

    ScriptParams* GetScriptParams() { return &sp; }
    const ScriptParamMap& GetScriptParamMap();
    virtual void SetScriptParams(const ScriptParamMap& spm);
    virtual void ApplyPalette(const OGPalette& palette) {}
    virtual OGPalette* GetPalette();
    virtual void ReceiveASVec3Message(int type, const vec3& vec_a, const vec3& vec_b) {}
    virtual void SaveHistoryState(std::list<SavedChunk>& chunk_list, int state_id);
    virtual bool SetFromDesc(const EntityDescription& desc);
    virtual void UpdateScriptParams() {}
    virtual void ChildLost(Object* obj) {}
    virtual void DrawDepthMap(const mat4& proj_view_matrix, const vec4* cull_planes, int num_cull_planes, Object::DrawType draw_type) {}
    virtual void PreDrawFrame(float curr_game_time) {}
    virtual void PreDrawCamera(float curr_game_time) {}
    virtual bool IsGroupDerived() const { return false; }
    virtual bool LockedChildren() { return !selectable_; }

    virtual void GetConnectionIDs(std::vector<int>* cons) {}

    // Remap all attachment references etc to new id's, used for prefabs.
    virtual void RemapReferences(std::map<int, int> id_map){};

    virtual void DrawImGuiEditor();
    virtual int GetGroupDepth();

    virtual Prefab* GetPrefabParent();
    virtual bool IsInPrefab();

    virtual void Select(bool val);
    virtual unsigned int Selected();

    // If this is non-zero that indicates that there's something wrong with the object data state.
    virtual ObjectSanityState GetSanity();
    virtual void GetShaderNames(std::map<std::string, int>& shaders) {}

    virtual bool IsMultiplayerSupported() { return true; }

   protected:
    unsigned int selected_;
    ScriptParams sp;
    mat4 transform_;
    mat4 rotation_mat_;
    vec3 scale_;
    vec3 translation_;
    quaternion loaded_rotation_;
    vec3 loaded_translation_;
    vec3 loaded_scale_;
    void UpdateTransform();
    virtual void Moved(Object::MoveType type);
    // These are accessed via ReceiveObjectMessage()
    virtual void Draw() {}
    virtual void SaveShadow() {}
    virtual void LoadShadow() {}
    virtual void Reload() {}
    virtual void FinalizeLoadedConnections();
    virtual void Reset() {}
    virtual void ToggleImposter() {}
    int LineCheckEditorCube(const vec3& start, const vec3& end, vec3* point, vec3* normal);

   private:
    int receive_depth;
    ObjectID id;
    bool rotation_updated;
    quaternion rotation_;
    vec3 rotation_euler_;
};

inline std::ostream& operator<<(std::ostream& out, const Object& obj) {
    out << "Object(id:" << obj.GetID() << ",type:" << obj.GetType() << ",file:" << obj.obj_file << ")";
    return out;
}