Opened 11 years ago

Closed 11 years ago

#2051 closed enhancement (fixed)

[PATCH] Health regeneration

Reported by: sanderd17 Owned by:
Priority: Nice to Have Milestone: Alpha 14
Component: UI & Simulation Keywords: patch
Cc: Patch:

Description

A little patch implementing health regeneration. It works nicely together with #2048. It has the following properties:

  • RegenRate can change any moment due to technologies or auras and will always be updated
  • Negative regeneration rates are supported. This can be usefull on auras, or on the Iberian fire ship. It could also be used for continuous damage (setting the regenRate negative after collision with a fire ball).

I do think that the current values are on the high side (up to 1 hitpoint/second regeneration). So those templates would need fixing. For regular units, 0.1 to 0.2 hitpoints/second would be enough.

Attachments (1)

regeneration.diff (21.3 KB ) - added by sanderd17 11 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 by historic_bruno, 11 years ago

It would be more efficient to only run the regen timer when RegenRate != 0. That adds a little more complexity to the patch to start or stop the timer, but I think it's worth it in terms of efficiency (currently the timer will be run for every entity with health).

It's strange that you have to check max hitpoints before increasing health, but not if the HP are already 0 before reducing it, shouldn't that be handled by the Increase and Reduce functions instead? There may be a reason for not doing that, but it seems sensible.

Maybe you should cancel the regen timer in the OnDestroy handler, when the entity is destroyed.

comment:2 by historic_bruno, 11 years ago

Also, I'm not sure if most units should have health regen (even if it's in the templates currently), that's more of a design decision.

comment:3 by historic_bruno, 11 years ago

Milestone: BacklogAlpha 14

comment:4 by sanderd17, 11 years ago

I also thought about only starting the timer when RegenRate != 0. But I wouldn't know how to do that. Currently, when the regenRate == 0, the function it executes is very minimal (only checking if it's zero, it does nothing else).

I check for max hitpoints for performance reasons. If you're at max hitpoints, the function still gets executed, and the engine is notified of a 0-change, which can all be avoided. I didn't check for hitpoints == 0, because that will happen only once per entity, so it isn't important performance-wise.

I also didn't know what happened with the timer when an entity is destroyed. I thought it destroyed itself (in which case that extra destroy code isn't needed). There are numerous timers still running when an entity is destroyed, but it will be easy to add anyway.

in reply to:  4 comment:5 by historic_bruno, 11 years ago

Replying to sanderd17:

I also thought about only starting the timer when RegenRate != 0. But I wouldn't know how to do that. Currently, when the regenRate == 0, the function it executes is very minimal (only checking if it's zero, it does nothing else).

My concern is more the overhead of the timer component having to iterate all these possibly useless timers. It wouldn't be hard to implement: you would have an initial check in init, the only way RegenRate will change is by a tech mod, so you update the cached value and check again in OnTechnologyModification (and techs are cumulative, so +this.template.RegenRate should be used as the base value). This also has the advantage of not constantly polling for tech changes in RegenRate, it will rarely change.

I check for max hitpoints for performance reasons. If you're at max hitpoints, the function still gets executed, and the engine is notified of a 0-change, which can all be avoided. I didn't check for hitpoints == 0, because that will happen only once per entity, so it isn't important performance-wise.

Right, I'm asking if Increase shouldn't check HP and do nothing if they are already at max HP? Otherwise all the code that calls Increase has to duplicate that (but the same isn't required for Reduce) It's not essential to your patch, but it is something I notice.

I also didn't know what happened with the timer when an entity is destroyed. I thought it destroyed itself (in which case that extra destroy code isn't needed). There are numerous timers still running when an entity is destroyed, but it will be easy to add anyway.

Most components clean up their timers in OnDestroy, so it's at least conventional, even if it's not necessary, I guess it could save the timer having to query a destroyed entity/component.

comment:6 by sanderd17, 11 years ago

I implemented the changes, and it seems to work better.

by sanderd17, 11 years ago

Attachment: regeneration.diff added

comment:7 by sanderd17, 11 years ago

Cleaned up the code a bit, and added template changes

  • normal units have no regeneration
  • healers and champions have 0.1 hp/sec regeneration
  • heros have 0.2 hp/sec regeneration

comment:8 by sanderd17, 11 years ago

Keywords: review removed
Resolution: fixed
Status: newclosed

fixed in r13647

Note: See TracTickets for help on using tickets.