Problem with if statement :S

I'm pretty confused because I've written a plug in for a program before and when i wanted to remake pretty much the same plug in it won't work.

This is the current code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void TemplateModule::OnRelayDataToClient(IPacket* packet, const IModule* owner)
{
	const unsigned char* bytes = static_cast<const unsigned char*>(packet->GetData());
	int packetId = bytes[0];
	static int andyId[3];
	static bool AndyFound = false;

	switch (packetId){
		case 0x69:{
			//if andys id is registered
			if(AndyFound){
			//if monster is dying
			if(bytes[5] == 0x08){
			//if id of the monster as dying is the same as andys id
			if(bytes[1] == andyId[0] && bytes[2] == andyId[1] && bytes[3] == andyId[2] && bytes[4] == andyId[3]){
				packet->SetFlag(IPacket::PacketFlag_Dead);
				AndyFound = false;
			}}}break;}

		case 0xAC:{
			//if assigned monster is andy
			if(bytes[5] == 0x9c && bytes[6] == 0x00){
			andyId[0] = bytes[1];
			andyId[1] = bytes[2];
			andyId[2] = bytes[3];
			andyId[3] = bytes[4];
			AndyFound = true;
			}break;}
	}
}


This is the old code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
void TemplateModule::OnRelayDataToClient(IPacket* packet, const IModule* owner)
{

	const unsigned char* bytes = static_cast<const unsigned char*>(packet->GetData());
	int packetId = bytes[0];
	static int AndyCurrId[3];

	switch (packetId)
	{
	case 0xac:
	{
		if(bytes[5] == 0x9c && bytes[6] == 0x00)
		{ 
		Chat(ChatName, false, "Andariel Found!");  
		AndyCurrId[0] = bytes[1];
		AndyCurrId[1] = bytes[2];
		AndyCurrId[2] = bytes[3];
		AndyCurrId[3] = bytes[4];
		}
	}
	case 0x69:
	{ 
		if(bytes[5] == 0x08 && bytes[11] == 0x0d && bytes[1] == AndyCurrId[0] && bytes[2] == AndyCurrId[1] && bytes[3] == AndyCurrId[2] && bytes[4] == AndyCurrId[3])
		{
		packet->SetFlag(IPacket::PacketFlag_Dead);
		Chat(ChatName, false, "Andariel Died!"); 

	int length = 12;

	unsigned char* buffer = new unsigned char[length];
	int offset = 0;

	buffer[offset++] = 0x69;

	buffer[offset++] = AndyCurrId[0];
	buffer[offset++] = AndyCurrId[1];
	buffer[offset++] = AndyCurrId[2];
	buffer[offset++] = AndyCurrId[3];

	buffer[offset++] = 0x09;

	buffer[offset++] = 0x00;
	buffer[offset++] = 0x00;

	buffer[offset++] = 0x00;
	buffer[offset++] = 0x00;

	buffer[offset++] = 0x00;
	buffer[offset++] = 0x00;

	IPacket* packet = _proxy->CreatePacket(buffer, length);
	packet->SetFlag(IPacket::PacketFlag_Hidden);
	_proxy->RelayDataToClient(packet, this);
	delete packet;

	delete[] buffer;
		}	  
	}
	}
}


This is the packet Log:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[ANDARIEL]

->ac 99 b5 ea 39 9c 00 41 58 8d 25 63 13 a1 b0 00 00 00 00 
->ac 99 b5 ea 39 9c 00 36 58 8c 25 63 13 a1 b0 00 00 00 00 
->ac 99 b5 ea 39 9c 00 36 58 8d 25 63 13 a1 b0 00 00 00 00 

ASSIGN NPC: -> ac 3b 94 00 8d 9c 00 12 58 4a 25 80 13 a1 b0 00 00 00 00 
Dying NPC: ->  69 3b 94 00 8d 08 23 58 7a 25 19 40 
Dead NPC: ->   69 3b 94 00 8d 09 31 58 7c 25 19 00 

ASSIGN NPC: -> ac d7 00 dc 2a 9c 00 12 58 4a 25 80 13 a1 b0 00 00 00 00 
Dying NPC: ->  69 d7 00 dc 2a 08 25 58 77 25 04 40 
Dead NPC: ->   69 d7 00 dc 2a 09 24 58 76 25 04 00 

[ASSIGN NPCAndariel] [19]
ac => Assign NPC[1]
99 b5 ea 39 => Id[4]
9c => Andariel[1]
00 => ?[1]
36 58 => X[2]
8d 25 => Y[2]
63 13 a1 b0 00 00 00 00 => ?[8]


The old code is pretty mashed up worsen written than the new one, i do not get a compilation error.
It just doesn't seem like the underlined code in the current code occurs, but the log says it does.
Last edited on
Did you initialize andyId ?
static int andyId[3];
Current code line 5
Last edited on
Yes, there you declared it. But did you assign some values to the elements of that array?
There being assigned on line 22,23,24 and 25?
And when that's done the AndyFound turns to true.

I put a MessageBox on line 14 and it's being displayed correctly when it's time for it to show up.
And between the next two rows(15-16), and it does not show up.

Another question i have is will:
1
2
3
4
int offset = 0;
while(offset<5){
andyId[offset++] = bytes[offset];
}

Return the same as:
1
2
3
4
andyId[0] = bytes[1];
andyId[1] = bytes[2];
andyId[2] = bytes[3];
andyId[3] = bytes[4];


The thing i can thing of is that:
1
2
static int
const unsigned char*

Are in different forms which makes the if goes like 1=2 and return false.
Last edited on
This should be in the beginners part of this forum right?
Don't forget to break out of the cases, if necessary.
Last edited on
So, now i did but that did not solve the problem :/
Your indexing is incorrectly done.

static int andyId[3]; // this array only have indexes (indices) of 0,1,2

so:
andyId[3] = bytes[4]; //incorrect - Array bounds error - AndyId does not have index 3

As a matter of fact because your decalartion (in your new code) looks like this:
1
2
static int andyId[3];
static bool AndyFound = false;


Then reading/writing to index andyId[3] will actually be reading/overwriting AndyFound variable
Last edited on
Thank you guestgulkan this did solve my problem, silly that i didn't see that.
Is this for a tibia OT server?
Diablo 2 =), or redvex a proxy for diablo 2.
Topic archived. No new replies allowed.