Skip to content

Commit 0fb5a21

Browse files
committed
Added iteration on repetitive get calls
1 parent e40a7b3 commit 0fb5a21

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

internal/characters/character.go

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,31 +1387,40 @@ func (c *Character) RecalculateStats() {
13871387
if c.TNLScale == 0 {
13881388
c.TNLScale = 1.0
13891389
}
1390-
c.Stats.Get("Strength").Base = raceInfo.Stats.Get("Strength").Base
1391-
c.Stats.Get("Speed").Base = raceInfo.Stats.Get("Speed").Base
1392-
c.Stats.Get("Smarts").Base = raceInfo.Stats.Get("Smarts").Base
1393-
c.Stats.Get("Vitality").Base = raceInfo.Stats.Get("Vitality").Base
1394-
c.Stats.Get("Mysticism").Base = raceInfo.Stats.Get("Mysticism").Base
1395-
c.Stats.Get("Perception").Base = raceInfo.Stats.Get("Perception").Base
1390+
for _, statName := range c.Stats.GetStatInfoNames() {
1391+
c.Stats.Get(statName).Base = raceInfo.Stats.Get(statName).Base
1392+
}
1393+
// c.Stats.Get("Strength").Base = raceInfo.Stats.Get("Strength").Base
1394+
// c.Stats.Get("Speed").Base = raceInfo.Stats.Get("Speed").Base
1395+
// c.Stats.Get("Smarts").Base = raceInfo.Stats.Get("Smarts").Base
1396+
// c.Stats.Get("Vitality").Base = raceInfo.Stats.Get("Vitality").Base
1397+
// c.Stats.Get("Mysticism").Base = raceInfo.Stats.Get("Mysticism").Base
1398+
// c.Stats.Get("Perception").Base = raceInfo.Stats.Get("Perception").Base
13961399
}
13971400

13981401
// Add any mods for equipment
1399-
c.Stats.Get("Strength").Mods = c.StatMod(string(statmods.Strength))
1400-
c.Stats.Get("Speed").Mods = c.StatMod(string(statmods.Speed))
1401-
c.Stats.Get("Smarts").Mods = c.StatMod(string(statmods.Smarts))
1402-
c.Stats.Get("Vitality").Mods = c.StatMod(string(statmods.Vitality))
1403-
c.Stats.Get("Mysticism").Mods = c.StatMod(string(statmods.Mysticism))
1404-
c.Stats.Get("Perception").Mods = c.StatMod(string(statmods.Perception))
1402+
for _, statName := range c.Stats.GetStatInfoNames() {
1403+
c.Stats.Get(statName).Mods = c.StatMod(statName)
1404+
}
1405+
// c.Stats.Get("Strength").Mods = c.StatMod(string(statmods.Strength))
1406+
// c.Stats.Get("Speed").Mods = c.StatMod(string(statmods.Speed))
1407+
// c.Stats.Get("Smarts").Mods = c.StatMod(string(statmods.Smarts))
1408+
// c.Stats.Get("Vitality").Mods = c.StatMod(string(statmods.Vitality))
1409+
// c.Stats.Get("Mysticism").Mods = c.StatMod(string(statmods.Mysticism))
1410+
// c.Stats.Get("Perception").Mods = c.StatMod(string(statmods.Perception))
14051411

14061412
// Recalculate stats
14071413
// Stats are basically:
14081414
// level*base + training + mods
1409-
c.Stats.Get("Strength").Recalculate(c.Level)
1410-
c.Stats.Get("Speed").Recalculate(c.Level)
1411-
c.Stats.Get("Smarts").Recalculate(c.Level)
1412-
c.Stats.Get("Vitality").Recalculate(c.Level)
1413-
c.Stats.Get("Mysticism").Recalculate(c.Level)
1414-
c.Stats.Get("Perception").Recalculate(c.Level)
1415+
for _, statName := range c.Stats.GetStatInfoNames() {
1416+
c.Stats.Get(statName).Recalculate(c.Level)
1417+
}
1418+
// c.Stats.Get("Strength").Recalculate(c.Level)
1419+
// c.Stats.Get("Speed").Recalculate(c.Level)
1420+
// c.Stats.Get("Smarts").Recalculate(c.Level)
1421+
// c.Stats.Get("Vitality").Recalculate(c.Level)
1422+
// c.Stats.Get("Mysticism").Recalculate(c.Level)
1423+
// c.Stats.Get("Perception").Recalculate(c.Level)
14151424

14161425
// Set HP/MP maxes
14171426
// This relies on the above stats so has to be calculated afterwards
@@ -1447,22 +1456,16 @@ func (c *Character) RecalculateStats() {
14471456
if c.userId != 0 {
14481457
changed := false
14491458
// return true if something has changed.
1450-
if beforeStats.Get("Strength").ValueAdj != c.Stats.Get("Strength").ValueAdj {
1451-
changed = true
1452-
} else if beforeStats.Get("Speed").ValueAdj != c.Stats.Get("Speed").ValueAdj {
1453-
changed = true
1454-
} else if beforeStats.Get("Smarts").ValueAdj != c.Stats.Get("Smarts").ValueAdj {
1455-
changed = true
1456-
} else if beforeStats.Get("Vitality").ValueAdj != c.Stats.Get("Vitality").ValueAdj {
1457-
changed = true
1458-
} else if beforeStats.Get("Mysticism").ValueAdj != c.Stats.Get("Mysticism").ValueAdj {
1459-
changed = true
1460-
} else if beforeStats.Get("Perception").ValueAdj != c.Stats.Get("Perception").ValueAdj {
1461-
changed = true
1462-
} else if beforeHealthMax != c.HealthMax {
1463-
changed = true
1464-
} else if beforeManaMax != c.ManaMax {
1465-
changed = true
1459+
for _, statName := range c.Stats.GetStatInfoNames() {
1460+
if beforeStats.Get(statName).ValueAdj != c.Stats.Get(statName).ValueAdj {
1461+
changed = true
1462+
break
1463+
}
1464+
}
1465+
if !changed {
1466+
if beforeHealthMax != c.HealthMax || beforeManaMax != c.ManaMax {
1467+
changed = true
1468+
}
14661469
}
14671470

14681471
if changed {

internal/stats/stats.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ func (s *Statistics) GetStatInfoNames() []string {
3535

3636
// Get returns a pointer to the StatInfo for the given name.
3737
func (s *Statistics) Get(name string) *StatInfo {
38-
switch strings.ToLower(name) {
38+
key := strings.ToLower(name)
39+
40+
// TODO: When we load the stats from a file, we need to check the map
41+
// if stat, ok := s.Stats[key]; ok {
42+
// copy := stat
43+
// return &copy
44+
// }
45+
46+
switch key {
3947
case "strength":
4048
return &s.Strength
4149
case "speed":
@@ -48,9 +56,9 @@ func (s *Statistics) Get(name string) *StatInfo {
4856
return &s.Mysticism
4957
case "perception":
5058
return &s.Perception
51-
default:
52-
return &StatInfo{}
5359
}
60+
61+
return &StatInfo{}
5462
}
5563

5664
// When saving to a file, we don't need to write all the properties that we calculate.

0 commit comments

Comments
 (0)