changeteamevent.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/commando/changeteamevent.cpp $*
  25. * *
  26. * $Author:: Patrick $*
  27. * *
  28. * $Modtime:: 1/17/02 3:30p $*
  29. * *
  30. * $Revision:: 22 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "changeteamevent.h"
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include "networkobjectfactory.h"
  39. #include "cnetwork.h"
  40. #include "networkobjectmgr.h"
  41. #include "playertype.h"
  42. #include "playermanager.h"
  43. #include "gamemode.h"
  44. #include "gamedata.h"
  45. #include "gameobjmanager.h"
  46. #include "spawn.h"
  47. #include "apppackettypes.h"
  48. #include "sctextobj.h"
  49. #include "translatedb.h"
  50. #include "string_ids.h"
  51. #include "c4.h"
  52. #include "beacongameobj.h"
  53. #include "weaponview.h"
  54. DECLARE_NETWORKOBJECT_FACTORY(cChangeTeamEvent, NETCLASSID_CHANGETEAMEVENT);
  55. //-----------------------------------------------------------------------------
  56. cChangeTeamEvent::cChangeTeamEvent(void)
  57. {
  58. SenderId = 0;
  59. Set_App_Packet_Type(APPPACKETTYPE_CHANGETEAMEVENT);
  60. }
  61. //-----------------------------------------------------------------------------
  62. void
  63. cChangeTeamEvent::Init(void)
  64. {
  65. WWASSERT(cNetwork::I_Am_Client());
  66. SenderId = cNetwork::Get_My_Id();
  67. Set_Network_ID(NetworkObjectMgrClass::Get_New_Client_ID());
  68. if (cNetwork::I_Am_Server()) {
  69. Act();
  70. } else {
  71. Set_Object_Dirty_Bit(0, BIT_CREATION, true);
  72. }
  73. }
  74. //-----------------------------------------------------------------------------
  75. void
  76. cChangeTeamEvent::Act(void)
  77. {
  78. WWASSERT(cNetwork::I_Am_Server());
  79. cPlayer * p_player = cPlayerManager::Find_Player(SenderId);
  80. if (p_player != NULL &&
  81. The_Game() != NULL &&
  82. //The_Game()->Is_Team_Game() &&
  83. (The_Game()->IsTeamChangingAllowed.Is_True() || p_player->Invulnerable.Is_True()))
  84. {
  85. int team = p_player->Get_Player_Type();
  86. WWASSERT(team == PLAYERTYPE_NOD || team == PLAYERTYPE_GDI);
  87. int new_team = PLAYERTYPE_NOD;
  88. if (team == PLAYERTYPE_NOD)
  89. {
  90. new_team = PLAYERTYPE_GDI;
  91. }
  92. else
  93. {
  94. new_team = PLAYERTYPE_NOD;
  95. }
  96. p_player->Set_Player_Type(new_team);
  97. WWDEBUG_SAY(("Client %d changed team.\n", SenderId));
  98. //
  99. // Only reset the player's cash if they've changed teams after 60 seconds
  100. // have elapsed.
  101. //
  102. DWORD playing_time = (TIMEGETTIME() - p_player->Get_Join_Time ());
  103. if (playing_time > 30000) {
  104. p_player->Set_Score(0);
  105. p_player->Set_Money(0);
  106. }
  107. SoldierGameObj * p_soldier = GameObjManager::Find_Soldier_Of_Client_ID(SenderId);
  108. if (p_soldier != NULL)
  109. {
  110. if ( COMBAT_STAR == p_soldier ) {
  111. WeaponViewClass::Reset();
  112. }
  113. // Defuse all C4 belonging to this guy
  114. SLNode<BaseGameObj> *objnode;
  115. for ( objnode = GameObjManager::Get_Game_Obj_List()->Head(); objnode; objnode = objnode->Next()) {
  116. PhysicalGameObj *obj = objnode->Data()->As_PhysicalGameObj();
  117. if ( obj && obj->As_C4GameObj() ) {
  118. if ( obj->As_C4GameObj()->Get_Owner() == p_soldier ) {
  119. obj->As_C4GameObj()->Defuse();
  120. }
  121. }
  122. if ( obj && obj->As_BeaconGameObj() ) {
  123. if ( obj->As_BeaconGameObj()->Get_Owner() == p_soldier ) {
  124. // disarm C4
  125. OffenseObjectClass unused (0.0F, 0);
  126. obj->As_BeaconGameObj()->Completely_Damaged( unused );
  127. }
  128. }
  129. }
  130. //
  131. // We have to respawn him and possibly change his model...
  132. // let's just destroy the soldier and leave the rest up to God.
  133. //
  134. p_soldier->Set_Delete_Pending();
  135. }
  136. //
  137. // Tell everyone
  138. //
  139. WideStringClass text;
  140. //text.Format(L"_%s_changed_teams!_", p_player->Get_Name());
  141. text.Format(L"%s %s", p_player->Get_Name(), TRANSLATE(IDS_MP_CHANGED_TEAMS));
  142. cScTextObj * p_message = new cScTextObj;
  143. p_message->Init(text, TEXT_MESSAGE_PUBLIC, false, HOST_TEXT_SENDER, -1);
  144. }
  145. Set_Delete_Pending();
  146. }
  147. //-----------------------------------------------------------------------------
  148. void
  149. cChangeTeamEvent::Export_Creation(BitStreamClass & packet)
  150. {
  151. WWASSERT(cNetwork::I_Am_Only_Client());
  152. cNetEvent::Export_Creation(packet);
  153. WWASSERT(SenderId > 0);
  154. packet.Add(SenderId);
  155. Set_Delete_Pending();
  156. }
  157. //-----------------------------------------------------------------------------
  158. void
  159. cChangeTeamEvent::Import_Creation(BitStreamClass & packet)
  160. {
  161. WWASSERT(cNetwork::I_Am_Server());
  162. cNetEvent::Import_Creation(packet);
  163. packet.Get(SenderId);
  164. WWASSERT(SenderId > 0);
  165. Act();
  166. }
  167. //-----------------------------------------------------------------------------
  168. bool
  169. cChangeTeamEvent::Is_Change_Team_Possible(void)
  170. {
  171. return
  172. cNetwork::I_Am_Client() &&
  173. GameModeManager::Find("Combat") != NULL &&
  174. GameModeManager::Find("Combat")->Is_Active() &&
  175. The_Game() != NULL &&
  176. //The_Game()->Is_Team_Game() &&
  177. The_Game()->IsTeamChangingAllowed.Is_True();
  178. }