Opened 18 months ago
Closed 16 months ago
#6657 closed defect (fixed)
Elite units small discrepancy between maxHitpoints and hitpoints
Reported by: | Langbart | Owned by: | Freagarach |
---|---|---|---|
Priority: | If Time Permits | Milestone: | Alpha 27 |
Component: | Simulation | Keywords: | |
Cc: | Patch: |
Description (last modified by )
to reproduce
- start the
units_demo
map
pyrogenesis -autostart="scenarios/units_demo" -autostart-victory="endless"
- seek the
Mauryan Hero Healer Acharya Chanakya
, somewhere in the back rows. You will find him, because he is the only one walking around .
- the
Healer
is going to someElite units
to heal them, for example theCretan Mercenary Archer
- select the
Cretan Mercenary Elite Archer
, toggle theDeveloper Overlay Alt+D
and the optionDisplay selection state
- the difference between
hitpoints
andmaxHitpoints
is only0.00000000000001 == 1E-14
- the difference between
- the
Mauryan Hero Healer
is the only walking around, because of its largevision
range he inherits from the simulation/templates/template_unit_hero.xml template
- other healers have only
30 vision
- simulation/templates/template_unit_support_healer.xml
minimal amount of units to reproduce the issue
- change the
units_demo.js
to only show three units. the combo is enough to create the discrepancy in health points.-
binaries/data/mods/public/maps/scenarios/units_demo.js
a b let maxh = 0; 31 31 let gap = 14; 32 32 33 33 let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); 34 for (let template of cmpTemplateManager.FindAllTemplates(actors))34 for (let template of ["units/mace/hero_pyrrhus_i", "units/iber/catafalque", "units/maur/elephant_archer_e"]) 35 35 { 36 36 print(template + "...\n");
-
intended behavior
- the
hitpoints
for allelite
units should be the same as themaxHitpoints
when the game starts
cause of the issue
See the comment by Stan
suggesting floating point math
❯ node Welcome to Node.js v19.1.0. > 0.1+0.2 0.30000000000000004
possible solution
one-line change, there is a calculation being made in the Health.js
file.
example units/maur/elephant_archer_e
# this.hitpoints * newMaxHitpoints / oldMaxHitpoints ❯ node Welcome to Node.js v19.1.0. > 233.75000000000003*292.18750000000006/233.75000000000003 # 292.1875
-
binaries/data/mods/public/simulation/components/Health.js
a b Health.prototype.RecalculateValues = function() 465 465 let newMaxHitpoints = ApplyValueModificationsToEntity("Health/Max", +this.template.Max, this.entity); 466 466 if (oldMaxHitpoints != newMaxHitpoints) 467 467 { 468 let newHitpoints = this.hitpoints * newMaxHitpoints/oldMaxHitpoints;468 let newHitpoints = (this.hitpoints === oldMaxHitpoints) ? newMaxHitpoints : this.hitpoints * newMaxHitpoints / oldMaxHitpoints; 469 469 this.maxHitpoints = newMaxHitpoints; 470 470 this.SetHitpoints(newHitpoints); 471 471 }
Attachments (3)
Change History (11)
by , 18 months ago
Attachment: | Chanakya.jpg added |
---|
by , 18 months ago
Attachment: | hitpoint_diff.jpg added |
---|
comment:1 by , 18 months ago
comment:2 by , 18 months ago
Description: | modified (diff) |
---|
follow-up: 5 comment:3 by , 18 months ago
Yes moast propably it is float imprecision: Bigger values are (absolute) more imprecise. Elite Vachii Gaia (Elephant Archer) have 292.18750000000006.
The imprecision alone is not a problem the difference between hitpoints and maxHitpoints is. For Advanced Vachii Gaia (Elephant Archer) maxHitpoints does round to 233.75000000000003 but hitpoints does that to. There is no difference and they need no heal.
Typeing 187 * 1.25
in to the in game console results in 233.75
an not 233.75000000000003
comment:4 by , 18 months ago
It was reported by @wowgetoffyourcellphone before as well. Maybe even this: https://github.com/JustusAvramenko/delenda_est/commit/cd88bed66168a0366ac816dcdd8a841de8e536fc.
comment:5 by , 18 months ago
Replying to phosit:
Typeing
187 * 1.25
in to the in game console results in233.75
an not 233.75000000000003
Source: ESLINT rule: no-loss-of-precision
In JS, Numbers are stored as double-precision floating-point numbers according to the IEEE 754 standard. Because of this, numbers can only retain accuracy up to a certain amount of digits. If the programmer enters additional digits, those digits will be lost in the conversion to the Number type and will result in unexpected behavior.
example - node.js
the second calculation has one more zero
node Welcome to Node.js v19.1.0. > 58.43750000000001 * 1.25 # 73.04687500000001 > 58.437500000000001 * 1.25 # 73.046875
comment:6 by , 18 months ago
Description: | modified (diff) |
---|
by , 18 months ago
Attachment: | maur_ele.png added |
---|
Might be float imprecision