#include #include #include #include #include "defs.h" #include "lf.h" #include "objects.h" #include "text.h" extern long curtime; char *capitalise(char *text) { if (strlen(text) > 0) { text[0] = toupper(text[0]); } return text; } 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 *getattrname(enum ATTRIB att) { switch (att) { case A_STR: return "strength"; case A_IQ: return "intelligence"; case A_DEX: return "dexterity"; default: break; } return "?unknown?"; } 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 *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 >= 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; } 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, "gem ", "gems ", &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, "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) { case 'y': // change to 'ies' text[strlen(text)-1] = '\0'; asprintf(&newtext, "%sies",text); break; case 's': case 'o': // append "es" asprintf(&newtext, "%ses",text); break; default: // append "s" asprintf(&newtext, "%ss",text); break; } return newtext; } 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 ""; } 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; 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; } if (nsides) { *nsides = atoi(p); } // bonus/plus if (bonus) { p = strtok_r(NULL, "+", &dummy); if (p) { *bonus = atoi(p); } else { *bonus = 0; } } free(localtext); return B_FALSE; } void texttospellopts(char *text, int *power, char *damstr, int *needgrab) { char *p; int n; char *argname[] = { "pw:", "dam:", "needgrab:", NULL, }; void *argval[] = { power, damstr, needgrab, NULL, }; char argtype[] = { 'i', 's', 'b', '\0', }; // defaults if (power) *power = 0; if (damstr) strcpy(damstr, ""); if (needgrab) *needgrab = B_FALSE; // 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"; }