Welcome to the Builder Academy

Question Separating out Skills and Spells to two different functions

More
19 Nov 2024 16:18 - 19 Nov 2024 18:35 #10436 by zi
Hello all. After a LOT (a few days) of messing around with spec_procs.c I'm stumped. I'd like to separate spells from skills so a player (not in around a guild mob) types 'spells' or 'skills' they get the appropriate return (specifically - list just spells with spells and just skills with skills).

I set up a sort_skills function to handle the array and start it AFTER the spells - right now I'm using TBA stock spells that go up to 54, and my skills start at 131 in spells.h. 

I'm close, I can taste it... when I adjust int a in sort_spells to 100 and call that function it will just return the skills- success! BUT when I set a to 1 for sort_spells and a =55 for sort_skills, I get garbage. I've changed all the integer variables as a test (int c instead of a) but that didn't make a difference.

Where am I going wrong? Thanks.
Code:
static int spell_sort_info[MAX_SKILLS + 1]; static int skill_sort_info[MAX_SKILLS + 1]; static int compare_spells(const void *x, const void *y) {   int   a = *(const int *)x,         b = *(const int *)y;   return strcmp(spell_info[a].name, spell_info[b].name); } void sort_skills(void) {   int a;   /* initialize array, avoiding reserved. */   for (a = 55; a <= 200; a++)     skill_sort_info[a] = a; /* I don't care about sorting right now just freakin show the skills*/   /* qsort(&skill_sort_info[1], MAX_SKILLS, sizeof(int), compare_spells);*/ } void sort_spells(void) {   int a;   /* initialize array, avoiding reserved. */   for (a = 1; a <= 200; a++)     spell_sort_info[a] = a;   qsort(&spell_sort_info[1], MAX_SKILLS, sizeof(int), compare_spells); } /* here's my how_good - took it out because it's pretty but has nothing to do with this problem */ void list_skills(struct char_data *ch) {   const char *overflow = "\r\n**OVERFLOW**\r\n";   int i, sortpos, ret;   size_t len = 0;   char buf2[MAX_STRING_LENGTH]; send_to_char(ch, "   --- [SKILL DATABASE] ---\r\n");   for (sortpos = 1; sortpos <= MAX_SKILLS; sortpos++) {     i = skill_sort_info[sortpos];     if (GET_LEVEL(ch) >= spell_info[i].min_level[(int) GET_CLASS(ch)]) {     ret = snprintf(buf2 + len, sizeof(buf2) - len, "%-20s %s\r\n", spell_info[i].name, how_good(GET_SKILL(ch, i)));       if (ret < 0 || len + ret >= sizeof(buf2))         break;       len += ret;     }   }   if (len >= sizeof(buf2))     strcpy(buf2 + sizeof(buf2) - strlen(overflow) - 1, overflow); /* strcpy: OK */   page_string(ch->desc, buf2, TRUE);   send_to_char(ch,  "\n    - You have %d practice%s available -\r\n", GET_PRACTICES(ch),         GET_PRACTICES(ch) == 1 ? "" : "s"); } void list_spells(struct char_data *ch) {   const char *overflow = "\r\n**OVERFLOW**\r\n";   int i, sortpos, ret;   size_t len = 0;   char buf2[MAX_STRING_LENGTH]; send_to_char(ch, "   --- [PROGRAM DATABASE] ---\r\n");   for (sortpos = 1; sortpos <= MAX_SKILLS; sortpos++) {     i = spell_sort_info[sortpos];     if (GET_LEVEL(ch) >= spell_info[i].min_level[(int) GET_CLASS(ch)]) {     ret = snprintf(buf2 + len, sizeof(buf2) - len, "%-20s %s\r\n", spell_info[i].name, how_good(GET_SKILL(ch, i)));       if (ret < 0 || len + ret >= sizeof(buf2))         break;       len += ret;     }   }   if (len >= sizeof(buf2))     strcpy(buf2 + sizeof(buf2) - strlen(overflow) - 1, overflow); /* strcpy: OK */   page_string(ch->desc, buf2, TRUE);   send_to_char(ch,  "\n    - You have %d download%s available -\r\n", GET_PRACTICES(ch),         GET_PRACTICES(ch) == 1 ? "" : "s"); }

_,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_
Making 3M Great Again!
telnet://3m.funcity.org:3000
_,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_
Last edit: 19 Nov 2024 18:35 by zi. Reason: really specify what I'd like for the output

Please Log in or Create an account to join the conversation.

More
20 Nov 2024 13:56 #10444 by thomas
The problem is that skill_sort_info looks like this when the sorting starts:
0,0,0,0,...,0,55,56,57,...
spell_info[0] has name !UNUSED! and will be sorted first, resulting in a list like this
0,0,0,0,0,...,89,123,67,...
Later you start out at sort_pos=1, an that isn't right. You need to start at position 55.

Note this is an educated guess, I haven't actually tried it. ;)

Please Log in or Create an account to join the conversation.

More
20 Nov 2024 16:08 #10445 by zi
Hey- thanks for the reply! Always cool to see a red profile reply to a thread!

Okay. So, changing the sortpos still returns garbage - when I call the function it spits out some of a trigger. interestingly enough, after calling sort_spells and then call sort_skills it spits out 15 or so spells and then garbage/crash.

I know this is pretty dense stuff but as I continue to learn C my gut says I'm missing something to do with how the array is parsed OR I'm missing something to reset a variable/pointer?

_,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_
Making 3M Great Again!
telnet://3m.funcity.org:3000
_,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

Please Log in or Create an account to join the conversation.

More
20 Nov 2024 20:18 #10446 by thomas
A closer look reveals it's a simple problem. Change
Code:
for (a = 1; a <= 200; a++)
to
Code:
for (a = 1; a < 55; a++)
in the sort_spells function. I've tested the rest and it looks good to me.

I recommend switching to using "MAX_SPELL" instead of the volatile "55", though

Please Log in or Create an account to join the conversation.

More
20 Nov 2024 21:11 - 20 Nov 2024 21:12 #10447 by zi
for list_spells I have a set to MAX_SPELLS (the 200 was a hold over from a test) so that will limit the array up to whatever, 55?
for list_skills I have a set to MAX_SKILLS (200) - the skills start at 131 and go to 200 (not really that many, but there's space for them in spells.h).

Here's my debugging:

If in list_spells (the original function that came with stock TBA) I change 'a' to 100 then it will list JUST the skills as it captures everything beyond 100 (131 being the first skill). Totally what I want.

The problem is when I set 'a' to 100 in my new list_skills function I don't get the same result. I copied list_spells over to list_skills, set all the same parameters, even changed the sort call with new variables - still get garbage. I'm screwing something up with C

_,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_
Making 3M Great Again!
telnet://3m.funcity.org:3000
_,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_
Last edit: 20 Nov 2024 21:12 by zi. Reason: wording- this gets complex real fast!

Please Log in or Create an account to join the conversation.

More
Yesterday 22:03 #10448 by thomas
Hm. This is the literal changes I made to your code to get it to work:
Code:
diff --git a/src/act.other.c b/src/act.other.c index 4798105..583d51a 100644 --- a/src/act.other.c +++ b/src/act.other.c @@ -277,8 +277,10 @@ ACMD(do_practice) if (*arg) send_to_char(ch, "You can only practice skills in your guild.\r\n"); - else + else { list_skills(ch); + list_spells(ch); + } } ACMD(do_visible) diff --git a/src/db.c b/src/db.c index fda7ac6..a1ef865 100644 --- a/src/db.c +++ b/src/db.c @@ -752,6 +752,7 @@ void boot_db(void) log("Sorting command list and spells."); sort_commands(); + sort_skills(); sort_spells(); log("Booting mail system."); diff --git a/src/spec_procs.c b/src/spec_procs.c index c852622..5745c42 100644 --- a/src/spec_procs.c +++ b/src/spec_procs.c @@ -35,6 +35,7 @@ static void npc_steal(struct char_data *ch, struct char_data *victim); /* Special procedures for mobiles. */ static int spell_sort_info[MAX_SKILLS + 1]; +static int skill_sort_info[MAX_SKILLS + 1]; @@ -45,13 +46,23 @@ static int compare_spells(const void *x, const void *y) return strcmp(spell_info[a].name, spell_info[b].name); } +void sort_skills(void) +{ + int a; + /* initialize array, avoiding reserved. */ + for (a = 55; a <= 200; a++) + skill_sort_info[a] = a; + + /* I don't care about sorting right now just freakin show the skills*/ + qsort(&skill_sort_info[1], MAX_SKILLS, sizeof(int), compare_spells); +} void sort_spells(void) { int a; /* initialize array, avoiding reserved. */ - for (a = 1; a <= MAX_SKILLS; a++) + for (a = 1; a <= 54; a++) spell_sort_info[a] = a; qsort(&spell_sort_info[1], MAX_SKILLS, sizeof(int), compare_spells); @@ -96,6 +107,7 @@ static const char *prac_types[] = { #define MAXGAIN(ch) (prac_params[MAX_PER_PRAC][(int)GET_CLASS(ch)]) #define SPLSKL(ch) (prac_types[prac_params[PRAC_TYPE][(int)GET_CLASS(ch)]]) +/* void list_skills(struct char_data *ch) { const char *overflow = "\r\n**OVERFLOW**\r\n"; @@ -116,10 +128,62 @@ void list_skills(struct char_data *ch) len += ret; } } + if (len >= sizeof(buf2)) + strcpy(buf2 + sizeof(buf2) - strlen(overflow) - 1, overflow); / * strcpy: OK * / + + page_string(ch->desc, buf2, TRUE); +} +*/ + +void list_skills(struct char_data *ch) +{ + const char *overflow = "\r\n**OVERFLOW**\r\n"; + int i, sortpos, ret; + size_t len = 0; + char buf2[MAX_STRING_LENGTH]; + send_to_char(ch, " --- [SKILL DATABASE] ---\r\n"); + for (sortpos = 1; sortpos <= MAX_SKILLS; sortpos++) { + i = skill_sort_info[sortpos]; + if (GET_LEVEL(ch) >= spell_info[i].min_level[(int) GET_CLASS(ch)]) { + ret = snprintf(buf2 + len, sizeof(buf2) - len, "%-20s %s\r\n", spell_info[i].name, how_good(GET_SKILL(ch, i))); + if (ret < 0 || len + ret >= sizeof(buf2)) + break; + len += ret; + } + } if (len >= sizeof(buf2)) strcpy(buf2 + sizeof(buf2) - strlen(overflow) - 1, overflow); /* strcpy: OK */ page_string(ch->desc, buf2, TRUE); + + send_to_char(ch, "\n - You have %d practice%s available -\r\n", GET_PRACTICES(ch), + GET_PRACTICES(ch) == 1 ? "" : "s"); +} + + +void list_spells(struct char_data *ch) +{ + const char *overflow = "\r\n**OVERFLOW**\r\n"; + int i, sortpos, ret; + size_t len = 0; + char buf2[MAX_STRING_LENGTH]; + send_to_char(ch, " --- [PROGRAM DATABASE] ---\r\n"); + for (sortpos = 1; sortpos <= MAX_SKILLS; sortpos++) { + i = spell_sort_info[sortpos]; + if (GET_LEVEL(ch) >= spell_info[i].min_level[(int) GET_CLASS(ch)]) { + ret = snprintf(buf2 + len, sizeof(buf2) - len, "%-20s %s\r\n", spell_info[i].name, how_good(GET_SKILL(ch, i))); + if (ret < 0 || len + ret >= sizeof(buf2)) + break; + len += ret; + } + } + if (len >= sizeof(buf2)) + strcpy(buf2 + sizeof(buf2) - strlen(overflow) - 1, overflow); /* strcpy: OK */ + + page_string(ch->desc, buf2, TRUE); + + send_to_char(ch, "\n - You have %d download%s available -\r\n", GET_PRACTICES(ch), + GET_PRACTICES(ch) == 1 ? "" : "s"); } SPECIAL(guild) diff --git a/src/spec_procs.h b/src/spec_procs.h index 3bb8bd7..df9cc60 100644 --- a/src/spec_procs.h +++ b/src/spec_procs.h @@ -34,7 +34,9 @@ const char *get_spec_func_name(SPECIAL(*func)); ****************************************************************************/ /* Utility functions */ void sort_spells(void); +void list_spells(struct char_data *ch); void list_skills(struct char_data *ch); +void sort_skills(void); /* Special functions */ SPECIAL(guild);
The following user(s) said Thank You: zi

Please Log in or Create an account to join the conversation.

Time to create page: 0.222 seconds