Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsCog Forum → Cam-Guided missiles -- JK Crashing probs
Cam-Guided missiles -- JK Crashing probs
2004-01-15, 2:37 PM #1
I'm working on a cam-guided missile (for the Nightfire TC), and I've got what I've done working so far, but I have a SERIOUS problem. When the rocket collides with a wall, JK crashes. I think it has something to do with setting the camera, but I can't figure it out. Here's the cog.

Code:
# Jedi Knight Cog Script
#
# WEAP_SENTINEL.COG
#
# WEAPON 7 script - Sentinel
#        - Rocket Launcher
#        - 4 rockets per reload
#        - Powerful
#        - Secondary Fire - Cam-Guided Missiles
#
# Source: M-16 Assault Rifle cog from the James Bond 007 mod (found on massassi.net)
# made by Ben Allen
# Modified by Darth Slaw
#
# (C) 2004 JK Mod: Nightfire TC
#==============================================================#
symbols

model     povModel=rldv.3do                   local
model     weaponMesh=strg.3do                 local

keyframe  mountAnim=strvmnt.key               local
keyframe  dismountAnim=bryvdis.key            local
keyframe  povfireAnim=bryvpst1.key            local
keyframe  reloadAnim=kyhlstr.key              local
keyframe  reloadgun=kyhlstr.key               local
keyframe  holsterAnim=kyhlstr.key             local

sound     mountSound=df_bry_ready.wav         local
sound     reloadSound=df_bry_ready.wav        local
sound     dismountSound=PutWeaponAway01.wav   local
sound     fireSound=pistol-1.wav              local
sound     outSound=pistout1.wav               local
sound     beep=beep2.wav                      local

template  projectile=+rocket                  local
template  CameraTpl=+Zoom_Cam                 local

thing     player                              local

flex      fireWait=1.0                        local
flex      holsterWait                         local
flex      autoAimFOV=25                       local

int       trackID=-1                          local
int       holsterTrack                        local
int       mode                                local
int       reload=0                            local
int       ammomax=4                           local
int       curammo=4                           local
int       ammobin=15                          local
int       weap=7                              local
int       guideon=0                           local
int       rocket                              local
int       OldFocus                            local
int       Dummy                               local
int       cam                                 local

vector    look                                local

message   activated
message   deactivated
message   selected
message   deselected
message   autoselect
message   fire
message   timer
message   reloadclip
message   killed
message   pulse
message   user0
message   stop_rocket

end
#==============================================================#
code
#------------------------------------------------------
fire:
	player = GetSourceRef();
	mode = GetSenderRef();
	//-------------------------------------------------------------
	// Check to see if we can fire
	// Is player still alive?
	if(GetThingHealth(player) <= 0)
	{
		Return;
	}
	if(reload > 0)	// reloading?
	{
		Return;
	}
	if(GetInv(player, ammobin) < 1.0)	// out of ammo?
	{
		curammo = 0;
		PlaySoundThing(outSound, player, 1.0, -1, -1, 0x80);
		if((GetAutoSwitch() & 1)) SelectWeapon(player, AutoselectWeapon(player, 1));
		Return;
	}
	//----------------------------------------------------------
	if(curammo <= 0 && GetInv(player, ammobin) > 0.0)	// clip is empty
	{
		reload = 1;
		PlaySoundThing(outSound, player, 1.0, -1, -1, 0x80);
		SetTimerEx(0.4, 2, 0.0, 0.0);
		Return;
	}
	if(guideon == 1)
	{
		call stop_rocket;
		return;
	}
	if(mode == 0)	// fire 1
	{
		SetFireWait(player, fireWait);
		SetPOVShake('0.0 -.003 0.0', '1.5 0.0 0.0', .05, 80.0);
		FireProjectile(player, projectile, fireSound, 8, '0.0168 0.1896 0.03', '0 0 0', 1.0, 0x30, autoAimFOV, autoAimFOV * 2);
		ChangeInv(player, ammobin, -1.0);
		jkPlayPOVKey(player, povfireAnim, 1, 0x38);
		curammo = curammo - 1;
		if(curammo <= 0 && GetInv(player, ammobin) > 0.0)	// time to reload?  - reload
		{
			reload = 1;
			PlaySoundThing(outSound, player, 1.0, -1, -1, 0x80);
			SetTimerEx(0.4, 2, 0.0, 0.0);
		}
		Return;
	}
	//-------------------------------------------------------
	else	// fire 2
	{
		SetFireWait(player, fireWait);
		SetPOVShake('0.0 -.003 0.0', '1.5 0.0 0.0', .05, 80.0);
		rocket = FireProjectile(player, projectile, fireSound, 8, '0.0168 0.1896 0.03', '0 0 0', 1.0, 0x30, autoAimFOV, autoAimFOV* 2);
		ChangeInv(player, ammobin, -1.0);
		jkPlayPOVKey(player, povfireAnim, 1, 0x38);
		curammo = curammo - 1;
		OldFocus = GetCurrentCamera();
		guideon = 1;
		setpulse(0.01);
		Return;
	}
	//-------------------------------------------------------

Return;
#------------------------------------------------------
activated:
	player = GetSourceRef();
	mode = GetSenderRef();
	jkSetWaggle(player, '0.0 0.0 0.0', 0);
	ActivateWeapon(player, fireWait, mode);

Return;
#------------------------------------------------------
deactivated:
	player = GetSourceRef();
	mode = GetSenderRef();
	jkSetWaggle(player, '10.0 7.0 0.0', 350);
	DeactivateWeapon(player, mode);

Return;
#------------------------------------------------------
selected:
	player = GetSourceRef();
	//print("MAC 10 selected");
	jkPrintUNIString(player, weap);
	// Setup the meshes and models.
	jkSetPOVModel(player, povModel);
	jkSetWeaponMesh(player, weaponMesh);
	SetArmedMode(player, 1);
	// Play mounting sound.
	PlayMode(player, 41);
	PlaySoundThing(mountSound, player, 1.0, -1.0, -1.0, 0x80);
	// Play the animation (NOLOOP + UNIQUE + ENDPAUSE).
	// The animation is held at the last frame after it is played.
	trackID = jkPlayPOVKey(player, mountAnim, 0, 0x14);
	jkSetWaggle(player, '10.0 7.0 0.0', 350);
	// Clear saber flags, and allow activation of the weapon
	jkClearFlags(player, 0x5);
	SetCurWeapon(player, weap);
	SetMountWait(player, GetKeyLen(mountAnim));

Return;
#------------------------------------------------------
deselected:
	player = GetSourceRef();
	PlaySoundThing(dismountSound, player, 1.0, -1, -1, 0x80);
	jkPlayPOVKey(player, dismountAnim, 0, 0x18);
	holsterWait = GetKeyLen(holsterAnim);
	SetMountWait(player, holsterWait);
	holsterTrack = PlayKey(player, holsterAnim, 1, 0x4);
	SetTimerEx(holsterWait, 2, 0.0, 0.0);
	if(trackID != -1)
	{
		jkStopPOVKey(player, trackID, 0);
		trackID = -1;
	}
	jkSetWaggle(player, '0.0 0.0 0.0', 0);

Return;
#------------------------------------------------------
autoselect:
	player = GetSourceRef();
	// If the player has the weapon
	if(GetInv(player, weap) != 0.0)
	{
		// If the player has ammo
		if(GetInv(player, ammobin) != 0.0)
		{
			ReturnEx(500.0);
		}
		else
		{
			ReturnEx(-1.0);
		}
	}
	else
	{
		ReturnEx(-1.0);
	}

Return;
#------------------------------------------------------
timer:
	StopKey(player, holsterTrack, 0.0);
	if(reload == 1 && GetInv(player, ammobin) > 0)
	{
		reload = 0;
		SetFireWait(player, (GetKeyLen(reloadAnim) + 0.7));
		PlaySoundThing(reloadSound, player, 1.0, -1, -1, 0x80);
		jkPlayPOVKey(player, reloadAnim, 1, 0x38);
		PlayKey(GetLocalPlayerThing(), reloadgun, 2, 32);
		if(GetInv(player, ammobin) < ammomax) curammo = GetInv(player, ammobin);
		else curammo = ammomax;
	}

Return;
#------------------------------------------------------
killed:
	player = GetLocalPlayerThing();
	if(GetSenderRef() != player) Return;
	curammo = ammomax;

Return;
#------------------------------------------------------
reloadclip:
	SetFireWait(player, (GetKeyLen(reloadAnim) + 0.7));
	PlaySoundThing(reloadSound, player, 1.0, -1, -1, 0x80);
	jkPlayPOVKey(player, reloadAnim, 1, 0x38);
	PlayKey(GetLocalPlayerThing(), reloadgun, 2, 32);
	if(GetInv(player, ammobin) < ammomax) curammo = GetInv(player, ammobin);
	else curammo = ammomax;
	reload = 0;

Return;
#------------------------------------------------------
stop_rocket:
	if(GetThingHealth(rocket) > 0) DestroyThing(rocket);
	guideon = 0;
	SetPulse(0);
	SetCameraFocus(0, player);
	if((OldFocus == 1) && (GetCurrentCamera() != 1)) CycleCamera();

Return;
#------------------------------------------------------
pulse:
	if(getthinghealth(rocket) == 0)
	{ 
		call stop_rocket;
		return;
	}
	StopThing(player);
	Dummy = FireProjectile(player, CameraTpl, -1, -1, '0 0 0', '0 0 0', 0, 0, 0, 0);
	look = GetThingLVec(Dummy);
	SetThingLook(rocket, look);
	SetThingVel(rocket, VectorScale(VectorNorm(look), 4));
	if(getthinghealth(rocket) != 0)
	{
		cam = FireProjectile(rocket, CameraTpl, -1, -1, '0 0.042 0', '0 0 0', 0, 0, 0, 0);
		SetCameraFocus(0, cam);
	}
	else
	{
		call stop_rocket;
		return;
	}

Return;
#------------------------------------------------------
user0:
	if(curammo >= ammomax)	// fire 2 with full clip
	{
		Print("The clip is full.");
		PlaySoundLocal(beep, 1.0, 0, 0x0);
		if(GetInv(player, ammobin) < ammomax) curammo = GetInv(player, ammobin);
		else curammo = ammomax;
		reload = 0;
		SetFireWait(player, 0.3);
	}
	else if(curammo < ammomax && GetInv(player, ammobin) > 0.0)	// reload the gun - reload
	{
		call reloadclip;
	}

Return;
#------------------------------------------------------
end


And here are the templates that are needed. Most are for the rocket explosions. (Don't worry about the cogs/models for the rocket; that part is not important at the moment)

Quote:
<font face="Verdana, Arial" size="2">_explosionn none orient=(0/0/0) type=explosion typeflags=0x1 damageclass=0x4
+lgflashh _explosionn thingflags=0x1 light=0.300000 timer=0.400000 typeflags=0x12 blasttime=0.200000 maxlight=1.000000
_weaponn none orient=(0/0/0) type=weapon collide=1 move=physics thingflags=0x20000000 mass=5 physflags=0x200 maxrotvel=90 damageclass=0x2 typeflags=0x1
+whitecloudd none orient=(0/0/0) type=particle timer=0.2 typeflags=0x3f material=00gsmoke.mat range=0.02 rate=128 maxthrust=30 elementsize=0.007 count=128
+dustcloudd +whitecloudd timer=0.12 material=dusty.mat range=0.015 rate=256 maxthrust=80 elementsize=0.01
+firecloudd +dustcloudd material=00explosion.mat range=0.050000 rate=64.000000 maxthrust=40.000000 count=64
+firecloudd3 +firecloudd maxthrust=20.000000 count=32
+firecloudd2 +firecloudd elementsize=0.012500
+debris_expp _explosionn timer=1.000000 sprite=debrisx.spr soundclass=exp_small.snd creatething=+firecloudd3 typeflags=0x7 blasttime=0.500000
_debriss none orient=(0/0/0) type=weapon collide=1 move=physics timer=1.1 airdrag=3.000000 physflags=0x201 vel=(0/4/0) angvel=(5/10/0) explode=+debris_expp damage=5 damageclass=0x1 typeflags=0xc
shrapnell_4 _debriss model3d=shrp_4.3do size=0.026000 movesize=0.026
shrapnell_3 _debriss model3d=shrp_3.3do size=0.028000 movesize=0.028
shrapnell_2 _debriss model3d=shrp_2.3do size=0.040000 movesize=0.04
shrapnell_1 _debriss model3d=shrp_1.3do size=0.045000 movesize=0.045
+rocket_exp _explosionn thingflags=0x1 light=0.2 timer=0.5 sprite=rldx.spr soundclass=exp_raildet.snd typeflags=0x17 damage=110 blasttime=0.7 force=300 maxlight=0.8 range=0.5 debris=shrapnell_1 debris=shrapnell_2 debris=shrapnell_3 debris=shrapnell_4
+rocket _weaponn thingflags=0x20000401 vel=(0/4/0) angvel=(0/0/90) size=0.003 movesize=0.003 typeflags=0x24020d timer=10 airdrag=0 height=0.003 physflags=0x200 explode=+rocket_exp fleshhit=+rocket_exp creatething=+lgflashh cog=class_rocket.cog model3d=rocket0.3do damage=50
+rocket2 +rocket thingflags=0x20000401 typeflags=0x24030c
+Zoom_Cam _weaponn size=0.001 movesize=0.001 maxvel=0 vel=(0/0/0) timer=0.01 thingflags=0 damageclass=0 typeflags=0xd
</font>


[changed the templates to a quote to prevent scrollage...]


------------------
I am Darth Slaw.
The Dark Lord of the Sith,
And part of the Nightfire mod team

[This message has been edited by GBK (edited January 16, 2004).]
May the mass times acceleration be with you.
2004-01-15, 4:02 PM #2
WAIT! I figured it out! I was right. It was something having to do with setting cameras to a removed object. I used a "removed" message in the rocket's class cog to reset the cam to the player's internal view before it was destroyed.
Anyway, I got it working now, so you don't need to worry about helping me out anymore.

------------------
I am Darth Slaw.
The Dark Lord of the Sith,
And part of the Nightfire mod team
May the mass times acceleration be with you.
2004-01-15, 4:37 PM #3
lol, good use of the forums guys, way to help each other out :P

------------------
nil nip nada zip zero naught lip zil
This is retarded, and I mean drooling at the mouth
2004-01-15, 7:16 PM #4
Quote:
<font face="Verdana, Arial" size="2">Originally posted by Darth Slaw:
WAIT! I figured it out! I was right. It was something having to do with setting cameras to a removed object. I used a "removed" message in the rocket's class cog to reset the cam to the player's internal view before it was destroyed.
Anyway, I got it working now, so you don't need to worry about helping me out anymore.
</font>


Its safer to put the code under Damaged:, so itll run before Removed: gets called. Trust me on this... [http://forums.massassi.net/html/wink.gif]

------------------
The future is here, and all bets are off.
And when the moment is right, I'm gonna fly a kite.
2004-01-16, 7:22 AM #5
Taking your word for it, GBK, (majority says: good idea!) I will change it to damaged instead of removed.

------------------
I am Darth Slaw.
The Dark Lord of the Sith,
And part of the Nightfire mod team
May the mass times acceleration be with you.
2004-01-16, 7:53 AM #6
Quote:
<font face="Verdana, Arial" size="2">Originally posted by Darth Slaw:
Taking your word for it, GBK, (majority says: good idea!) I will change it to damaged instead of removed.</font>


Heh.


Ill explain why, if you want to know...


Ok, say youve got a missle flying around. Youve got the camera attached to it. Now, everone knows, that if the object that the camera is attached to gets destroyed, the game goes by-by. In order to prevent this, the camera must be switched away *before* the object gets destroyed.

The problem with the obvious method, putting the code under Removed:, is that removed doesnt always get run in time. Im serious, sometimes the game will crash anyway, even with the code in place. Damaged: will always run before Removed: (unless, of course, you used DestroyThing() on the object...) so its usually safer. There are a few things you can do to make it even safer, though...

1) Put the code under both. It may seem silly, but its worth it. Better yet, make the code a subfuction and simple call it from both. Save space. [http://forums.massassi.net/html/biggrin.gif]

2) Dont attach the camera to the missile. Instead, attach it to a ghost that is itself attached to the missile. AttachThingToThing() works wonders. [http://forums.massassi.net/html/biggrin.gif] Then, under Removed: in the missle cog, switch the cam back to the player and destroy the ghost.


#2 works best. Thats what I usually ended up doing in all my piolatable missle cogs. (or any other cog that managed the camera on a destructable object, for that matter..)


Im sure there are other ways to do this, but those above should do the trick... [http://forums.massassi.net/html/wink.gif]

------------------
The future is here, and all bets are off.
And when the moment is right, I'm gonna fly a kite.
2004-01-16, 9:16 AM #7
I'll do what you said. I also have the camera template firing at the nose of the missile (0.042 JKU forward) in a pulse message (after setting the lookvector). The pulse is 0.01 and the timer of the cam is 0.01, so they are not skyrocketing the level thing count.
Also, before you last posted, I started using the damagething(rocket blah). I need it to damage the rocket so that it detonates and doesn't just disappear in midair. So I guess either way I would have ended up doing that.

Thanks for your help and insight, GBK [http://forums.massassi.net/html/smile.gif] (and I'm glad I decided not to delete the topic when I "fixed" the thing. whew!).

------------------
I am Darth Slaw.
The Dark Lord of the Sith,
And part of the Nightfire mod team

[This message has been edited by Darth Slaw (edited January 16, 2004).]
May the mass times acceleration be with you.

↑ Up to the top!