#include #include #include #include #include "defs.h" #include "lf.h" #include "nexus.h" #include "objects.h" #include "text.h" extern long curtime; int needan(char *text) { if (isvowel(tolower(text[0]))) { return B_TRUE; } return B_FALSE; } char *capitalise(char *text) { if (strlen(text) > 0) { text[0] = toupper(text[0]); } return text; } // capitalise all words char *capitaliseall(char *text) { if (strlen(text) > 0) { char *p; for (p = text ; *p; p++) { if (p == text) { // first letter *p = toupper(*p); } else if (*(p-1) == ' ') { // first letter after a space *p = toupper(*p); } } } return text; } char *dicetotext(int ndice, int nsides, int bonus, int *min, int *max, char *dicebuf, char *minmaxbuf) { int localmin, localmax; if (ndice == NA) ndice = 0; if (nsides == NA) nsides = 0; if (bonus == NA) bonus = 0; // ie. rolled a 1 on all dice localmin = (ndice * 1) + bonus; // ie. rolled max on all dice localmax = (ndice * nsides) + bonus; if (min) { *min = localmin; } if (max) { *max = localmax; } if (dicebuf) { if ((ndice == 0) || (nsides == 0)) { sprintf(dicebuf, "%d", bonus); } else { if (bonus) { sprintf(dicebuf, "%dd%d%c%d", ndice, nsides, (bonus > 0) ? '+' : '-', abs(bonus)); } else { sprintf(dicebuf, "%dd%d", ndice, nsides); } } } if (minmaxbuf) { if (localmin == localmax) { sprintf(minmaxbuf, "%d", localmin); } else { sprintf(minmaxbuf, "%d-%d", localmin, localmax); } } return dicebuf; } int flip(int ch) { switch (ch) { case 'a': return 0x0250; case 'b': return 'q'; case 'c': return 0x0254; case 'd': return 'p'; case 'e': return 0x01dd; case 'f': return 0x025f; case 'g': return 0x0183; case 'h': return 0x0265; case 'i': return 0x0131; case 'j': return 0x027e; case 'k': return 0x029e; case 'l': return 0x0283; case 'm': return 0x026f; case 'n': return 'u'; case 'r': return 0x0279; case 't': return 0x0287; case 'v': return 0x028c; case 'w': return 0x028d; case 'y': return 0x028e; case '.': return 0x02d9; case '[': return ']'; case '(': return ')'; case '{': return '}'; case '?': return 0x00bf; case '!': return 0x00a1; case '<': return '>'; case '_': return 0x203e; } return ch; } char *getattrabbrev(enum ATTRIB att) { switch (att) { case A_NONE: return "??"; case A_STR: return "St"; case A_IQ: return "Iq"; case A_DEX: return "Dx"; case A_CON: return "Cn"; } return "??"; } char *getattrname(enum ATTRIB att) { switch (att) { case A_NONE: return "?attrib_none?"; case A_STR: return "strength"; case A_IQ: return "intelligence"; case A_DEX: return "dexterity"; case A_CON: return "constitution"; } return "?badattrib?"; } int gethitconferlifetime(char *text, int *min, int *max) { int howlong; int localmin,localmax; if (text) { char loctext[BUFLEN]; char *word, *dummy; strcpy(loctext,text); word = strtok_r(loctext, "-", &dummy); if (word) { localmin = atoi(word); word = strtok_r(NULL, "-", &dummy); if (word) { localmax = atoi(word); howlong = rnd(localmin,localmax); } else { howlong = PERMENANT; } } else { localmin = -1; localmax = -1; howlong = PERMENANT; } } else { localmin = -1; localmax = -1; howlong = PERMENANT; } if (min) *min = localmin; if (max) *max = localmax; return howlong; } char *getpossessive(char *text) { char lastchar; // you -> your if (!strcmp(text, "you")) { return "r"; } // xxxs -> xxxs' lastchar = text[strlen(text)-1]; if (tolower(lastchar) == 's') { return "'"; } // default: 's return "'s"; } char *getdrunktext(flag_t *drunkflag) { int bracket; bracket = (drunkflag->lifetime / DRUNKTIME) + 1; if (bracket == 1) { return "tipsy"; } else if (bracket == 2) { return "drunk"; } else { return "very drunk"; } return "??drunk??"; } char *getsizetext(enum LFSIZE sz) { switch (sz) { case SZ_ENORMOUS: return "enormous"; case SZ_HUGE: return "huge"; case SZ_LARGE: return "large"; case SZ_HUMAN: return "human-sized"; case SZ_MEDIUM: return "medium"; case SZ_SMALL: return "small"; case SZ_MINI: case SZ_TINY: return "extremely small"; default: return "unknown-sized"; } return "unknown-sized"; } char *gettimetext(char *retbuf) { int hours,mins,secs; splittime(&hours, &mins, &secs); sprintf(retbuf, "%02d:%02d:%02d",hours,mins,secs); return retbuf; } char *gettimetextfuzzy(char *retbuf, int wantpm) { int hours,mins,secs; int pm = B_FALSE; splittime(&hours, &mins, &secs); if (hours > 12) { hours -= 12; pm = B_TRUE; } if (mins == 0) { sprintf(retbuf, "exactly %d o'clock", hours); } else if (mins <= 15) { sprintf(retbuf, "a little after %d o'clock", hours); } else if (mins <= 25) { sprintf(retbuf, "nearly half past %d", hours); } else if (mins <= 35) { sprintf(retbuf, "around half past %d", hours); } else if (mins <= 45) { sprintf(retbuf, "coming up to %d o'clock", (hours == 12) ? 1 : (hours+1)); } else { sprintf(retbuf, "nearly %d o'clock", (hours == 12) ? 1 : (hours+1)); } if (wantpm) { strcat(retbuf, " in the "); if (pm) { strcat(retbuf, "afternoon"); } else { strcat(retbuf, "morning"); } } return retbuf; } char *getweighttext(float weight, char *buf) { if (weight == 0) { sprintf(buf, "nothing"); } else if (weight >= 1) { if ((int)weight == weight) { // ie. is weight an integer? sprintf(buf, "%0.0f kg",weight); } else { sprintf(buf, "%0.1f kg",weight); } } else { sprintf(buf, "%0.0f grams", weight * 1000); } return buf; } char *is(lifeform_t *lf) { if (isplayer(lf)) return "are"; else return "is"; } int isvowel (char c) { switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': return B_TRUE; } return B_FALSE; } // allocates and returns new string char *makeplural(char *text) { char lastlet; char *newtext; int rv; newtext = strdup(text); // scrolls newtext = strrep(newtext, "berry ", "berries ", &rv); if (rv) return newtext; newtext = strrep(newtext, "block ", "blocks ", &rv); if (rv) return newtext; newtext = strrep(newtext, "can ", "cans ", &rv); if (rv) return newtext; newtext = strrep(newtext, "chunk ", "chunks ", &rv); if (rv) return newtext; newtext = strrep(newtext, "flask ", "flasks ", &rv); if (rv) return newtext; newtext = strrep(newtext, "gem ", "gems ", &rv); if (rv) return newtext; newtext = strrep(newtext, "leaf", "leaves", &rv); if (rv) return newtext; newtext = strrep(newtext, "loaf ", "loaves ", &rv); if (rv) return newtext; newtext = strrep(newtext, "lump ", "lumps ", &rv); if (rv) return newtext; newtext = strrep(newtext, "piece ", "pieces ", &rv); if (rv) return newtext; newtext = strrep(newtext, "pile ", "piles ", &rv); if (rv) return newtext; newtext = strrep(newtext, "pool ", "pools ", &rv); if (rv) return newtext; newtext = strrep(newtext, "potion ", "potions ", &rv); if (rv) return newtext; newtext = strrep(newtext, "puddle ", "puddles ", &rv); if (rv) return newtext; newtext = strrep(newtext, "ring ", "rings ", &rv); if (rv) return newtext; newtext = strrep(newtext, "scroll ", "scrolls ", &rv); if (rv) return newtext; newtext = strrep(newtext, "splash ", "splashes ", &rv); if (rv) return newtext; newtext = strrep(newtext, "set ", "sets ", &rv); if (rv) return newtext; newtext = strrep(newtext, "suit ", "suits ", &rv); if (rv) return newtext; newtext = strrep(newtext, "vial ", "vials ", &rv); if (rv) return newtext; // newtext = strrep(newtext, "pair ", "pairs ", &rv); // don't return // default lastlet = text[strlen(text)-1]; switch (lastlet) { char *temptext; case 'y': // change to 'ies' temptext = strdup(text); temptext[strlen(temptext)-1] = '\0'; asprintf(&newtext, "%sies",temptext); free(temptext); break; case 's': case 'o': // append "es" asprintf(&newtext, "%ses",text); break; default: // append "s" asprintf(&newtext, "%ss",text); break; } return newtext; } char *makeuppercase(char *text) { if (strlen(text) > 0) { char *p; for (p = text ; *p; p++) { *p = toupper(*p); } } return text; } int needses(char *text) { if (text[strlen(text)-1] == 's') { return B_TRUE; } if (strlen(text) >= 2) { if ((text[strlen(text)-2] == 'c') && (text[strlen(text)-1] == 'h')) { return B_TRUE; } } return B_FALSE; } char *noprefix(char *obname) { char *p; p = strchr(obname, ' '); if (p) { p++; return p; } else { return obname; } } char *numtotext(int num, char *buf) { switch (num) { case 1: sprintf(buf, "a"); break; case 2: sprintf(buf, "two"); break; case 3: sprintf(buf, "three"); break; case 4: sprintf(buf, "four"); break; case 5: sprintf(buf, "five"); break; case 6: sprintf(buf, "six"); break; case 7: sprintf(buf, "seven"); break; case 8: sprintf(buf, "eight"); break; case 9: sprintf(buf, "nine"); break; case 10: sprintf(buf, "ten"); break; default: sprintf(buf, "%d",num); break; } return buf; } // convert number to roman numerals // only copes with 1-10 char *roman(int num) { switch (num) { case 1: return "I"; case 2: return "II"; case 3: return "III"; case 4: return "IV"; case 5: return "V"; case 6: return "VI"; case 7: return "VII"; case 8: return "VIII"; case 9: return "IX"; case 10: return "X"; } return ""; } int speedtokph(int speed) { return speed * speed * speed; } void splittime(int *hours, int *mins, int *secs) { long left; left = curtime; *hours = left / 3600; left -= (*hours * 3600); *mins = left / 60; left -= (*mins * 60); *secs = left; } char *strrep(char *text, char *oldtok, char *newtok, int *rv) { char *temp; temp = strdup(" "); // ooooooo is this bad?? dostrrep(text, &temp, oldtok, newtok, rv); // swap text = realloc(text, strlen(temp)+1); // extra space for NUL strcpy(text, temp); free(temp); return text; } // returns TRUE if any replacements made char *dostrrep(char* in, char** out, char* oldtok, char* newtok, int *rv) { char *temp; char *found = strstr(in, oldtok); int idx; if(!found) { *out = realloc(*out, strlen(in) + 1); // oooooooo crashing in realloc strcpy(*out, in); if (rv) *rv = B_FALSE; return *out; } idx = found - in; *out = realloc(*out, strlen(in) - strlen(oldtok) + strlen(newtok) + 1); strncpy(*out, in, idx); strcpy(*out + idx, newtok); strcpy(*out + idx + strlen(newtok), in + idx + strlen(oldtok)); temp = malloc(idx+strlen(newtok)+1); strncpy(temp,*out,idx+strlen(newtok)); temp[idx + strlen(newtok)] = '\0'; dostrrep(found + strlen(oldtok), out, oldtok, newtok, rv); temp = realloc(temp, strlen(temp) + strlen(*out) + 1); strcat(temp,*out); free(*out); *out = temp; if (rv) *rv = B_TRUE; return *out; } int strpixmatch(char *haystack, char *needle) { int matched = B_FALSE; char *hword, *nword, *hcont,*ncont; if (strchr(needle, ' ') || strchr(haystack, ' ')) { char lochaystack[BUFLEN], locneedle[BUFLEN]; strcpy(lochaystack, haystack); strcpy(locneedle, needle); // match word for word nword = strtok_r(locneedle, " ", &ncont); hword = strtok_r(lochaystack, " ", &hcont); while (nword && hword) { // all typed words must match if (strcasestr(hword, nword)) { matched = B_TRUE; } else { matched = B_FALSE; break; } nword = strtok_r(NULL, " ", &ncont); hword = strtok_r(NULL, " ", &hcont); if (nword && !hword) { matched = B_FALSE; } } /* if (!matched && !strchr(needle, ' ')) { // now try matching typed word against second word in spellname strcpy(lochaystack, haystack); hword = strtok_r(lochaystack, " ", &hcont); while (hword) { if (strcasestr(hword, needle)) { matched = B_TRUE; break; } else { matched = B_FALSE; } hword = strtok_r(NULL, " ", &hcont); if (!hword) { matched = B_FALSE; } } } */ } else { if (strcasestr(haystack, needle)) { matched = B_TRUE; } } return matched; } int texttodice(char *text, int *ndice, int *nsides, int *bonus) { char *dummy; char *localtext; char *p,*plusloc; localtext = strdup(text); // number of dice p = strtok_r(localtext, "d", &dummy); if (!p) { return B_TRUE; } if (ndice) { *ndice = atoi(p); } // sides on each die p = strtok_r(NULL, "d", &dummy); if (!p) { return B_TRUE; } // strip out bonus plusloc = strchr(p, '+'); if (plusloc) *plusloc = '\0'; plusloc = strchr(p, '-'); if (plusloc) *plusloc = '\0'; if (nsides) { *nsides = atoi(p); } free(localtext); localtext = strdup(text); // bonus/plus if (bonus) { p = strchr(localtext, '+'); if (p) { *bonus = atoi(p+1); } else { p = strchr(localtext, '-'); if (p) { *bonus = -(atoi(p+1)); } else { *bonus = 0; } } } free(localtext); return B_FALSE; } void texttospellopts(char *text, int *power, char *damstr, int *needgrab, int *range) { char *p; int n; char *argname[] = { "pw:", "dam:", "needgrab:", "range:", NULL, }; void *argval[] = { power, damstr, needgrab, range, NULL, }; char argtype[] = { 'i', 's', 'b', 'i', '\0', }; // defaults if (power) *power = 0; if (damstr) strcpy(damstr, ""); if (needgrab) *needgrab = B_FALSE; if (range) *range = 0; if (!strlen(text)) { return; } // for each arg for (n = 0; argname[n]; n++) { // search for it in text... for (p = text ; *p ; p++) { if (!strncmp(p, argname[n], strlen(argname[n])) ) { char localval[BUFLEN]; char *valfull; strcpy(localval, p + strlen(argname[n])); valfull = strtok(localval, ";"); if (valfull) { if (argval[n]) { if (argtype[n] == 'i') { *((int *)argval[n]) = atoi(valfull); } else if (argtype[n] == 'b') { *((int *)argval[n]) = atoi(valfull) ? B_TRUE : B_FALSE; } else if (argtype[n] == 's') { strcpy((char *)argval[n], valfull); } } break; } } } } } char *you(lifeform_t *lf) { if (isplayer(lf)) { return "You"; } return "It"; } char *you_l(lifeform_t *lf) { if (isplayer(lf)) { return "you"; } return "it"; } char *your(lifeform_t *lf) { if (isplayer(lf)) { return "Your"; } return "Its"; } char *your_l(lifeform_t *lf) { if (isplayer(lf)) { return "your"; } return "its"; }