/* * Pickup inventory code. * * The reason this code exists is that we want a cross between two * different inventories. For example, if we're carrying 3 potions of * healing and we pick up one more, we want the display to mention * just one potion of healing, but with the inventory letter for the * resulting "4 potions of healing" inventory item. * * We assume, here, that INVOBJs being picked up correspond one-to-one * to INVOBJs in the resulting inventory. Since inventory collapsing * works the same way for all inventories, this should be true. */ #include #include #include "vars.h" #include "pline.h" #include "display.h" #include "stdio-util.h" #include "pickup-inv.h" typedef struct piline PILINE; struct piline { PILINE *link; char *text; } ; struct pickup_inv { PILINE *list; PILINE **listt; char *srctext; } ; PICKUP_INV *pickup_inv_init(void) { PICKUP_INV *pi; pi = malloc(sizeof(PICKUP_INV)); pi->list = 0; pi->listt = 0; pi->srctext = 0; return(pi); } void pickup_inv_cb(MWC_OP op, INVOBJ *io, void *piv) { PICKUP_INV *pi; PILINE *l; FILE *f; pi = piv; switch (op) { case MWC_BEGIN: pi->listt = &pi->list; pi->srctext = 0; break; case MWC_PRE: if (pi->srctext) panic("pickup_inv PRE with text"); f = fopenmalloc(&pi->srctext); (*objtypes[io->v[0]->type].ops->format)(f,io); fclose(f); break; case MWC_POST: if (! pi->srctext) panic("pickup_inv POST with no text"); l = malloc(sizeof(PILINE)); f = fopenmalloc(&l->text); format_inv_letter(io->xwi,f); fprintf(f,"%s",pi->srctext); fclose(f); free(pi->srctext); pi->srctext = 0; *pi->listt = l; pi->listt = &l->link; break; case MWC_DONE: if (pi->srctext) panic("pickup_inv DONE with text"); break; case MWC_END: if (pi->srctext) panic("pickup_inv END with text"); *pi->listt = 0; break; } } int pickup_inv_report_one(PICKUP_INV *pi) { if (pi->list && !pi->list->link) { pline("%s",pi->list->text); free(pi->list->text); free(pi->list); free(pi); return(1); } return(0); } void pickup_inv_report_list(void *piv) { PICKUP_INV *pi; PILINE *l; DLLS line(FILE *f) { if (! l) return(DL_L_DONE); fprintf(f,"%s",l->text); l = l->link; return(DL_L_MORE); } DLKS key(int ch) { switch (ch) { case ' ': case '\r': case '\n': return(DL_K_CONTINUE); break; case '\e': return(DL_K_ABORT); break; } return(DL_K_IGNORE); } pi = piv; l = pi->list; display_list(&line,"Picked up",&key); while ((l = pi->list)) { pi->list = l->link; free(l->text); free(l); } free(pi); }