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
#42
03/02/2011 (7:25 am)
Yeah that could be a problem. We use onTickTrigger and onEnterTrigger and onLeaveTrigger. If those are not working then that will break the code.
#43
http://www.garagegames.com/community/forums/viewthread/102443
I never received a responce.
03/02/2011 (7:31 am)
This brings back memories from 2009:http://www.garagegames.com/community/forums/viewthread/102443
I never received a responce.
#44
I added in an echo for the flag change and for some reason it is not firing.
03/02/2011 (10:01 am)
Does the flag shape have to be a certain name?I added in an echo for the flag change and for some reason it is not firing.
#45
03/02/2011 (11:09 am)
No you add the NeutralFlag from the Flag category and put it in the simgroup. The code doesn't look at the name you give it but it looks at the objects in the simgroup which there should only be the trigger and one of the flags. This happens in the onTickTrigger of each trigger. onTickTrigger has to be functional in T3D for this to work.
#46
Looking at the com ref for the new t3d to see if i can figure this out. Not that good at scripting but I am trying. (why oh why could this not be vb)
03/02/2011 (11:31 am)
So this script is pretty much sunk. I read the posts but I still think that may be what is causing this script to not work. I tested in an old TGE and it worked beautifulLooking at the com ref for the new t3d to see if i can figure this out. Not that good at scripting but I am trying. (why oh why could this not be vb)
Torque Owner J L
Psycho Hamster Games
www.garagegames.com/community/forums/viewthread/118873