int probtype(char let) { int i = bases[letindex(let)]; int prob = rn2(100); while((prob -= objects[i].oc_prob) >= 0) i++; if(objects[i].oc_olet != let || !objects[i].oc_name) panic("probtype(%c) error, i=%d", let, i); return(i); }
void init_objects() { int i, j, first, last, sum, end; char let, *tmp; /* init base; if probs given check that they add up to 100, otherwise compute probs; shuffle descriptions */ end = nitems(objects); first = 0; while( first < end ) { let = objects[first].oc_olet; last = first+1; while(last < end && objects[last].oc_olet == let && objects[last].oc_name != NULL) last++; i = letindex(let); if((!i && let != ILLOBJ_SYM) || bases[i] != 0) error("initialization error"); bases[i] = first; if(let == GEM_SYM) setgemprobs(); check: sum = 0; for(j = first; j < last; j++) sum += objects[j].oc_prob; if(sum == 0) { for(j = first; j < last; j++) objects[j].oc_prob = (100+j-first)/(last-first); goto check; } if(sum != 100) error("init-prob error for %c", let); if(objects[first].oc_descr != NULL && let != TOOL_SYM){ /* shuffle, also some additional descriptions */ while(last < end && objects[last].oc_olet == let) last++; j = last; while(--j > first) { i = first + rn2(j+1-first); tmp = objects[j].oc_descr; objects[j].oc_descr = objects[i].oc_descr; objects[i].oc_descr = tmp; } } first = last; } }
int probtype(char let) { int i = bases[letindex(let)]; int prob = rn2(100); prob -= objects[i].oc_prob; while(prob >= 0) { ++i; prob -= objects[i].oc_prob; } if((objects[i].oc_olet != let) || (objects[i].oc_name == NULL)) { panic("probtype(%c) error, i=%d", let, i); } return i; }
static void setgemprobs() { int j,first; extern xchar dlevel; first = bases[letindex(GEM_SYM)]; for(j = 0; j < 9-dlevel/3; j++) objects[first+j].oc_prob = 0; first += j; if(first >= LAST_GEM || first >= nitems(objects) || objects[first].oc_olet != GEM_SYM || objects[first].oc_name == NULL) printf("Not enough gems? - first=%d j=%d LAST_GEM=%d\n", first, j, LAST_GEM); for(j = first; j < LAST_GEM; j++) objects[j].oc_prob = (20+j-first)/(LAST_GEM-first); }
struct obj *readobjnam(char *bp) { char *p; int i; int cnt; int spe; int spesgn; int typ; int heavy; char let; char *un; char *dn; char *an; /* int the = 0; */ /* char *oname = 0; */ heavy = 0; typ = heavy; spesgn = typ; spe = spesgn; cnt = spe; let = 0; un = 0; dn = un; an = dn; for(p = bp; *p != 0; ++p) { if(('A' <= *p) && (*p <= 'Z')) { *p += ('a' - 'A'); } } if(strncmp(bp, "the ", 4) == 0) { /* the = 1; */ bp += 4; } else if(strncmp(bp, "an ", 3) == 0) { cnt = 1; bp += 3; } else if(strncmp(bp, "a ", 2) == 0) { cnt = 1; bp += 2; } if((cnt == 0) && (digit(*bp) != 0)) { cnt = atoi(bp); while(digit(*bp) != 0) { ++bp; } while(*bp == ' ') { ++bp; } } if(cnt == 0) { /* %% What with "gems" etc.? */ cnt = 1; } if((*bp == '+') || (*bp == '-')) { if(*bp == '+') { spesgn = 1; } else { spesgn = 01; } ++bp; spe = atoi(bp); while(digit(*bp) != 0) { ++bp; } while(*bp == ' ') { ++bp; } } else { p = rindex(bp, '('); if(p != 0) { if((p > bp) && (p[-1] == ' ')) { p[-1] = 0; } else { *p = 0; } ++p; spe = atoi(p); while(digit(*p) != 0) { ++p; } if(strcmp(p, ")") != 0) { spe = 0; } else { spesgn = 1; } } } /* * Now we have the actual name, as delivered by xname, say: * green positions called whisky * scrolls labeled "QWERTY" * egg * dead zruties * fortune cookies * very heavy iron ball named hoei * wand of wishing * elven cloak */ for(p = bp; *p != 0; ++p) { if(strncmp(p, " named ", 7) == 0) { *p = 0; /* oname = p + 7; */ } } for(p = bp; *p != 0; ++p) { if(strncmp(p, " called ", 8) == 0) { *p = 0; un = p + 8; } } for(p = bp; *p != 0; ++p) { if(strncmp(p, " labeled ", 9) == 0) { *p = 0; dn = p + 9; } } int flag = 0; /* First change to singular if necessary */ if(cnt != 1) { /* Find "cloves of garlic", "worthless pieces of blue glass" */ for(p = bp; *p != 0; ++p) { if(strncmp(p, "s of ", 5) == 0) { *p = p[1]; while(*p != 0) { ++p; *p = p[1]; } flag = 1; break; } } if(flag == 0) { /* Remove -s or -es (boxes) or -ies (rubies, zruties) */ p = eos(bp); if(p[-1] == 's') { if(p[-2] == 'e') { if(p[-3] == 'i') { if(strcmp(p - 7, "cookies") == 0) { p[-1] = 0; flag = 1; } if(flag == 0) { strcpy(p - 3, "y"); flag = 1; } } if(flag == 0) { /* Note: cloves / knives from clove / knife */ if(strcmp(p - 6, "knives") == 0) { strcpy(p - 3, "fe"); flag = 1; } } if(flag == 0) { /* Note: nurses, axes but boxes */ if(strcmp(p - 5, "boxes") == 0) { p[-2] = 0; flag = 1; } } } if(flag == 0) { p[-1] = 0; } } else { if(strcmp(p - 9, "homunculi") == 0) { /* !! Makes string longer */ strcpy(p - 1, "us"); flag = 1; } if(flag == 0) { if(strcmp(p - 5, "teeth") == 0) { strcpy(p - 5, "tooth"); flag = 1; } } /* Here we cannot find the plural suffix */ } } } flag = 0; if(strcmp(bp, "amulet of yendor") == 0) { typ = AMULET_OF_YENDOR; flag = 2; } if(flag != 2) { p = eos(bp); /* Note: ring mail is not a ring! */ if(strcmp(p - 5, " mail") == 0) { let = ARMOR_SYM; an = bp; flag = 1; } if(flag != 1) { for(i = 0; i < sizeof(wrpsym); ++i) { int j = strlen(wrp[i]); if(strncmp(bp, wrp[i], j) == 0) { let = wrpsym[i]; bp += j; if(strncmp(bp, " of ", 4) == 0) { an += 4; } flag = 1; break; } if(strcmp(p - j, wrp[i]) == 0) { let = wrpsym[i]; p -= j; *p = 0; if(p[-1] == ' ') { p[-1] = 0; } dn = bp; flag = 1; break; } } } if(flag != 1) { if(strcmp(p - 6, " stone") == 0) { p[-6] = 0; let = GEM_SYM; an = bp; flag = 1; } } if(flag != 1) { if(strcmp(bp, "very heavy iron ball") == 0) { heavy = 1; typ = HEAVY_IRON_BALL; flag = 2; } } if(flag != 2) { if(flag != 1) { an = bp; } if((an == 0) && (dn == 0) && (un == 0)) { if(let == 0) { let = wrpsym[rn2(sizeof(wrpsym))]; } typ = probtype(let); flag = 2; } if(flag != 2) { i = 1; if(let != 0) { i = bases[letindex(let)]; } while((i <= NROFOBJECTS) && ((let == 0) || (objects[i].oc_olet == let))) { if((an != 0) && (strcmp(an, objects[i].oc_name) != 0)) { ++i; continue; } if((dn != 0) && (strcmp(an, objects[i].oc_descr) != 0)) { ++i; continue; } if((un != 0) && (strcmp(un, objects[i].oc_uname) != 0)) { ++i; continue; } typ = i; flag = 2; break; } if(flag != 2) { if(let == 0) { let = wrpsym[rn2(sizeof(wrpsym))]; } typ = probtype(let); } } } } struct obj *otmp; let = objects[typ].oc_olet; if((let == FOOD_SYM) && (typ >= CORPSE)) { if(typ > ((CORPSE + 'Z') - '@')) { let = ((typ - CORPSE) + '@') + (('a' - 'Z') - 1); } else { let = ((typ - CORPSE) + '@') + (0); } } otmp = mksobj(let, typ); if(heavy != 0) { otmp->owt = 15; } if((cnt > 0) && (index("%?!*)", let) != 0) && ((cnt < 4) || ((let == WEAPON_SYM) && (typ <= ROCK) && (cnt < 20)))) { otmp->quan = cnt; } if(spesgn == -1) { otmp->cursed = 1; } if((spe > 3) && (spe > otmp->spe)) { spe = 0; } else if(let == WAND_SYM) { spe = otmp->spe; } if((spe == 3) && (u.uluck < 0)) { spesgn = -1; } if((let != WAND_SYM) && (spesgn == -1)) { spe = -spe; } if(let == BALL_SYM) { spe = 0; } else if(let == AMULET_SYM) { spe = -1; } else if((typ == WAN_WISHING) && (rn2(10) != 0)) { spe = 0; } else if(let == ARMOR_SYM) { spe += (10 - objects[typ].a_ac); if((spe > 5) && (rn2(spe - 5) != 0)) { otmp->cursed = 1; } } otmp->spe = spe; return otmp; }
void init_objects() { int i; int j; int first; int last; int sum; int end; char let; char *tmp; /* * Init base; If probabilities given check that they add up to 100, * otherwise compute probabilities; Shuffle descriptions */ end = sizeof(objects) / sizeof(objects[0]); first = 0; while(first < end) { let = objects[first].oc_olet; last = first + 1; while((last < end) && (objects[last].oc_olet == let) && (objects[last].oc_name != NULL)) { ++last; } i = letindex(let); if(((i == 0) && (let != ILLOBJ_SYM)) || (bases[i] != 0)) { panic("initialization error"); } bases[i] = first; while(1) { #ifdef MKLEV if(let == GEM_SYM) { extern xchar dlevel; for(j = 0; j < (9 - (dlevel / 3)); ++j) { objects[first + j].oc_prob = 0; } first += j; if((first >= last) || (first >= LAST_GEM)) { printf("Not enough gems? - first=%d last=%d j=%d LAST_GEM=%d\n", first, last, j, LAST_GEM); } for(j = first; j < LAST_GEM; ++j) { objects[j].oc_prob = ((20 + j) - first) / (LAST_GEM - first); } } #endif sum = 0; for(j = first; j < last; ++j) { sum += objects[j].oc_prob; } if(sum == 0) { for(j = first; j < last; ++j) { objects[j].oc_prob = ((100 + j) - first) / (last - first); } continue; } break; } if(sum != 100) { #ifdef MKLEV panic("init-prob error for %c", let); #else hack_error("init-prob error for %c", let); #endif } /* * Shuffling is rather meaningless in mklev, * but we must update last anyway */ if((objects[first].oc_descr != NULL) && (let != TOOL_SYM)) { /* Shuffle, also some additional descriptions */ while((last < end) && (objects[last].oc_olet == let)) { ++last; } j = last; --j; while(j > first) { i = first + rn2((j + 1) - first); tmp = objects[j].oc_descr; objects[j].oc_descr = objects[i].oc_descr; objects[i].oc_descr = tmp; --j; } } first = last; } }