Source:NetHack 3.4.3/src/o init.c

From NetHackWiki
(Redirected from Source:Ref/init objects)
Jump to: navigation, search

Below is the full text to src/o_init.c from NetHack 3.4.3. To link to a particular line, write [[o_init.c#line123]], for example.

Top of file

  1. /*	SCCS Id: @(#)o_init.c	3.4	1999/12/09	*/
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

  1. #include "hack.h"
  2. #include "lev.h"	/* save & restore info */
  3.  
  4. STATIC_DCL void FDECL(setgemprobs, (d_level*));
  5. STATIC_DCL void FDECL(shuffle,(int,int,BOOLEAN_P));
  6. STATIC_DCL void NDECL(shuffle_all);
  7. STATIC_DCL boolean FDECL(interesting_to_discover,(int));
  8.  
  9.  
  10. static NEARDATA short disco[NUM_OBJECTS] = DUMMY;

shuffle_tiles

  1. #ifdef USE_TILES
  2. STATIC_DCL void NDECL(shuffle_tiles);
  3. extern short glyph2tile[];	/* from tile.c */
  4.  
  5. /* Shuffle tile assignments to match descriptions, so a red potion isn't
  6. * displayed with a blue tile and so on.
  7. *
  8. * Tile assignments are not saved, and shouldn't be so that a game can
  9. * be resumed on an otherwise identical non-tile-using binary, so we have
  10. * to reshuffle the assignments from oc_descr_idx information when a game
  11. * is restored.  So might as well do that the first time instead of writing
  12. * another routine.
  13. */
  14. STATIC_OVL void
  15. shuffle_tiles()
  16. {
  17. 	int i;
  18. 	short tmp_tilemap[NUM_OBJECTS];
  19.  
  20. 	for (i = 0; i < NUM_OBJECTS; i++)
  21. 		tmp_tilemap[i] =
  22. 			glyph2tile[objects[i].oc_descr_idx + GLYPH_OBJ_OFF];
  23.  
  24. 	for (i = 0; i < NUM_OBJECTS; i++)
  25. 		glyph2tile[i + GLYPH_OBJ_OFF] = tmp_tilemap[i];
  26. }
  27. #endif	/* USE_TILES */

setgemprobs

  1. STATIC_OVL void
  2. setgemprobs(dlev)
  3. d_level *dlev;
  4. {
  5. 	int j, first, lev;
  6.  
  7. 	if (dlev)
  8. 	    lev = (ledger_no(dlev) > maxledgerno())
  9. 				? maxledgerno() : ledger_no(dlev);
  10. 	else
  11. 	    lev = 0;
  12. 	first = bases[GEM_CLASS];
  13.  
  14. 	for(j = 0; j < 9-lev/3; j++)
  15. 		objects[first+j].oc_prob = 0;
  16. 	first += j;
  17. 	if (first > LAST_GEM || objects[first].oc_class != GEM_CLASS ||
  18. 	    OBJ_NAME(objects[first]) == (char *)0) {
  19. 		raw_printf("Not enough gems? - first=%d j=%d LAST_GEM=%d",
  20. 			first, j, LAST_GEM);
  21. 		wait_synch();
  22. 	    }
  23. 	for (j = first; j <= LAST_GEM; j++)
  24. 		objects[j].oc_prob = (171+j-first)/(LAST_GEM+1-first);
  25. }

shuffle

  1. /* shuffle descriptions on objects o_low to o_high */
  2. STATIC_OVL void
  3. shuffle(o_low, o_high, domaterial)
  4. 	int o_low, o_high;
  5. 	boolean domaterial;
  6. {
  7. 	int i, j, num_to_shuffle;
  8. 	short sw;
  9. 	int color;
  10.  
  11. 	for (num_to_shuffle = 0, j=o_low; j <= o_high; j++)
  12. 		if (!objects[j].oc_name_known) num_to_shuffle++;
  13. 	if (num_to_shuffle < 2) return;
  14.  
  15. 	for (j=o_low; j <= o_high; j++) {
  16. 		if (objects[j].oc_name_known) continue;
  17. 		do
  18. 			i = j + rn2(o_high-j+1);
  19. 		while (objects[i].oc_name_known);
  20. 		sw = objects[j].oc_descr_idx;
  21. 		objects[j].oc_descr_idx = objects[i].oc_descr_idx;
  22. 		objects[i].oc_descr_idx = sw;
  23. 		sw = objects[j].oc_tough;
  24. 		objects[j].oc_tough = objects[i].oc_tough;
  25. 		objects[i].oc_tough = sw;
  26. 		color = objects[j].oc_color;
  27. 		objects[j].oc_color = objects[i].oc_color;
  28. 		objects[i].oc_color = color;
  29.  
  30. 		/* shuffle material */
  31. 		if (domaterial) {
  32. 			sw = objects[j].oc_material;
  33. 			objects[j].oc_material = objects[i].oc_material;
  34. 			objects[i].oc_material = sw;
  35. 		}
  36. 	}
  37. }

init_objects

  1. void
  2. init_objects()
  3. {
  4. register int i, first, last, sum;
  5. register char oclass;
  6. #ifdef TEXTCOLOR
  7. # define COPY_OBJ_DESCR(o_dst,o_src) \
  8. 			o_dst.oc_descr_idx = o_src.oc_descr_idx,\
  9. 			o_dst.oc_color = o_src.oc_color
  10. #else
  11. # define COPY_OBJ_DESCR(o_dst,o_src) o_dst.oc_descr_idx = o_src.oc_descr_idx
  12. #endif
  13.  
  14. 	/* bug fix to prevent "initialization error" abort on Intel Xenix.
  15. 	 * reported by mikew@semike
  16. 	 */
  17. 	for (i = 0; i < MAXOCLASSES; i++)
  18. 		bases[i] = 0;
  19. 	/* initialize object descriptions */
  20. 	for (i = 0; i < NUM_OBJECTS; i++)
  21. 		objects[i].oc_name_idx = objects[i].oc_descr_idx = i;
  22. 	/* init base; if probs given check that they add up to 1000,
  23. 	   otherwise compute probs */
  24. 	first = 0;
  25. 	while( first < NUM_OBJECTS ) {
  26. 		oclass = objects[first].oc_class;
  27. 		last = first+1;
  28. 		while (last < NUM_OBJECTS && objects[last].oc_class == oclass) last++;
  29. 		bases[(int)oclass] = first;
  30.  
  31. 		if (oclass == GEM_CLASS) {
  32. 			setgemprobs((d_level *)0);
  33.  
  34. 			if (rn2(2)) { /* change turquoise from green to blue? */
  35. 			    COPY_OBJ_DESCR(objects[TURQUOISE],objects[SAPPHIRE]);
  36. 			}
  37. 			if (rn2(2)) { /* change aquamarine from green to blue? */
  38. 			    COPY_OBJ_DESCR(objects[AQUAMARINE],objects[SAPPHIRE]);
  39. 			}
  40. 			switch (rn2(4)) { /* change fluorite from violet? */
  41. 			    case 0:  break;
  42. 			    case 1:	/* blue */
  43. 				COPY_OBJ_DESCR(objects[FLUORITE],objects[SAPPHIRE]);
  44. 				break;
  45. 			    case 2:	/* white */
  46. 				COPY_OBJ_DESCR(objects[FLUORITE],objects[DIAMOND]);
  47. 				break;
  48. 			    case 3:	/* green */
  49. 				COPY_OBJ_DESCR(objects[FLUORITE],objects[EMERALD]);
  50. 				break;
  51. 			}
  52. 		}
  53. 	check:
  54. 		sum = 0;
  55. 		for(i = first; i < last; i++) sum += objects[i].oc_prob;
  56. 		if(sum == 0) {
  57. 			for(i = first; i < last; i++)
  58. 			    objects[i].oc_prob = (1000+i-first)/(last-first);
  59. 			goto check;
  60. 		}
  61. 		if(sum != 1000)
  62. 			error("init-prob error for class %d (%d%%)", oclass, sum);
  63. 		first = last;
  64. 	}
  65. 	/* shuffle descriptions */
  66. 	shuffle_all();
  67. #ifdef USE_TILES
  68. 	shuffle_tiles();
  69. #endif
  70. }

shuffle_all

  1. STATIC_OVL void
  2. shuffle_all()
  3. {
  4. 	int first, last, oclass;
  5.  
  6. 	for (oclass = 1; oclass < MAXOCLASSES; oclass++) {
  7. 		first = bases[oclass];
  8. 		last = first+1;
  9. 		while (last < NUM_OBJECTS && objects[last].oc_class == oclass)
  10. 			last++;
  11.  
  12. 		if (OBJ_DESCR(objects[first]) != (char *)0 &&
  13. 				oclass != TOOL_CLASS &&
  14. 				oclass != WEAPON_CLASS &&
  15. 				oclass != ARMOR_CLASS &&
  16. 				oclass != GEM_CLASS) {
  17. 			int j = last-1;
  18.  
  19. 			if (oclass == POTION_CLASS)
  20. 			    j -= 1;  /* only water has a fixed description */
  21. 			else if (oclass == AMULET_CLASS ||
  22. 				 oclass == SCROLL_CLASS ||
  23. 				 oclass == SPBOOK_CLASS) {
  24. 			    while (!objects[j].oc_magic || objects[j].oc_unique)
  25. 				j--;
  26. 			}
  27.  
  28. 			/* non-magical amulets, scrolls, and spellbooks
  29. 			 * (ex. imitation Amulets, blank, scrolls of mail)
  30. 			 * and one-of-a-kind magical artifacts at the end of
  31. 			 * their class in objects[] have fixed descriptions.
  32. 			 */
  33. 			shuffle(first, j, TRUE);
  34. 		}
  35. 	}
  36.  
  37. 	/* shuffle the helmets */
  38. 	shuffle(HELMET, HELM_OF_TELEPATHY, FALSE);
  39.  
  40. 	/* shuffle the gloves */
  41. 	shuffle(LEATHER_GLOVES, GAUNTLETS_OF_DEXTERITY, FALSE);
  42.  
  43. 	/* shuffle the cloaks */
  44. 	shuffle(CLOAK_OF_PROTECTION, CLOAK_OF_DISPLACEMENT, FALSE);
  45.  
  46. 	/* shuffle the boots [if they change, update find_skates() below] */
  47. 	shuffle(SPEED_BOOTS, LEVITATION_BOOTS, FALSE);
  48. }

find_skates

  1. /* find the object index for snow boots; used [once] by slippery ice code */
  2. int
  3. find_skates()
  4. {
  5. register int i;
  6. register const char *s;
  7.  
  8. for (i = SPEED_BOOTS; i <= LEVITATION_BOOTS; i++)
  9. 	if ((s = OBJ_DESCR(objects[i])) != 0 && !strcmp(s, "snow boots"))
  10. 	    return i;
  11.  
  12. impossible("snow boots not found?");
  13. return -1;	/* not 0, or caller would try again each move */
  14. }

oinit

  1. void
  2. oinit()			/* level dependent initialization */
  3. {
  4. 	setgemprobs(&u.uz);
  5. }

savenames

  1. void
  2. savenames(fd, mode)
  3. int fd, mode;
  4. {
  5. 	register int i;
  6. 	unsigned int len;
  7.  
  8. 	if (perform_bwrite(mode)) {
  9. 	    bwrite(fd, (genericptr_t)bases, sizeof bases);
  10. 	    bwrite(fd, (genericptr_t)disco, sizeof disco);
  11. 	    bwrite(fd, (genericptr_t)objects,
  12. 		   sizeof(struct objclass) * NUM_OBJECTS);
  13. 	}
  14. 	/* as long as we use only one version of Hack we
  15. 	   need not save oc_name and oc_descr, but we must save
  16. 	   oc_uname for all objects */
  17. 	for (i = 0; i < NUM_OBJECTS; i++)
  18. 	    if (objects[i].oc_uname) {
  19. 		if (perform_bwrite(mode)) {
  20. 		    len = strlen(objects[i].oc_uname)+1;
  21. 		    bwrite(fd, (genericptr_t)&len, sizeof len);
  22. 		    bwrite(fd, (genericptr_t)objects[i].oc_uname, len);
  23. 		}
  24. 		if (release_data(mode)) {
  25. 		    free((genericptr_t)objects[i].oc_uname);
  26. 		    objects[i].oc_uname = 0;
  27. 		}
  28. 	    }
  29. }

restnames

  1. void
  2. restnames(fd)
  3. register int fd;
  4. {
  5. 	register int i;
  6. 	unsigned int len;
  7.  
  8. 	mread(fd, (genericptr_t) bases, sizeof bases);
  9. 	mread(fd, (genericptr_t) disco, sizeof disco);
  10. 	mread(fd, (genericptr_t) objects, sizeof(struct objclass) * NUM_OBJECTS);
  11. 	for (i = 0; i < NUM_OBJECTS; i++)
  12. 	    if (objects[i].oc_uname) {
  13. 		mread(fd, (genericptr_t) &len, sizeof len);
  14. 		objects[i].oc_uname = (char *) alloc(len);
  15. 		mread(fd, (genericptr_t)objects[i].oc_uname, len);
  16. 	    }
  17. #ifdef USE_TILES
  18. 	shuffle_tiles();
  19. #endif
  20. }

discover_object

  1. void
  2. discover_object(oindx, mark_as_known, credit_hero)
  3. register int oindx;
  4. boolean mark_as_known;
  5. boolean credit_hero;
  6. {
  7. if (!objects[oindx].oc_name_known) {
  8. 	register int dindx, acls = objects[oindx].oc_class;
  9.  
  10. 	/* Loop thru disco[] 'til we find the target (which may have been
  11. 	   uname'd) or the next open slot; one or the other will be found
  12. 	   before we reach the next class...
  13. 	 */
  14. 	for (dindx = bases[acls]; disco[dindx] != 0; dindx++)
  15. 	    if (disco[dindx] == oindx) break;
  16. 	disco[dindx] = oindx;
  17.  
  18. 	if (mark_as_known) {
  19. 	    objects[oindx].oc_name_known = 1;
  20. 	    if (credit_hero) exercise(A_WIS, TRUE);
  21. 	}
  22. 	if (moves > 1L) update_inventory();
  23. }
  24. }

undiscover_object

  1. /* if a class name has been cleared, we may need to purge it from disco[] */
  2. void
  3. undiscover_object(oindx)
  4. register int oindx;
  5. {
  6. if (!objects[oindx].oc_name_known) {
  7. 	register int dindx, acls = objects[oindx].oc_class;
  8. 	register boolean found = FALSE;
  9.  
  10. 	/* find the object; shift those behind it forward one slot */
  11. 	for (dindx = bases[acls];
  12. 	      dindx < NUM_OBJECTS && disco[dindx] != 0
  13. 		&& objects[dindx].oc_class == acls; dindx++)
  14. 	    if (found)
  15. 		disco[dindx-1] = disco[dindx];
  16. 	    else if (disco[dindx] == oindx)
  17. 		found = TRUE;
  18.  
  19. 	/* clear last slot */
  20. 	if (found) disco[dindx-1] = 0;
  21. 	else impossible("named object not in disco");
  22. 	update_inventory();
  23. }
  24. }

interesting_to_discover

  1. STATIC_OVL boolean
  2. interesting_to_discover(i)
  3. register int i;
  4. {
  5. 	/* Pre-discovered objects are now printed with a '*' */
  6. return((boolean)(objects[i].oc_uname != (char *)0 ||
  7. 	    (objects[i].oc_name_known && OBJ_DESCR(objects[i]) != (char *)0)));
  8. }

dodiscovered

  1. /* items that should stand out once they're known */
  2. static short uniq_objs[] = {
  3. 	AMULET_OF_YENDOR,
  4. 	SPE_BOOK_OF_THE_DEAD,
  5. 	CANDELABRUM_OF_INVOCATION,
  6. 	BELL_OF_OPENING,
  7. };
  8.  
  9. int
  10. dodiscovered()				/* free after Robert Viduya */
  11. {
  12. register int i, dis;
  13. int	ct = 0;
  14. char *s, oclass, prev_class, classes[MAXOCLASSES];
  15. winid tmpwin;
  16. 	char buf[BUFSZ];
  17.  
  18. tmpwin = create_nhwindow(NHW_MENU);
  19. putstr(tmpwin, 0, "Discoveries");
  20. putstr(tmpwin, 0, "");
  21.  
  22. /* gather "unique objects" into a pseudo-class; note that they'll
  23. also be displayed individually within their regular class */
  24. for (i = dis = 0; i < SIZE(uniq_objs); i++)
  25. 	if (objects[uniq_objs[i]].oc_name_known) {
  26. 	    if (!dis++)
  27. 		putstr(tmpwin, iflags.menu_headings, "Unique Items");
  28. 		Sprintf(buf, "  %s", OBJ_NAME(objects[uniq_objs[i]]));
  29. 	    putstr(tmpwin, 0, buf);
  30. 	    ++ct;
  31. 	}
  32. /* display any known artifacts as another pseudo-class */
  33. ct += disp_artifact_discoveries(tmpwin);
  34.  
  35. /* several classes are omitted from packorder; one is of interest here */
  36. Strcpy(classes, flags.inv_order);
  37. if (!index(classes, VENOM_CLASS)) {
  38. 	s = eos(classes);
  39. 	*s++ = VENOM_CLASS;
  40. 	*s = '\0';
  41. }
  42.  
  43. for (s = classes; *s; s++) {
  44. 	oclass = *s;
  45. 	prev_class = oclass + 1;	/* forced different from oclass */
  46. 	for (i = bases[(int)oclass];
  47. 	     i < NUM_OBJECTS && objects[i].oc_class == oclass; i++) {
  48. 	    if ((dis = disco[i]) && interesting_to_discover(dis)) {
  49. 		ct++;
  50. 		if (oclass != prev_class) {
  51. 		    putstr(tmpwin, iflags.menu_headings, let_to_name(oclass, FALSE));
  52. 		    prev_class = oclass;
  53. 		}
  54. 		Sprintf(buf, "%s %s",(objects[dis].oc_pre_discovered ? "*" : " "),
  55. 				obj_typename(dis));
  56. 		putstr(tmpwin, 0, buf);
  57. 	    }
  58. 	}
  59. }
  60. if (ct == 0) {
  61. 	You("haven't discovered anything yet...");
  62. } else
  63. 	display_nhwindow(tmpwin, TRUE);
  64. destroy_nhwindow(tmpwin);
  65.  
  66. return 0;
  67. }
  68.  
  69. /*o_init.c*/