# HG changeset patch
# User Maikel de Vries
# Date 1292278935 -3600
# Node ID 6cfb432a6e388bba4a22db06a5120e8d5a0aef4a
# Parent  d0264df54a408b1edfe5e8787cd5824945eda5a7
Physical: Helper functions to Set and Get MaxEnergy
Only needed in tut04, these make the necessary callbacks.

diff -r d0264df54a40 -r 6cfb432a6e38 docs/sdk/script/fn/GetBreath.xml
--- a/docs/sdk/script/fn/GetBreath.xml	Mon Dec 13 23:14:42 2010 +0100
+++ b/docs/sdk/script/fn/GetBreath.xml	Mon Dec 13 23:22:15 2010 +0100
@@ -9,7 +9,7 @@
     <subcat>Living Beings</subcat>
     <version>4.6.5.0 CP</version>
     <syntax><rtype>int</rtype></syntax>
-    <desc>Returns the current breath value of an object, this is the number of frames an object can still hold his breath</desc>
+    <desc>Returns the current breath value of an object, this is the number of frames an object can still hold its breath</desc>
     <related><funclink>DoBreath</funclink></related>
   </func>
   <author>jwk</author><date>2002-06</date>
diff -r d0264df54a40 -r 6cfb432a6e38 planet/System.c4g/Helpers.c
--- a/planet/System.c4g/Helpers.c	Mon Dec 13 23:14:42 2010 +0100
+++ b/planet/System.c4g/Helpers.c	Mon Dec 13 23:22:15 2010 +0100
@@ -105,6 +105,26 @@
 	return true;
 }
 
+// Sets the MaxEnergy value of an object and does the necessary callbacks.
+global func SetMaxEnergy(int value)
+{
+	if (!this)
+		return;
+	var old_maxenergy = this.MaxEnergy;
+	this.MaxEnergy = value;
+	// Change current energy percentage wise and implicit callback.
+	DoEnergy(GetEnergy() * (value - old_maxenergy) / old_maxenergy);
+	return;
+}
+
+// Returns the MaxEnergy value of an object.
+global func GetMaxEnergy()
+{
+	if (!this)
+		return;
+	return this.MaxEnergy;
+}
+
 // Sets the MaxBreath value of an object and does the necessary callbacks.
 global func SetMaxBreath(int value)
 {
diff -r d0264df54a40 -r 6cfb432a6e38 planet/Tutorial.c4f/Tutorial04.c4s/Script.c
--- a/planet/Tutorial.c4f/Tutorial04.c4s/Script.c	Mon Dec 13 23:14:42 2010 +0100
+++ b/planet/Tutorial.c4f/Tutorial04.c4s/Script.c	Mon Dec 13 23:22:15 2010 +0100
@@ -37,8 +37,7 @@
 	
 	// Second section: Weak opponent with javelins.
 	var spearman1 = CreateObject(Clonk, 1050, 560, NO_OWNER);
-	spearman1.MaxEnergy = 40000;
-	spearman1->DoEnergy(-10);
+	spearman1->SetMaxEnergy(40000);
 	spearman1->CreateContents(Javelin);
 	spearman1->AI_GuardArea(800, 400, 400, 250);
 	AddEffect("IntContentRemoval", spearman1, 100, 0);
@@ -51,16 +50,14 @@
 	// Third section: Two opponents in a tower.
 	// Lower part: a weak spearman.
 	var spearman2 = CreateObject(Clonk, 1753, 410, NO_OWNER);
-	spearman2.MaxEnergy = 40000;
-	spearman2->DoEnergy(-10);
+	spearman2->SetMaxEnergy(40000);
 	spearman2->CreateContents(Javelin);
 	spearman2->AI_GuardArea(1350, 200, 500, 400);
 	AddEffect("IntContentRemoval", spearman2, 100, 0);
 	CreateObject(EnergyBar)->SetTarget(spearman2);
 	// Upper part: a normal bowman.
 	var bowman = CreateObject(Clonk, 1732, 330, NO_OWNER);
-	bowman.MaxEnergy = 45000;
-	bowman->DoEnergy(-5);
+	bowman->SetMaxEnergy(45000);
 	bowman->CreateContents(Bow)->CreateContents(Arrow);
 	bowman->AI_GuardArea(1350, 200, 500, 400);
 	AddEffect("IntContentRemoval", bowman, 100, 0);
@@ -72,8 +69,7 @@
 	
 	// Fourth section: Opponent with sword and shield.
 	var swordman = CreateObject(Clonk, 2250, 330, NO_OWNER);
-	swordman.MaxEnergy = 60000;
-	swordman->DoEnergy(10);
+	swordman->SetMaxEnergy(60000);
 	swordman->CreateContents(Shield);
 	swordman->CreateContents(Sword);
 	swordman->AI_GuardArea(2050, 300, 300, 100);
diff -r d0264df54a40 -r 6cfb432a6e38 src/game/object/C4Object.cpp
--- a/src/game/object/C4Object.cpp	Mon Dec 13 23:14:42 2010 +0100
+++ b/src/game/object/C4Object.cpp	Mon Dec 13 23:22:15 2010 +0100
@@ -1365,10 +1365,7 @@
 	if (iChange < 0) UpdatLastEnergyLossCause(iCausedByPlr);
 	// Living things: ask effects for change first
 	if (pEffects && Alive)
-	{
 		pEffects->DoDamage(this, iChange, iCause, iCausedByPlr);
-		if (!iChange) return;
-	}
 	// Do change
 	iChange = BoundBy<int32_t>(iChange, -Energy, GetPropertyInt(P_MaxEnergy) - Energy);
 	Energy += iChange;
