Source:NetHack 5.0.0/src/stairs.c
Jump to navigation
Jump to search
Below is the full text to stairs.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/stairs.c#line12]], for example.
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. /* NetHack 5.0 stairs.c $NHDT-Date: 1704043695 2023/12/31 17:28:15 $ $NHDT-Branch: keni-luabits2 $:$NHDT-Revision: 1.207 $ */ 2. /* Copyright (c) 2024 by Pasi Kallinen */ 3. /* NetHack may be freely redistributed. See license for details. */ 4. 5. #include "hack.h" 6. 7. void 8. stairway_add( 9. coordxy x, coordxy y, 10. boolean up, boolean isladder, 11. d_level *dest) 12. { 13. stairway *tmp = (stairway *) alloc(sizeof (stairway)); 14. 15. (void) memset((genericptr_t) tmp, 0, sizeof (stairway)); 16. tmp->sx = x; 17. tmp->sy = y; 18. tmp->up = up; 19. tmp->isladder = isladder; 20. tmp->u_traversed = FALSE; 21. assign_level(&(tmp->tolev), dest); 22. tmp->next = gs.stairs; 23. gs.stairs = tmp; 24. } 25. 26. void 27. stairway_free_all(void) 28. { 29. stairway *tmp = gs.stairs; 30. 31. while (tmp) { 32. stairway *tmp2 = tmp->next; 33. free(tmp); 34. tmp = tmp2; 35. } 36. gs.stairs = NULL; 37. } 38. 39. stairway * 40. stairway_at(coordxy x, coordxy y) 41. { 42. stairway *tmp = gs.stairs; 43. 44. while (tmp && !(tmp->sx == x && tmp->sy == y)) 45. tmp = tmp->next; 46. return tmp; 47. } 48. 49. stairway * 50. stairway_find(d_level *fromdlev) 51. { 52. stairway *tmp = gs.stairs; 53. 54. while (tmp) { 55. if (tmp->tolev.dnum == fromdlev->dnum 56. && tmp->tolev.dlevel == fromdlev->dlevel) 57. break; /* return */ 58. tmp = tmp->next; 59. } 60. return tmp; 61. } 62. 63. stairway * 64. stairway_find_from(d_level *fromdlev, boolean isladder) 65. { 66. stairway *tmp = gs.stairs; 67. 68. while (tmp) { 69. if (tmp->tolev.dnum == fromdlev->dnum 70. && tmp->tolev.dlevel == fromdlev->dlevel 71. && tmp->isladder == isladder) 72. break; /* return */ 73. tmp = tmp->next; 74. } 75. return tmp; 76. } 77. 78. stairway * 79. stairway_find_dir(boolean up) 80. { 81. stairway *tmp = gs.stairs; 82. 83. while (tmp && !(tmp->up == up)) 84. tmp = tmp->next; 85. return tmp; 86. } 87. 88. stairway * 89. stairway_find_type_dir(boolean isladder, boolean up) 90. { 91. stairway *tmp = gs.stairs; 92. 93. while (tmp && !(tmp->isladder == isladder && tmp->up == up)) 94. tmp = tmp->next; 95. return tmp; 96. } 97. 98. stairway * 99. stairway_find_special_dir(boolean up) 100. { 101. stairway *tmp = gs.stairs; 102. 103. while (tmp) { 104. if (tmp->tolev.dnum != u.uz.dnum && tmp->up != up) 105. return tmp; 106. tmp = tmp->next; 107. } 108. return tmp; 109. } 110. 111. /* place you on the special staircase */ 112. void 113. u_on_sstairs(int upflag) 114. { 115. stairway *stway = stairway_find_special_dir(upflag); 116. 117. if (stway) 118. u_on_newpos(stway->sx, stway->sy); 119. else 120. u_on_rndspot(upflag); 121. } 122. 123. /* place you on upstairs (or special equivalent) */ 124. void 125. u_on_upstairs(void) 126. { 127. stairway *stway = stairway_find_dir(TRUE); 128. 129. if (stway) 130. u_on_newpos(stway->sx, stway->sy); 131. else 132. u_on_sstairs(0); /* destination upstairs implies moving down */ 133. } 134. 135. /* place you on dnstairs (or special equivalent) */ 136. void 137. u_on_dnstairs(void) 138. { 139. stairway *stway = stairway_find_dir(FALSE); 140. 141. if (stway) 142. u_on_newpos(stway->sx, stway->sy); 143. else 144. u_on_sstairs(1); /* destination dnstairs implies moving up */ 145. } 146. 147. boolean 148. On_stairs(coordxy x, coordxy y) 149. { 150. return (stairway_at(x, y) != NULL); 151. } 152. 153. boolean 154. On_ladder(coordxy x, coordxy y) 155. { 156. stairway *stway = stairway_at(x, y); 157. 158. return (boolean) (stway && stway->isladder); 159. } 160. 161. boolean 162. On_stairs_up(coordxy x, coordxy y) 163. { 164. stairway *stway = stairway_at(x, y); 165. 166. return (boolean) (stway && stway->up); 167. } 168. 169. boolean 170. On_stairs_dn(coordxy x, coordxy y) 171. { 172. stairway *stway = stairway_at(x, y); 173. 174. return (boolean) (stway && !stway->up); 175. } 176. 177. /* return True if 'sway' is a branch staircase and hero has used these stairs 178. to visit the branch */ 179. boolean 180. known_branch_stairs(stairway *sway) 181. { 182. return (sway && sway->tolev.dnum != u.uz.dnum && sway->u_traversed); 183. } 184. 185. /* describe staircase 'sway' based on whether hero knows the destination */ 186. char * 187. stairs_description( 188. stairway *sway, /* stairs/ladder to describe */ 189. char *outbuf, /* result buffer */ 190. boolean stcase) /* True: "staircase" or "ladder", always singular; 191. * False: "stairs" or "ladder"; caller needs to deal 192. * with singular vs plural when forming a sentence */ 193. { 194. d_level tolev; 195. const char *stairs, *updown; 196. 197. tolev = sway->tolev; 198. stairs = sway->isladder ? "ladder" : stcase ? "staircase" : "stairs"; 199. updown = sway->up ? "up" : "down"; 200. 201. if (!known_branch_stairs(sway)) { 202. /* ordinary stairs or branch stairs to not-yet-visited branch */ 203. Sprintf(outbuf, "%s %s", stairs, updown); 204. if (sway->u_traversed) { 205. boolean specialdepth = (tolev.dnum == quest_dnum 206. || single_level_branch(&tolev)); /* knox */ 207. int to_dlev = specialdepth ? dunlev(&tolev) : depth(&tolev); 208. 209. Sprintf(eos(outbuf), " to level %d", to_dlev); 210. } 211. } else if (u.uz.dnum == 0 && u.uz.dlevel == 1 && sway->up) { 212. /* stairs up from level one are a special case; they are marked 213. as having been traversed because the hero obviously started 214. the game by coming down them, but the remote side varies 215. depending on whether the Amulet is being carried */ 216. Sprintf(outbuf, "%s%s %s %s", 217. !u.uhave.amulet ? "" : "branch ", 218. stairs, updown, 219. !u.uhave.amulet ? "out of the dungeon" 220. /* minimize our expectations about what comes next */ 221. : (on_level(&tolev, &earth_level) 222. || on_level(&tolev, &air_level) 223. || on_level(&tolev, &fire_level) 224. || on_level(&tolev, &water_level)) 225. ? "to the Elemental Planes" 226. : "to the end game"); 227. } else { 228. /* known branch stairs; tacking on destination level is too verbose */ 229. Sprintf(outbuf, "branch %s %s to %s", 230. stairs, updown, svd.dungeons[tolev.dnum].dname); 231. /* dungeons[].dname is capitalized; undo that for "The <Branch>" */ 232. (void) strsubst(outbuf, "The ", "the "); 233. } 234. return outbuf; 235. } 236. 237. /*stairs.c*/