purchaserequestevent.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/purchaserequestevent.cpp $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 8/14/02 11:57a $*
  29. * *
  30. * $Revision:: 11 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "purchaserequestevent.h"
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include "networkobjectfactory.h"
  39. #include "cnetwork.h"
  40. #include "networkobjectmgr.h"
  41. #include "gameobjmanager.h"
  42. #include "vendor.h"
  43. #include "playertype.h"
  44. #include "purchaseresponseevent.h"
  45. #include "apppackettypes.h"
  46. #include "consolemode.h"
  47. DECLARE_NETWORKOBJECT_FACTORY(cPurchaseRequestEvent, NETCLASSID_PURCHASEREQUESTEVENT);
  48. //-----------------------------------------------------------------------------
  49. cPurchaseRequestEvent::cPurchaseRequestEvent(void)
  50. {
  51. SenderId = 0;
  52. PurchaseType = VendorClass::TYPE_SUPPLY;
  53. ItemIndex = 0;
  54. AltSkinIndex = -1;
  55. Set_App_Packet_Type(APPPACKETTYPE_PURCHASEREQUESTEVENT);
  56. }
  57. //-----------------------------------------------------------------------------
  58. void
  59. cPurchaseRequestEvent::Init(VendorClass::PURCHASE_TYPE type, int item_index, int alt_skin_index)
  60. {
  61. WWASSERT(cNetwork::I_Am_Client());
  62. SenderId = cNetwork::Get_My_Id();
  63. PurchaseType = type;
  64. ItemIndex = item_index;
  65. AltSkinIndex = alt_skin_index;
  66. Set_Network_ID(NetworkObjectMgrClass::Get_New_Client_ID());
  67. if (cNetwork::I_Am_Server()) {
  68. Act();
  69. } else {
  70. Set_Object_Dirty_Bit(0, BIT_CREATION, true);
  71. }
  72. }
  73. //-----------------------------------------------------------------------------
  74. void
  75. cPurchaseRequestEvent::Act(void)
  76. {
  77. WWASSERT(cNetwork::I_Am_Server());
  78. //
  79. // Lookup the data needed to make the purchase
  80. //
  81. SoldierGameObj *player = GameObjManager::Find_Soldier_Of_Client_ID(SenderId);
  82. VendorClass::PURCHASE_ERROR result = VendorClass::PERR_UNKNOWN;
  83. WWASSERT(The_Game() != NULL);
  84. if (The_Game()->Is_Gameplay_Permitted())
  85. {
  86. //
  87. // Attempt to make the purchase
  88. //
  89. result = VendorClass::Purchase_Item (player, (VendorClass::PURCHASE_TYPE)PurchaseType, ItemIndex, AltSkinIndex, false);
  90. }
  91. else
  92. {
  93. result = VendorClass::PERR_NO_FACTORY;
  94. }
  95. //
  96. // Show the server admin who bought the vehicle.
  97. //
  98. if (cNetwork::I_Am_Only_Server()) {
  99. if (result == VendorClass::PERR_SUCCESS && (VendorClass::PURCHASE_TYPE)PurchaseType == VendorClass::TYPE_VEHICLE) {
  100. cPlayer* player = cPlayerManager::Find_Player(SenderId);
  101. StringClass short_name(true);
  102. player->Get_Name().Convert_To(short_name);
  103. ConsoleBox.Print_Maybe("%s purchased a vehicle\n", short_name.Peek_Buffer());
  104. }
  105. }
  106. //
  107. // Now send the response to the client
  108. //
  109. cPurchaseResponseEvent * p_event = new cPurchaseResponseEvent;
  110. p_event->Init((int) result, SenderId);
  111. Set_Delete_Pending();
  112. }
  113. //-----------------------------------------------------------------------------
  114. void
  115. cPurchaseRequestEvent::Export_Creation(BitStreamClass & packet)
  116. {
  117. WWASSERT(cNetwork::I_Am_Client());
  118. cNetEvent::Export_Creation(packet);
  119. WWASSERT(SenderId > 0);
  120. packet.Add(SenderId);
  121. packet.Add(PurchaseType);
  122. packet.Add(ItemIndex);
  123. packet.Add(AltSkinIndex);
  124. Set_Delete_Pending();
  125. }
  126. //-----------------------------------------------------------------------------
  127. void
  128. cPurchaseRequestEvent::Import_Creation(BitStreamClass & packet)
  129. {
  130. cNetEvent::Import_Creation(packet);
  131. WWASSERT(cNetwork::I_Am_Server());
  132. packet.Get(SenderId);
  133. packet.Get(PurchaseType);
  134. packet.Get(ItemIndex);
  135. packet.Get(AltSkinIndex);
  136. WWASSERT(SenderId > 0);
  137. Act();
  138. }