bool CreatureObject::unpilotShip()
{
	//-- Only do this on the authoritative pilot.
	if (!isAuthoritative())
	{
		DEBUG_REPORT_LOG(true, ("CreatureObject::unpilotShip(): server id=[%d], ship id=[%s], skipping call because ship is not authoritative.",
			static_cast<int>(GameServer::getInstance().getProcessId()),
			getNetworkId().getValueString().c_str()));
		return false;
	}

	//-- Ensure ships are enabled.
	if (!ConfigServerGame::getShipsEnabled())
		return false;

	bool unpilotedShip = false;

	ShipObject* ship = getPilotedShip();
	if (!ship)
		return false;

	ServerObject * const containingObject = NON_NULL(safe_cast<ServerObject *>(ContainerInterface::getContainedByObject(*this)));

	// If directly contained by a ship, we use the pilot slot, else we use the pob pilot slot
	SlotId const pilotSlotId = containingObject->asShipObject() ? ShipSlotIdManager::getShipPilotSlotId() : ShipSlotIdManager::getPobShipPilotSlotId();

	// If this creature is piloting the ship, he is in the pilot slot of either the ship or an object contained by a cell of the ship.
	SlottedContainer * const slottedContainer = ContainerInterface::getSlottedContainer(*containingObject);
	if (slottedContainer)
	{
		//-- Check for a pilot.
		Container::ContainerErrorCode errorCode = Container::CEC_Success;
		CachedNetworkId pilotId = slottedContainer->getObjectInSlot(pilotSlotId, errorCode);
		if (errorCode == Container::CEC_Success)
		{
			ServerObject * const pilot = safe_cast<ServerObject*>(pilotId.getObject());
			FATAL(pilot != this, ("Somehow told to unpilot a ship when we are not the pilot of the ship!"));

			// Transfer pilot to location of the object it was contained by, which may or may not have been the ship, but it something with a pilot slot.
			CellObject * const destCell = dynamic_cast<CellObject *>(containingObject->getAttachedTo());
			if (destCell)
			{
				Transform tr(containingObject->getTransform_o2p());
				// push them back a meter as well if they are unpiloting a pob ship
				if (!containingObject->asShipObject())
					tr.move_l(Vector(0.f, 0.f, -1.f));
				IGNORE_RETURN(ContainerInterface::transferItemToCell(*destCell, *this, tr, 0, errorCode));
			}
			else
				IGNORE_RETURN(ContainerInterface::transferItemToWorld(*this, containingObject->getTransform_o2w(), 0, errorCode));
			if (errorCode == Container::CEC_Success)
				unpilotedShip = true;
		}
	}

	Client * const client = getClient();
	if (client)
	{
		client->removeControlledObject(*ship);
		// make sure the client can start receiving updates for the ship
		ShipClientUpdateTracker::queueForUpdate(*client, *ship);
	}

	return unpilotedShip;
}
bool CreatureObject::pilotShip(ServerObject &pilotSlotObject)
{
	//-- Ensure ships are enabled.
	if (!ConfigServerGame::getShipsEnabled())
	{
		return false;
	}

	//-- Ensure we (the pilot) are not already piloting a ship.
	if (getPilotedShip())
	{
		DEBUG_FATAL(true, ("pilotShip(): pilot id=[%s] template=[%s] is already piloting a ship.", getNetworkId().getValueString().c_str(), getObjectTemplateName()));
		return false;
	}

	//-- Ensure both the ship and the pilot are authoritative on this server.
	//   (They should be by design of how this function gets called.)
	if (!isAuthoritative() || !pilotSlotObject.isAuthoritative())
	{
		DEBUG_WARNING(true, ("pilotShip(): pilot id=[%s] or ship id=[%s] was not authoritative on this server, mounting aborted.", getNetworkId().getValueString().c_str(), pilotSlotObject.getNetworkId().getValueString().c_str()));
		return false;
	}

	ShipObject *shipObject = ShipObject::getContainingShipObject(&pilotSlotObject);
	NOT_NULL(shipObject);

	//-- Reject if the ship is not in the world cell
	if (!shipObject->isInWorldCell())
	{
		DEBUG_WARNING(true, ("pilotShip(): ship id=[%s] is not in the world cell, mounting ship aborted.", shipObject->getNetworkId().getValueString().c_str()));
		return false;
	}

	// If directly contained by a ship, we use the pilot slot, else we use the pob pilot slot
	SlotId const pilotSlotId = pilotSlotObject.asShipObject() ? ShipSlotIdManager::getShipPilotSlotId() : ShipSlotIdManager::getPobShipPilotSlotId();

	Container::ContainerErrorCode errorCode = Container::CEC_Success;
	bool const transferSuccess = ContainerInterface::transferItemToSlottedContainer(pilotSlotObject, *this, pilotSlotId, NULL, errorCode);
	DEBUG_FATAL(transferSuccess && (errorCode != Container::CEC_Success), ("pilotShip(): transferItemToSlottedContainer() returned success but container error code returned error %d.", static_cast<int>(errorCode)));

	if (transferSuccess)
	{
		Client * const client = getClient();
		if (client)
			client->addControlledObject(*shipObject);
	}

	//-- Indicate transfer success.
	return transferSuccess;
}
示例#3
0
std::string MultiplexNetwork::getGlobalName(const std::string& local_vertex_name, const std::string& network_name) const {
	network_id net = getNetworkId(network_name);
	vertex_id local_id = getNetwork(net).getVertexId(local_vertex_name);
	global_identity global_id = getGlobalIdentity(local_id, net);
	return getGlobalName(global_id);
}
示例#4
0
bool MultiplexNetwork::containsVertex(const std::string& global_identity_name, const std::string& network_name) const {
	global_identity global_id = getGlobalIdentity(global_identity_name);
	network_id net = getNetworkId(network_name);
	return containsVertex(global_id,net);
}
示例#5
0
void MultiplexNetwork::mapIdentity(const std::string& global_identity_name, const std::string& local_vertex_name, const std::string& network_name) {
	if (!containsGlobalName(global_identity_name)) throw ElementNotFoundException("global identity " + global_identity_name);
	if (!getNetwork(network_name).containsVertex(local_vertex_name)) throw ElementNotFoundException("local vertex name " + local_vertex_name);
	if (!containsNetwork(network_name)) throw ElementNotFoundException("network " + network_name);
	mapIdentity(getGlobalIdentity(global_identity_name),getNetwork(network_name).getVertexId(local_vertex_name),getNetworkId(network_name));
}
示例#6
0
void CharacterNetwork::Deserialize(RakNet::DeserializeParameters *deserializeParameters)
{
    //printf("Character %llu---Deserialize\n", GetNetworkID());
    //printf("true id: %s\n", getNetworkId());
    
//#if defined(COCOS2D_JAVASCRIPT) && ISCLIENT==1
//#ifndef ISCLIENT
    RakNet::TimeMS time = RakNet::GetTimeMS();
    //printf("Deserialize: %u\n", time -_lastDeserializeTime);
    //printf("c: %llu\n", RakNet::GetTime() - deserializeParameters->timeStamp);
    _lastDeserializeTime = time;
//#endif
    
    RakNet::VariableDeltaSerializer::DeserializationContext deserializationContext;
    
    // Deserialization is written similar to serialization
    // Note that the Serialize() call above uses two different reliability types. This results in two separate Send calls
    // So Deserialize is potentially called twice from a single Serialize
    variableDeltaSerializer.BeginDeserialize(&deserializationContext, &deserializeParameters->serializationBitstream[0]);
    if (variableDeltaSerializer.DeserializeVariable(&deserializationContext, _x)){
        //printf("%s _x changed to %f\n", getNetworkId(), _x);
    }
    if (variableDeltaSerializer.DeserializeVariable(&deserializationContext, _y)){
        //printf("%s _y changed to %f\n", getNetworkId(), _y);
    }
    
    if (variableDeltaSerializer.DeserializeVariable(&deserializationContext, _velocityX))
        //printf("_velocityX changed to %f\n", _velocityX);
    if (variableDeltaSerializer.DeserializeVariable(&deserializationContext, _velocityY))
        //printf("_velocityY changed to %f\n", _velocityY);
    
    if (variableDeltaSerializer.DeserializeVariable(&deserializationContext, _flipX)){
        printf("_flipX changed to %d\n", _flipX);
    }
    if (variableDeltaSerializer.DeserializeVariable(&deserializationContext, _state)){
        //printf("_state changed to %d\n", _state);
    }
    if (variableDeltaSerializer.DeserializeVariable(&deserializationContext, _characterState)){
        //printf("_characterState changed to %d\n", _characterState);
    }
    if (variableDeltaSerializer.DeserializeVariable(&deserializationContext, _previousCharacterState)){
        //printf("_previousCharacterState changed to %d\n", _previousCharacterState);
    }
    if (variableDeltaSerializer.DeserializeVariable(&deserializationContext, _collisionRect)){
        printf("_collisionRect changed to %f %f %f %f\n", _collisionRect.origin.x, _collisionRect.origin.y, _collisionRect.size.width, _collisionRect.size.height);
    }
    variableDeltaSerializer.EndDeserialize(&deserializationContext);
    
    /*
    variableDeltaSerializer.BeginDeserialize(&deserializationContext, &deserializeParameters->serializationBitstream[1]);
    
    variableDeltaSerializer.EndDeserialize(&deserializationContext);
    */
    
    // Every time we get a network packet, we write it to the transformation history class.
    // This class, given a time in the past, can then return to us an interpolated position of where we should be in at that time
    //transformationHistory.Write(_x, _y, _velocityX, _velocityY, _state, RakNet::GetTime());
    transformationHistory.Write(_x, _y, _velocityX, _velocityY, _state, deserializeParameters->timeStamp);
    
#if defined(COCOS2D_JAVASCRIPT) && ISCLIENT==1
    if (!_jsObjectCreated && !_native)
    {
        // manipulate js side context`
        _jsObjectCreated = true;
        jsval networkObjects = CharacterNetwork::getNetworkObjects();
        if(networkObjects != JSVAL_VOID)
        {
            // get js object from c++ object
            js_proxy_t *proxy = js_get_or_create_proxy<CharacterNetwork>(_cx(), this);
            if (!proxy)
                printf("can't get proxy for object when createEntityFromNetwork\n");
            else
            {
                jsval jsCharacter;
                JSObject *tmpObject = proxy->obj;
                jsCharacter = OBJECT_TO_JSVAL(tmpObject);
                JS_SetProperty(_cx(), &networkObjects.toObject(), getNetworkId(), &jsCharacter);
            }
        }
    }
    // actually all game object, whether native or not, should correct property from server packet
    /* test
    else if (_jsObjectCreated && !_native)
    {
        jsval jsCharacter = CharacterNetwork::getCharacterById(getNetworkId());
        if (jsCharacter != JSVAL_VOID)
        {
            cocos2d::CCPoint point = ccp(_x, _y);
            //printf("js get d: %f %f\n", direction.x, direction.y);
            jsval jsPoint = ccpoint_to_jsval(_cx(), point);
            _sc()->executeFunctionWithOwner(jsCharacter, "setPosition", 1, &jsPoint);
        }
        
        
    }
    */
#endif
    
}