Code Search for Developers
 
 
  

ShotState.cpp from Scorched 3D at Krugle


Show ShotState.cpp syntax highlighted

////////////////////////////////////////////////////////////////////////////////
//    Scorched3D (c) 2000-2003
//
//    This file is part of Scorched3D.
//
//    Scorched3D is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    Scorched3D is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Scorched3D; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////

#include <engine/ShotState.h>
#include <engine/ActionController.h>
#include <actions/Resurrection.h>
#include <placement/PlacementTankPosition.h>
#include <coms/ComsMessageSender.h>
#include <coms/ComsPlayerStateMessage.h>
#include <common/Logger.h>
#include <common/RandomGenerator.h>
#include <common/OptionsScorched.h>
#include <common/OptionsTransient.h>
#include <tank/TankState.h>
#include <tank/TankScore.h>
#include <tank/TankAccessories.h>
#include <tank/TankPosition.h>
#include <target/TargetLife.h>

ShotState::ShotState(ScorchedContext &context,
	PlayShots &playShots) :
	context_(context), playShots_(playShots),
	firstTime_(true)
{
}

ShotState::~ShotState()
{
}

void ShotState::enterState(const unsigned state)
{
	// Set all player kills this turn to 0 (used for multikill)
	{
		std::map<unsigned int, Tank *> &tanks = 
			context_.tankContainer->getPlayingTanks();
		std::map<unsigned int, Tank *>::iterator itor;
		for (itor = tanks.begin();
			itor != tanks.end();
			itor++)
		{
			Tank *tank = (*itor).second;
			tank->getScore().setTurnKills(0);
		}
	}

	// Reset the counts in the action controller
	context_.actionController->resetTime();
	context_.actionController->clear();

	// Add all shots that should be run at the start of the round
	// to the action controller
	playShots_.playShots(context_, true);

	// Reset the amount of time taken
	firstTime_ = true;
	lastTime_ = false;

	// Add all of the new events
	context_.actionController->getEvents().initialize(context_);
}

bool ShotState::acceptStateChange(const unsigned state, 
		const unsigned nextState,
		float frameTime)
{
	if (!context_.actionController->noReferencedActions() ||
		firstTime_)
	{
		// The action controller will now have shots to simulate
		// We continue simulation until there are no actions left
		// in the action controller
		firstTime_ = false;
	}
	else
	{
		if (!lastTime_)
		{
			lastTime_ = true;

			// Add all the shots that should be run at the end of the round
			// to the action controller
			playShots_.playShots(context_, false);
			
			// Resurect any tanks that have more lives
			resurectTanks();
		}
		else
		{
			// We have finished all shots
			//Logger::log(formatString(
			//	"Finished playing Shots"));
			context_.actionController->getEvents().clear();
			context_.actionController->logProfiledActions();

			return true;
		}
	}
	return false;
}

void ShotState::resurectTanks()
{
	std::map<unsigned int, Tank *> &tanks =
		context_.tankContainer->getPlayingTanks();
	std::map<unsigned int, Tank *>::iterator itor;
	for (itor = tanks.begin();
		itor != tanks.end();
		itor++)
	{
		Tank *tank = (*itor).second;

		// Check for any dead tanks that can be rezed
		if (!tank->getState().getSpectator() &&
			tank->getState().getState() == TankState::sDead &&
			(tank->getState().getLives() > 0 ||
			tank->getState().getMaxLives() == 0))
		{
			Vector tankPos = PlacementTankPosition::placeTank(
				tank->getPlayerId(), tank->getTeam(),
				context_,
				context_.actionController->getRandom());

			Resurrection *rez = new Resurrection(
				tank->getPlayerId(), tankPos);
			context_.actionController->addAction(rez);
		}
	}	
}




See more files for this project here

Scorched 3D

Scorched3D is a 3D remake of the popular 2D artillery game Scorched Earth.\r\nScorched3D can be played against the computer, other players and remotely across the internet or LAN.

Project homepage: http://sourceforge.net/projects/scorched3d
Programming language(s): C,C++,XML
License: gpl2

  Action.cpp
  Action.h
  ActionController.cpp
  ActionController.h
  ActionReferenced.cpp
  ActionReferenced.h
  EventContainer.cpp
  EventContainer.h
  GameState.cpp
  GameState.h
  GameStateI.cpp
  GameStateI.h
  GameStateStimulusI.cpp
  GameStateStimulusI.h
  MainLoop.cpp
  MainLoop.h
  MainLoopI.cpp
  MainLoopI.h
  MetaClass.cpp
  MetaClass.h
  ModDirs.cpp
  ModDirs.h
  ModFileEntry.cpp
  ModFileEntry.h
  ModFiles.cpp
  ModFiles.h
  ModInfo.cpp
  ModInfo.h
  PhysicsParticle.cpp
  PhysicsParticle.h
  PhysicsParticleObject.cpp
  PhysicsParticleObject.h
  PlayShots.cpp
  PlayShots.h
  ScorchedCollisionIds.cpp
  ScorchedCollisionIds.h
  ScorchedContext.cpp
  ScorchedContext.h
  ShotState.cpp
  ShotState.h
  SyncCheck.cpp
  SyncCheck.h
  ViewPoints.cpp
  ViewPoints.h