Fixes for the onHungerChange event

Want to discuss changes to the UOX3 source code? Got a code-snippet you'd like to post? Anything related to coding/programming goes here!
Post Reply
Grimson
Developer
Posts: 802
Joined: Sat Jun 04, 2005 1:52 am
Location: Germany
Has thanked: 0
Been thanked: 0

Fixes for the onHungerChange event

Post by Grimson »

UOX3 offers the onHungerChange to customize the hunger system. But with the current code it has two "bugs":
  1. It won't use the global scriptID.
  2. It's only called when the hunger level decreases, but not when it increases.
I did fix them, hopefully without causing any additional bugs ;).

Here is a cvs diff of the fixes (or download it):

Code: Select all

Index: UOXJSPropertyFuncs.cpp
===================================================================
RCS file: /cvsroot/openuo/projects/uox3/source/UOXJSPropertyFuncs.cpp,v
retrieving revision 1.25
diff -u -r1.25 UOXJSPropertyFuncs.cpp
--- UOXJSPropertyFuncs.cpp	22 Jan 2006 09:47:44 -0000	1.25
+++ UOXJSPropertyFuncs.cpp	23 Jan 2006 23:36:15 -0000
@@ -667,6 +667,11 @@
 		CChar *gPriv = (CChar *)JS_GetPrivate( cx, obj );
 		if( !ValidateObject( gPriv ) )
 			return JS_FALSE;
+		
+		//Prepare the onHungerChange Event
+		const UI16 HungerTrig = gPriv->GetScriptTrigger();
+		cScript *toHungerExecute = JSMapping->GetScript( HungerTrig );
+		cScript *globalExecute = JSMapping->GetScript( (UI16)0 );
 
 		JSEncapsulate encaps( cx, vp );
 
@@ -734,7 +739,18 @@
 				case CCP_CHARPACK:															break;
 				case CCP_FAME:			gPriv->SetFame( (SI16)encaps.toInt() );				break;
 				case CCP_KARMA:			gPriv->SetKarma( (SI16)encaps.toInt() );			break;
-				case CCP_HUNGER:		gPriv->SetHunger( (SI08)encaps.toInt() );			break;
+				case CCP_HUNGER:
+					gPriv->SetHunger( (SI08)encaps.toInt() );
+					//Call the onHungerChange Event
+					if( toHungerExecute != NULL )
+					{
+						toHungerExecute->OnHungerChange( gPriv, gPriv->GetHunger() );
+					}
+					else
+					{
+						globalExecute->OnHungerChange( gPriv, gPriv->GetHunger() );
+					}
+					break;
 				case CCP_FROZEN:		gPriv->SetFrozen( encaps.toBool() );				break;
 				case CCP_COMMANDLEVEL:	gPriv->SetCommandLevel( (UI08)encaps.toInt() );		break;
 				case CCP_RACEID:
Index: cPlayerAction.cpp
===================================================================
RCS file: /cvsroot/openuo/projects/uox3/source/cPlayerAction.cpp,v
retrieving revision 1.30
diff -u -r1.30 cPlayerAction.cpp
--- cPlayerAction.cpp	13 Dec 2005 23:45:23 -0000	1.30
+++ cPlayerAction.cpp	23 Jan 2006 23:53:43 -0000
@@ -656,6 +656,17 @@
 		//Remove a food item
 		bool iDeleted = i->IncAmount( -1 );
 		targNPC->SetHunger( static_cast<SI08>(targNPC->GetHunger() + 1) );
+		const UI16 HungerTrig = targNPC->GetScriptTrigger();
+		cScript *toHungerExecute = JSMapping->GetScript( HungerTrig );
+		cScript *globalExecute = JSMapping->GetScript( (UI16)0 );
+		if( toHungerExecute != NULL )
+		{
+			toHungerExecute->OnHungerChange( targNPC, targNPC->GetHunger() );
+		}
+		else
+		{
+			globalExecute->OnHungerChange( targNPC, targNPC->GetHunger() );
+		}
 		if( iDeleted )
 			return true; //stackdeleted
 	}
Index: gumps.cpp
===================================================================
RCS file: /cvsroot/openuo/projects/uox3/source/gumps.cpp,v
retrieving revision 1.22
diff -u -r1.22 gumps.cpp
--- gumps.cpp	11 Dec 2005 09:18:56 -0000	1.22
+++ gumps.cpp	23 Jan 2006 23:50:18 -0000
@@ -1777,6 +1777,12 @@
 			return;
 		}
 		UI16 k;
+		
+		//Prepare the onHungerChange Event
+		const UI16 HungerTrig = j->GetScriptTrigger();
+		cScript *toHungerExecute = JSMapping->GetScript( HungerTrig );
+		cScript *globalExecute = JSMapping->GetScript( (UI16)0 );
+
 		switch( index )
 		{
 			case 1:		j->SetName( reply );						break;	// Name
@@ -1837,7 +1843,17 @@
 				if( j->IsNpc() )
 					j->SetNpcWander( reply.toByte() );
 				break;
-			case 24:	j->SetHunger( reply.toByte() );					break;	// Hunger
+			case 24:
+				j->SetHunger( reply.toByte() );
+				if( toHungerExecute != NULL )
+				{
+					toHungerExecute->OnHungerChange( j, j->GetHunger() );
+				}
+				else
+				{
+					globalExecute->OnHungerChange( j, j->GetHunger() );
+				}
+				break;	// Hunger
 			case 25:	j->SetPoisonStrength( reply.toUByte() );		break;	// Poison
 			case 26:	j->SetWeight( reply.toShort() );				break;	// Weight
 			case 27:	j->SetCarve( reply.toShort() );					break;	// Carve
Index: targeting.cpp
===================================================================
RCS file: /cvsroot/openuo/projects/uox3/source/targeting.cpp,v
retrieving revision 1.23
diff -u -r1.23 targeting.cpp
--- targeting.cpp	21 Nov 2005 19:37:25 -0000	1.23
+++ targeting.cpp	23 Jan 2006 23:47:25 -0000
@@ -962,6 +962,17 @@
 			i->SetAttackFirst( false );
 			i->SetWar( false );
 			i->SetHunger( 6 );
+			const UI16 HungerTrig = i->GetScriptTrigger();
+			cScript *toHungerExecute = JSMapping->GetScript( HungerTrig );
+			cScript *globalExecute = JSMapping->GetScript( (UI16)0 );
+			if( toHungerExecute != NULL )
+			{
+				toHungerExecute->OnHungerChange( i, i->GetHunger() );
+			}
+			else
+			{
+				globalExecute->OnHungerChange( i, i->GetHunger() );
+			}
 			CItem *c = NULL;
 			for( CItem *j = i->FirstItem(); !i->FinishedItems(); j = i->NextItem() )
 			{
Index: uox3.cpp
===================================================================
RCS file: /cvsroot/openuo/projects/uox3/source/uox3.cpp,v
retrieving revision 1.37
diff -u -r1.37 uox3.cpp
--- uox3.cpp	16 Dec 2005 19:02:49 -0000	1.37
+++ uox3.cpp	23 Jan 2006 23:36:19 -0000
@@ -1342,7 +1342,7 @@
 		UI16 hungerRate = cwmWorldState->ServerData()->SystemTimer( tSERVER_HUNGERRATE );
 		if( mChar.WillHunger() && hungerRate > 0 )
 		{
-			if( mChar.GetCommandLevel() > PLAYER_CMDLEVEL && !mChar.IsDead() && !mChar.IsInvulnerable() )
+			if( mChar.GetCommandLevel() == PLAYER_CMDLEVEL && !mChar.IsDead() && !mChar.IsInvulnerable() )
 			{
 				if( mChar.GetTimer( tCHAR_HUNGER ) <= cwmWorldState->GetUICurrentTime() || cwmWorldState->GetOverflow() )
 				{
@@ -1351,9 +1351,16 @@
 						mChar.DecHunger();
 						const UI16 HungerTrig = mChar.GetScriptTrigger();
 						cScript *toExecute = JSMapping->GetScript( HungerTrig );
+						cScript *globalExecute = JSMapping->GetScript( (UI16)0 );
 						bool doHunger = true;
 						if( toExecute != NULL )
+						{
 							doHunger = !toExecute->OnHungerChange( (&mChar), mChar.GetHunger() );
+						}
+						else
+						{
+							doHunger = !globalExecute->OnHungerChange( (&mChar), mChar.GetHunger() );
+						}
 						if( doHunger )
 						{
 							switch( mChar.GetHunger() )
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

Hm. If I knew what to do with a diff file, I'd add your changes to the CVS :P
-= Ho Eyo He Hum =-
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

Ok, added your fixes to the CVS - figured the diff stuff out ;P
-= Ho Eyo He Hum =-
Grimson
Developer
Posts: 802
Joined: Sat Jun 04, 2005 1:52 am
Location: Germany
Has thanked: 0
Been thanked: 0

Post by Grimson »

Argh, just found a nice Bug. If you have no global script the server will crash when the event occurs. Here is the fix: Download

Code: Select all

Index: UOXJSPropertyFuncs.cpp
===================================================================
RCS file: /cvsroot/openuo/projects/uox3/source/UOXJSPropertyFuncs.cpp,v
retrieving revision 1.27
diff -u -r1.27 UOXJSPropertyFuncs.cpp
--- UOXJSPropertyFuncs.cpp	27 Jan 2006 22:53:49 -0000	1.27
+++ UOXJSPropertyFuncs.cpp	28 Jan 2006 02:52:44 -0000
@@ -755,7 +755,7 @@
 					{
 						toHungerExecute->OnHungerChange( gPriv, gPriv->GetHunger() );
 					}
-					else
+					else if( globalExecute != NULL )
 					{
 						globalExecute->OnHungerChange( gPriv, gPriv->GetHunger() );
 					}
Index: cPlayerAction.cpp
===================================================================
RCS file: /cvsroot/openuo/projects/uox3/source/cPlayerAction.cpp,v
retrieving revision 1.32
diff -u -r1.32 cPlayerAction.cpp
--- cPlayerAction.cpp	27 Jan 2006 22:53:49 -0000	1.32
+++ cPlayerAction.cpp	28 Jan 2006 02:52:44 -0000
@@ -663,7 +663,7 @@
 		{
 			toHungerExecute->OnHungerChange( targNPC, targNPC->GetHunger() );
 		}
-		else
+		else if( globalExecute != NULL )
 		{
 			globalExecute->OnHungerChange( targNPC, targNPC->GetHunger() );
 		}
Index: gumps.cpp
===================================================================
RCS file: /cvsroot/openuo/projects/uox3/source/gumps.cpp,v
retrieving revision 1.23
diff -u -r1.23 gumps.cpp
--- gumps.cpp	27 Jan 2006 22:53:50 -0000	1.23
+++ gumps.cpp	28 Jan 2006 02:52:44 -0000
@@ -1849,7 +1849,7 @@
 				{
 					toHungerExecute->OnHungerChange( j, j->GetHunger() );
 				}
-				else
+				else if( globalExecute != NULL )
 				{
 					globalExecute->OnHungerChange( j, j->GetHunger() );
 				}
Index: targeting.cpp
===================================================================
RCS file: /cvsroot/openuo/projects/uox3/source/targeting.cpp,v
retrieving revision 1.24
diff -u -r1.24 targeting.cpp
--- targeting.cpp	27 Jan 2006 22:53:50 -0000	1.24
+++ targeting.cpp	28 Jan 2006 02:52:44 -0000
@@ -969,7 +969,7 @@
 			{
 				toHungerExecute->OnHungerChange( i, i->GetHunger() );
 			}
-			else
+			else if( globalExecute != NULL )
 			{
 				globalExecute->OnHungerChange( i, i->GetHunger() );
 			}
Index: uox3.cpp
===================================================================
RCS file: /cvsroot/openuo/projects/uox3/source/uox3.cpp,v
retrieving revision 1.40
diff -u -r1.40 uox3.cpp
--- uox3.cpp	27 Jan 2006 22:53:50 -0000	1.40
+++ uox3.cpp	28 Jan 2006 02:52:44 -0000
@@ -722,7 +722,7 @@
 						{
 							doHunger = !toExecute->OnHungerChange( (&mChar), mChar.GetHunger() );
 						}
-						else
+						else if( globalExecute != NULL )
 						{
 							doHunger = !globalExecute->OnHungerChange( (&mChar), mChar.GetHunger() );
 						}
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

Ok, revised code is up =P
-= Ho Eyo He Hum =-
Post Reply