/* make a random new object, returning pointer */ Object* create_object(int itemlevel) { Object* newObject; int r; int ok = false; while (! ok) { newObject = ((Object*) checkmalloc(sizeof(Object))); r= random_range(135); if (r < 20) make_thing(newObject,-1); else if (r < 40) make_food(newObject,-1); else if (r < 50) make_scroll(newObject,-1); else if (r < 60) make_potion(newObject,-1); else if (r < 70) make_weapon(newObject,-1); else if (r < 80) make_armor(newObject,-1); else if (r < 90) make_shield(newObject,-1); else if (r < 100) make_stick(newObject,-1); else if (r < 110) make_boots(newObject,-1); else if (r < 120) make_cloak(newObject,-1); else if (r < 130) make_ring(newObject,-1); else make_artifact(newObject,-1); /* not ok if object is too good for level, or if unique and already made */ /* 1/100 chance of finding object if too good for level */ ok = ((newObject->uniqueness < UNIQUE_MADE) && ((newObject->level < itemlevel+random_range(3)) || (random_range(100)==23))); if (!ok) { free_obj( newObject, true ); } } if (newObject->uniqueness == UNIQUE_UNMADE) Objects[newObject->id].uniqueness=UNIQUE_MADE; return(newObject); }
int main(void) { //OC_comp_fp_t f_ptr; struct Ordered_container* container_ptr = OC_create_container(&thing_comp_f); OC_insert(container_ptr, make_thing(5)); OC_insert(container_ptr, make_thing(3)); OC_insert(container_ptr, make_thing(4)); OC_insert(container_ptr, make_thing(1)); OC_insert(container_ptr, make_thing(2)); OC_insert(container_ptr, make_thing(99)); OC_insert(container_ptr, make_thing(-2)); for (int i = 0; i < 7; ++i) { printf("Element %d: %d\n", i, *(int*)(*(container_ptr->array + i))); } printf("size = %d\n", OC_get_size(container_ptr)); printf("Empty = %d\n", OC_empty(container_ptr)); int search_num = 5; int* data_ptr = OC_find_item(container_ptr, &search_num); printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr)); search_num = 99; printf("Apply_if_arg returned: %d\n", OC_apply_if_arg(container_ptr, (OC_apply_if_arg_fp_t)apply_if_arg_tester, &search_num)); data_ptr = OC_find_item_arg(container_ptr, &search_num, alt_thing_comp_f); printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr)); printf("Things: %d\n", g_num_things); OC_apply(container_ptr, &delete_thing); OC_clear(container_ptr); printf("Things: %d\n", g_num_things); printf("size = %d\n", OC_get_size(container_ptr)); printf("Empty = %d\n", OC_empty(container_ptr)); OC_destroy_container(container_ptr); void* test = malloc(8); printf("Old way %p New Way %p\n", *(void**)test, test); return 0; }
int main(void) { //OC_comp_fp_t f_ptr; struct Ordered_container* container_ptr = OC_create_container(&thing_comp_f); OC_insert(container_ptr, make_thing(5)); OC_insert(container_ptr, make_thing(3)); OC_insert(container_ptr, make_thing(4)); OC_insert(container_ptr, make_thing(1)); OC_insert(container_ptr, make_thing(2)); OC_insert(container_ptr, make_thing(99)); OC_insert(container_ptr, make_thing(-2)); printf("size = %d\n", OC_get_size(container_ptr)); printf("Empty = %d\n", OC_empty(container_ptr)); Print_container(container_ptr, sizeof(int)); int search_num = 5; int* data_ptr = OC_find_item(container_ptr, &search_num); printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr)); search_num = 99; printf("Apply_if_arg returned: %d\n", OC_apply_if_arg(container_ptr, (OC_apply_if_arg_fp_t)apply_if_arg_tester, &search_num)); data_ptr = OC_find_item_arg(container_ptr, &search_num, alt_thing_comp_f); printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr)); printf("Things: %d\n", g_num_things); OC_apply(container_ptr, &delete_thing); OC_clear(container_ptr); printf("Things: %d\n", g_num_things); printf("size = %d\n", OC_get_size(container_ptr)); printf("Empty = %d\n", OC_empty(container_ptr)); Print_container(container_ptr, sizeof(int)); OC_destroy_container(container_ptr); return 0; }
/* gain for an item */ void acquire(int blessing) { char otype; int index,id = ABORT; pob newthing; if (blessing < 0) { index = random_item(); if (index == ABORT) mprint("You feel fortunate."); else { print1("Smoke drifts out of your pack.... "); print2("Destroyed: "); nprint2(itemid(Player.possessions[index])); morewait(); dispose_lost_objects(1,Player.possessions[index]); } } else { newthing = ((pob) checkmalloc(sizeof(objtype))); /* DAG this assignment looks unneccessary */ newthing->id = -1; if (gamestatusp(CHEATED)) print1("Acquire which kind of item: !?][}{)/=%%\\& "); else print1("Acquire which kind of item: !?][}{)/=%%\\ "); otype = mgetc(); switch (otype) { case (POTION&0xff): if (blessing > 0) id = itemlist(POTIONID,NUMPOTIONS); else id = random_range(NUMPOTIONS); if (id < 0) print2("You feel stupid."); else make_potion(newthing,id); break; case (SCROLL&0xff): if (blessing > 0) id = itemlist(SCROLLID,NUMSCROLLS); else id = random_range(NUMSCROLLS); if (id < 0) print2("You feel stupid."); else make_scroll(newthing,id); break; case (RING&0xff): if (blessing > 0) id = itemlist(RINGID,NUMRINGS); else id = random_range(NUMRINGS); if (id < 0) print2("You feel stupid."); else make_ring(newthing,id); break; case (STICK&0xff): if (blessing > 0) id = itemlist(STICKID,NUMSTICKS); else id = random_range(NUMSTICKS); if (id < 0) print2("You feel stupid."); else make_stick(newthing,id); break; case (ARMOR&0xff): if (blessing > 0) id = itemlist(ARMORID,NUMARMOR); else id = random_range(NUMARMOR); if (id < 0) print2("You feel stupid."); else make_armor(newthing,id); break; case (SHIELD&0xff): if (blessing > 0) id = itemlist(SHIELDID,NUMSHIELDS); else id = random_range(NUMSHIELDS); if (id < 0) print2("You feel stupid."); else make_shield(newthing,id); break; case (WEAPON&0xff): if (blessing > 0) id = itemlist(WEAPONID,NUMWEAPONS); else id = random_range(NUMWEAPONS); if (id < 0) print2("You feel stupid."); else make_weapon(newthing,id); break; case (BOOTS&0xff): if (blessing > 0) id = itemlist(BOOTID,NUMBOOTS); else id = random_range(NUMBOOTS); if (id < 0) print2("You feel stupid."); else make_boots(newthing,id); break; case (CLOAK&0xff): if (blessing > 0) id = itemlist(CLOAKID,NUMCLOAKS); else id = random_range(NUMCLOAKS); if (id < 0) print2("You feel stupid."); else make_cloak(newthing,id); break; case (FOOD&0xff): if (blessing > 0) id = itemlist(FOODID,NUMFOODS); else id = random_range(NUMFOODS); if (id < 0) print2("You feel stupid."); else make_food(newthing,id); break; case (THING&0xff): if (blessing > 0) id = itemlist(THINGID,NUMTHINGS); else id = random_range(NUMTHINGS); if (id < 0) print2("You feel stupid."); else make_thing(newthing,id); break; case (ARTIFACT&0xff): if (gamestatusp(CHEATED)) id = itemlist(ARTIFACTID,NUMARTIFACTS); else id = -1; if (id < 0) print2("You feel stupid."); else make_artifact(newthing,id); break; default: print2("You feel stupid."); } xredraw(); if (id != ABORT) { if (blessing > 0) { newthing->known = 2; Objects[id].known = 1; } newthing->used = FALSE; gain_item(newthing); } else { /* DAG newthing allocated but was not freed... was YA memory leak */ /* use free() rather than free_obj() since newthing not initialized */ free( (char *) newthing ); } } }