Guest User

Untitled

a guest
Jul 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. mob
  2. var
  3. list/effects = list() //list of all effects affecting the mob
  4. tmp
  5. list/effect_registry = list() //a readable hashmap of effects sorted by their [id] and [id].[sub_id]
  6. proc
  7. AddEffect(effect/e,time=world.time) //mob redirect for effect.Add()
  8. e.Add(src,time)
  9.  
  10. RemoveEffect(effect/e,time=world.time) //mob redirect for effect.Remove()
  11. e.Remove(src,time)
  12.  
  13. effect
  14. var
  15. id
  16. sub_id = "global"
  17.  
  18. active = 0 //whether the current effect is still active
  19. start_time = 0 //The time that the effect was added to the target
  20.  
  21. duration = 1#INF //for timed and ticker effects (lifetime of timed and ticker effects)
  22.  
  23. tick_delay = 0 //for ticker effects (delay between ticks)
  24. ticks = 0 //for ticker effects (current number of ticks passed)
  25.  
  26. stacks = 1 //for stacking effects (current number of stacks)
  27. max_stacks = 1 //for stacking effects (maximum number of stacks)
  28.  
  29. proc
  30. Add(mob/target,time=world.time)
  31. if(world.time-time>=duration) //reject any effects trying to be added after the duration is already expired.
  32. active = 0
  33. return 0
  34. var/list/registry = target.effect_registry
  35. var/uid
  36. if(id&&sub_id)
  37. uid = "[id].[sub_id]" //create the unique id
  38.  
  39. var/effect/e = registry[uid] //check the target's registry for a matching effect by unique id
  40. if(e && !Override(target,e,time)) //if we found an effect matching our uniqueid, we tell this effect to attempt to override it. If it fails, we return 0
  41. return 0
  42. if(id)
  43. var/list/group = registry[id] //get the current id group.
  44. if(!group)
  45. //if there is nothing matching the current effect id group
  46. registry[id] = src
  47. else if(istype(group))
  48. //if there is already a list of effects in the id group, add this effect to the group
  49. group += src
  50. else if(istype(group,/effect))
  51. //if there is a single effect in the id group, make it a list with the item and this effect in it.
  52. registry[id] = list(group,src)
  53. else
  54. //if there is something else in place, fail to add this effect. (Possibly adding class immunities via a string?)
  55. return 0
  56.  
  57. //Add() hasn't returned yet, so this effect is going to be added to the registry via unique id and the target's effects list.
  58. if(uid)
  59. registry[uid] = src
  60. target.effects += src
  61.  
  62. Added(target,time) //call the Added() hook.
  63. return 1 //return success
  64.  
  65. Override(mob/target,effect/overriding,time=world.time)
  66. if(max_stacks>1 && max_stacks==overriding.max_stacks) //check if the max number of stacks match between the two and are greater than 1
  67. Stack(target,overriding) //stack up the two effects.
  68. overriding.Cancel(target,time)
  69. overriding.Overridden(target,src,time) //this is our old code. Cancel the old one, and allow the new one to be added.
  70. return 1
  71.  
  72. Stack(mob/target,effect/overriding)
  73. stacks = min(stacks + overriding.stacks, max_stacks) //set this element's stacks to the old effect's stacks, then replace it.
  74.  
  75. Timer(mob/target,time=world.time)
  76. set waitfor = 0 //make this not block the interpreter
  77. while(active&&world.time<=start_time+duration) //continue to wait until the effect is no longer active, or the timer has expired.
  78. sleep(min(10,start_time+duration-world.time)) //wait a maximum of 1 second. This is to prevent already-canceled effects from hanging out in the scheduler for too long.
  79. Expire(target,world.time)
  80.  
  81. Ticker(mob/target,time=world.time)
  82. set waitfor = 0
  83. while(active&&world.time<=start_time+duration)
  84. Ticked(target,++ticks,world.time) //call the Ticked() hook. This is another override that allows you to define what these ticker effects will do every time they tick.
  85. sleep(min(tick_delay,start_time+duration-world.time)) //sleep for the minimum period
  86. Expire(target,world.time)
  87.  
  88. Cancel(mob/target,time=world.time)
  89. if(active)
  90. Remove(target,time)
  91. Canceled(target,time)
  92.  
  93. Remove(mob/target,time=world.time)
  94. var/list/registry = target.effect_registry
  95. if(id&&sub_id)
  96. var/uid = "[id].[sub_id]"
  97.  
  98. //some complex logic to test whether this object is actually in the registry. Fail out if the data isn't right.
  99. if(registry[uid]==src)
  100. registry -= uid //remove the uid of the object from the registry
  101.  
  102. if(id)
  103. var/list/group = registry[id]
  104. if(istype(group)) //if the id group is a list
  105. group -= src
  106. if(group.len==1) //if the removal of this effect left the list at length 1, we need to reset the id registry to be the item at group index 1 instead of a list.
  107. registry[id] = group[1]
  108. else if(group.len==0) //if the removal of this effect left the list at length 0 (this shouldn't happen), we need to remove the id registry from the registry entirely.
  109. registry -= id
  110. else if(group==src) //otherwise, we need to remove the entire id registry
  111. registry -= id
  112.  
  113. //remove the effect from the list and the registry
  114. target.effects -= src
  115.  
  116. active = 0
  117. Removed(target,time) //call the Removed() hook and return success
  118. return 1
  119.  
  120. Expire(mob/target,time=world.time)
  121. if(active) //only actually do this if the effect is currently marked as active.
  122. active = 0
  123. Remove(target,time)
  124. Expired(target,time)
  125.  
  126. Added(mob/target,time=world.time) //Added() is called when an effect is added to a target.
  127. active = 1
  128. start_time = time
  129. if(duration<1#INF) //if the effect has a duration
  130. if(tick_delay) //and the effect has a tick delay, initiate the ticker.
  131. Ticker(target,time)
  132. else //otherwise, initiate the timer
  133. Timer(target,time)
  134.  
  135. Ticked(mob/target,time=world.time) //Ticked() is called when a ticker effect successfully reaches a new tick.
  136.  
  137. Removed(mob/target,time=world.time) //Removed() is called when an effect is removed from a target.
  138.  
  139. Overridden(mob/target,effect/override,time=world.time) //Overridden() is called when an effect overrides another existing effect.
  140.  
  141. Expired(mob/target,time=world.time) //Expired() is called when a timed or a ticker effect successfully reaches the end of its duration.
  142.  
  143. Canceled(mob/target,time=world.time) //Canceled() is called when an effect has been manually canceled via Cancel().
Advertisement
Add Comment
Please, Sign In to add comment