Code Search for Developers
 
 
  

ComsTargetStateMessage.cpp from Scorched 3D at Krugle


Show ComsTargetStateMessage.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 <client/ScorchedClient.h>
#include <server/ScorchedServer.h>
#include <coms/ComsTargetStateMessage.h>
#include <tank/TankTeamScore.h>
#include <tank/TankContainer.h>
#include <target/TargetState.h>
#include <tankai/TankAIAdder.h>
#include <common/Logger.h>
#include <movement/TargetMovement.h>
#include <set>

ComsTargetStateMessage::ComsTargetStateMessage(bool fullmessage) : 
	ComsMessage("ComsTargetStateMessage"),
	fullmessage_(fullmessage)
{

}

ComsTargetStateMessage::~ComsTargetStateMessage()
{

}

bool ComsTargetStateMessage::writeMessage(NetBuffer &buffer)
{
	std::map<unsigned int, Target *> targets;
	{
		std::map<unsigned int, Target *> &possibletargets =
			ScorchedServer::instance()->getTargetContainer().getTargets();
		std::map<unsigned int, Target *>::iterator itor;

		for (itor = possibletargets.begin();
			itor != possibletargets.end();
			itor++)
		{
			Target *target = (*itor).second;
			if (target->isTarget())
			{
				// If its not a full message only send the state of targets
				// than can move
				if (fullmessage_ || target->getTargetState().getMovement())
				{
					targets[(*itor).first] = (*itor).second;
				}
			}
		}
	}

	// Add full message ident
	buffer.addToBuffer(fullmessage_);

	// Add count
	buffer.addToBuffer((int) targets.size());

	// For each target
	std::map<unsigned int, Target *>::iterator itor;
	for (itor = targets.begin();
		itor != targets.end();
		itor++)
	{
		// Add each tank
		Target *target = (*itor).second;
		buffer.addToBuffer(target->getPlayerId());

		if (!target->writeMessage(buffer)) return false;
	}

	// Add all of the movement information
	if (!ScorchedServer::instance()->getTargetMovement().writeMessage(buffer)) return false;
	return true;
}

bool ComsTargetStateMessage::readMessage(NetBufferReader &reader)
{
#ifndef S3D_SERVER
	// Update all targets with the state from the targets on the
	// server
	std::set<unsigned int> updatedTargets;
	if (!reader.getFromBuffer(fullmessage_)) return false;
	int count = 0;
	if (!reader.getFromBuffer(count)) return false;
	for (int i=0; i<count; i++)
	{
		unsigned int playerId;
		if (!reader.getFromBuffer(playerId)) return false;
		Target *target = ScorchedClient::instance()->getTargetContainer().
			getTargetById(playerId);
		if (target)
		{
			if (!target->readMessage(reader)) return false;
			updatedTargets.insert(target->getPlayerId());
		}
		else
		{
			if (playerId >= TankAIAdder::MIN_TARGET_ID)
			{
				target = new Target(
					playerId, "", 
					ScorchedClient::instance()->getContext());
				ScorchedClient::instance()->getTargetContainer().
					addTarget(target);

				if (!target->readMessage(reader)) return false;
				updatedTargets.insert(target->getPlayerId());
			}
			else
			{
				std::string name;
				reader.getFromBuffer(name);
				Logger::log(formatString("Error: Failed to find target %u\"%s\"",
					playerId, name.c_str()));
				return false;
			}
		}
	}

	if (fullmessage_)
	{
		// Remove any targets the client has but the server does not
		std::map<unsigned int, Target *> targets = // Note copy
			ScorchedClient::instance()->getTargetContainer().getTargets();
		std::map<unsigned int, Target *>::iterator itor;
		for (itor = targets.begin();
			itor != targets.end();
			itor++)
		{
			unsigned int playerId = (*itor).first;
			Target *target = (*itor).second;
			if (target->isTarget())
			{
				if (updatedTargets.find(playerId) == updatedTargets.end())
				{
					Target *removedTarget = 
						ScorchedClient::instance()->getTargetContainer().removeTarget(playerId);
					delete removedTarget;
				}
			}
		}
	}

	// Get all of the movement information
	if (!ScorchedClient::instance()->getTargetMovement().readMessage(reader)) return false;
#endif
	
	return true;
}




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

  ComsAddPlayerMessage.cpp
  ComsAddPlayerMessage.h
  ComsAdminMessage.cpp
  ComsAdminMessage.h
  ComsBuyAccessoryMessage.cpp
  ComsBuyAccessoryMessage.h
  ComsChannelMessage.cpp
  ComsChannelMessage.h
  ComsChannelTextMessage.cpp
  ComsChannelTextMessage.h
  ComsConnectAcceptMessage.cpp
  ComsConnectAcceptMessage.h
  ComsConnectMessage.cpp
  ComsConnectMessage.h
  ComsConnectRejectMessage.cpp
  ComsConnectRejectMessage.h
  ComsDefenseMessage.cpp
  ComsDefenseMessage.h
  ComsFileAkMessage.cpp
  ComsFileAkMessage.h
  ComsFileMessage.cpp
  ComsFileMessage.h
  ComsGameStateMessage.cpp
  ComsGameStateMessage.h
  ComsGameStoppedMessage.cpp
  ComsGameStoppedMessage.h
  ComsGiftMoneyMessage.cpp
  ComsGiftMoneyMessage.h
  ComsHaveModFilesMessage.cpp
  ComsHaveModFilesMessage.h
  ComsHeightMapMessage.cpp
  ComsHeightMapMessage.h
  ComsInitializeMessage.cpp
  ComsInitializeMessage.h
  ComsKeepAliveMessage.cpp
  ComsKeepAliveMessage.h
  ComsLevelMessage.cpp
  ComsLevelMessage.h
  ComsLinesMessage.cpp
  ComsLinesMessage.h
  ComsMessage.cpp
  ComsMessage.h
  ComsMessageHandler.cpp
  ComsMessageHandler.h
  ComsMessageSender.cpp
  ComsMessageSender.h
  ComsNewGameMessage.cpp
  ComsNewGameMessage.h
  ComsPlayMovesMessage.cpp
  ComsPlayMovesMessage.h
  ComsPlayedMoveMessage.cpp
  ComsPlayedMoveMessage.h
  ComsPlayerAimMessage.cpp
  ComsPlayerAimMessage.h
  ComsPlayerReadyMessage.cpp
  ComsPlayerReadyMessage.h
  ComsPlayerStateMessage.cpp
  ComsPlayerStateMessage.h
  ComsPlayerStatusMessage.cpp
  ComsPlayerStatusMessage.h
  ComsRmPlayerMessage.cpp
  ComsRmPlayerMessage.h
  ComsScoreMessage.cpp
  ComsScoreMessage.h
  ComsStartGameMessage.cpp
  ComsStartGameMessage.h
  ComsSyncCheckMessage.cpp
  ComsSyncCheckMessage.h
  ComsTargetStateMessage.cpp
  ComsTargetStateMessage.h
  ComsTimerStartMessage.cpp
  ComsTimerStartMessage.h