Welcome to the Builder Academy

Question Newbie code snippet problem

More
17 Feb 2016 04:52 - 17 Feb 2016 16:29 #5599 by evorg
EDIT: I forgot about act.h (DOH)
EDIT: Please ignore!

I'm trying to copy/paste this snippet into my TBA mud:

www.circlemud.org/pub/CircleMUD/contrib/...ts/skills/forage.txt

I'm getting this error when I make:
Code:
$ make make ../bin/circle make[1]: Entering directory '/home/Parker/tba/src' gcc -g -O2 -Wall -Wno-char-subscripts -c -o class.o class.c gcc -g -O2 -Wall -Wno-char-subscripts -c -o interpreter.o interpreter.c interpreter.c:151:44: error: ‘do_forage’ undeclared here (not in a function) { "forage" , "fora" , POS_STANDING, do_forage , 0, 0 }, ^ <builtin>: recipe for target 'interpreter.o' failed make[1]: *** [interpreter.o] Error 1 make[1]: Leaving directory '/home/Parker/tba/src' Makefile:30: recipe for target 'all' failed make: *** [all] Error 2

I followed the instructions and I added my own object and added the command to spells.h,
spell_parser.c and interpreter.c. The skill itself is in act.other.c - What am I doing wrong?

My code looks like this:
Code:
ACMD(do_forage) { struct obj_data *item_found = '\0'; int item_no = 27219; /* Initialize with first item poss. */ *buf = '\0'; if GET_CLASS(ch) != CLASS_THIEF && GET_LEVEL(ch) <= 100) { send_to_char("You have no idea how to forage for survival!\r\n", ch); return; } if(GET_MOVE(ch) < 100) { send_to_char("You do not have enough energy right now.\r\n", ch); return; } if(SECT(ch->in_room) != SECT_FIELD && SECT(ch->in_room) != SECT_FOREST && SECT(ch->in_room) != SECT_HILLS && SECT(ch->in_room) != SECT_MOUNTAIN && SECT(ch->in_room) != SECT_SWAMP) { send_to_char("You cannot forage on this type of terrain!\r\n", ch); return; } if(GET_SKILL(ch, SKILL_FORAGE) <= 0) { send_to_char("You have no idea how to forage!\r\n", ch); return; } send_to_char("You start searching the area for signs of food.\r\n", ch); act("$n starts foraging the area for food.\r\n", FALSE, ch, 0, 0, TO_ROOM); if(number(1,101) > GET_SKILL(ch, SKILL_FORAGE)) { WAIT_STATE(ch, PULSE_VIOLENCE * 2); GET_MOVE(ch) -= (100 - GET_LEVEL(ch)); send_to_char("\r\nYou have no luck finding anything to eat.\r\n", ch); return; } else { switch (number(1,2)) { case 1: item_no = 27219; break; /*<--- Here are the objects you need to code */ case 2: /* Add more or remove some, just change the */ item_no = 10; break; /* switch(number(1, X) */ } WAIT_STATE( ch, PULSE_VIOLENCE * 2); /* Not really necessary */ GET_MOVE(ch) -= (150 - GET_LEVEL(ch)); item_found = read_object( item_no, VIRTUAL); obj_to_char(item_found, ch); sprintf(buf, "%sYou have found %s!\r\n", buf, item_found->short_description); send_to_char(buf, ch); act("$n has found something in his forage attempt.\r\n", FALSE, ch, 0, 0, TO_ROOM); return; } }

Thanks.
Last edit: 17 Feb 2016 16:29 by evorg.

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

More
18 Feb 2016 19:41 #5600 by thomas
Replied by thomas on topic Newbie code snippet problem
You need to add the line

ACMD(do_forage);

above the long array in interpreter.c. It's called "declaring" the function, and you need to declare a function before using it (like do_forage in the line youæve added already).

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

More
18 Feb 2016 23:18 #5601 by WhiskyTest
Learning from old snippets is great, that's how I started.
You've probably done this already, but for anyone else learning from these older snippets there are a few changes before you can drop them into tbaMUD...

Send_to_char is backwards:
- send_to_char("You have no idea how to forage for survival!\r\n", ch);
+ send_to_char(ch, "You have no idea how to forage for survival!\r\n");

Sprintf is deprecated, you should use snprintf:
- sprintf(buf, "%sYou have found %s!\r\n", buf, item_found->short_description);
+ snprintf(buf, sizeof(buf), "%sYou have found %s!\r\n", buf, item_found->short_description);

number is oldschool, use rand_number - or even dice (rolls 1x 101 sided dice in the below example)
- if(number(1,101) > GET_SKILL(ch, SKILL_FORAGE)) {
+ if(rand_number(1,101) > GET_SKILL(ch, SKILL_FORAGE)) {
|+ if(dice(1,101) > GET_SKILL(ch, SKILL_FORAGE)) {
The following user(s) said Thank You: Chime

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

More
19 Feb 2016 00:09 #5602 by evorg
Replied by evorg on topic Newbie code snippet problem
Thank you for the nice and informative responses!

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

More
01 Apr 2018 18:10 - 01 Apr 2018 18:43 #7862 by Nibiru
Replied by Nibiru on topic Newbie code snippet problem
I'm trying to implement this snippet.
However this line of the code is creating a compiler error.

ACMD(do_forage)
{
struct obj_data *item_found = '\0';
int item_no = 10053; /* Initialize with first item poss. */
*buf = '\0';

$ make
make ../bin/circle
make[1]: Entering directory '/home/kadiya/tbamud/src'
gcc -g -O2 -Wall -Wno-char-subscripts -Wno-unused-but-set-variable -c -o act.other.o act.other.c
act.other.c: In function ‘do_forage’:
act.other.c:858:5: error: ‘buf’ undeclared (first use in this function)
*buf = '\0';


When I change *buf to int buf it will compile but any attempt to use the command instantly crashes the mud.

Any suggestions?

When the crash happens it is at the point where the action to forage would be taking place. It functions properly if on wrong terrain or if the player does not have the forage skill.

Nibiru
Last edit: 01 Apr 2018 18:43 by Nibiru. Reason: Additional Information:

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

More
01 Apr 2018 18:56 - 01 Apr 2018 18:57 #7863 by JTP
Replied by JTP on topic Newbie code snippet problem
You need:

char buf[MAX_INPUT_LENGTH] = "\0";

instead of *buf = '\0';
Last edit: 01 Apr 2018 18:57 by JTP.

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

Time to create page: 0.210 seconds