Game Development Community

Turrets?

by Adam · in Torque Game Engine · 02/27/2002 (8:32 am) · 71 replies

I haven't been able to find much in the way of turret info on the boards, and after a look through the engine and scripts, I see nothing related to turrets, in fact the inclusion of the turrets.cs file from Tribes 2 crashes the build. Is there any kind of core turret functionality in the engine at the moment?
Page «Previous 1 2 3 4 Last »
#1
02/27/2002 (9:11 am)
umm ..
all I found was TurretObjectType :)
and only defined in one place ..
they im afraid like forcefields doors and paths
have been savagely removed ..
.
its all good ... ive prepared a simple turret class
and as soon as the modeler makes it have seperate parts .. I can work on the rotation of the "movable body" section for aiming other than that it is
scriptable, editer addable, damagable, mountable and loads the weapons and stuff .... umm i think the model needs some work as in a LOS collision for bullets but .. that is someone else's job :)
anyhow I would be happy to make this available if your interested... it was a fairly simple addition ..
starting with completing the declaration of TurretObjectType
and adding the source files scripts and model
#2
02/27/2002 (10:44 am)
Thankyou Badguy but I think due to time constraints we're going to put if off for the moment but your offer is greatly appreciated.
#3
02/27/2002 (11:23 am)
ok if anyone else reading this is interested ..
lemme know here I will provide it
#4
02/27/2002 (11:49 am)
Hell yes. Why not :))

Turrets a go go.

Phil.
#5
02/27/2002 (11:54 am)
Also Very Interested

Thanks.
#6
02/27/2002 (12:28 pm)
ok cool ..
should I post em here?
as two source files and a script?
umm I would like to spare a model but ive only one..

again they are not quite complete as ive yet to work on the body rotations..
as well I plan to have them mount a base..
so there will be changes ..
but if you would like to work with what I have now i can post it here
#7
02/27/2002 (12:32 pm)
why not make it a resource?
#8
02/27/2002 (12:32 pm)
heh ok no waiting here ..

../engine/game/Turret.h
//
//
//
//	Filename : Turret.h

#ifndef _TURRET_H_
#define _TURRET_H_

#ifndef _SHAPEBASE_H_
#include "game/shapeBase.h"
#endif
#ifndef _BOXCONVEX_H_
#include "collision/boxConvex.h"
#endif

struct TurretData: public ShapeBaseData 
{
	typedef ShapeBaseData Parent;

	static	void		consoleInit();
	static	void		initPersistFields();
	virtual	void		packData(BitStream* stream);
	virtual void		unpackData(BitStream* stream);
	bool				preload(bool server, char errorBuffer[256]);

						DECLARE_CONOBJECT(TurretData);
						TurretData();
						~TurretData();
};

class Turret : public ShapeBase
{
protected:
private:
	typedef ShapeBase			Parent;
	TurretData	*				mDataBlock;
	SimObjectPtr<ShapeBase>		mControlObject;
public:
								DECLARE_CONOBJECT(Turret);
								
   static void					initPersistFields();
   static void					consoleInit();
								
	bool						onAdd();
	void						onRemove();
	void						processTick(const Move*);
	void						interpolateTick(F32 dt);
	void						advanceTime(F32 dt);

	bool						onNewDataBlock(GameBaseData* dptr);
	void						onCameraScopeQuery(NetConnection *cr, CameraScopeQuery *);
	bool						writePacketData(GameConnection *, BitStream *stream);
	void						readPacketData(GameConnection *, BitStream *stream);
	U32							packUpdate(NetConnection *, U32 mask, BitStream *stream);
	void						unpackUpdate(NetConnection *, BitStream *stream);
	void						enableCollision();
	void						disableCollision();
	void						mountObject(ShapeBase* obj, U32 node);
	bool						buildPolyList(AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere);
	void						buildConvex(const Box3F& box, Convex* convex);									

	void						setPosition(const Point3F& pos,const QuatF& rot);
	void						setRenderPosition(const Point3F& pos, const QuatF& rot);
	void						setTransform(const MatrixF& newMat);

	// Object control			
	void						setControllingClient(GameConnection* client);
	void						setControlObject(ShapeBase*);
	ShapeBase	*				getControlObject();
								
								Turret();
								~Turret();
};

#endif //_TURRET_H_

../engine/game/Turret.cc
//
// Built from Logic and code found in Player class
//
//	Filename : Turret.cc

#include "Turret.h"
#include "console/console.h"
#include "console/consoleTypes.h"
#include "game/gameConnection.h"
#include "core/bitStream.h"

IMPLEMENT_CO_DATABLOCK_V1(TurretData);

void TurretData::consoleInit()
{

}

void TurretData::initPersistFields()
{
   Parent::initPersistFields();
}

void TurretData::packData(BitStream* stream)
{
	Parent::packData(stream);
}

void TurretData::unpackData(BitStream* stream)
{
	Parent::unpackData(stream);
}

bool TurretData::preload(bool server, char errorBuffer[256])
{
   if (!Parent::preload(server, errorBuffer))
      return false;
	return true;
}

TurretData::TurretData()
{

}

TurretData::~TurretData()
{

}

IMPLEMENT_CO_NETOBJECT_V1(Turret);

void Turret::initPersistFields()
{
   Parent::initPersistFields();
}

void Turret::consoleInit()
{

}

bool Turret::onAdd()
{
	if(!Parent::onAdd() || !mDataBlock)
		return false;

	addToScene();
	
	if(isServerObject())
	{
		scriptOnAdd();
	}

	return true;
}

void Turret::onRemove()
{
	setControlObject(0);
	scriptOnRemove();
	removeFromScene();

	Parent::onRemove();
}

void Turret::processTick(const Move* move)
{
	Move pMove,cMove;
	if(move) 
	{
		setImageTriggerState(0,move->trigger[0]);
		setImageTriggerState(1,move->trigger[1]);
	}
	if(mControlObject)
	{
		if(!move)
			mControlObject->processTick(0);
		else 
		{
			pMove = NullMove;
			cMove = *move;
			if(isMounted())
			{
				// Filter Jump trigger if mounted
				pMove.trigger[2] = move->trigger[2];
				cMove.trigger[2] = false;
			}
			if(move->freeLook)
			{
				// Filter yaw/pitch/roll when freelooking.
				pMove.yaw = move->yaw;
				pMove.pitch = move->pitch;
				pMove.roll = move->roll;
				pMove.freeLook = true;
				cMove.freeLook = false;
				cMove.yaw = cMove.pitch = cMove.roll = 0;
			}

			mControlObject->processTick((mDamageState == Enabled)? &cMove: &NullMove);
//			mControlObject->processTick(move);
//			move = &pMove;
		}
	}

	Parent::processTick(move);
}

void Turret::interpolateTick(F32 dt)
{
	if (mControlObject)
		mControlObject->interpolateTick(dt);

	// Client side interpolation
	Parent::interpolateTick(dt);
}

void Turret::advanceTime(F32 dt)
{
	Parent::advanceTime(dt);
}

bool Turret::onNewDataBlock(GameBaseData *dptr)
{
	mDataBlock = dynamic_cast<TurretData*>(dptr);
	if (!mDataBlock || !Parent::onNewDataBlock(dptr))
		return false;

	scriptOnNewDataBlock();

	return true;
}

void Turret::onCameraScopeQuery(NetConnection *connection, CameraScopeQuery *query)
{
	if(mControlObject.isNull() || mControlObject == mMount.object)
		Parent::onCameraScopeQuery(connection, query);
	else
	{
		connection->objectInScope(this);
		if(isMounted())
			connection->objectInScope(mMount.object);
		mControlObject->onCameraScopeQuery(connection, query);
	}
}

bool Turret::writePacketData(GameConnection *con, BitStream *stream)
{
	return Parent::writePacketData(con, stream);
}

void Turret::readPacketData(GameConnection *con, BitStream *stream)
{
	Parent::readPacketData(con, stream);
}

U32  Turret::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
{
	U32 retMask = Parent::packUpdate(con, mask, stream);
//	if(mask & InitialUpdateMask) 
	if(stream->writeFlag(mask & GameBase::InitialUpdateMask))
	{
		Point3F pos;
		Point4F	rot;
		getTransform().getColumn(3,&pos);
		con->writeCompressed(stream, pos);
		getTransform().getColumn(1,&rot);
		stream->write(rot.x);
		stream->write(rot.y);
		stream->write(rot.z);
		stream->write(rot.w);
//		con->writeCompressed(stream, rot);
	}
//	Con::printf("I run Again Here dont I???");
	return retMask;
}

void Turret::unpackUpdate(NetConnection *con, BitStream *stream)
{
	Parent::unpackUpdate(con,stream);
	if(stream->readFlag()) 
	{
		Point3F pos;
		QuatF rot;
		rot.set(0,0,0, 1);
//		Con::printf("I run Again dont I???");
		con->readCompressed(stream, &pos);
		stream->read(&rot.x);
		stream->read(&rot.y);
		stream->read(&rot.z);
		stream->read(&rot.w);
		setPosition(pos, rot);
	}
}

void Turret::setControllingClient(GameConnection* client)
{
	Parent::setControllingClient(client);
	if(mControlObject)
		mControlObject->setControllingClient(client);
}

// Object control
void Turret::setControlObject(ShapeBase *obj)
{
	if(mControlObject)
	{
		mControlObject->setControllingObject(0);
		mControlObject->setControllingClient(0);
	}
	if(obj == this || obj == 0)
		mControlObject = 0;
	else
	{
		if(ShapeBase* coo = obj->getControllingObject())
			coo->setControlObject(0);
		if(GameConnection* con = obj->getControllingClient())
			con->setControlObject(0);

		mControlObject = obj;
		mControlObject->setControllingObject(this);
		mControlObject->setControllingClient(getControllingClient());
	}
}

void Turret::disableCollision()
{
	Parent::disableCollision();
	for(ShapeBase* ptr = getMountList(); ptr; ptr = ptr->getMountLink())
		ptr->disableCollision();
}

void Turret::enableCollision()
{
	Parent::enableCollision();
	for (ShapeBase* ptr = getMountList(); ptr; ptr = ptr->getMountLink())
		ptr->enableCollision();
}

void Turret::mountObject(ShapeBase* obj, U32 node)
{
	Parent::mountObject(obj, node);
//	setControlObject(obj);

	// Clear objects off the working list that are from objects mounted to us.
	//  (This applies mostly to players...)
//	for(CollisionWorkingList* itr = mConvex.getWorkingList().wLink.mNext; itr != &mConvex.getWorkingList(); itr = itr->wLink.mNext)
//	{
//		if(itr->mConvex->getObject() == obj) 
//		{
//			CollisionWorkingList* cl = itr;
//			itr = itr->wLink.mPrev;
//			cl->free();
//		}
//	}
}

void Turret::setPosition(const Point3F& pos,const QuatF& rot)
{
	MatrixF mat;
//   MatrixF mat;
//	if (isMounted()) 
//	{
//		// Use transform from mounted object
//		MatrixF nmat,zrot;
//		mMount.object->getMountTransform(mMount.node,&nmat);
//		zrot.set(EulerF(0, 0, rot.z));
//		mat.mul(nmat,zrot);
//	}
//	else 
	{
		mat.set(EulerF(0, 0, rot.z));
		mat.setColumn(3,pos);
	}
//	rot.setMatrix(&mat);
//	mat.setColumn(3,pos);
	Parent::setTransform(mat);
}

void Turret::setRenderPosition(const Point3F& pos, const QuatF& rot)
{
	MatrixF mat;
	rot.setMatrix(&mat);
	mat.setColumn(3,pos);
	Parent::setRenderTransform(mat);
}

void Turret::setTransform(const MatrixF& newMat)
{
	Parent::setTransform(newMat);
}

bool Turret::buildPolyList(AbstractPolyList* polyList, const Box3F& box, const SphereF& sphere)
{
	return Parent::buildPolyList(polyList, box, sphere);
/*	Point3F pos;
	getTransform().getColumn(3,&pos);
	IMat.setColumn(3,pos);
	polyList->setTransform(&IMat, Point3F(1,1,1));
	polyList->setObject(this);
	polyList->addBox(mObjBox);
	return true;*/
}

void Turret::buildConvex(const Box3F& box, Convex* convex)
{
	Parent::buildConvex(box, convex);
/*   if (mShapeInstance == NULL)
      return;

   // These should really come out of a pool
   mConvexList->collectGarbage();

   Box3F realBox = box;
   mWorldToObj.mul(realBox);
   realBox.min.convolveInverse(mObjScale);
   realBox.max.convolveInverse(mObjScale);

   if (realBox.isOverlapped(getObjBox()) == false)
      return;

   Convex* cc = 0;
   CollisionWorkingList& wl = convex->getWorkingList();
   for (CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext) {
      if (itr->mConvex->getType() == BoxConvexType &&
          itr->mConvex->getObject() == this) {
         cc = itr->mConvex;
         break;
      }
   }
   if (cc)
      return;

   // Create a new convex.
   BoxConvex* cp = new OrthoBoxConvex;
   mConvexList->registerObject(cp);
   convex->addToWorkingList(cp);
   cp->init(this);

   mObjBox.getCenter(&cp->mCenter);
   cp->mSize.x = mObjBox.len_x() / 2.0f;
   cp->mSize.y = mObjBox.len_y() / 2.0f;
   cp->mSize.z = mObjBox.len_z() / 2.0f;
*/
}

ShapeBase* Turret::getControlObject()
{
	return mControlObject;
}

Turret::Turret()
{
	mTypeMask |= TurretObjectType;

	mControlObject		= 0;
	mDataBlock			= 0;
}

Turret::~Turret()
{

}

../server/scripts/Turrets/Turret.cs
//
//
//
//	Filename : Turret.cs

datablock TurretData(aTurret)
{
	category						= "Turrets";
	shapeFile						= "~/data/shapes/Turrets/TurretBase.dts";
	max[RocketAmmo]					= 1000;
	mountPose[0]					= sitting;
	numMountPoints					= 2;  
	isProtectedMountPoint[0]		= true;
	cameraMaxDist					= 31;
	cameraOffset					= 2.5;
	maxEnergy						= 280;
	rechargeRate					= 0.8;
	maxDamage						= 3.15;
	destroyedLevel					= 3.15;
	inv[RocketAmmo]					= 1000;
	maxInv[RocketAmmo]				= 1000;
};

function TurretData::create(%data)
{
	// The mission editor invokes this method when it wants to create
	// an object of the given datablock type.  For the mission editor
	// we always create "static" re-spawnable rotating objects.
	%obj = new Turret() 
	{
		dataBlock = %data;
		static = true;
//		rotate = true;
	};
	%obj.mountable = true;
	
	return %obj;
}

function TurretData::onAdd(%this,%obj)
{
	%obj.setEnergyLevel(%this.MaxEnergy);
	%obj.mountImage(MissileLauncherImage, 0);
	echo("Mounted MissileLauncherImage");
}

function TurretData::onTrigger(%this, %obj, %triggerNum, %val)
{
	echo("Made it in to Trigger");
	
	messageAll('ImageData', 'Image State : %1 %2 %3', %obj.getImageState(0), %obj.getDataBlock().inv[RocketAmmo], %obj.getInventory(%this, RocketAmmo));
		
}

function TurretData::onDamage(%this, %obj, %delta)
{
	Parent::onDamage(%this, %obj);
	echo("taking damage Turret Data"); // Badguy
	%currentDamage = %obj.getDamageLevel();
	if(%currentDamage > %obj.getDataBlock().destroyedLevel)
	{
		if(%obj.getDamageState() !$= "Destroyed")
		{
			if(%obj.respawnTime !$= "")
				%obj.marker.schedule = %obj.marker.data.schedule(%obj.respawnTime, "respawn", %obj.marker); 
			%obj.setDamageState(Destroyed);
			%obj.mountable=false;
		}
	}
	else
	{
		if(%obj.getDamageState() !$= "Enabled")
		%obj.setDamageState(Enabled);
	}
}

function TurretData::onDestroyed(%data, %obj, %prevState)
{
    if(%obj.lastDamagedBy)
    {
        %destroyer = %obj.lastDamagedBy;
        game.vehicleDestroyed(%obj, %destroyer);
		messageAll('VehicleDestruction', '%1 Destroyed a %2 with a %3', %destroyer, %data.className, %obj.lastDamageType);
        //error("vehicleDestroyed( "@ %obj @", "@ %destroyer @")");
    }
    
//	radiusVehicleExplosion(%data, %obj);
   if(%obj.turretObject)
      if(%obj.turretObject.getControllingClient())
         %obj.turretObject.getDataBlock().playerDismount(%obj.turretObject);
   for(%i = 0; %i < %obj.getDatablock().numMountPoints; %i++)
   {
      if (%obj.getMountNodeObject(%i)) {
         %flingee = %obj.getMountNodeObject(%i);
         %flingee.getDataBlock().doDismount(%flingee, true);
         %xVel = 250.0 - (getRandom() * 500.0);
         %yVel = 250.0 - (getRandom() * 500.0);
         %zVel = (getRandom() * 100.0) + 50.0;
         %flingVel = %xVel @ " " @ %yVel @ " " @ %zVel;
         %flingee.applyImpulse(%flingee.getTransform(), %flingVel);
         echo("got player..." @ %flingee.getClassName());
         %flingee.damage(0, %obj.getPosition(), 0.4, $DamageType::Crash); 
      }
   }

   %data.deleteAllMounted(%obj);
   %obj.schedule(2000, "delete");
   MissionCleanup.add(%obj);
}

function TurretData::damage(%data, %targetObject, %sourceObject, %position, %amount, %damageType, %momVec, %theClient, %proj)
{
   if(%proj !$= "")
   {
		messageAll('ProjectileInfo', 'a Projectile hit!');
      if(%amount > 0 && %targetObject.lastDamageProj !$= %proj)
      {
         %targetObject.lastDamageProj = %proj;
         %targetObject.lastDamageAmount = %amount;
      }
      else if(%targetObject.lastDamageAmount < %amount)
         %amount = %amount - %targetObject.lastDamageAmount;
      else
         return;
   }

    if(%sourceObject)
    {
        %targetObject.lastDamagedBy = %sourceObject;
        %targetObject.lastDamageType = %damageType;
    }
    else 
        %targetObject.lastDamagedBy = 0;

//   if (%data.isShielded)
//    %amount = %data.checkShields(%amount, %damageType);
//   if(%amount>999)
//		%targetObject.applyDamage(%amount);	
   
   %damageScale = %data.damageScale[%damageType];
   if(%damageScale !$= "")
      %amount *= %damageScale;
	messageAll('VehicleData', 'Damage amount %1', %amount);
	if(%amount != 0)
		%targetObject.applyDamage(%amount);

   if(%targetObject.getDamageState() $= "Destroyed" )
   {
      if( %momVec !$= "")
         %targetObject.setMomentumVector(%momVec);
   }
}

lemme know if there are any difficulties ..
umm as well Please make sure to complete the forward declaration of
TurretObjectType..
which if searched for can be fullfilled like the others
as well to mount add this type to the player.cs onCollision method
#9
02/27/2002 (12:37 pm)
ok wow its huge ...
should it be a resource?
I can do that ...
.. hmm on second thought ..
I will complete it first ..
before I make it a resource as it doesnt rotate and doesnt mount on a base yet
but ive already moved into forcefields and doors..
waiting for a model to use

P.S. heh the tabbing dont look like that in my code :)
#10
04/17/2002 (11:54 am)
Sorry to resurrect this but...

Badguy, did you get this finished? I was about to start working on turrets and thought I'd check the forums first.
#11
04/17/2002 (1:31 pm)
nope :)
wasnt enuff interest ..
not to mention that the new design im playing with dont have them :)

youll find that this code here will work ok ..
needs to be worked on tho (for rotating the turret)
maybe soe other features...
#12
04/18/2002 (9:56 am)
Ok. I'll get to work on that then :)
#13
04/18/2002 (10:54 am)
mmm, what does this code do? i didnt felt like reading through the code to figure it out.. i mean.. what "features" are coded in this?
#14
04/18/2002 (11:47 am)
hmm, so this is a non rotating turret?

Might be useful in some contexts, I'll have a look at this as well when i get home.
#15
04/18/2002 (5:05 pm)
it is simple right now..

mountable, firing, camera lookat.

that is pretty much it
needs the logic if the user wants to rotate yes ..
but after that im sure its close to done what a turret would do
#16
05/01/2002 (7:18 pm)
I currently working on a tank game.Can the turret be mount on a tank
#17
05/20/2002 (8:49 am)
would a mountable gun that the player holds be counted as a turret or would it be so different it needed totally recoding?
#18
06/04/2002 (12:34 pm)
badguy any more work on turrets yet?
#19
06/04/2002 (3:30 pm)
Hey badguy
Any progress on this? I know I am chasing a player operable turret for my game.
#20
06/07/2002 (4:00 pm)
This is some really neat code. I'd like to see it completed to the point where it became a resource.
Page «Previous 1 2 3 4 Last »