Bug in Drink containers

  • cunning
  • Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
11 months 4 weeks ago - 11 months 4 weeks ago #8716 by cunning
Bug in Drink containers was created by cunning
I thought i was losing my mind after my game kept crashing. So i tested on a fresh GIT of tbamud. I have confirmed what I am seeing, and i am trying to fix this bug now. What happens is that name_from_drinkcon and name_to_drinkcon are not working as intended.

The keyword (liquid type) is to be removed and replaced with the new liquid type. However, we are getting a reiteration of that string, which deletes one word and places the entire string. This is a beer mug output, after i poured it out, refilled from fountain and poured out again. Normally I would just have "MUG WATER" but you can see what i get below.

00H 100M 82V (news) (motd) > stat mug
Name: 'an empty beer mug', Keywords: mug beer beer beer beer beer beer

VNum: [34304], RNum: [ 4627], Idnum: [30000115], Type: LIQ CONTAINER, SpecProc:
None
L-Desc: 'An empty beer mug is here ready for use.'
A-Desc: '<None>'
Extra descs: [beermug mug]
Can be worn on: TAKE HOLD
Set char bits : NOBITS
Extra flags : NOBITS
Weight: 3, Value: 1, Cost/day: 0, Timer: 0, Min level: 1
In room: 65535 (Nowhere), In object: None, Carried by: Austinpowers, Worn by: No
body
Capacity: 2, Contains: 0, Poisoned: NO, Liquid: water
Affections: None
Triggers:
Last edit: 11 months 4 weeks ago by cunning.

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

  • cunning
  • Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
11 months 4 weeks ago #8717 by cunning
Replied by cunning on topic Bug in Drink containers
Further testing shows that filling from fountain, then pouring out starts the chain reaction. I am keeping the output and debug in case Thomas wants to log into my dev server. I however have been able to reproduce multiple times now.

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

  • cunning
  • Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
11 months 4 weeks ago #8718 by cunning
Replied by cunning on topic Bug in Drink containers
May 16 09:55:24 2020 :: AustinPowers: pour mug out
May 16 09:55:24 2020 :: NAME_FROM: liqlen = 4
May 16 09:55:24 2020 :: NAME_FROM: maxlen = 10
May 16 09:55:24 2020 :: 1) NAME_FROM: name = mug green beer
May 16 09:55:24 2020 :: 2) NAME_FROM: name = mug green beer
May 16 09:55:24 2020 :: 1) NAME_FROM: name = mug green beer
May 16 09:55:24 2020 :: 3) NAME_FROM: name = mug green beer, maxlen = 10
May 16 09:55:24 2020 :: 4) NAME_FROM: name = mug green beer, new_name
May 16 09:55:24 2020 :: 5) NAME_FROM: name = mug green beer, new_name mug green , maxlen = 10
May 16 09:55:24 2020 :: 1) NAME_FROM: name = green beer
May 16 09:55:24 2020 :: 2) NAME_FROM: name = green beer
May 16 09:55:24 2020 :: 1) NAME_FROM: name = green beer
May 16 09:55:24 2020 :: 3) NAME_FROM: name = green beer, maxlen = 7
May 16 09:55:24 2020 :: 4) NAME_FROM: name = green beer, new_name mug green
May 16 09:55:24 2020 :: 5) NAME_FROM: name = green beer, new_name mug green green , maxlen = 6
May 16 09:55:24 2020 :: 1) NAME_FROM: name = beer
May 16 09:55:24 2020 :: 2) NAME_FROM: name = beer
May 16 09:55:24 2020 :: 1) NAME_FROM: name = beer
May 16 09:55:26 2020 :: AustinPowers: stat mug

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

More
11 months 3 weeks ago #8719 by thomas
Replied by thomas on topic Bug in Drink containers
Thank you for the bug report. I know this is aprt of the mud that's seen multiple fixes over the years and seem to remember bugs allt the way back to circle 2.0 for this.
I'll have a look tuesday.

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

More
11 months 3 weeks ago #8722 by JTP
Replied by JTP on topic Bug in Drink containers
I wonder if this has something to do with that I sometimes gets really heavy drink containers. That is rather annoying.

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

  • cunning
  • Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
11 months 2 weeks ago - 11 months 2 weeks ago #8736 by cunning
Replied by cunning on topic Bug in Drink containers
No. That has to do with weight change name_from and name_to*, and if you dig back into D20, encumberage is something that needs to be accounted for.
Last edit: 11 months 2 weeks ago by cunning.

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

  • cunning
  • Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
11 months 1 week ago #8739 by cunning
Replied by cunning on topic Bug in Drink containers
any word on this yet? I reverted back to the old code version and had no issue with name-xx-drinkcon. I have not had time to dig into this myself, i am blasted with work and some medical stuff.

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

More
11 months 1 week ago #8740 by thomas
Replied by thomas on topic Bug in Drink containers
I'm glad you've found a workaround. I'm a bit more busy than expacted right now, what with corona, home office and other, more private matters. I'll look into it when Io get the chance :)

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

More
11 months 1 week ago - 11 months 1 week ago #8741 by JTP
Replied by JTP on topic Bug in Drink containers
Well I just know for a fact that we have several times had a drink container that suddently weighed so Much that when someone died they couldn’t Pick it up again. But not every time. So a bit Strange. But it was some times almost like it Got heavier and heavier over time
Last edit: 11 months 1 week ago by JTP.

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

  • cunning
  • Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
11 months 1 week ago #8743 by cunning
Replied by cunning on topic Bug in Drink containers
JTP,
These are not the same issues, I suggest you open another topic to discuss them. I do not want to confuse the issue here.

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

More
11 months 1 week ago #8750 by WhiskyTest
Replied by WhiskyTest on topic Bug in Drink containers
Yeah I've reproduced this in the current code.
I think the bug is in 'name_from_drinkcon()'

Manipulating strings is definitely not my strong suite..
I googled up some C code which removes duplicate words from strings, and also removes trailing whitespace, this is what I ended up with:
/**
 * Remove all occurrences of a given word in string.
 */
void removeAll(char *str, const char *toRemove)
{
    int i, j, stringLen, toRemoveLen;
    int found;

    stringLen   = strlen(str);      // Length of string
    toRemoveLen = strlen(toRemove); // Length of word to remove


    for(i=0; i <= stringLen - toRemoveLen; i++)
    {
        /* Match word with string */
        found = 1;
        for(j=0; j<toRemoveLen; j++)
        {
            if(str[i + j] != toRemove[j])
            {
                found = 0;
                break;
            }
        }

        /* If it is not a word */
        if(str[i + j] != ' ' && str[i + j] != '\t' && str[i + j] != '\n' && str[i + j] != '\0') 
        {
            found = 0;
        }

        /*
         * If word is found then shift all characters to left
         * and decrement the string length
         */
        if(found == 1)
        {
            for(j=i; j<=stringLen - toRemoveLen; j++)
            {
                str[j] = str[j + toRemoveLen];
            }

            stringLen = stringLen - toRemoveLen;

            // We will match next occurrence of word from current index.
            i--;
        }
    }
    
}

char *ltrim(const char *s)
{
  char *r = strdup(s);
  if (r != NULL)
  {
    char *fr = r + strlen(s) - 1;
    while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr;
    *++fr = 0;
  }

  return r;
}

void name_from_drinkcon(struct obj_data *obj)
{
  const char *liqname;

  if (!obj || (GET_OBJ_TYPE(obj) != ITEM_DRINKCON && GET_OBJ_TYPE(obj) != ITEM_FOUNTAIN))
    return;

  liqname = drinknames[GET_OBJ_VAL(obj, 2)];
 
  removeAll(obj->name, liqname);

  obj->name = ltrim(obj->name);
 
}
I'm not sure this is the correct way to handle changing strings, I suspect I need to free the old one and create a new one? If anyone can confirm that would be great :D

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

More
11 months 1 week ago #8754 by thomas
Replied by thomas on topic Bug in Drink containers
You're not freeing the old obj->name before putting a new one in. If you change name_from_drinkcon to this, it should probably be ok:
void name_from_drinkcon(struct obj_data *obj)
{
  const char *liqname;
  char *new_name;

  if (!obj || (GET_OBJ_TYPE(obj) != ITEM_DRINKCON && GET_OBJ_TYPE(obj) != ITEM_FOUNTAIN))
    return;

  liqname = drinknames[GET_OBJ_VAL(obj, 2)];
 
  removeAll(obj->name, liqname);
  new_name = ltrim(obj->name); 
  free(obj->name);
  obj->name = new_name;
 
}
The following user(s) said Thank You: WhiskyTest

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

More
11 months 1 week ago #8755 by thomas
Replied by thomas on topic Bug in Drink containers
strictly speaking, the ltrim() function right trims

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

More
11 months 1 week ago #8766 by WhiskyTest
Replied by WhiskyTest on topic Bug in Drink containers
Thanks Thomas!
I've made your suggested changes and tested - looks good so far :)

I renamed the new functions and put them into Utils.c as the may come in handy for other features.

I've created a pull request on GitHub
The following user(s) said Thank You: thomas, lacrc

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

  • cunning
  • Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
11 months 6 days ago #8770 by cunning
Replied by cunning on topic Bug in Drink containers
I have been using it for a while. here are two complimentary functions as well.
char *ltrim (char *arg) {

  for (; isspace((int)(*arg)); arg++);

  return (arg);

}

char *rtrim (char *arg) {

       while (*(arg+(strlen(arg)-1)) == ' ') {
         *(arg + (strlen(arg) -1)) = '\0';
       }

  return (arg);

}

char *trim (char *arg) {

  for (; isspace((int)(*arg)); arg++);

  while (*(arg+(strlen(arg)-1)) == ' ') {
    *(arg + (strlen(arg) -1)) = '\0';              
  }

  return (arg);

}

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

Time to create page: 0.142 seconds