void ProtocolGame::parseContainerUpdateItem(InputMessage& msg)
{
    int containerId = msg.getU8();
    int slot = msg.getU8();
    ItemPtr item = internalGetItem(msg);
    g_lua.callGlobalField("Game", "onContainerUpdateItem", containerId, slot, item);
}
void ProtocolGame::parseSafeTradeRequest(InputMessage& msg)
{
    msg.getString(); // name
    int count = msg.getU8();

    for(int i = 0; i < count; i++)
        internalGetItem(msg);
}
void ProtocolGame::parseTileTransformThing(InputMessage& msg)
{
    Position pos = parsePosition(msg);
    int stackPos = msg.getU8();

    int thingId = msg.getU16();
    assert(thingId != 0);
    if(thingId == 0x0061 || thingId == 0x0062 || thingId == 0x0063) {
        parseCreatureTurn(msg);
    } else {
        ThingPtr thing = internalGetItem(msg, thingId);
        g_map.removeThingByPos(pos, stackPos);
        g_map.addThing(thing, pos, stackPos);
    }
}
void ProtocolGame::parseOpenContainer(InputMessage& msg)
{
    int containerId = msg.getU8();
    int itemId = msg.getU16();
    std::string name = msg.getString();
    int capacity = msg.getU8();
    bool hasParent = (msg.getU8() != 0);
    int itemCount = msg.getU8();

    std::vector<ItemPtr> items;
    items.reserve(itemCount);
    for(int i = 0; i < itemCount; i++) {
        ItemPtr item = internalGetItem(msg);
        items.push_back(item);
    }

    g_lua.callGlobalField("Game", "onContainerOpen", containerId, itemId, name, capacity, hasParent, items);
}
Esempio n. 5
0
Thing* ProtocolGame78::internalGetThing(NetworkMessage& msg)
{
	//get thing type
	uint16_t thingId = msg.getU16();

	if(thingId == 0x0061 || thingId == 0x0062) {
		//creatures
		Creature* creature = NULL;

		if(thingId == 0x0062){ //creature is known
			uint32_t creatureID = msg.getU32();
			creature = Creatures::getInstance().getCreature(creatureID);

			if(!creature){
			return NULL;
            }

            //read creature properties
            uint8_t hp = msg.getU8();
            creature->setHealth(hp);
            if(creature->getHealth() > 100){
                return NULL;
            }
            Notifications::onCreatureChangeHealth(creature->getID(), hp);
            //
		}
		else if(thingId == 0x0061){ //creature is not known
			//perhaps we have to remove a known creature
			uint32_t removeID = msg.getU32();
			Creatures::getInstance().removeCreature(removeID);
			Notifications::onRemoveCreature(removeID);
			//add a new creature
			uint32_t creatureID = msg.getU32();
			creature = Creatures::getInstance().addCreature(creatureID);

			creature->setName(msg.getString());

			if(!creature){
			return NULL;
            }

            //read creature properties
            uint8_t hp = msg.getU8();
            creature->setHealth(hp);
            if(creature->getHealth() > 100){
                return NULL;
            }
            Notifications::onCreatureChangeHealth(creature->getID(), hp);
            // NOTE (Kilouco): Let's make it comes before onAddCreature. Now battlewindow should works fine.

            Notifications::onAddCreature(creatureID);
		}

		uint8_t direction;
		if(!msg.getU8(direction) || direction > 3){
			return NULL;
		}
		creature->setLookDir((Direction)direction);
		creature->setTurnDir((Direction)direction);
		//

		if(!internalSetOutfit(msg, creature->getOutfit())){
			return NULL;
		}
		creature->loadOutfit();

		//check if we can read 6 bytes
		if(!msg.canRead(6)){
			return NULL;
		}

		creature->setLightLevel(msg.getU8());
		creature->setLightColor(msg.getU8());

		creature->setSpeed(msg.getU16());

		creature->setSkull(msg.getU8());
		creature->setShield(msg.getU8());

		return creature;
	}
	else if(thingId == 0x0063){
		//creature turn
		uint32_t creatureID = msg.getU32();
		Creature* creature = Creatures::getInstance().getCreature(creatureID);
		if(!creature){
			return NULL;
		}
		//check if we can read 1 byte
		uint8_t direction;
		if(!msg.getU8(direction) || direction > 3){
			return NULL;
		}
		creature->setTurnDir((Direction)direction);

		return creature;
	}
	else{
		//item
		return internalGetItem(msg, thingId);
	}
}
void ProtocolGame::parseAddInventoryItem(InputMessage& msg)
{
    int slot = msg.getU8();
    ItemPtr item = internalGetItem(msg);
    g_game.processInventoryChange(slot, item);
}
void ProtocolGame::parseContainerAddItem(InputMessage& msg)
{
    int containerId = msg.getU8();
    ItemPtr item = internalGetItem(msg);
    g_game.processContainerAddItem(containerId, item);
}
ThingPtr ProtocolGame::internalGetThing(InputMessage& msg)
{
    ThingPtr thing;

    int thingId = msg.getU16();
    assert(thingId != 0);
    if(thingId == 0x0061 || thingId == 0x0062) { // add new creature
        CreaturePtr creature;

        if(thingId == 0x0062) { //creature is known
            uint id = msg.getU32();

            CreaturePtr knownCreature = g_map.getCreatureById(id);
            if(knownCreature)
                creature = knownCreature;
            else
                logTraceError("server says creature is known, but its not on creatures list");
        } else if(thingId == 0x0061) { //creature is not known
            uint removeId = msg.getU32();
            uint id = msg.getU32();
            std::string name = msg.getString();

            if(name.length() > 0) // every creature name must start with a capital letter
                name[0] = toupper(name[0]);

            g_map.removeCreatureById(removeId);

            if(id == m_localPlayer->getId())
                creature = m_localPlayer;
            else if(id >= Proto::PlayerStartId && id < Proto::PlayerEndId)
                creature = PlayerPtr(new Player);
            else if(id >= Proto::MonsterStartId && id < Proto::MonsterEndId)
                creature = MonsterPtr(new Monster);
            else if(id >= Proto::NpcStartId && id < Proto::NpcEndId)
                creature = NpcPtr(new Npc);
            else
                logTraceError("creature id is invalid");

            creature->setId(id);
            creature->setName(name);
        }

        uint8 healthPercent = msg.getU8();
        Otc::Direction direction = (Otc::Direction)msg.getU8();
        Outfit outfit = internalGetOutfit(msg);

        Light light;
        light.intensity = msg.getU8();
        light.color = msg.getU8();

        int speed = msg.getU16();
        int skull = msg.getU8();
        int shield = msg.getU8();

        int emblem = -1;
        if(thingId == 0x0061) // emblem is sent only in packet type 0x61
            emblem = msg.getU8();

        bool passable = (msg.getU8() == 0);

        if(creature) {
            creature->setHealthPercent(healthPercent);
            creature->setDirection(direction);
            creature->setOutfit(outfit);
            creature->setLight(light);
            creature->setSpeed(speed);
            creature->setSkull(skull);
            creature->setShield(shield);
            if(emblem != -1)
                creature->setEmblem(emblem);
            creature->setPassable(passable);
            creature->setDirection(direction);

            if(creature == m_localPlayer) {
                m_localPlayer->setKnown(true);
            }
        }

        thing = creature;
    } else if(thingId == 0x0063) { // creature turn
        parseCreatureTurn(msg);
    } else // item
        thing = internalGetItem(msg, thingId);

    return thing;
}