Joy of Snippets: vdd.txt help anyone?

From: Mathew Earle Reuther (graymere@zipcon.net)
Date: 08/02/02


After having managed to get my color woes under control (I shall hereby
declare not underlining every bloody thing to be "under control) I moved
on to a VDD snippet from the rooms section of the FTP site.  I'm including
the full text of MY version in this email, but so as not to completely
thash your mailboxes, I'm just providing a link to the original.

ftp://ftp.circlemud.org/pub/CircleMUD/contrib/code/rooms/vdd.txt

Basic point of the code is to take a condition and give two different room
description options depending on which one is true.  So say you're using a
day/night conditional (it's one that was included) and you come into a
room at night, it might say: The citizens of the city seem to be at home
safe in bed, only the few nightcrawlers and watchmen seem to be anywhere
around . . . while in the day you'd see something implying the activity of
the faceless masses during more active hours.

The problem is, currently it finds the very last conditional and prints
everything after that.  So if you have a 3 paragraph description and four
or five of the conditionals, you end up with a tiny fraction of your room
description.  Bummer. :)

I'm at the limit of my abilities here, and any assistance folks can
render, I'd appreciate it.  I understand what the code is doing in
general, I just can't figure out what's gone wrong with it.  Getting rid
of the 50 lines of errors, compiling it with only one warning (somewhere
after while in ProcRoomDesc it suggested brackets, anyone see why please
let me know) and having it not crash the mud seemed to be good work
already. *grin*

These are the four functions which handle workings of the string
replacement code.  I left out the other junk.

int FindOpenClose(char *buf, char** begin, char** end)
{
  int Level=0;

  while (*buf && *buf!='{')
    buf++;
  if (!(*buf))
    return 0;
  *begin=(++buf);
  Level++;
  while (*buf && Level) {
    if (*buf=='{') Level++;
    else if (*buf=='}')
      Level--;
    buf++;
    }
  if (Level)
    return 0;
  *end=buf;
  return 1;
}

int CheckVDD(char *TextBuf, struct char_data *ch)
{
  struct char_data *temp;
  int First;

  if (!strn_cmp(TextBuf,"time",4))
    First=(time_info.hours>=5&&time_info.hours<=21);
    else if (!strn_cmp(TextBuf,"clouds",6))
      First=weather_info.sky==SKY_CLOUDLESS;
    else if (!strn_cmp(TextBuf,"rain",4))
      First=weather_info.sky<SKY_RAINING;
    else if (!strn_cmp(TextBuf,"random00",8))
      First=rand_number(0, 1000)==0;
    else if (!strn_cmp(TextBuf,"random25",8))
      First=rand_number(0, 3)==0;
    else if (!strn_cmp(TextBuf,"random33",8))
      First=rand_number(0, 2)==0;
    else if (!strn_cmp(TextBuf,"random50",8))
      First=rand_number(0, 1)==0;
    else if (!strn_cmp(TextBuf,"god",3)) {
      temp=world[ch->in_room].people;
    for (;temp;temp=temp->next_in_room) {
      if (IS_NPC(temp))
        continue;
      if (GET_LEVEL(temp)<LVL_IMMORT)
        continue;
      if (GET_INVIS_LEV(temp))
        continue;
      break;
      }

    First=!temp;
    }
    else return -1;
  return First;
}

int DoVDDReplace(char *Read, char *Write, char *pos, struct char_data *ch)
{
/*  int Level=0; */ /* Unused Var */
  char *Begin, *End;
  int First;

  *Write=0;
  strncat(Write,Read,pos-Read);
  pos+=2;
  First=CheckVDD(pos,ch); /* set First to val returned from CheckVVD */
    if (First==-1)
      return 0;
    if (!FindOpenClose(pos, &Begin, &End))
      return 0;
    pos=End;
    if (!First)
      if (!FindOpenClose(pos, &Begin, &End))
        return 0;
      strncat(Write, Begin, End-Begin-1);
    if (First)
      if (!FindOpenClose(pos, &Begin, &End))
        return 0;
    strcat(Write,End);
  return 1;
}

char *ProcRoomDesc(char *desc, struct char_data *ch)
{
  static char BufA[MAX_STRING_LENGTH], BufB[MAX_STRING_LENGTH];
  char *ReadingBuf, *WritingBuf, *pos; /* , *T; */

  ReadingBuf=BufA;
  WritingBuf=BufB;
  strcpy(ReadingBuf,desc);
  while (pos=strstr(ReadingBuf,"||")) {
    if (!DoVDDReplace(ReadingBuf, WritingBuf, pos, ch))
      return desc;
    /*  T=ReadingBuf;
        ReadingBuf=WritingBuf;
        WritingBuf=T; */
        WritingBuf=ReadingBuf;
        }
  return ReadingBuf;
}

Any assistance would be appreciated.  If this gets up and running I'll be
sure to drop the updated version over on the FTP site so folks can use it
if they please.

-Mathew

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT