Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "ICmpPlayer.h" 21 : 22 : #include "graphics/Color.h" 23 : #include "maths/FixedVector3D.h" 24 : #include "simulation2/scripting/ScriptComponent.h" 25 : #include "simulation2/system/Component.h" 26 : #include "simulation2/system/InterfaceScripted.h" 27 : #include "simulation2/system/Message.h" 28 : 29 : BEGIN_INTERFACE_WRAPPER(Player) 30 137 : END_INTERFACE_WRAPPER(Player) 31 : 32 : class CCmpPlayerScripted : public ICmpPlayer 33 : { 34 : public: 35 11944 : DEFAULT_SCRIPT_WRAPPER_BASIC(PlayerScripted) 36 : 37 4 : static void ClassInit(CComponentManager& componentManager) 38 : { 39 4 : componentManager.SubscribeToMessageType(MT_PlayerWon); 40 4 : componentManager.SubscribeToMessageType(MT_PlayerDefeated); 41 4 : } 42 : 43 54 : void Serialize(ISerializer& serialize) final 44 : { 45 54 : serialize.Bool("isActive", m_IsActive); 46 54 : m_Script.Serialize(serialize); 47 54 : } 48 : 49 0 : void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) final 50 : { 51 0 : deserialize.Bool("isActive", m_IsActive); 52 0 : m_Script.Deserialize(GetSimContext().GetComponentManager(), paramNode, deserialize, GetEntityId()); 53 0 : } 54 : 55 1335 : void HandleMessage(const CMessage& msg, bool global) final 56 : { 57 1335 : const int msgType{msg.GetType()}; 58 : 59 : // Handle messages that were subscribed to in ClassInit. 60 1335 : if (msgType == MT_PlayerWon || msgType == MT_PlayerDefeated) 61 : { 62 0 : m_IsActive = false; 63 0 : if (!m_Script.HasMessageHandler(msg, global)) 64 : return; 65 : } 66 : 67 : // Handle messages that were subscribed to within the JS implementation of the interface. 68 1335 : m_Script.HandleMessage(msg, global); 69 : } 70 : 71 354 : CColor GetDisplayedColor() override 72 : { 73 354 : return m_Script.Call<CColor>("GetDisplayedColor"); 74 : } 75 : 76 0 : CFixedVector3D GetStartingCameraPos() override 77 : { 78 0 : return m_Script.Call<CFixedVector3D>("GetStartingCameraPos"); 79 : } 80 : 81 0 : CFixedVector3D GetStartingCameraRot() override 82 : { 83 0 : return m_Script.Call<CFixedVector3D>("GetStartingCameraRot"); 84 : } 85 : 86 0 : bool HasStartingCamera() override 87 : { 88 0 : return m_Script.Call<bool>("HasStartingCamera"); 89 : } 90 : 91 0 : bool HasWon() override 92 : { 93 0 : return m_Script.Call<bool>("HasWon"); 94 : } 95 : 96 0 : bool IsRemoved() override 97 : { 98 0 : return m_Script.Call<bool>("IsRemoved"); 99 : } 100 : 101 0 : bool IsActive() final 102 : { 103 0 : return m_IsActive; 104 : } 105 : 106 : private: 107 : // Serialize this player state variable in C++ so that mods can't manipulate this value in order to 108 : // reveal the map locally. 109 : // Once it's set to `false` it's never set to true again. To prevent mods from temporarily changing 110 : // it. 111 : bool m_IsActive{true}; 112 : }; 113 : 114 274 : REGISTER_COMPONENT_SCRIPT_WRAPPER(PlayerScripted)