Public Group

# Coding problem

This topic is 3853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So, i'm hookin autostance up and for some reason it wont save the autostance and also the players are saying they arn't actually ever dropping into the stance whether attacking or being attacked.. can anyone see why?
 void do_autostance(CHAR_DATA *ch, char *argument )
{
char arg [MAX_INPUT_LENGTH];

one_argument( argument, arg, MAX_INPUT_LENGTH );
if (IS_NPC(ch)) return;
if (!str_cmp(arg,"none"))
{
send_to_char("Autostance set to None.\n\r", ch );
ch-&gt;stance[AUTODROP] = STANCE_NONE;
}
else if (!str_cmp(arg, "crane"))
{
send_to_char("Autostance set to Crane.\n\r",ch );
ch-&gt;stance[AUTODROP] = STANCE_CRANE;
}
else if (!str_cmp(arg, "bull"))
{
send_to_char("Autostance set to Bull.\n\r", ch );
ch-&gt;stance[AUTODROP] = STANCE_BULL;
}
else if (!str_cmp(arg, "viper"))
{
send_to_char("Autostance set to Viper.\n\r", ch );
ch-&gt;stance[AUTODROP] = STANCE_VIPER;
}
else if (!str_cmp(arg, "mongoose"))
{
send_to_char("Autostance set to Mongoose.\n\r", ch);
ch-&gt;stance[AUTODROP] = STANCE_MONGOOSE;
}
else if (!str_cmp(arg, "swallow") && ch-&gt;stance[STANCE_CRANE] &gt;= 200 && ch-&gt;stance[STANCE_MONGOOSE] &gt;= 200 )
{
send_to_char("Autostance set to Swallow.\n\r", ch);
ch-&gt;stance[AUTODROP] = STANCE_SWALLOW;
}
else if (!str_cmp(arg, "lion") && ch-&gt;stance[STANCE_BULL] &gt;= 200 && ch-&gt;stance[STANCE_VIPER] &gt;= 200 )
{
send_to_char("Autostance set to Lion.\n\r", ch);
ch-&gt;stance[AUTODROP] = STANCE_LION;
}
else if (!str_cmp(arg, "falcon") && ch-&gt;stance[STANCE_MONGOOSE] &gt;= 200 && ch-&gt;stance[STANCE_BULL] &gt;= 200 )
{
send_to_char("Autostance set to Falcon.\n\r", ch);
ch-&gt;stance[AUTODROP] = STANCE_FALCON;
}
else if (!str_cmp(arg, "cobra") && ch-&gt;stance[STANCE_VIPER] &gt;= 200 && ch-&gt;stance[STANCE_CRANE] &gt;= 200 )
{
send_to_char("Autostance set to Cobra.\n\r", ch);
ch-&gt;stance[AUTODROP] = STANCE_COBRA;
}
else if (!str_cmp(arg, "panther") && ch-&gt;stance[STANCE_VIPER] &gt;= 200 && ch-&gt;stance[STANCE_MONGOOSE] &gt;= 200 )
{
send_to_char("Autostance set to Panther.\n\r", ch);
ch-&gt;stance[AUTODROP] = STANCE_PANTHER;
}
else if (!str_cmp(arg, "grizzlie") && ch-&gt;stance[STANCE_BULL] &gt;= 200 && ch-&gt;stance[STANCE_CRANE] &gt;= 200 )
{
send_to_char("Autostance set to Grizzlie.\n\r", ch);
ch-&gt;stance[AUTODROP] = STANCE_GRIZZLIE;
}
else send_to_char ("No such Stance.\n\r", ch);
}

void autodrop(CHAR_DATA *ch)
{
char buf [MAX_INPUT_LENGTH];
char buf2 [MAX_INPUT_LENGTH];
char stancename [10];

if (IS_NPC(ch)) return;
if (ch-&gt;stance[AUTODROP]==STANCE_NONE) return;
else if (ch-&gt;stance[AUTODROP]==STANCE_VIPER) sprintf(stancename,"viper");
else if (ch-&gt;stance[AUTODROP]==STANCE_CRANE) sprintf(stancename,"crane");
else if (ch-&gt;stance[AUTODROP]==STANCE_MONGOOSE) sprintf(stancename,"mongoose");
else if (ch-&gt;stance[AUTODROP]==STANCE_BULL) sprintf(stancename,"bull");
else if (ch-&gt;stance[AUTODROP]==STANCE_SWALLOW) sprintf(stancename, "swallow");
else if (ch-&gt;stance[AUTODROP]==STANCE_LION) sprintf(stancename, "lion");
else if (ch-&gt;stance[AUTODROP]==STANCE_FALCON) sprintf(stancename, "falcon");
else if (ch-&gt;stance[AUTODROP]==STANCE_PANTHER) sprintf(stancename, "panther");
else if (ch-&gt;stance[AUTODROP]==STANCE_COBRA) sprintf(stancename, "cobra");
else if (ch-&gt;stance[AUTODROP]==STANCE_GRIZZLIE) sprintf(stancename, "grizzlie");
else return;
if (ch-&gt;stance[0] &lt; 1)
{
ch-&gt;stance[0] = ch-&gt;stance[AUTODROP];;
sprintf(buf, "You fall into the %s stance.", stancename);
act(buf, ch, NULL, NULL, TO_CHAR);
sprintf(buf2, "\$n falls into the %s stance.",stancename);
act(buf2, ch, NULL, NULL, TO_ROOM);
}
}


Thanks:) [Edited by - Sandman on May 30, 2008 5:18:21 AM]

##### Share on other sites
Game design is for discussion of game mechanics, rather than coding related stuff.

It might help to be a bit clearer about what the code is supposed to do, and what it actually does. If you aren't sure exactly what it is doing, then step through it in a debugger and see for yourself what the behaviour is. Also, comment how it is doing it a bit more, because there are a few things in that code that look a bit odd.

You'll probably find that the above steps will find your problem fairly quickly, and if it doesn't, we'll be better equipped to find the problem for you.

EDIT: also, [ source ] tags are best for posting code.

##### Share on other sites
I answered your other post abour saving it.

Regarding dropping in to the stance, is the autodrop() function ever called? (probably needs to go somewhere in fight.c - maybe multi_hit(...) ? )

##### Share on other sites
Alright in the MUD there are 10 stances viper,mongoose, ect. as read above. This code is meant to set the command do_autostance, in which you do autostance viper and when you start a fight, as long as you are not in a stance already, and have an autostance set, it will automatically make you drop into the stance you wish instead of manually typing stance viper every time before you start a fight. I fixed the autostance so that it will actually make the person automatically make you drop into the stance by making a call to autodrop in set_fighting in fight.c. What it does right now is say You autodrop into a fighting stance. I wish it to say you autodrop into the x fighting stance. It also needs to save to the player file. Which I totally have no clue how to do. In which it will save the stance you have autostance set to in the playerfile, so when you log back in you do not have to set your autostance again.

##### Share on other sites
Quote:
 Original post by GutlanWhat it does right now is say You autodrop into a fighting stance. I wish it to say you autodrop into the x fighting stance.

This is probably because you're using sprintf() wrong in your big else if block. Specifically, you've missed out the format specifiers.

You currently have:
 else if (ch->stance[AUTODROP]==STANCE_VIPER) sprintf(stancename,"viper");   else if (ch->stance[AUTODROP]==STANCE_CRANE) sprintf(stancename,"crane");   else if (ch->stance[AUTODROP]==STANCE_MONGOOSE) sprintf(stancename,"mongoose");   else if (ch->stance[AUTODROP]==STANCE_BULL) sprintf(stancename,"bull");// ...etc

You should have something like:
 else if (ch->stance[AUTODROP]==STANCE_VIPER)  {    //             FORMAT SPECIFIER!    //                  |||||    //                  VVVVV    sprintf(stancename, "%s", "viper"); } else if (ch->stance[AUTODROP]==STANCE_CRANE)  {    sprintf(stancename, "%s", "crane"); } else if (ch->stance[AUTODROP]==STANCE_MONGOOSE)  {    sprintf(stancename, "%s", "mongoose"); } else if (ch->stance[AUTODROP]==STANCE_BULL)  {    sprintf(stancename, "%s", "bull"); }// ...etc

Alternatively, you could use strncpy() which is a bit simpler and a lot safer than sprintf and more appropriate for a case where you're just copying a string from one buffer to another, without any additional formatting.

Quote:
 It also needs to save to the player file. Which I totally have no clue how to do. In which it will save the stance you have autostance set to in the playerfile, so when you log back in you do not have to set your autostance again.

You'll probably want to look into functions such as fopen(), fclose(), fread(), fwrite(), fprintf(), fscanf() and fflush() for handling file IO.

##### Share on other sites
Quote:
Original post by Sandman
Quote:
 Original post by GutlanWhat it does right now is say You autodrop into a fighting stance. I wish it to say you autodrop into the x fighting stance.

This is probably because you're using sprintf() wrong in your big else if block. Specifically, you've missed out the format specifiers.

I was under the impression that that would work, its just a really bad idea?

Quote:
Original post by Sandman
Quote:
 It also needs to save to the player file. Which I totally have no clue how to do. In which it will save the stance you have autostance set to in the playerfile, so when you log back in you do not have to set your autostance again.

You'll probably want to look into functions such as fopen(), fclose(), fread(), fwrite(), fprintf(), fscanf() and fflush() for handling file IO.

I covered what he'd need to do to save and load the field in one of his other threads here. No response as to if he actually tried it though.

##### Share on other sites
Quote:
 Original post by xaninI was under the impression that that would work, its just a really bad idea?

Actually no you're right, it's fine, I'm having a stupid day. Although arguably anything with the letters 'printf' in it could be considered a really bad idea. I'm assuming the project is pure C rather than C++ though, so he's probably limited in his choices.

I'm not entirely sure what the problem is; I don't quite understand how the stance array is being used, which doesn't really help. What the hell is the significance of stance[0]?

My advice to the OP would be to step through with a debugger.

##### Share on other sites
Quote:
Original post by Sandman
Quote:
 Original post by xaninI was under the impression that that would work, its just a really bad idea?

Actually no you're right, it's fine, I'm having a stupid day. Although arguably anything with the letters 'printf' in it could be considered a really bad idea. I'm assuming the project is pure C rather than C++ though, so he's probably limited in his choices.

I'm not entirely sure what the problem is; I don't quite understand how the stance array is being used, which doesn't really help. What the hell is the significance of stance[0]?

My advice to the OP would be to step through with a debugger.

It's a Godwars derivative mud, so it is (if i recall rightly) a pure C environment when stock.

I'm also not sure what the problem is because he hasn't given us any information about what doesn't work. As far as I can tell, what he posted looks like it should work - or generate compile errors. Since hes not posting about compiler errors and he's not providing any of the output from when the function is called, its sort of hard to tell. Without more information, sending him to the debugger is going to be the best bet I imagine...

1. 1
2. 2
Rutin
19
3. 3
4. 4
khawk
14
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013643
×