• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solved How to use NetworkMessage: sendIcons, sendCreatureSquare, sendCancelWalk

whitevo

Feeling good, thats what I do.
Joined
Jan 2, 2015
Messages
3,452
Solutions
1
Reaction score
625
Location
Estonia
On protocolgame.cpp file we can find some network messages what assume source uses.
But trough Lua we can also send those messages to server? (or client?)

I picked up 3 useful NetworkMessages
Here are the problems I ran into:
here is the c++ code found in source
Code:
void ProtocolGame::sendIcons(uint16_t icons)
{
    NetworkMessage msg;
    msg.addByte(0xA2);
    msg.add<uint16_t>(icons);
    writeToOutputBuffer(msg);
}
and here is the lua function I made
Code:
function Player.sendIcons(player, iconByte)
local msg = NetworkMessage()
    msg:addByte(0xA2)
    msg:addU16(iconByte)
    msg:sendToPlayer(player)
    msg:delete()
end
I tested with iconByte's:
0x00 = nothing
0x01 = poison
0x02 = fire

How do you set more than 1 icon at once?
because only one icon was allowed at once.
How do you remove that icon?
Excluding using the 0x00 code to remove all

source function
Code:
void ProtocolGame::sendCreatureSquare(const Creature* creature, SquareColor_t color)
{
    if (!canSee(creature)) {        return;    }

    NetworkMessage msg;
    msg.addByte(0x93);
    msg.add<uint32_t>(creature->getID());
    msg.addByte(0x01);
    msg.addByte(color);
    writeToOutputBuffer(msg);
}

my Lua function
Code:
function Creature.sendCreatureSquare(creature, colorByte)
local msg = NetworkMessage()
    msg:addByte(0x93)
    msg:addU32(creature:getId())
    msg:addByte(0x01)
    msg.addByte(colorByte);
    msg:sendToPlayer(creature)
    msg:delete()
end
Well couldn't get that work at all. crashed every time xD
What am I doing wrong?
What colorBytes I can use?

source function
Code:
void ProtocolGame::sendCancelWalk()
{
    NetworkMessage msg;
    msg.addByte(0xB5);
    msg.addByte(player->getDirection());
    writeToOutputBuffer(msg);
}
Here I just want to know, can I use it on creature like every 500milliseconds?
So that he wont even try to move?
Right now when I want to make creature not able to move. I set the ground ActionID which makes him teleport back where he cam from on step out.
 
Solution
On protocolgame.cpp file we can find some network messages what assume source uses.
But trough Lua we can also send those messages to server? (or client?)

I picked up 3 useful NetworkMessages
Here are the problems I ran into:
here is the c++ code found in source
Code:
void ProtocolGame::sendIcons(uint16_t icons)
{
    NetworkMessage msg;
    msg.addByte(0xA2);
    msg.add<uint16_t>(icons);
    writeToOutputBuffer(msg);
}
and here is the lua function I made
Code:
function Player.sendIcons(player, iconByte)
local msg = NetworkMessage()
    msg:addByte(0xA2)
    msg:addU16(iconByte)
    msg:sendToPlayer(player)
    msg:delete()
end
I tested with iconByte's:
0x00 = nothing
0x01 = poison
0x02 = fire

How do you set more than 1...
On protocolgame.cpp file we can find some network messages what assume source uses.
But trough Lua we can also send those messages to server? (or client?)

I picked up 3 useful NetworkMessages
Here are the problems I ran into:
here is the c++ code found in source
Code:
void ProtocolGame::sendIcons(uint16_t icons)
{
    NetworkMessage msg;
    msg.addByte(0xA2);
    msg.add<uint16_t>(icons);
    writeToOutputBuffer(msg);
}
and here is the lua function I made
Code:
function Player.sendIcons(player, iconByte)
local msg = NetworkMessage()
    msg:addByte(0xA2)
    msg:addU16(iconByte)
    msg:sendToPlayer(player)
    msg:delete()
end
I tested with iconByte's:
0x00 = nothing
0x01 = poison
0x02 = fire

How do you set more than 1 icon at once?
because only one icon was allowed at once.
How do you remove that icon?
Excluding using the 0x00 code to remove all

source function
Code:
void ProtocolGame::sendCreatureSquare(const Creature* creature, SquareColor_t color)
{
    if (!canSee(creature)) {        return;    }

    NetworkMessage msg;
    msg.addByte(0x93);
    msg.add<uint32_t>(creature->getID());
    msg.addByte(0x01);
    msg.addByte(color);
    writeToOutputBuffer(msg);
}

my Lua function
Code:
function Creature.sendCreatureSquare(creature, colorByte)
local msg = NetworkMessage()
    msg:addByte(0x93)
    msg:addU32(creature:getId())
    msg:addByte(0x01)
    msg.addByte(colorByte);
    msg:sendToPlayer(creature)
    msg:delete()
end
Well couldn't get that work at all. crashed every time xD
What am I doing wrong?
What colorBytes I can use?

source function
Code:
void ProtocolGame::sendCancelWalk()
{
    NetworkMessage msg;
    msg.addByte(0xB5);
    msg.addByte(player->getDirection());
    writeToOutputBuffer(msg);
}
Here I just want to know, can I use it on creature like every 500milliseconds?
So that he wont even try to move?
Right now when I want to make creature not able to move. I set the ground ActionID which makes him teleport back where he cam from on step out.
sendIcons:
You have to send all the icons every time you want to update them.
https://github.com/otland/forgottenserver/blob/master/src/player.cpp#L494-L526
It would be a nightmare to implement in lua cause you cant iterate over player conditions and cant get their icons.

But to send more than 1 icon you just have to sum the values (0x01 + 0x02)

sendSquare:
https://otland.net/threads/tfs-1-2-sendsquare-cid-color-sec.245200/

sendCancelWalk:
I doubt it will work and it's not the greatest solution, the actionid is far better but the ideal would be to recreate the function "doCreatureSetNoMove" from TFS 0.x
 
Solution
How do you set more than 1 icon at once?
first of all take a look here https://github.com/otland/forgottenserver/blob/master/src/const.h#L336-L353
there you have the values of each icon, what you need to do is add all you want just like player flags and send it, since you said that you arent much experienced in C i assume you dont know the << operator, its bit shift operation, kind of self explanatory, x = y << 2, literally it means assign x the result of shifting y to the left by two bits, a "real thing" of it.

ICON_POISON = 1 << 0: shift 0 bits
binary representation > 0000 0001 = 1 (decimal)
ICON_BURN = 1 << 1: shift 1 bits
binary representation > 0000 0010 = 2
ICON_ENERGY = 1 << 2: shift 2 bits
binary representation > 0000 0100 = 4
ICON_DRUNK = 1 << 3: shift 3 bits
binary representation > 0000 1000 = 8

if you want to send this four icons to client just add its values, which is 15.

Code:
function Player.sendIcons(self)
    local msg = NetworkMessage()
    msg:addByte(0xA2)
    msg:addU16(015) --[[ doesnt matter positional numeral system.
                         (0 tells the parser it is dealing with a constant
                          x and b are an arbitrary choice, just to know what base it is)
                          msg:addU16(0xF) 15 to hexdecimal
                          msg:addU16(0b1111) 15 to binary --]]
    msg:sendToPlayer(self)
    msg:delete()
end

this code will send poison, burn, energy and drunk icons to client.

to get this values easier without doing this convertion binary/decimal you can use LuaDemo, print(1 << 14) and get the output, (notice that bitwise operators became native in lua only after 5.3, tfs runs 5.1, so you cant use this to your code) a trick is that ever you will move only one bit, you can do 2 ^ x, x is the amount of bit moves, so 2 ^ 3 = 8.

How do you remove that icon?
you do the reverse, you subtract the value of dropped icon.

my Lua function
Code:
function Creature.sendCreatureSquare(creature, colorByte)
local msg = NetworkMessage()
msg:addByte(0x93)
msg:addU32(creature:getId())
msg:addByte(0x01)
msg.addByte(colorByte);
msg:sendToPlayer(creature)
msg:delete()
end
Well couldn't get that work at all. crashed every time xD
What am I doing wrong?
What colorBytes I can use?

idk if you noticed but you have a dot instead of a colon here msg.addByte(colorByte).
and the answer of @MatheusMkalo you got what you asked i guess.

Here I just want to know, can I use it on creature like every 500milliseconds?

this byte is used by cipsoft to make a global cooldown to player actions (tfs has it but need a lot of improvements, imo), and that "rollback" (forget the name they gave to it) when you trying to pass through a player, you shouldnt use this to prevent from walking because of the spam it will do in server scheduler, and will get worse even more depending on amout of players that it is been sending out.
 
Last edited:
Back
Top