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

attach_to_message.cpp « Message « Online « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fb2096d8a033869678c0d616d4a2ba15211e47e0 (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
//-----------------------------------------------------------------------------
//           Name: attach_to_message.cpp
//      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.
//
//-----------------------------------------------------------------------------
#include "attach_to_message.h"

#include <Main/engine.h>
#include <Online/online.h>
#include <Utility/binn_util.h>

namespace OnlineMessages {
    AttachToMessage::AttachToMessage(ObjectID parent_id, ObjectID child_id, uint32_t bone_id, bool attach, bool mirrored) :
        OnlineMessageBase(OnlineMessageCategory::LEVEL_PERSISTENT),
        bone_id(bone_id), attach(attach), mirrored(mirrored) {

        this->parent_id = Online::Instance()->GetOriginalID(parent_id);
        this->child_id = Online::Instance()->GetOriginalID(child_id);
    }

    binn* AttachToMessage::Serialize(void* object) {
        AttachToMessage* t = static_cast<AttachToMessage*>(object);
        binn* l = binn_object();

        binn_object_set_int32(l, "parent_id", t->parent_id);
        binn_object_set_int32(l, "child_id", t->child_id);
        binn_object_set_uint32(l, "bone_id", t->bone_id);
        binn_object_set_bool(l, "attach", t->attach);
        binn_object_set_bool(l, "mirrored", t->mirrored);

        return l;
    }

    void AttachToMessage::Deserialize(void* object, binn* l) {
        AttachToMessage* t = static_cast<AttachToMessage*>(object);

        binn_object_get_int32(l, "parent_id", &t->parent_id);
        binn_object_get_int32(l, "child_id", &t->child_id);
        binn_object_get_uint32(l, "bone_id", &t->bone_id);

        BOOL attach, mirrored;
        binn_object_get_bool(l, "attach", &attach);
        binn_object_get_bool(l, "mirrored", &mirrored);
        t->attach = attach;
        t->mirrored = mirrored;
    }

    void AttachToMessage::Execute(const OnlineMessageRef& ref, void* object, PeerID from) {
        AttachToMessage* t = static_cast<AttachToMessage*>(object);
        ObjectID parent_object_id = Online::Instance()->GetObjectID(t->parent_id);
        ObjectID child_object_id = Online::Instance()->GetObjectID(t->child_id);

        SceneGraph* graph = Engine::Instance()->GetSceneGraph();

        // We need to make sure that we actually know what objects
        // the sender is referencing
        Object * parent = graph->GetObjectFromID(parent_object_id);
        Object * child = graph->GetObjectFromID(child_object_id);

        if (parent != nullptr && child != nullptr) {
            if (t->attach) {
                child->SetParent(parent);
                if (child->GetType() == EntityType::_item_object) {
                    ItemObject* item_object = (ItemObject*)child;
                    MovementObject* movement_object = (MovementObject*)parent;
                    AttachmentSlotList attachment_slots;
                    movement_object->rigged_object()->AvailableItemSlots(item_object->item_ref(), &attachment_slots);
                    for (AttachmentSlotList::iterator iterator = attachment_slots.begin(); iterator != attachment_slots.end(); ++iterator) {
                        AttachmentSlot& slot = (*iterator);
                        if (slot.type == (AttachmentType)t->bone_id && slot.mirrored == t->mirrored) {
                            movement_object->AttachItemToSlotEditor(item_object->GetID(), slot.type, slot.mirrored, slot.attachment_ref, true);
                            break;
                        }
                    }

                } else if (parent->GetType() == EntityType::_movement_object) {
                    MovementObject * mov = (MovementObject *)parent;
                    RiggedObject * rigged = mov->rigged_object();
                    // this is copied and pasted from the "original code" -rewrite so that it does not default construct inside the vector
                    rigged->children.resize(rigged->children.size() + 1);

                    // dummy data
                    AttachedEnvObject &attached_env_object = rigged->children.back();
                    attached_env_object.bone_connection_dirty = true;
                    attached_env_object.direct_ptr = child;
                    attached_env_object.bone_connects[0].bone_id = t->bone_id;
                    attached_env_object.bone_connects[0].num_connections = 1;
                }
            } else {
                if (child->GetType() == EntityType::_item_object) {
                    parent->Disconnect(*child, true, false);
                } else {
                    parent->ChildLost(child);
                }
            }
        }
    }

    void* AttachToMessage::Construct(void *mem) {
        return new(mem) AttachToMessage(0, 0, 0, false, false);
    }

    void AttachToMessage::Destroy(void* object) {
        AttachToMessage* t = static_cast<AttachToMessage*>(object);
        t->~AttachToMessage();
    }
}