/* Say something! */ void Niall_Reply(char *Buffer,int BufSize) { WORD *Word; /* Clear the buffer. */ Buffer[0]=0; /* Check we have some words to say */ if(WordList==NULL) { Niall_Warning("Corrupted brain (Not initialised)."); Niall_NewDictionary(); return; } if(CountProbs(WordList)==0) { strcpy(Buffer,"I cannot speak yet!"); return; } /* Speak some words of wisdom. */ StringWord(Buffer,BufSize,WordList); Buffer[0]=toupper(Buffer[0]); //fprintf(stderr, "speaking 1\n"); safeStrcat(Buffer,BufSize,"."); //fprintf(stderr, "speaking 2\n"); }
/* Add the next word on to the end of the buffer */ static void StringWord(char *Buffer,int BufSize,WORD *ThisWord) { int nProbs,iProb,total; WORD *NextWord; ASCN *Assoc; /* Randomly select an index for the next word. */ nProbs = CountProbs(ThisWord); if(nProbs<1) { Niall_Warning("Corrupted brain (Unlinked word)."); return; } /* Taken from the rand(3) manual page... */ iProb = (int)( (float)nProbs*(float)rand() / ((float)RAND_MAX+1.0) ); /* Find the next word. */ for(total=0,Assoc=ThisWord->Associations;Assoc;Assoc=Assoc->Next) { total+=Assoc->Probability; if(total>iProb) { NextWord = GetWord(Assoc->Word); if(NextWord != END_SENTENCE) { if(strlen(Buffer)) safeStrcat(Buffer,BufSize," "); safeStrcat(Buffer,BufSize,NextWord->Data); StringWord(Buffer,BufSize,NextWord); return; } else return; } } Niall_Warning("Corrupted brain (Loop Overflow)."); }
/** * Formats AGI string. * This function turns a AGI string into a real string expanding values * according to the AGI format specifiers. * @param s string containing the format specifier * @param n logic number */ char *TextMgr::stringPrintf(const char *originalText) { static char resultPrintfBuffer[2000]; Common::String resultString; char z[16]; debugC(3, kDebugLevelText, "logic %d, '%s'", _vm->_game.curLogicNr, originalText); while (*originalText) { switch (*originalText) { case '%': originalText++; switch (*originalText++) { int i; case 'v': i = strtoul(originalText, NULL, 10); while (*originalText >= '0' && *originalText <= '9') originalText++; sprintf(z, "%015i", _vm->getVar(i)); i = 99; if (*originalText == '|') { originalText++; i = strtoul(originalText, NULL, 10); while (*originalText >= '0' && *originalText <= '9') originalText++; } if (i == 99) { // remove all leading 0 // don't remove the 3rd zero if 000 for (i = 0; z[i] == '0' && i < 14; i++) ; } else { i = 15 - i; } safeStrcat(resultString, z + i); break; case '0': i = strtoul(originalText, NULL, 10) - 1; safeStrcat(resultString, _vm->objectName(i)); break; case 'g': i = strtoul(originalText, NULL, 10) - 1; safeStrcat(resultString, _vm->_game.logics[0].texts[i]); break; case 'w': i = strtoul(originalText, NULL, 10) - 1; safeStrcat(resultString, _vm->_words->getEgoWord(i)); break; case 's': i = strtoul(originalText, NULL, 10); safeStrcat(resultString, stringPrintf(_vm->_game.strings[i])); break; case 'm': i = strtoul(originalText, NULL, 10) - 1; if (_vm->_game.logics[_vm->_game.curLogicNr].numTexts > i) safeStrcat(resultString, stringPrintf(_vm->_game.logics[_vm->_game.curLogicNr].texts[i])); break; } while (*originalText >= '0' && *originalText <= '9') originalText++; break; case '\\': originalText++; // FALL THROUGH default: resultString += *originalText++; break; } } assert(resultString.size() < sizeof(resultPrintfBuffer)); strcpy(resultPrintfBuffer, resultString.c_str()); return resultPrintfBuffer; }
/** * Formats AGI string. * This function turns a AGI string into a real string expanding values * according to the AGI format specifiers. * @param s string containing the format specifier * @param n logic number */ char *AgiEngine::agiSprintf(const char *s) { static char agiSprintf_buf[768]; Common::String p; char z[16]; debugC(3, kDebugLevelText, "logic %d, '%s'", _game.lognum, s); while (*s) { switch (*s) { case '%': s++; switch (*s++) { int i; case 'v': i = strtoul(s, NULL, 10); while (*s >= '0' && *s <= '9') s++; sprintf(z, "%015i", getvar(i)); i = 99; if (*s == '|') { s++; i = strtoul(s, NULL, 10); while (*s >= '0' && *s <= '9') s++; } if (i == 99) { // remove all leading 0 // don't remove the 3rd zero if 000 for (i = 0; z[i] == '0' && i < 14; i++) ; } else { i = 15 - i; } safeStrcat(p, z + i); break; case '0': i = strtoul(s, NULL, 10) - 1; safeStrcat(p, objectName(i)); break; case 'g': i = strtoul(s, NULL, 10) - 1; safeStrcat(p, _game.logics[0].texts[i]); break; case 'w': i = strtoul(s, NULL, 10) - 1; safeStrcat(p, _game.egoWords[i].word); break; case 's': i = strtoul(s, NULL, 10); safeStrcat(p, agiSprintf(_game.strings[i])); break; case 'm': i = strtoul(s, NULL, 10) - 1; if (_game.logics[_game.lognum].numTexts > i) safeStrcat(p, agiSprintf(_game.logics[_game.lognum].texts[i])); break; } while (*s >= '0' && *s <= '9') s++; break; case '\\': s++; // FALL THROUGH default: p += *s++; break; } } assert(p.size() < sizeof(agiSprintf_buf)); strcpy(agiSprintf_buf, p.c_str()); return agiSprintf_buf; }