Current time: 07-30-2010, 06:31 PM Hello There, Guest! (LoginRegister)






Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Utility]-[cJASS] Player Manager
03-11-2010, 04:46 PM (This post was last modified: 05-27-2010 03:46 AM by aeramus.)
Post: #1
[Utility]-[cJASS] Player Manager
Host Tracking
    JASS Programming
  1. include "cj_types.j"
  2.  
  3. library PlayerManager initializer Ini {
  4. /*===================================================================
  5. Name: C Player Manager
  6. Version: 3.4
  7. Author: Nestharus
  8.  
  9. Settings:
  10. *///===================================================================
  11. //remove all units owned by players not in map at ini
  12. define REMOVE_UNITS_ON_INI_FOR_INACTIVE = false
  13.  
  14. //remove all units owned by player when that player leaves, done after event fire
  15. define REMOVE_UNITS_ON_LEAVE = false
  16.  
  17. define TRACK_HUMAN = true
  18. define TRACK_COMPUTER = false
  19. define TRACK_ACTIVE = false
  20. define TRACK_INACTIVE = false
  21.  
  22. define KICK_PLAYER_ON_REMOVE = true
  23.  
  24. //fire events when a human leaves/removed
  25. define HUMAN_OUT_EVENT = true
  26.  
  27. //fire events when a computer is removed
  28. define COMPUTER_OUT_EVENT = false
  29.  
  30. //for use with GetHiddenPlayer
  31. //player 14 is suggested as it has bounty etc enabled
  32. define private HIDDEN_PLAYER_ID = 14
  33. /*//===================================================================
  34.   Player Manager keeps track of players in linked lists for easier traversal, faster loops, and less operations.
  35.   It has 5 collections of players-
  36.   HumanPlayer
  37.   Has all active human players in the game in order
  38.  
  39.   ComputerPlayer
  40.   Has all active computer players in the game in order
  41.  
  42.   ActivePlayer
  43.   Has all active players in the game in order
  44.  
  45.   InactivePlayer
  46.   Has all inactive players in the game in order
  47.  
  48.   Players
  49.   Has all players in the game in order
  50.  
  51.   All collections are kept updated through out the game except for Players as it will always contain
  52.   all players 0-15 within the game. All collections always maintain initial order.
  53.  
  54.   Example- 1, 3, 5, and 8 are active human players
  55.   5 leaves
  56.   1, 3, 8 would now be in human players in that order
  57.  
  58.   InactivePlayers are stored in the order of leaving players, so it's possible to see who
  59.   left first and who left last.
  60.  
  61.   All functions and collections relating to specific features are only in the code if specified in settings.
  62.   For this reason, all setting definitions that relates to code that might be removed are not private.
  63.   To keep code from breaking and keep code efficient, use #if with the settings.
  64.  
  65. API-
  66.   Structs:
  67.   HumanPlayer, ComputerPlayer, ActivePlayer, InactivePlayer
  68.  
  69.   Propertiers:
  70.   thistype first
  71.   First player (id)
  72.   thistype last
  73.   Last player (id)
  74.   thistype next
  75.   Next player (id)
  76.   thistype previous
  77.   Previous player (id)
  78.   player get
  79.   Get player (id). If get is null, player is not in the collection.
  80.   static int count
  81.   Gets the total player count
  82.  
  83.   Methods:
  84.   HumanPlayer and ComputerPlayer only-
  85.  
  86.   void remove()
  87.   Removes player from the game
  88.  
  89.   static triggercondition addOnOut(boolexpr c)
  90.   Adds boolexpr to player out trigger and returns the triggercondition
  91.  
  92.   static void removeOnOut(triggercondition c)
  93.   Removes triggercondition from player out trigger
  94.  
  95.   Players
  96.   player get
  97.   Gets the player handle
  98.   bool active
  99.   Is player active?
  100.   static player lastRemoved
  101.   Stores last removed player for event response
  102.   static int lastRemovedId
  103.   Stores the id of last removed player for event response
  104.   static player getLocal
  105.   GetLocalPlayer() but faster
  106.   static player hidden
  107.   HIDDEN_PLAYER_ID
  108. ===================================================================*/
  109. private player localPlayer
  110. private bool isPlayerActive[]
  111. private player lastRemovedPlayer = null
  112. private int lastRemovedPlayerId = 0
  113. private player players[]
  114.  
  115. private trigger humanLeaves;
  116.  
  117. define private REMOVE_PLAYER(PLAYER_TYPE, PLAYER_OUT_EVENT) = {
  118. public void remove() {
  119. #if KICK_PLAYER_ON_REMOVE
  120. RemovePlayer(.get, PLAYER_GAME_RESULT_DEFEAT)
  121. if (localPlayer == .get) {
  122. EndGame(false)
  123. }
  124. #endif
  125.  
  126. REMOVE_PLAYER(PLAYER_TYPE, .get, PLAYER_OUT_EVENT)
  127. }
  128. }
  129.  
  130. define private PLAYER_STRUCT(PLAYER_TYPE) = {
  131. private struct P_##PLAYER_TYPE extends array {
  132. public static thistype first = 0
  133. public static thistype last = 0
  134. public static int count = 0
  135.  
  136. public player get;
  137. public thistype next;
  138. public thistype previous;
  139. }//end struct P_##PLAYER_TYPE
  140.  
  141. struct PLAYER_TYPE extends array {
  142. public player operator get() {
  143. return P_##PLAYER_TYPE[this].get;
  144. }//end operator getPlayer
  145.  
  146. public static thistype operator first() {
  147. return P_##PLAYER_TYPE[this].first;
  148. }//end static operator first
  149.  
  150. public static thistype operator last() {
  151. return P_##PLAYER_TYPE[this].last;
  152. }//end static operator last
  153.  
  154. public static int operator count() {
  155. return P_##PLAYER_TYPE[this].count;
  156. }//end static operator count
  157.  
  158. public thistype operator next() {
  159. return P_##PLAYER_TYPE[this].next;
  160. }//end operator next
  161.  
  162. public thistype operator previous() {
  163. return P_##PLAYER_TYPE[this].previous;
  164. }//end operator previous
  165.  
  166. #if PLAYER_TYPE == HumanPlayer
  167. REMOVE_PLAYER(PLAYER_TYPE, HUMAN_OUT_EVENT)
  168.  
  169. #if HUMAN_OUT_EVENT
  170. public static trigger out = CreateTrigger();
  171.  
  172. public static triggercondition addOnOut(boolexpr c) {return TriggerAddCondition(out, c);}
  173. public static void removeOnOut(triggercondition c) {TriggerRemoveCondition(out, c);}
  174. #endif
  175. #endif
  176.  
  177. #if PLAYER_TYPE == ComputerPlayer
  178. REMOVE_PLAYER(PLAYER_TYPE, COMPUTER_OUT_EVENT)
  179.  
  180. #if COMPUTER_OUT_EVENT
  181. public static trigger out = CreateTrigger();
  182.  
  183. public static triggercondition addOnOut(boolexpr c) {return TriggerAddCondition(out, c);}
  184. public static void removeOnOut(triggercondition c) {TriggerRemoveCondition(out, c);}
  185. #endif
  186. #endif
  187. }//end struct Players
  188. }//end define PLAYER_STRUCT
  189.  
  190. #if TRACK_HUMAN
  191. PLAYER_STRUCT(HumanPlayer);
  192. #endif
  193.  
  194. #if TRACK_COMPUTER
  195. PLAYER_STRUCT(ComputerPlayer);
  196. #endif
  197.  
  198. #if TRACK_ACTIVE
  199. PLAYER_STRUCT(ActivePlayer);
  200. #endif
  201.  
  202. #if TRACK_INACTIVE
  203. PLAYER_STRUCT(InactivePlayer);
  204. #endif
  205.  
  206. struct Players extends array {
  207. public player operator get() {
  208. return players[this]
  209. }//end player operator get()
  210.  
  211. public bool operator active() {
  212. return isPlayerActive[this]
  213. }//end bool operator active
  214.  
  215. public static player operator lastRemoved() {
  216. return lastRemovedPlayer
  217. }//end player operator lastRemoved
  218.  
  219. public static int operator lastRemovedId() {
  220. return lastRemovedPlayerId
  221. }//end player operator lastRemovedId
  222.  
  223. public static player operator getLocal() {
  224. return localPlayer
  225. }//end player operator local
  226.  
  227. public static player operator hidden() {
  228. return players[HIDDEN_PLAYER_ID];
  229. }//end player operator hidden
  230. }//end struct Players
  231.  
  232. #if REMOVE_UNITS_ON_INI_FOR_INACTIVE || REMOVE_UNITS_ON_LEAVE
  233. private group removeUnitGroup = CreateGroup();
  234. private boolexpr removeUnits
  235. #endif
  236.  
  237. define private REMOVE_FROM_PLAYER_STRUCT(PLAYER_TYPE, WHICH_PLAYER_ID) = {
  238. //decrease count
  239. P_##PLAYER_TYPE.count--
  240.  
  241. //remove from PLAYER_TYPE struct
  242. if (WHICH_PLAYER_ID == P_##PLAYER_TYPE.last) {
  243. P_##PLAYER_TYPE.last = P_##PLAYER_TYPE[WHICH_PLAYER_ID].previous
  244. }//end if
  245. if (WHICH_PLAYER_ID == P_##PLAYER_TYPE.first) {
  246. P_##PLAYER_TYPE.first = P_##PLAYER_TYPE[WHICH_PLAYER_ID].next
  247. }//end elseif
  248.  
  249. P_##PLAYER_TYPE[WHICH_PLAYER_ID].next.previous = P_##PLAYER_TYPE[WHICH_PLAYER_ID].previous
  250. P_##PLAYER_TYPE[WHICH_PLAYER_ID].previous.next = P_##PLAYER_TYPE[WHICH_PLAYER_ID].next
  251.  
  252. //set to null
  253. P_##PLAYER_TYPE[WHICH_PLAYER_ID].get = null
  254. }//end define REMOVE_FROM_PLAYER_STRUCT(PLAYER_TYPE, WHICH_PLAYER)
  255.  
  256. define private ADD_TO_PLAYER_STRUCT(PLAYER_TYPE, WHICH_PLAYER_ID) = {
  257. //increase count
  258. P_##PLAYER_TYPE.count++
  259.  
  260. //add to PLAYER_TYPE struct
  261. P_##PLAYER_TYPE.last.next = WHICH_PLAYER_ID
  262. P_##PLAYER_TYPE[WHICH_PLAYER_ID].previous = P_##PLAYER_TYPE.last
  263. P_##PLAYER_TYPE.last = WHICH_PLAYER_ID
  264. P_##PLAYER_TYPE.last.next = bj_MAX_PLAYER_SLOTS
  265.  
  266. if (P_##PLAYER_TYPE.first.get == null) {//if empty
  267. P_##PLAYER_TYPE.first = WHICH_PLAYER_ID
  268. P_##PLAYER_TYPE.first.previous = bj_MAX_PLAYER_SLOTS
  269. }//if
  270.  
  271. //set the get
  272. P_##PLAYER_TYPE[WHICH_PLAYER_ID].get = players[WHICH_PLAYER_ID]
  273. }//end define ADD_TO_PLAYER_STRUCT
  274.  
  275. define private REMOVE_PLAYER(PLAYER_TYPE, WHICH_PLAYER, TRIGGER_CHECK) = {
  276. //update event variables
  277. lastRemovedPlayer = WHICH_PLAYER
  278. lastRemovedPlayerId = GetPlayerId(lastRemovedPlayer)
  279.  
  280. if (isPlayerActive[lastRemovedPlayerId]) {//if the player is active
  281. //remove player from specific player struct
  282. #if PLAYER_TYPE == HumanPlayer //human
  283. #if TRACK_HUMAN
  284. REMOVE_FROM_PLAYER_STRUCT(PLAYER_TYPE, lastRemovedPlayerId)
  285. #endif
  286. #else //computer
  287. #if TRACK_COMPUTER
  288. REMOVE_FROM_PLAYER_STRUCT(PLAYER_TYPE, lastRemovedPlayerId)
  289. #endif
  290. #endif
  291.  
  292. //remove from actives
  293. #if TRACK_ACTIVE
  294. REMOVE_FROM_PLAYER_STRUCT(ActivePlayer, lastRemovedPlayerId)
  295. #endif
  296.  
  297. //add to inactives
  298. #if TRACK_INACTIVE
  299. ADD_TO_PLAYER_STRUCT(InactivePlayer, lastRemovedPlayerId)
  300. #endif
  301.  
  302. //set active to false
  303. isPlayerActive[lastRemovedPlayerId] = false
  304.  
  305. //fire trigger
  306. #if TRIGGER_CHECK
  307. TriggerEvaluate(PLAYER_TYPE.out)
  308. #endif
  309.  
  310. //remove units on leave
  311. #if REMOVE_UNITS_ON_LEAVE
  312. GroupEnumUnitsOfPlayer(removeUnitGroup, lastRemovedPlayer, removeUnits)
  313. #endif
  314. }//end if
  315. }//end define REMOVE_PLAYER
  316.  
  317. private void Ini() {
  318. int i = 0
  319.  
  320. humanLeaves = CreateTrigger()
  321.  
  322. //do ini if either REMOVE_UNITS
  323. #if REMOVE_UNITS_ON_INI_FOR_INACTIVE || REMOVE_UNITS_ON_LEAVE
  324. removeUnits = Condition(lambda bool() {
  325. RemoveUnit(GetFilterUnit())
  326. return false
  327. })
  328. #endif
  329.  
  330. //ini 0 through bj_MAX_PLAYERS - 1
  331. do {
  332. players[i] = Player(i)
  333.  
  334. if (GetPlayerSlotState(players[i]) == PLAYER_SLOT_STATE_PLAYING) then
  335. #if TRACK_ACTIVE
  336. ADD_TO_PLAYER_STRUCT(ActivePlayer, i)
  337. #endif
  338.  
  339. if (GetPlayerController(players[i]) == MAP_CONTROL_USER) then
  340. #if TRACK_HUMAN
  341. ADD_TO_PLAYER_STRUCT(HumanPlayer, i)
  342.  
  343. if (HumanPlayer.count > 1) {
  344. TriggerRegisterPlayerEvent(humanLeaves, players[i], EVENT_PLAYER_LEAVE)
  345. }//end if
  346. #endif
  347.  
  348. #if TRACK_COMPUTER
  349. else
  350. ADD_TO_PLAYER_STRUCT(ComputerPlayer, i)
  351. #endif
  352. endif
  353.  
  354. isPlayerActive[i] = true
  355.  
  356. #if REMOVE_UNITS_ON_INI_FOR_INACTIVE || TRACK_INACTIVE
  357. else
  358. #if REMOVE_UNITS_ON_INI_FOR_INACTIVE
  359. GroupEnumUnitsOfPlayer(removeUnitGroup, players[i], removeUnits)
  360. #endif
  361.  
  362. #if TRACK_INACTIVE
  363. ADD_TO_PLAYER_STRUCT(InactivePlayer, i)
  364. #endif
  365. #endif
  366. endif
  367. } whilenot ++i == bj_MAX_PLAYERS
  368.  
  369. //ini bj_MAX_PLAYERS through bj_MAX_PLAYER_SLOTS-1
  370. do {
  371. players[i] = Player(i)
  372. } whilenot ++i == bj_MAX_PLAYER_SLOTS
  373.  
  374. #if TRACK_HUMAN
  375. if (HumanPlayer.count > 1) {
  376. TriggerRegisterPlayerEvent(humanLeaves, HumanPlayer.first.get, EVENT_PLAYER_LEAVE)
  377. TriggerAddCondition(humanLeaves, Condition(lambda bool() {
  378. REMOVE_PLAYER(HumanPlayer, GetTriggerPlayer(), HUMAN_OUT_EVENT)
  379. return false
  380. }))
  381. }//end if
  382. else {
  383. DestroyTrigger(humanLeaves)
  384. humanLeaves = null
  385. }//else
  386. #endif
  387. localPlayer = GetLocalPlayer()
  388. }//end function Ini()
  389. }

Find all posts by this user
Quote this message in a reply
03-12-2010, 02:11 AM
Post: #2
RE: [Utility]-[vJASS][cJASS] Player Manager
the purpose of this is?

Per vicis vos perago reddo quod lectio is ; vos mos have animadverto ut is opes nusquam procul totus , quod vos mos have attero quinque minutes of vestri vita. - Latin
Find all posts by this user
Quote this message in a reply
03-12-2010, 11:51 AM
Post: #3
RE: [Utility]-[vJASS][cJASS] Player Manager
I guess you'll have to read the documentation to find out : P
Find all posts by this user
Quote this message in a reply
03-13-2010, 02:27 PM
Post: #4
RE: [Utility]-[vJASS][cJASS] Player Manager
Approved
Find all posts by this user
Quote this message in a reply
Post Reply