SendPacket Negative Number

Get Help on scripting in POL with configurations, config setups, script trouble shooting, etc.
Post Reply
Daviex
Neophyte Poster
Posts: 39
Joined: Sat Sep 20, 2008 3:53 am

SendPacket Negative Number

Post by Daviex »

Hi to all.

I work on a fix for our Resurrection code.

Simply, when i go to res, it crash. I see that this bug appear only when i am on a negative Z, like Jhelom Arena.

If i disable this script, it work, but we need it for a fix. So now i ask you:

There's any type of problem with SendPacket and Negative Numbers?

If Yes, anyone have any idea how to resolve this?

Thanks.

Sorry for my English xD
Korbedda
New User
Posts: 8
Joined: Wed Mar 14, 2012 4:05 pm

Re: SendPacket Negative Number

Post by Korbedda »

I think we will need more information on this. Can you copy the malfunctioning script here?

However, I suspect what the problem is. While working with packets, do you have anything like this in the code?

Code: Select all

packet.setInt8(position, player.z);
The z coordinate goes from -128 to 127 and therefore can be stored in 1 byte. But the code above will not work, you have to use this:

Code: Select all

packet.setInt8(position, player.z + (player.z < 0)*0x100);
I encountered this on POL97, so it may well be solved in current revision, but it seems that enconding integers to packets does not handle negative numbers. E-script integers in POL are stored in 32 bytes, and they are signed. That means that the first bit (in big-endian) carries the sign. 0 for positive, 1 for negative. So, instead of going between 0 and (2^32 - 1), the possible numbers go from -(2^16) to (2^16-1). In bits, number zero looks like 32 zeros, but -1 looks like 32 ones. For the byte designating z in packets, 127 is 01111111, but -128 is 10000000. Basically you have to encode an UNSIGNED byte to the packet, mapping numbers between -128 and -1 to numbers 128-255. For example, -50 is 11001110, but unsigned byte with the same bits would give 256-50=206. So, in order to give 206 instead of -50, you have to add 256 to every negative number, which is solved by the code above.
Daviex
Neophyte Poster
Posts: 39
Joined: Sat Sep 20, 2008 3:53 am

Re: SendPacket Negative Number

Post by Daviex »

hey.

Thanks for reply.

This is the part of code ( i wrote the Packet in Escript: )

Code: Select all

  var packet :=
    //"78" +
    ConvertIntToHex(mobile.serial, 4) +
    ConvertIntToHex(mobile.graphic, 2) +
    ConvertIntToHex(mobile.x, 2) +
    ConvertIntToHex(mobile.y, 2) +
    ConvertIntToHex(mobile.z, 1) +
    ConvertIntToHex(mobile.facing, 1) +
    ConvertIntToHex(mobile.color, 2) +
    "0000";

    foreach equip in (ListEquippedItems(mobile))
      if (equip.layer)

        packet := packet + ConvertIntToHex(equip.serial, 4);

        if (equip.color == 0)
          packet := packet + ConvertIntToHex(equip.graphic, 2);
        else
        //senza questo cambiamento di grafica l'npc se indossa qualcosa rimaen grigio anche se gli setto il colore
          packet := packet + ConvertIntToHex(equip.graphic + 32768, 2);
        endif

        packet := packet + ConvertIntToHex(equip.layer, 1);

        if (equip.color != 0)
          packet := packet + ConvertIntToHex(equip.color, 2);
        endif
      endif
    endforeach

    packet := packet + "00000000";

    SendPacket(who, "78" + ConvertIntToHex(Cint((Len(packet) / 2) + 2), 2) + packet);
Pratically, it take the Z of the mobile, and when it is a negative number, client crash...

So, i want to know if there exist any FIX...

Thanks ^^'
Korbedda
New User
Posts: 8
Joined: Wed Mar 14, 2012 4:05 pm

Re: SendPacket Negative Number

Post by Korbedda »

I think the problem is in the function ConvertIntToHex, could you post it here, too?
Also, I recommend using Packet functions instead of building a string, it is much cleaner.
Daviex
Neophyte Poster
Posts: 39
Joined: Sat Sep 20, 2008 3:53 am

Re: SendPacket Negative Number

Post by Daviex »

Ok there's code of ConvertIntToHex

Code: Select all

function ConvertIntToHex(astring, alength)
  astring := Hex(astring);
  astring := CStr(astring);
  if (astring["0x"])
    astring := astring[3, len(astring)];
  endif
  if (len(astring) > alength * 2)
    return;
  endif
  while (len(astring) < alength * 2)
    astring := "0"+ astring;
  endwhile    
  return astring;
endfunction

//zandor ottimizzata zandor 27.08.2006
function DeleteObject(who, lobj)
  SendPacket(who, "1D"+ ConvertIntToHex(lobj.serial, 4));
endfunction
Korbedda
New User
Posts: 8
Joined: Wed Mar 14, 2012 4:05 pm

Re: SendPacket Negative Number

Post by Korbedda »

Here is the flaw:
Imagine you have negative z, like -1. As I explained above, signed variables have different bit structure.
Hex(-1) will give 0xFFFFFFFF. You are trying to write this into 1 byte, which invokes your failsafe condition and returns nothing. What happens next, I am not sure, but I think this "nothing" is treated as an empty string ("") and the packet has a "missing byte" in it. If client gets such damaged packet, it typically freezes or crashes. As I said, I recommend using Packet functions. If you need an immediate fix, this should do the trick:

Code: Select all

replace
ConvertIntToHex(mobile.z, 1)
with
ConvertIntToHex(mobile.z+(mobile.z<0)*0x100, 1)
Zik
Novice Poster
Posts: 41
Joined: Sun Feb 07, 2010 9:39 am

Re: SendPacket Negative Number

Post by Zik »

I solved such problem, some time ago, so in case someone needs it:

Code: Select all

function ConvertIntToHex(astring, alength)
var minus := 0;
if (astring<0) minus :=1; endif
  astring := Hex(astring);
  astring := CStr(astring);
  if (astring["0x"])
    astring := astring[3, len(astring)];
  endif
  if (len(astring) > alength * 2 && !minus)
    return;
  else
  	while (len(astring) > alength * 2)
    		astring := astring[2, len(astring)];
  	endwhile 
  endif
  while (len(astring) < alength * 2)
    astring := "0"+ astring;
  endwhile 
  return astring;
endfunction
Post Reply