BF2 Style Capture the Flag II
by mb · 03/04/2007 (12:32 pm) · 46 comments
Download Code File
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
// Area Trigger
// Author: Michael
// Place in server/scripts directory
// Add a simset in world creator
// Add a trigger, and place it in the sim set
// Add a model object and place it in that simset also.
// I used sphere.dts as the neutral flag to start with.
//-----------------------------------------------------------------------------
// Flag Datablocks
//-----------------------------------------------------------------------------
// Team 1 Flag
datablock StaticShapeData( TeamOneFlag )
{
category = "Flags";
shapeFile = "~/data/shapes/flag/flag1.dts";
};
// Team 2 Flag
datablock StaticShapeData( TeamTwoFlag )
{
category = "Flags";
shapeFile = "~/data/shapes/flag/flag2.dts";
};
// Neutral Flag
datablock StaticShapeData( NeutralFlag )
{
category = "Flags";
shapeFile = "~/data/shapes/flag/sphere.dts";
};
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// This is the trigger
//-----------------------------------------------------------------------------
datablock TriggerData(AreaTrigger)
{
tickPeriodMS = 50;
team1InZone = 0; // total players in zone
team2InZone = 0; // total players in zone
team1Flag = 0; // Team 1 flag count 0-100
team2Flag = 0; // Team 2 flag count 0-100
flagModel = 0; // Tracks what flag is up or down ( 0=Neutral, 1=Team 1, 2=Team 2 )
};
//-----------------------------------------------------------------------------
function AreaTrigger::onEnterTrigger(%this,%trigger,%obj)
{
// Make sure this is a client (vehicles will stop a capture)
//echo ("Datablock ID ::: " @ %obj);
//echo ("Datablock ::: " @ %obj.dataBlock);
//echo ("obj.getState ::: " @ %obj.getState());
if ( %obj.getState() $= "Dead" )
{
return;
}
// If both teams are in the zone do nothing.
if ( ((%this.team1InZone >=1) && (%this.team2InZone >=1)) )
{
return;
//echo( "BOTH TEAMS ARE IN TRIGGER - DO NOTHING!!!");
}
else
{
if (%obj.client.team.teamID == 1)// team 1 entered zone
{
//echo( "TEAM 1 entered the trigger");
%trigger.team1InZone++;
}
if (%obj.client.team.teamID == 2)// team 2 entered zone
{
//echo( "TEAM 2 entered the trigger");
%trigger.team2InZone++;
}
}
Parent::onEnterTrigger(%this,%trigger,%obj);
}
function AreaTrigger::onLeaveTrigger(%this,%trigger,%obj)
{
if (%obj.client.team.teamID == 1) // team 1 left zone
{
%trigger.team1InZone--; // decrement team1InZone
if ( %trigger.team1InZone <= 0) // dont ever go below zero
{
%trigger.team1InZone = 0;
}
//echo( "TEAM 1 left the trigger:" @ %trigger.team1InZone);
}
if (%obj.client.team.teamID == 2)// team 2 left zone
{
%trigger.team2InZone--;
if ( %trigger.team2InZone <= 0) // dont ever go below zero
{
%trigger.team2InZone = 0;
}
//echo( "TEAM 2 left the trigger" @%trigger.team2InZone);
}
Parent::onLeaveTrigger(%this,%trigger,%obj);
}
function AreaTrigger::onTickTrigger(%this,%trigger)
{
//-----------------------------------------------------------------------
// onTickTrigger
//
// 1. If both teams are in the zone do nothing.
// 2. Loop thru all the objects in the trigger.
// 3. Set %obj to be the current object that is being used in the for loop.
// 4. Check if the %obj is a client.
// 5. If it is not a client then do nothing.
// 6. If the %obj is a client then check if its on Team 1.
// 7. Check if the Team 2 Flag is up. If it is up bring it down (varys on how many of that team is in the zone. (Do this until the flag is down)
// 8. Dont let the flag variable go below zero.
// 9. Check if the Team 1 Flag is up. If it is not up, then bring it up 1. (Do this until the flag is up)
// 10. 10. Dont let the flag variable go above 100.
// 11. Do steps 6,7,8,9,10 but this time for Team 2. Lower team 1 flag, Raise team 2 flag.
//echo( "onTickTrigger");
//$center = %trigger.getWorldBoxCenter(); // gets the center of the trigger
// 1. If both teams are in the zone do nothing.
if ( ((%this.team1InZone >=1) && (%this.team2InZone >=1)) )
{
echo( "Both teams in zone, do nothing.");
return;
}
else
{
// 2.loop thru all the objects in the trigger
for(%x=0; %x < %trigger.getNumObjects(); %x++)
{
// 3. Set %obj to be the current object that is being used in the for loop.
%obj = %trigger.getObject(%x);
if (%obj.getState() $= "Dead")
{
return;
}
else
{
// 6. If the %obj is a client then check if its on Team 1.
if (%obj.client.team.teamID == 1) // team 1 entered zone
{
// 7. Check if the Team 2 Flag is up. If it is up bring it down 1. (Do this until the flag is down)
if (%trigger.team2Flag != 0)
{
%trigger.team2Flag = ( %trigger.team2Flag - %trigger.team1InZone ); // If team 2 flag is not 0 then bring it down
// 8. Dont let the flag variable go below zero.
if ( (%trigger.team2Flag <= 0) || (%trigger.flagModel == 0) )
{
%trigger.team2Flag = 0;
//%trigger.flagModel = 1; // Set the flag model to team 1
//echo( "FLAG CHANGED TO: " @ %trigger.flagModel);
//%obj.client.team.score++; // Add a point to this team
}
// print message to client
%message = "team2Flag: " @ %trigger.team2Flag;
%time = 1;
%lines = 1;
centerPrint( %obj.client, %message, %time, %lines );
//echo( "team2Flag: " @ %trigger.team2Flag);
}
else
{
// 9. Check if the Team 1 Flag is up. If it is not up, then bring it up 1. (Do this until the flag is up)
if (%trigger.team1Flag < 100) // if team 1 flag is not 100 then bring it up 1
{
%trigger.team1Flag = ( %trigger.team1Flag + %trigger.team1InZone );
// 10. Dont let the flag variable go above 100.
if ( %trigger.team1Flag >= 100)
{
%trigger.team1Flag = 100; // dont ever go above 100
%obj.client.team.score++; // Add a point to this team
commandToClient(%obj.client, 'SetScoreCounter', %obj.client.team.score);
$TeamOne.capturedPoints++;
$TeamTwo.capturedPoints--;
messageAll('MsgClientScoreChanged', "", $TeamOne.capturedPoints, $TeamTwo.capturedPoints);
commandToClient(%obj.client, 'SetCapCounter', %obj.client.team.capturedPoints);
echo( "Team capturedPoints: " @ $TeamOne.capturedPoints );
}
// print message to client
%message = "team1Flag: " @ %trigger.team1Flag;
%time = 1;
%lines = 1;
centerPrint( %obj.client, %message, %time, %lines );
//echo( "team1Flag: " @ %trigger.team1Flag);
}
}
}
// 11. Repeat for team 2
if (%obj.client.team.teamID == 2) // team 2 entered zone
{
if (%trigger.team1Flag != 0) //if team 1 flag is still up, bring it down 1
{
%trigger.team1Flag = ( %trigger.team1Flag - %trigger.team2InZone );
if ( (%trigger.team1Flag <= 0) || (%trigger.flagModel == 0) ) // dont ever go below zero
{
%trigger.team1Flag = 0;
//echo( "FLAG CHANGED TO: " @ %trigger.flagModel);
//%obj.client.team.score++; // Add a point to this team
}
// print message to client
%message = "team1Flag: " @ %trigger.team1Flag;
%time = 1;
%lines = 1;
centerPrint( %obj.client, %message, %time, %lines );
//echo( "team1Flag: " @ %trigger.team1Flag);
}
else
{
if (%trigger.team2Flag < 100) // if team 2 flag is not 100 then bring it up 1
{
%trigger.team2Flag = ( %trigger.team2Flag + %trigger.team2InZone );
if ( %trigger.team2Flag >= 100) // dont ever go above 100
{
%trigger.team2Flag = 100;
%obj.client.team.score++; // Add a point to this team
commandToClient(%obj.client, 'SetScoreCounter', %obj.client.team.score);
$TeamTwo.capturedPoints++;
$TeamOne.capturedPoints--;
messageAll('MsgClientScoreChanged', "", $TeamOne.capturedPoints, $TeamTwo.capturedPoints);
commandToClient(%obj.client, 'SetCapCounter', %obj.client.team.capturedPoints);
echo( "Team capturedPoints: " @ $TeamTwo.capturedPoints );
}
// print message to client
%message = "team2Flag: " @ %trigger.team2Flag;
%time = 2;
%lines = 1;
centerPrint( %obj.client, %message, %time, %lines );
//echo( "team2Flag: " @ %trigger.team2Flag);
}
}
}
}
}
}
Parent::onTickTrigger(%this,%trigger);
}
// Simset object triggers
function ShapeBase::onTrigger( %this , %triggerID, %triggerStatus)
{
if( %triggerStatus )
{
//test variables
//echo( "Something entered the trigger I am associated with: " @ %this );
//echo( "%this: " @ %this );
//echo( "%triggerID: " @ %triggerID );
//echo( "%triggerStatus: " @ %triggerStatus );
}
else
{
//echo( "onTrigger:: Something left the trigger I am associated with: " @ %this );
// pause anim
//%this.pauseThread( 0 );
}
}
function ShapeBase::onTriggerTick( %this , %triggerID )
{
//echo( "Something is still in the trigger I am associated with: " @ %this );
// loop thru all the objects in the trigger
for(%i=0; %i < %triggerID.getNumObjects(); %i++)
{
%obj = %triggerID.getObject(%i); // Set %obj to be the current object that is being used in the for loop.
// Check if the %obj is a client.
if (! %obj.client)
{
//return; // If it is not a client then do nothing.
}
else
{
//-----------------------------------------------------
// TEAM 1
//-----------------------------------------------------
//
// Put code here to check teams, and raise the flag
//
//-----------------------------------------------------
if (%obj.client.team.teamID == 1) // if team 1 entered zone
{
if ($TeamOne != %obj.client.team)// Set up a global variable for team1
{
$TeamOne = %obj.client.team;
}
//echo (" ****** " @ %obj.client.team.teamID );
//echo ("****************** %this.getGroup() : " @ %this.getGroup() );
if ( %triggerID.team1Flag == 2 ) // Team 2 Flag is up so play anim in reverse
{
}
// Change Flags
if ( (%triggerID.team2Flag == 0) && (%triggerID.flagModel != 1))
{
%id = %this.getID();
%group = %this.getGroup(); // get sim group
%pos = %this.getPosition(); // get position of 'old' flag
%this.startFade(1000,0,true); // fade old flag out
%this.schedule(1000, setHidden ,true); // hide old flag
%flag = new StaticShape() // spawn a new flag at the old flags position
{
dataBlock = TeamOneFlag;
position = %pos;
};
%group.add(%flag); // add new flag to simGroup/simSet
%group.remove(%id); // remove old flag from simGroup/simSet
%triggerID.flagModel = 1; // set variable flagModel to team 1
}
else
{
if ((%triggerID.flagModel == 1) && (%triggerID.team1Flag < 100)) // if team1 flag is visible, and is not up all the way
{
// run anim
//%this.setThreadDir( 0, true );
//%this.playThread(0, "run");
}
else
{
//%this.stopThread( 0 );
}
}
}
//-----------------------------------------------------
// TEAM 2
//-----------------------------------------------------
//
// Put code here to check teams, and raise the flag
//
//-----------------------------------------------------
if (%obj.client.team.teamID == 2) // if team 2 entered zone
{
if ($TeamTwo != %obj.client.team)// Set up a global variable for team2
{
$TeamTwo = %obj.client.team;
}
//echo (" ****** " @ %obj.client.team.teamID );
if ( %triggerID.team1Flag == 1 ) // Team 1 Flag is up so play anim in reverse
{
}
if ( (%triggerID.team1Flag == 0) && (%triggerID.flagModel != 2))
{
%id = %this.getID();
%group = %this.getGroup(); // get sim group
%pos = %this.getPosition(); // get position of 'old' flag
%this.startFade(1000,0,true); // fade old flag out
%this.schedule(1000, setHidden ,true); // hide old flag
%flag = new StaticShape() // spawn a new flag at the old flags position
{
dataBlock = TeamTwoFlag;
position = %pos;
};
%group.add(%flag); // add new flag to simGroup/simSet
%group.remove(%id); // remove old flag from simGroup/simSet
%triggerID.flagModel = 2; // set variable flagModel to team 1
}
else
{
if ((%triggerID.flagModel == 2) && (%triggerID.team2Flag < 100)) // if team2 flag is visible, and is not up all the way
{
// run anim
//%this.setThreadDir( 0, true );
//%this.playThread(0, "run");
}
else
{
//%this.stopThread( 0 );
}
}
}
}
}
}
function SimGroup::onTrigger(%this, %triggerId, %triggerStatus )
{
for ( %count = 0 ; %count < %this.getCount() ; %count++ )
{
%this.getObject( %count ).onTrigger( %triggerId , %triggerStatus );
}
}
function SimGroup::onTriggerTick(%this, %triggerId)
{
for ( %count = 0 ; %count < %this.getCount() ; %count ++ )
{
%this.getObject( %count ).onTriggerTick( %triggerId );
}
}About the author
#2
03/04/2007 (4:34 pm)
[.code] blocks would be nice [/.code]
#3
To add teams try the "team implementation" resource found here: www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2312
@chris
oops sorry forgot the code blocks :)
btw: there's a lot of echos, that I used to debug, left in the code. I left it in so if anyone has problems they can un-comment them.
03/05/2007 (7:14 am)
@ jace To add teams try the "team implementation" resource found here: www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2312
@chris
oops sorry forgot the code blocks :)
btw: there's a lot of echos, that I used to debug, left in the code. I left it in so if anyone has problems they can un-comment them.
#4
03/05/2007 (4:07 pm)
Thanks for this resource. Glad you left the echo in too, very helpful for learning:)
#5
03/06/2007 (7:10 am)
no prob and thanks! And thanks to Edward Maurina II for showing me how to do the simset stuff :)
#6
06/14/2007 (7:58 am)
Whats a simset? Where are the instructions? I have the teams implemented, but i dont know how to implement this...
#7
Name it whatever you want. You will see it in the mission list as a folder. Add the trigger and put it in the simset folder. Add the sphere object to the same simset. Make sure you size the trigger to whatever size you want... thats about it.
Do the same for any other capture areas you want to add.
06/14/2007 (4:01 pm)
In the World Editor hit f4, go to mission objects->system->simsetName it whatever you want. You will see it in the mission list as a folder. Add the trigger and put it in the simset folder. Add the sphere object to the same simset. Make sure you size the trigger to whatever size you want... thats about it.
Do the same for any other capture areas you want to add.
#8
06/15/2007 (2:38 am)
I have a mission objects->system->simgroup, is that the same thing? There is no mission objects->system->simset in my editor though.
#9
06/15/2007 (7:03 am)
Yes thats it.
#10
My game is moving along nicely thanks to you and all the other Torquers out there.
Q
11/26/2007 (2:49 am)
Awesome resource ... I just finished implementing the Team resource and then put this one in. :)My game is moving along nicely thanks to you and all the other Torquers out there.
Q
#11
Can you give me an example of how yours is included in your mission file? I think that is where my problem may be.
When I echo out the current flagModel I always get nothing.
Do I have to add the fields to my AreaTrigger?
It is probably something stupid that I have done. :(
Thanks
11/26/2007 (7:40 am)
Sorry me again ... I was testing this and for some unknown reason my flag is never changed.Can you give me an example of how yours is included in your mission file? I think that is where my problem may be.
When I echo out the current flagModel I always get nothing.
Do I have to add the fields to my AreaTrigger?
It is probably something stupid that I have done. :(
Thanks
#12
2. Add the trigger and place it in the sim set you created.
3. Add a model object and place it in the same simset. I named it sphere.dts as the neutral flag to start with. The flags are defined in the trigger near the top.
11/26/2007 (9:02 am)
1. Add a simset in world creator2. Add the trigger and place it in the sim set you created.
3. Add a model object and place it in the same simset. I named it sphere.dts as the neutral flag to start with. The flags are defined in the trigger near the top.
datablock StaticShapeData( NeutralFlag )
{
category = "Flags";
shapeFile = "~/data/shapes/flag/sphere.dts";
};
#13
Thanks for the response. I have done this ... here is my code.
In the mission file:
In the AreaTrigger.cs file:
My flags are the Demo ones for now that I have colour coded, Red, Blue and Green respectively. They show correctly in Torque Show Tool.
Basically when I approach the trigger, it presents me the counter and goes through the process ... but the marker is never changed. I will attempt to run the code with the spher model instead as the Neutral flag, as this might be my problem.
Thanks again for the response.
11/26/2007 (7:40 pm)
Hi,Thanks for the response. I have done this ... here is my code.
In the mission file:
new SimGroup(ControlPoint_1) {
canSaveDynamicFields = "1";
new TSStatic(FlagPole_1) {
dataBlock = "NeutralFlag";
canSaveDynamicFields = "1";
position = "350.58 299.971 217.691";
rotation = "1 0 0 0";
scale = "1 1 1";
shapeName = "~/data/shapes/markers/neutral/flagpole.dts";
receiveSunLight = "1";
receiveLMLighting = "1";
useAdaptiveSelfIllumination = "0";
useCustomAmbientLighting = "0";
customAmbientSelfIllumination = "0";
customAmbientLighting = "0 0 0 1";
useLightingOcclusion = "1";
dataBlock = "NeutralFlag";
};
new Trigger(Trigger_1) {
canSaveDynamicFields = "1";
position = "350.242 300.407 217.422";
rotation = "-1 0 0 4.01071";
scale = "1 1 1";
dataBlock = "AreaTrigger";
polyhedron = "0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 -1.0000000 0.0000000 0.0000000 0.0000000 1.0000000";
tickPeriodMS = 50;
team1InZone = 0;
team2InZone = 0;
team1Flag = 0;
team2Flag = 0;
flagModel = 0;
};
};In the AreaTrigger.cs file:
// Team 1 Flag
datablock StaticShapeData( TeamOneFlag )
{
category = "Flags";
shapeFile = "~/data/shapes/markers/red/flagpole.dts";
};
// Team 2 Flag
datablock StaticShapeData( TeamTwoFlag )
{
category = "Flags";
shapeFile = "~/data/shapes/markers/blue/flagpole.dts";
};
// Neutral Flag
datablock StaticShapeData( NeutralFlag )
{
category = "Flags";
shapeFile = "~/data/shapes/markers/neutral/flagpole.dts";
};My flags are the Demo ones for now that I have colour coded, Red, Blue and Green respectively. They show correctly in Torque Show Tool.
Basically when I approach the trigger, it presents me the counter and goes through the process ... but the marker is never changed. I will attempt to run the code with the spher model instead as the Neutral flag, as this might be my problem.
Thanks again for the response.
#14
Check your paths to the objects and check the console for errors.
12/04/2007 (10:20 pm)
Quinton, I can't see anything wrong with what you have. Here's what one of mine looks like:new SimGroup(RedBaseTrigger) {
canSaveDynamicFields = "1";
new Trigger(Trigger) {
canSaveDynamicFields = "1";
position = "120.564 351.487 52.5101";
rotation = "1 0 0 0";
scale = "44.6512 46.7642 20.5854";
dataBlock = "AreaTrigger";
polyhedron = "0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 -1.0000000 0.0000000 0.0000000 0.0000000 1.0000000";
};
new StaticShape(flag) {
canSaveDynamicFields = "1";
position = "146.14 322.519 52.5101";
rotation = "1 0 0 0";
scale = "1 1 1";
dataBlock = "NeutralFlag";
receiveSunLight = "1";
receiveLMLighting = "1";
useAdaptiveSelfIllumination = "0";
useCustomAmbientLighting = "0";
customAmbientSelfIllumination = "0";
customAmbientLighting = "0 0 0 1";
};
};Check your paths to the objects and check the console for errors.
#15
12/04/2007 (10:22 pm)
Oh btw mine are using StaticShape and yours is TSStatic. That could be your problem. edit < That IS the problem... you defined them as StaticShape but in the mission file you added them as TSStatic... I don't think that will work>
#16
Thanks man. Much appreciated ... this is what happens when you base you game off other code ... I have since started from scratch and added the aspects of the game I want a piece at a time which has made the progress slower but I have managed to sort out little problems like this. :)
Thanks again, your code works extremely well ... wooohooooo
12/05/2007 (4:27 am)
Ah cool.Thanks man. Much appreciated ... this is what happens when you base you game off other code ... I have since started from scratch and added the aspects of the game I want a piece at a time which has made the progress slower but I have managed to sort out little problems like this. :)
Thanks again, your code works extremely well ... wooohooooo
#17
12/05/2007 (6:30 am)
Good to hear! Keep me updated on your progress. :D
#18
04/04/2009 (2:56 pm)
Can anyone confirm this works with 1.5 or not ?
#19
Not sure what's causing that right now. Also would I be able to have multiple flags in one mission?
04/17/2009 (6:50 pm)
I have got this working in TGEA 1.8.1 but I''m getting this in my console.clientCmdSetScoreCounter: Unknown command. clientCmdSetCapCounter: Unknown command.
Not sure what's causing that right now. Also would I be able to have multiple flags in one mission?
#20
does anyone knows a script for the bf ticket scoring system? i found nothing at the searchfunction.
08/07/2009 (3:47 am)
i've got the same problem at t3d.does anyone knows a script for the bf ticket scoring system? i found nothing at the searchfunction.
Torque 3D Owner Jace
-Jace