GroupControl.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. *
  20. * FILE
  21. * GroupControl.cpp
  22. *
  23. * DESCRIPTION
  24. * Group controller
  25. *
  26. * PROGRAMMER
  27. * Denzil E. Long, Jr.
  28. *
  29. * VERSION INFO
  30. * $Author: Pat_p $
  31. * $Revision: 2 $
  32. * $Modtime: 4/26/00 10:31a $
  33. * $Archive: /Commando/Code/Scripts/GroupControl.cpp $
  34. *
  35. ******************************************************************************/
  36. #include "groupcontrol.h"
  37. #include "group.h"
  38. #include "scripts.h"
  39. #include <assert.h>
  40. GroupController _GroupController;
  41. GroupController* GroupController::_mInstance = NULL;
  42. /******************************************************************************
  43. *
  44. * NAME
  45. * GroupController::Instance
  46. *
  47. * DESCRIPTION
  48. * Retrieve GroupController instance.
  49. *
  50. * INPUTS
  51. * NONE
  52. *
  53. * RESULTS
  54. * GroupController - GroupController instance pointer.
  55. *
  56. ******************************************************************************/
  57. GroupController* GroupController::Instance(void)
  58. {
  59. assert(_mInstance != NULL);
  60. return _mInstance;
  61. }
  62. /******************************************************************************
  63. *
  64. * NAME
  65. * GroupController::GroupController
  66. *
  67. * DESCRIPTION
  68. * Construct GroupController
  69. *
  70. * INPUTS
  71. * NONE
  72. *
  73. * RESULTS
  74. * NONE
  75. *
  76. ******************************************************************************/
  77. GroupController::GroupController()
  78. {
  79. _mInstance = this;
  80. }
  81. /******************************************************************************
  82. *
  83. * NAME
  84. * GroupController::~GroupController
  85. *
  86. * DESCRIPTION
  87. * Destruct GroupController
  88. *
  89. * INPUTS
  90. * NONE
  91. *
  92. * RESULTS
  93. * NONE
  94. *
  95. ******************************************************************************/
  96. GroupController::~GroupController()
  97. {
  98. mGroups.Remove_All();
  99. _mInstance = NULL;
  100. }
  101. /******************************************************************************
  102. *
  103. * NAME
  104. * GroupController::AddToGroup
  105. *
  106. * DESCRIPTION
  107. * Add a game object to the specified Group.
  108. *
  109. * INPUTS
  110. * GroupName - Name of the team to object to.
  111. * GameObject - Object to add.
  112. *
  113. * RESULTS
  114. * Success - Success / Failure condition
  115. *
  116. ******************************************************************************/
  117. bool GroupController::AddToGroup(const char* groupName, GameObject* object)
  118. {
  119. assert(groupName != NULL);
  120. assert(object != NULL);
  121. if ((groupName != NULL) && (object != NULL))
  122. {
  123. // Find the team to add the GameObject to.
  124. Group* group = FindOrCreateGroup(groupName);
  125. if (group != NULL)
  126. {
  127. group->AddMember(object);
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. /******************************************************************************
  134. *
  135. * NAME
  136. * GroupController::RemoveFromGroup
  137. *
  138. * DESCRIPTION
  139. * Remove a game object from the specified Group.
  140. *
  141. * INPUTS
  142. * GroupName - Name of group to remove object from.
  143. * GameObject - Object to remove.
  144. *
  145. * RESULTS
  146. * NONE
  147. *
  148. ******************************************************************************/
  149. void GroupController::RemoveFromGroup(const char* groupName, GameObject* object)
  150. {
  151. assert(groupName != NULL);
  152. assert(object != NULL);
  153. Group* group = FindGroup(groupName);
  154. if (group != NULL)
  155. {
  156. group->RemoveMember(object);
  157. }
  158. }
  159. /******************************************************************************
  160. *
  161. * NAME
  162. * GroupController::FindGroup
  163. *
  164. * DESCRIPTION
  165. * Find a group by name,
  166. *
  167. * INPUTS
  168. * GroupName - Name of group to find.
  169. *
  170. * RESULTS
  171. * Group - Pointer to Group; NULL if not found.
  172. *
  173. ******************************************************************************/
  174. Group* GroupController::FindGroup(const char* groupName)
  175. {
  176. SLNode<Group>* node = mGroups.Head();
  177. // Go through all the Groups
  178. while (node != NULL)
  179. {
  180. Group* group = node->Data();
  181. assert(group != NULL);
  182. // If there is a group with a matching name then return with that group.
  183. const char* name = group->GetName();
  184. if (stricmp(groupName, name) == 0)
  185. {
  186. return group;
  187. }
  188. node = node->Next();
  189. }
  190. return NULL;
  191. }
  192. /******************************************************************************
  193. *
  194. * NAME
  195. * GroupController::FindOrCreateGroup
  196. *
  197. * DESCRIPTION
  198. * Create a new group with the specified name. If the group already exists
  199. * then the existing group will be returned.
  200. *
  201. * INPUTS
  202. * GroupName - Name of group to find or create.
  203. *
  204. * RESULTS
  205. * Group - Pointer to Group; NULL if failure to create group.
  206. *
  207. ******************************************************************************/
  208. Group* GroupController::FindOrCreateGroup(const char* groupName)
  209. {
  210. Group* group = FindGroup(groupName);
  211. // Create the group only if it doesn't already exist.
  212. if (group == NULL)
  213. {
  214. Group* group = new Group(groupName);
  215. assert(group != NULL);
  216. if (group != NULL)
  217. {
  218. mGroups.Add_Tail(group);
  219. }
  220. }
  221. return group;
  222. }
  223. /******************************************************************************
  224. *
  225. * NAME
  226. * GroupController::SendGroupCustomEvent
  227. *
  228. * DESCRIPTION
  229. * Send a Group a custom event.
  230. *
  231. * INPUTS
  232. * GroupName - Name of group to send event to.
  233. * GameObject - Object event is from.
  234. * Event - Event to send to Group.
  235. * Data - Event specific data parameter.
  236. *
  237. * RESULTS
  238. * NONE
  239. *
  240. ******************************************************************************/
  241. void GroupController::SendGroupCustomEvent(const char* groupName, GameObject* from,
  242. int event, int data)
  243. {
  244. Group* group = FindGroup(groupName);
  245. if (group != NULL)
  246. {
  247. group->SendCustomEvent(from, event, data);
  248. }
  249. }