2025-06-03 23:10:15 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2010-01-22 12:03:14 -08:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2010-01-22 12:03:14 -08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2010-01-22 12:03:14 -08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-22 12:03:14 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "ICmpOwnership.h"
|
|
|
|
|
|
|
|
|
|
#include "simulation2/MessageTypes.h"
|
2025-08-02 12:24:35 -07:00
|
|
|
#include "simulation2/helpers/Player.h"
|
|
|
|
|
#include "simulation2/system/Component.h"
|
|
|
|
|
#include "simulation2/system/Message.h"
|
|
|
|
|
|
|
|
|
|
#include <string>
|
2010-01-22 12:03:14 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Basic ICmpOwnership implementation.
|
|
|
|
|
*/
|
2022-03-03 14:42:26 -08:00
|
|
|
class CCmpOwnership final : public ICmpOwnership
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static void ClassInit(CComponentManager& componentManager)
|
|
|
|
|
{
|
|
|
|
|
componentManager.SubscribeToMessageType(MT_Destroy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DEFAULT_COMPONENT_ALLOCATOR(Ownership)
|
|
|
|
|
|
2010-09-05 02:38:30 -07:00
|
|
|
player_id_t m_Owner;
|
2010-01-22 12:03:14 -08:00
|
|
|
|
2010-04-23 09:09:03 -07:00
|
|
|
static std::string GetSchema()
|
|
|
|
|
{
|
|
|
|
|
return
|
|
|
|
|
"<a:example/>"
|
|
|
|
|
"<a:help>Allows this entity to be owned by players.</a:help>"
|
|
|
|
|
"<empty/>";
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-03 23:10:15 -07:00
|
|
|
void Init(const CParamNode&) override
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
2010-09-05 02:38:30 -07:00
|
|
|
m_Owner = INVALID_PLAYER;
|
2010-01-22 12:03:14 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Deinit() override
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Serialize(ISerializer& serialize) override
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
|
|
|
|
serialize.NumberI32_Unbounded("owner", m_Owner);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-03 23:10:15 -07:00
|
|
|
void Deserialize(const CParamNode&, IDeserializer& deserialize) override
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
2010-09-17 10:53:26 -07:00
|
|
|
deserialize.NumberI32_Unbounded("owner", m_Owner);
|
2010-01-22 12:03:14 -08:00
|
|
|
}
|
|
|
|
|
|
2025-06-03 05:13:41 -07:00
|
|
|
void HandleMessage(const CMessage& msg, bool /*global*/) override
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
|
|
|
|
switch (msg.GetType())
|
|
|
|
|
{
|
|
|
|
|
case MT_Destroy:
|
|
|
|
|
{
|
|
|
|
|
// Reset the owner so this entity is e.g. removed from population counts
|
2010-09-05 02:38:30 -07:00
|
|
|
SetOwner(INVALID_PLAYER);
|
2010-01-22 12:03:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
player_id_t GetOwner() const override
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
|
|
|
|
return m_Owner;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void SetOwner(player_id_t playerID) override
|
2010-01-22 12:03:14 -08:00
|
|
|
{
|
|
|
|
|
if (playerID == m_Owner)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-09-05 02:38:30 -07:00
|
|
|
player_id_t old = m_Owner;
|
2010-01-22 12:03:14 -08:00
|
|
|
m_Owner = playerID;
|
|
|
|
|
|
|
|
|
|
CMessageOwnershipChanged msg(GetEntityId(), old, playerID);
|
2010-05-01 02:48:39 -07:00
|
|
|
GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg);
|
2010-01-22 12:03:14 -08:00
|
|
|
}
|
2012-08-07 22:18:20 -07:00
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void SetOwnerQuiet(player_id_t playerID) override
|
2012-08-07 22:18:20 -07:00
|
|
|
{
|
|
|
|
|
if (playerID != m_Owner)
|
|
|
|
|
m_Owner = playerID;
|
|
|
|
|
}
|
2010-01-22 12:03:14 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
REGISTER_COMPONENT_TYPE(Ownership)
|