05 Jan, 2013, thucar wrote in the 1st comment:
Votes: 0
Hey.

Just took up ROM 2.4b6 codebase and am rebuilding it quite extensively. The problem is, I have no prior C experience but with my 5 years of PHP I'm making good progress.

Anyhow, the issue I'm having is: I need to get the name of the weapon skill being used by the opponent, in a fight.

The reason for this is I'm trying to use different damage messages for different weapons.
I set up a new table for the messages and I look up the correct message by skill name.


So now I've rewritten the end of dam_message with
sn = number_range(0, MAX_FORMS-1);
sk = form_lookup (skill_table[get_weapon_sn (ch)].name);
if (sk >=0)
pForm = form_table[sk].data[sn].messages[0];
else
pForm = "You hit";
esn = number_range(0, MAX_FORMS-1);
esk = form_lookup (skill_table[get_weapon_sn (victim)].name);
if (esk >=0)
eForm = form_table[esk].data[esn].messages[1];
else
eForm = "hits";

<–>

sprintf (buf1, "{3$n %s and %s $N%c{x", eForm, vp, punct);
sprintf (buf2, "{2%s and %s $N%c{x", pForm, vs, punct);
sprintf (buf3, "{4$n %s and %s you%c{x", eForm, vp, punct);


It works fine for the attacker. But the defender is considered to always hold the same weapon as the attacker:
<812hp 100m 100mv> 
A practice dummy swings their axe and misses you.
You swing your axe and miss a practice dummy.
A practice dummy is in awful condition.

<812hp 100m 100mv> wield spear
You stop using a wooden axe.
You wield a wooden spear.
You don't even know which end is up on a wooden spear.
A practice dummy brings their spear up in an upwards arc and scratches you.
You bring your spear up in an upwards arc and miss a practice dummy.
A practice dummy is in awful condition.


Also, the bystander sees:
A practice dummy swings their axe and misses Bob.
Bob hits and misses a practice dummy.

<1500hp 330m 460mv>
Bob stops using a wooden axe.
Bob wields a wooden spear.
A practice dummy brings their spear up in an upwards arc and scratches Bob.
Bob hits and misses a practice dummy.


Any thoughts on the matter would be welcome.
05 Jan, 2013, Rarva.Riendf wrote in the 2nd comment:
Votes: 0
I think it is because the method you call to get the string use a static variable.
It means that each time you call it, the previous value will be replaced by the one you get in the last call

message1 = message(toto)
message2 = message(tata)

You would think message2 != message1 , but a sa matter of fact, both are the same and have the message2 value.

Pretty easy to fall in this trap. (I have been there..)

[edit] nm…probably not that, missing too much code to guess what happens
05 Jan, 2013, thucar wrote in the 3rd comment:
Votes: 0
Yeah, was worth a shot so I created two separate lookups for char and victim. The end results stay the same.

Some more code then. That should be all the relevant code.

tables.c
const struct form_type  form_table [] = {
/* {char * skill, char * data.messages[MAX_FORMS] int data.skPercent, int data.modifier}, */
{"spear", {
{{"You bring your spear up in an upwards arc", "brings their spear up in an upwards arc"}, 15, 0},
{{"You execute some cool move", "executes some cool move"}, 25, 5},
{{"You goof around", "goofs around"}, 35, 10},}
},
{"axe", {
{{"You bring your axe down in a wide arc", "brings their axe down in a wide arc"}, 15, 0},
{{"You swing your axe", "swings their axe"}, 25, 5},
{{"You goof around with your axe", "goofs around with their axe"}, 35, 10},}
},
{"dagger", {
{{"You fumble and almost drop your dagger as you stab", "fumbles and almost drops their dagger as they stab"}, 15, 0},
{{"You slice quickly with your dagger", "slices quickly with their dagger"}, 25, 5},
{{"You thrust your dagger forward", "thrusts their dagger forward"}, 35, 10},}
},
{"dodge", {
{{"You duck", "ducks"}, 15, 0},
{{"You quickly lean back", "quickly leans back"}, 25, 5},
{{"You take a quick step to the side", "takes a quick step to the side"}, 35, 10},}
},
{NULL, {{{"", ""}, 0, 0}, {{"", ""}, 0, 0}}}
};


lookup.c
/*
* Lookup forms by skill name.
*/
int form_lookup (const char *name)
{
int sn;

for (sn = 0; form_table[sn].skill != NULL; sn++)
{
if (form_table[sn].skill == name)
return sn;
}
return -1;
}


fight.c
else
{
sn = number_range(0, MAX_FORMS-1);
sk = form_lookup_ch (skill_table[get_weapon_sn (ch)].name);
if (sk >=0)
pForm = form_table[sk].data[sn].messages[0];
else
pForm = "You hit";
esn = number_range(0, MAX_FORMS-1);
esk = form_lookup_vic (skill_table[get_weapon_sn (victim)].name);
if (esk >=0)
eForm = form_table[esk].data[esn].messages[1];
else
eForm = "hits";

if (dt >= 0 && dt < MAX_SKILL)
attack = skill_table[dt].noun_damage;
else if (dt >= TYPE_HIT && dt < TYPE_HIT + MAX_DAMAGE_MESSAGE)
attack = attack_table[dt - TYPE_HIT].noun;
else
{
bug ("Dam_message: bad dt %d.", dt);
dt = TYPE_HIT;
attack = attack_table[0].name;
}

if (immune)
{
if (ch == victim)
{
sprintf (buf1, "{3$n is unaffected by $s own %s.{x", attack);
sprintf (buf2, "{2Luckily, you are immune to that.{x");
}
else
{
sprintf (buf1, "{3$N is unaffected by $n's %s!{x", attack);
sprintf (buf2, "{2$N is unaffected by your %s!{x", attack);
sprintf (buf3, "{4$n's %s is powerless against you.{x",
attack);
}
}
else
{
if (ch == victim)
{
sprintf (buf1, "{3$n's %s %s $m%c{x", attack, vp, punct);
sprintf (buf2, "{2Your %s %s you%c{x", attack, vp, punct);
}
else
{
// sprintf (buf1, "{3$n's %s %s $N%c{x", attack, vp, punct);
// sprintf (buf2, "{2Your %s %s $N%c{x", attack, vp, punct);
// sprintf (buf3, "{4$n %s %s you%c{x", attack, vp, punct);
sprintf (buf1, "{3$n %s and %s $N%c{x", eForm, vp, punct);
sprintf (buf2, "{2%s and %s $N%c{x", pForm, vs, punct);
sprintf (buf3, "{4$n %s and %s you%c{x", eForm, vp, punct);
}
}
}

if (ch == victim)
{
act (buf1, ch, NULL, NULL, TO_ROOM);
act (buf2, ch, NULL, NULL, TO_CHAR);
}
else
{
act (buf1, ch, NULL, victim, TO_NOTVICT);
act (buf2, ch, NULL, victim, TO_CHAR);
act (buf3, ch, NULL, victim, TO_VICT);
}
return;
}
05 Jan, 2013, plamzi wrote in the 4th comment:
Votes: 0
Problem is here:
if (form_table[sn].skill == name)


Can't compare strings like this in C. See how your codebase does it and use its util functions, or read up on string.h "strcmp" and "strstr" funcs.
05 Jan, 2013, thucar wrote in the 5th comment:
Votes: 0
Wish that had been it. Changed to ctrcmp but the issue still remains. Also, as you can see from my example, the form_lookup works. It does retrieve the required message for the skills. I'm pretty sure the issue is the original data I'm sending to the lookup.

As if get_weapon_sn (victim) was retrieving the get_weapon_sn (ch) result.
05 Jan, 2013, Rarva.Riendf wrote in the 6th comment:
Votes: 0
plamzi said:
Problem is here:
Can't compare strings like this in C. See how your codebase does it and use its util functions, or read up on string.h "strcmp" and "strstr" funcs.


You can if they both come from the same constant table. It is not wise to play this game though as you can be quickly pulling your hair out the second your forget to stick to the table.

I suggest you use a debugger and follow step by the step what the value are. I think it will be obvious why the value used are the same then.
05 Jan, 2013, plamzi wrote in the 7th comment:
Votes: 0
thucar said:
Wish that had been it. Changed to ctrcmp but the issue still remains. Also, as you can see from my example, the form_lookup works. It does retrieve the required message for the skills. I'm pretty sure the issue is the original data I'm sending to the lookup.

As if get_weapon_sn (victim) was retrieving the get_weapon_sn (ch) result.


If you're actively working on your code, refrain from posting here. Wait until you are really stuck. The reason being you may end up finding a solution much quicker, and will not waste the time of a potentially large number of people.
05 Jan, 2013, Rarva.Riendf wrote in the 8th comment:
Votes: 0
plamzi said:
If you're actively working on your code, refrain from posting here. Wait until you are really stuck. The reason being you may end up finding a solution much quicker, and will not waste the time of a potentially large number of people.


Nobody is forced to asnwer the thread you know :) He is a noob in C as he said, so he WILL waste people time with code problem, C not being very friendly to begin with. Though I wish newb learn to use gdb before posting easy question about 'why my value is wrong' when they could easily fire it and see where it does get change. Best tip it to direct them to GDB first. (and an IDE to ease the process)
05 Jan, 2013, thucar wrote in the 9th comment:
Votes: 0
plamzi said:
If you're actively working on your code, refrain from posting here. Wait until you are really stuck. The reason being you may end up finding a solution much quicker, and will not waste the time of a potentially large number of people.


Hrm.. maybe I should have rephrased my original question.

1. Am I correct to assume that in a fight with a char and a mob, get_weapon_sn (ch) returns the players weapon skill?

2. Am I correct to assume that in the same fight get_weapon_sn (victim) returns the mobs weapon skill?

3. If the mob still the (victim) and the player a (ch) to a bystander?
05 Jan, 2013, plamzi wrote in the 10th comment:
Votes: 0
Rarva.Riendf said:
plamzi said:
If you're actively working on your code, refrain from posting here. Wait until you are really stuck. The reason being you may end up finding a solution much quicker, and will not waste the time of a potentially large number of people.


Nobody is forced to asnwer the thread you know :) He is a noob in C as he said, so he WILL waste people time with code problem, C not being very friendly to begin with. Though I wish newb learn to use gdb before posting easy question about 'why my value is wrong' when they could easily fire it and see where it does get change. Best tip it to direct them to GDB first. (and an IDE to ease the process)


I don't feel forced to answer at all. I'm trying to help him learn how to use this forum right, just like I'm trying to help him with his coding problem. Everyone should learn not to make people troubleshoot code that no longer exists. Do you disagree?
05 Jan, 2013, thucar wrote in the 11th comment:
Votes: 0
Rarva.Riendf said:
I suggest you use a debugger and follow step by the step what the value are. I think it will be obvious why the value used are the same then.



Thank you for the plain reminder. I'm using PHPStorm as my IDE right now and have yet to figure out how to debug C with that, but with some sprintf, and send_to_char I managed to get closer to the problem.

It seems the strings that get inserted into buffers with the three sprintf's at the end of my dam_message function here…
sprintf (buf1, "{3$n %s and %s $N%c{x", eForm, vp, punct);
sprintf (buf2, "{2%s and %s $N%c{x", pForm, vs, punct);
sprintf (buf3, "{4$n %s and %s you%c{x", eForm, vp, punct);
}
}
}

if (ch == victim)
{
act (buf1, ch, NULL, NULL, TO_ROOM);
act (buf2, ch, NULL, NULL, TO_CHAR);
}
else
{
act (buf1, ch, NULL, victim, TO_NOTVICT);
act (buf2, ch, NULL, victim, TO_CHAR);
act (buf3, ch, NULL, victim, TO_VICT);
}
return;


…are exactly what they should be. So I'm even more puzzled now because there is nothing being done between these sprintf's and the act's
05 Jan, 2013, Rarva.Riendf wrote in the 12th comment:
Votes: 0
plamzi said:
I don't feel forced to answer at all. I'm trying to help him learn how to use this forum right, just like I'm trying to help him with his coding problem. Everyone should learn not to make people troubleshoot code that no longer exists. Do you disagree?

I agree what you are saying, just not how you said it :)

>1. Am I correct to assume that in a fight with a char and a mob, get_weapon_sn (ch) returns the players weapon skill?
>2. Am I correct to assume that in the same fight get_weapon_sn (victim) returns the mobs weapon skill?

It is never correct to assume. Use GDB , put a breakpoint, and check the value by yourself. We have no idea what your get_weapon_skill method does maybe it has been modified and check if the victim is a npc and in this case, return the victim->fighting skill, WHO knows… unless we see the code we cannot know.
Probably not the problem though, only reason would be the mob is considered like a mirror of yourself…(gives me an idea hehe….)

3. If the mob still the (victim) and the player a (ch) to a bystander?

Again the best way to know that is to follow code running step by step, really do it, and it will probably become obvious where the problem is. reading code and executing it in your mind is not easy and you often miss stupid details.

The code "looks" fine as it is. But since a single char can change everything you will understand that it is always hard to help people just looking at code.
And since most people here have heavily modified code, unless they have a basic rom code running somewhere (wich I do not) it is hard to run gdb in your place.
05 Jan, 2013, Rarva.Riendf wrote in the 13th comment:
Votes: 0
Quote
…are exactly what they should be. So I'm even more puzzled now because there is nothing being done between these sprintf's and the act's


if buf1 buf2 and buf3 have the right values and all are different , then something is very wrong with you act method.
But you are you really sure about that.

and can you confirm that your sk != esk
06 Jan, 2013, thucar wrote in the 14th comment:
Votes: 0
And it is how I suspected… there's nothing wrong with my code… it was my understanding of the dam_message function, duh!

What I failed to understand, was that the dam_message handles one hit at a time, so if a player lands a hit, the ch is the player and the victim is the mob. And if a mob hits the player, the roles are reversed.

So thanks to everyone offering advice and we can mark this issue closed.
0.0/14