Game Development Community

Drowning

by Stefan Rampp · in General Discussion · 04/09/2003 (10:40 am) · 26 replies

Just a short code snipit/addition for Realmwars: the following functions add a 'drowning behavior' to the orc/elf. Just add these functions/modify the exisiting ones:

$UnderwaterDamage = 5;		//damage done every $UnderwaterCheckTime ms if the player is underwater
$UnderwaterInitTime = 10000;	//initial time before the player gets damage
$UnderwaterCheckTime = 1000;	//time to check underwater status (ms)

function Orc::onEnterLiquid(%this, %obj, %coverage, %type)
{
   	Parent::onEnterLiquid(%this, %obj, %coverage, %type);
	$Drowning::Schedule = schedule($UnderwaterInitTime, 0, "checkUnderwater", %obj );
}

function Orc::onLeaveLiquid(%this, %obj, %type)
{
   	cancel($Drowning::Schedule);
}

function Elf::onEnterLiquid(%this, %obj, %coverage, %type)
{
   	Parent::onEnterLiquid(%this, %obj, %coverage, %type);
	$Drowning::Schedule = schedule($UnderwaterInitTime, 0, "checkUnderwater", %obj );
}

function Elf::onLeaveLiquid(%this, %obj, %type)
{
   	cancel($Drowning::Schedule);
}

function checkUnderwater(%player)
{
	//Send a ray from the eye point to the sky to see if it crosses a waterblock.
	//If it does, this means that the player is underwater.  The raycast only returns true
	//it it intersects the water surface!!
	%eye = %player.getEyePoint();
	%end = VectorAdd(%eye, "0 0 100");
	%searchMasks = $TypeMasks::WaterObjectType;

	if(ContainerRayCast(%eye, %end, %searchMasks)) %player.applyDamage($UnderwaterDamage); 
	

	$Drowning::Schedule = schedule(1000, 0, "checkUnderwater", %player );
}

Whenever the head of the orc/elf is underwater and $UnderwaterInitTime ms have passed, the player suffers $UnderwaterDamage points of damage every $UnderwaterCheckTime ms until the player gets his head out of the water again.

I hope this is somewhat useful.

Stefan.
Page «Previous 1 2
#1
04/11/2003 (12:34 pm)
Thanks Stefan, will add this soon!
#2
04/17/2003 (7:04 pm)
What Script????
#3
04/17/2003 (7:43 pm)
Where would we add this if we wanted to use it in our game.. : )
#4
04/17/2003 (7:51 pm)
Looks like Stefan's script parts belong in the player.cs file located in the ~/server/scripts/ directory. Note that ~/ means root directory of the game mod.
#5
04/17/2003 (8:48 pm)
weird, I added it to the fps game and just changed the name form orc to player and it gives an error...
I am also a coding newb so bear with me.. : )

Error-----------------------------------------------------------------

fps/server/scripts/player.cs Line: 974 - Syntax error.
>>> Advanced script error report. Line 1947.
>>> Some error context, with ## on sides of error halt:
//-----------------------------Underwater-------------------------------------



function ##P##layer::onEnterLiquid(%this, %obj, %coverage, %type)

{

Parent::onEnterLiquid(%this, %obj, %coverage, %type);
>>> Error report complete.
#6
04/17/2003 (8:52 pm)
Then again I am assuming there is other code involved.. : ) as I dont see any thing relating to the drowning function anywhere else.. : ) so must be realm wars specific no?
heheh
darn and I need drowning in my game.. : ) oh well, will have to wait for next update to realm wars I guess.. : )
Oh and if soemone knows how to do drowning.. hook a brother up eh!, I also need to add a sound function to this, Would I just use the same as a deathcry? if so I should be able to pull it from the other code.. I want it to do a glub glub sound.. or a gasping sound.. :)
anyways back to building.. : )
#7
04/18/2003 (3:00 am)
@Tom -

So you replaced 'Orc' and 'Elf' with 'Player' it seems (Noting the context location on function Player::onEnterLiquid()).

And this is coming from someone without C experience too! (Myself that is) ;)

- Chris
#8
04/18/2003 (3:41 am)
Try changing orc and elf to LightMaleHumanArmor.
#9
04/18/2003 (4:37 am)
I actually wrote these script functions for a mission for realmwars. I just put the functions in a separate file (e.g. "drowning.cs") and put it in the mission folder. Then I added exec(".\drowing.cs") to the beginning of the mission file and it worked at least on a one machine setup. The reason I did it like this is that I wanted it to be compatible to realmwars and so I wasn't "allowed" to change any of the realmwars scripts. I guess J. Donovan Stanley's advice would work, since Orc and Elf are both derived from PlayerData and not form Player. LightMaleHumanArmor is derived from PlayerData, too. I have tried to use PlayerData::onEnterLiquid... but with no luck. I'm not quite sure why this doesn't work yet, but I'll look into it when I have time.

- Stefan
#10
04/18/2003 (5:56 am)
Well it works.. : )
I add the $underwater.....
to the fornt of player.cs were the lava and other water types were and I took out the elf and changed the orcs name to LightMaleHumanArmor
and bingo.. my guy is drowning right now as we speak.. glurg glurg....
now to add sound.. : )
#11
04/18/2003 (6:05 am)
I'm glad to hear that! ... I mean I'm sorry for your LightMaleHumanArmor guy.

- Stefan
#12
04/18/2003 (6:36 am)
thats ok.. : ) he is tough...
Now I need to figure out this sound.. hehehe.. : )
Thanks again for the code.. : )
#13
12/18/2004 (4:17 pm)
I implemented the code above by just changing player.cs ... however, this function function checkUnderwater(%player) never gets called.

I put echo statements in checkUnderwater and onEnterLiquid ... the echo statement for onEnterLiquid shows, but not checkUnderwater, so I'm guessing something is wrong with that Schedule command or it's behaviour has changed ?!?

I had to also remove this line

Parent::onEnterLiquid(%this, %obj, %coverage, %type);

as it gave an error saying that, that functions doesn't exist.
#14
12/18/2004 (5:00 pm)
With a little more digging around (putting echo statements everywhere) ... I was able to find out that as soon as the Player enters the water the onEnterLiquid function is fired off and then as soon his head goes under the onLeaveLiquid function is fired off - thereby, cancelling out the Drowning Schedule.

Anyone know why this behaviour is happening ?
#15
12/18/2004 (5:08 pm)
First guess (and it's only a guess) is that you aren't using the same node within the player object for the two "layer" transitions. This may be stock (bug) in torque, it may be due to changes you've made.

It sounds as if your player is being seen as entering water when the center or feet of the player object is entering the water block, but the onLeaveLiquid is being called when the eye or camera position passes the waterblock threshold. This would explain the effect you are seeing, even though it's not the way it should be.
#16
12/18/2004 (11:35 pm)
That sounds like a problem with the waterblock to me. Something similiar happened to me, when the waterblock was actually just a layer, so check the size/scale of the block and change the z-value, maybe that helps.
#17
12/19/2004 (4:28 am)
Ahh yes ! lol i remeber the first time i polayed realm wars and i fell in the water and then i was stuck there for ages trying to get out i finnaly found you could get ou by the boats but the ammount of time it took was infuriating .
cant wait till this is implemented into the game.
#18
12/20/2004 (6:47 am)
Its already in CVS actually, Doug Barker wrote up all the code and implemented it way back by the end of October just pull and you shall have the last update of it that was checked into the GG RW CVS.
#19
12/20/2004 (3:22 pm)
@Stephen ... that sounded like that would be the problem, but I checked that - I also noticed that all the particle effects such as bubbles etc happen when the player is in the water datablock or when the players fires off - so it would seem that the Z value is correct.
#20
02/09/2005 (10:27 pm)
For some reason this isnt working for me. I have tried Orc, Armor, Player and LightMaleHumanArmor. I added an echo statement to the function and it is not being called. I placed the code at the beginning of the player.cs file in server/scripts. what could i be doing wrong?
Page «Previous 1 2