Skip to content

Commit

Permalink
modding: Negative tile damage cannot heal more than max health
Browse files Browse the repository at this point in the history
  • Loading branch information
yairm210 committed Nov 7, 2023
1 parent c9212e1 commit 75c4be3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions core/src/com/unciv/logic/battle/MapUnitCombatant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MapUnitCombatant(val unit: MapUnit) : ICombatant {

override fun takeDamage(damage: Int) {
unit.health -= damage
if (unit.health > 100) unit.health = 100 // For cheating modders, e.g. negative tile damage

This comment has been minimized.

Copy link
@SomeTroglodyte

SomeTroglodyte Nov 7, 2023

Collaborator

unit.health = (unit.health - damage).coerceIn(0, 100)

Joking. But - that "cheating" is actually a good idea. Maybe do a re-worded version of the UniqueType that speaks of healing... Hot Springs that heal some extra every turn... Allow UniqueTarget.Improvement - build a Wellness center on top that heals less but yields a lotta Gold, the greedy basterds...

if (unit.health < 0) unit.health = 0
if (isDefeated()) unit.destroy()
}
Expand Down
12 changes: 7 additions & 5 deletions core/src/com/unciv/logic/map/mapunit/UnitTurnManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.unciv.logic.map.mapunit

import com.unciv.UncivGame
import com.unciv.logic.battle.MapUnitCombatant
import com.unciv.logic.civilization.LocationAction
import com.unciv.logic.civilization.NotificationCategory
import com.unciv.logic.civilization.NotificationIcon
Expand Down Expand Up @@ -83,7 +84,7 @@ class UnitTurnManager(val unit: MapUnit) {
}.maxByOrNull { it.second }
?: return
if (damage == 0) return
unit.health -= damage
MapUnitCombatant(unit).takeDamage(damage)
val improvementName = citadelTile.improvement!! // guarded by `getUnpillagedImprovement() != null` above
val improvementIcon = "ImprovementIcons/$improvementName"
val locations = LocationAction(citadelTile.position, unit.currentTile.position)
Expand Down Expand Up @@ -112,18 +113,19 @@ class UnitTurnManager(val unit: MapUnit) {

private fun doTerrainDamage() {
val tileDamage = unit.getDamageFromTerrain()
unit.health -= tileDamage
if (tileDamage == 0) return

if (unit.health <= 0) {
MapUnitCombatant(unit).takeDamage(tileDamage)

This comment has been minimized.

Copy link
@SomeTroglodyte

SomeTroglodyte Nov 7, 2023

Collaborator

allocating a single-use class instance - would that add up? Nah, nano fraction of total NextTurn... But plonking the actual implementation in Companion then calling that from the instance could avoid this. Extra call indirection, potentially inlineable, should be cheaper than any tiny quantum GC stress.


if (unit.isDestroyed) {
unit.civ.addNotification(
"Our [${unit.name}] took [$tileDamage] tile damage and was destroyed",
unit.currentTile.position,
NotificationCategory.Units,
unit.name,
NotificationIcon.Death
)
unit.destroy()
} else if (tileDamage > 0) unit.civ.addNotification(
} else unit.civ.addNotification(
"Our [${unit.name}] took [$tileDamage] tile damage",
unit.currentTile.position,
NotificationCategory.Units,
Expand Down

0 comments on commit 75c4be3

Please sign in to comment.