MCPcopy Index your code
hub / github.com/NetHack/NetHack / weight

Function weight

src/mkobj.c:1887–1976  ·  view source on GitHub ↗

* Calculate the weight of the given object. This will recursively follow * and calculate the weight of any containers. * * Note: It is possible to end up with an incorrect weight if some part * of the code messes with a contained object and doesn't update the * container's weight. * * Note too: obj->owt is an unsigned int and objects[].oc_weight an * unsigned

Source from the content-addressed store, hash-verified

1885 * use and return unsigned int instead of signed int.
1886 */
1887int
1888weight(struct obj *obj)
1889{
1890 int wt = (int) objects[obj->otyp].oc_weight; /* weight of 1 'otyp' */
1891
1892 if (obj->quan < 1L) {
1893 impossible("Calculating weight of %ld %s?",
1894 obj->quan, simpleonames(obj));
1895 return 0;
1896 }
1897 /* glob absorption means that merging globs combines their weight
1898 while quantity stays 1; mksobj(), obj_absorb(), and shrink_glob()
1899 manage glob->owt and there is nothing for weight() to do except
1900 return the current value as-is */
1901 if (obj->globby) {
1902 /* 5.0: in 3.6.x this checked for owt==0 and then used
1903 owt as-is when non-zero or objects[].oc_weight if zero;
1904 we don't do that anymore because it confused calculating
1905 the weight of a container when a glob inside shrank down
1906 to 0 and was about to be deleted [mksobj() now initializes
1907 owt for globs sooner and the subsequent o->owt = weight(o)
1908 general initialization is benignly redundant for globs] */
1909 return (int) obj->owt;
1910 }
1911 if (Is_container(obj) || obj->otyp == STATUE) {
1912 struct obj *contents;
1913 int cwt;
1914
1915 if (obj->otyp == STATUE && ismnum(obj->corpsenm)) {
1916 int msize = (int) mons[obj->corpsenm].msize, /* 0..7 */
1917 minwt = (msize + msize + 1) * 100;
1918
1919 /* default statue weight is 1.5 times corpse weight */
1920 wt = 3 * (int) mons[obj->corpsenm].cwt / 2;
1921 /* some monsters that never leave a corpse when they die have
1922 corpse weight defined as 0; statues resembling them need to
1923 have non-zero weight; others are so tiny (killer bee) that
1924 they weigh barely more than nothing or so insubstantial
1925 (wraith) that they actually weigh nothing; statues of such
1926 need more heft */
1927 if (wt < minwt)
1928 wt = minwt;
1929 /* this has no effect because statues don't stack */
1930 wt *= (int) obj->quan;
1931 }
1932
1933 cwt = 0; /* contents weight */
1934 for (contents = obj->cobj; contents; contents = contents->nobj)
1935 cwt += weight(contents);
1936 /*
1937 * The weight of bags of holding is calculated as the weight
1938 * of the bag plus the weight of the bag's contents modified
1939 * as follows:
1940 *
1941 * Bag status Weight of contents
1942 * ---------- ------------------
1943 * cursed 2x
1944 * blessed x/4 [rounded up: (x+3)/4]

Callers 15

fill_zooFunction · 0.85
chweponFunction · 0.85
resetobjsFunction · 0.85
drop_upon_deathFunction · 0.85
mk_mplayerFunction · 0.85
lay_an_eggFunction · 0.85
l_obj_add_to_containerFunction · 0.85
hmon_hitmon_misc_objFunction · 0.85
flooreffectsFunction · 0.85
readobjnam_postparse1Function · 0.85
readobjnamFunction · 0.85
shield_simple_nameFunction · 0.85

Calls 3

simpleonamesFunction · 0.85
eaten_statFunction · 0.85
impossibleFunction · 0.70

Tested by

no test coverage detected