Source:NetHack 3.6.4/src/region.c
Jump to navigation
Jump to search
Below is the full text to region.c from the source code of NetHack 3.6.4. To link to a particular line, write [[Source:NetHack 3.6.4/src/region.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 3.6 region.c $NHDT-Date: 1573957877 2019/11/17 02:31:17 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.45 $ */ 2. /* Copyright (c) 1996 by Jean-Christophe Collet */ 3. /* NetHack may be freely redistributed. See license for details. */ 4. 5. #include "hack.h" 6. #include "lev.h" 7. 8. /* 9. * This should really go into the level structure, but 10. * I'll start here for ease. It *WILL* move into the level 11. * structure eventually. 12. */ 13. 14. static NhRegion **regions; 15. static int n_regions = 0; 16. static int max_regions = 0; 17. 18. #define NO_CALLBACK (-1) 19. 20. boolean FDECL(inside_gas_cloud, (genericptr, genericptr)); 21. boolean FDECL(expire_gas_cloud, (genericptr, genericptr)); 22. boolean FDECL(inside_rect, (NhRect *, int, int)); 23. boolean FDECL(inside_region, (NhRegion *, int, int)); 24. NhRegion *FDECL(create_region, (NhRect *, int)); 25. void FDECL(add_rect_to_reg, (NhRegion *, NhRect *)); 26. void FDECL(add_mon_to_reg, (NhRegion *, struct monst *)); 27. void FDECL(remove_mon_from_reg, (NhRegion *, struct monst *)); 28. boolean FDECL(mon_in_region, (NhRegion *, struct monst *)); 29. 30. #if 0 31. NhRegion *FDECL(clone_region, (NhRegion *)); 32. #endif 33. void FDECL(free_region, (NhRegion *)); 34. void FDECL(add_region, (NhRegion *)); 35. void FDECL(remove_region, (NhRegion *)); 36. 37. #if 0 38. void FDECL(replace_mon_regions, (struct monst *,struct monst *)); 39. void FDECL(remove_mon_from_regions, (struct monst *)); 40. NhRegion *FDECL(create_msg_region, (XCHAR_P,XCHAR_P,XCHAR_P,XCHAR_P, 41. const char *,const char *)); 42. boolean FDECL(enter_force_field, (genericptr,genericptr)); 43. NhRegion *FDECL(create_force_field, (XCHAR_P,XCHAR_P,int,long)); 44. #endif 45. 46. STATIC_DCL void FDECL(reset_region_mids, (NhRegion *)); 47. 48. static callback_proc callbacks[] = { 49. #define INSIDE_GAS_CLOUD 0 50. inside_gas_cloud, 51. #define EXPIRE_GAS_CLOUD 1 52. expire_gas_cloud 53. }; 54. 55. /* Should be inlined. */ 56. boolean 57. inside_rect(r, x, y) 58. NhRect *r; 59. int x, y; 60. { 61. return (boolean) (x >= r->lx && x <= r->hx && y >= r->ly && y <= r->hy); 62. } 63. 64. /* 65. * Check if a point is inside a region. 66. */ 67. boolean 68. inside_region(reg, x, y) 69. NhRegion *reg; 70. int x, y; 71. { 72. int i; 73. 74. if (reg == (NhRegion *) 0 || !inside_rect(&(reg->bounding_box), x, y)) 75. return FALSE; 76. for (i = 0; i < reg->nrects; i++) 77. if (inside_rect(&(reg->rects[i]), x, y)) 78. return TRUE; 79. return FALSE; 80. } 81. 82. /* 83. * Create a region. It does not activate it. 84. */ 85. NhRegion * 86. create_region(rects, nrect) 87. NhRect *rects; 88. int nrect; 89. { 90. int i; 91. NhRegion *reg; 92. 93. reg = (NhRegion *) alloc(sizeof(NhRegion)); 94. (void) memset((genericptr_t)reg, 0, sizeof(NhRegion)); 95. /* Determines bounding box */ 96. if (nrect > 0) { 97. reg->bounding_box = rects[0]; 98. } else { 99. reg->bounding_box.lx = COLNO; 100. reg->bounding_box.ly = ROWNO; 101. reg->bounding_box.hx = 0; /* 1 */ 102. reg->bounding_box.hy = 0; 103. } 104. reg->nrects = nrect; 105. reg->rects = (nrect > 0) ? (NhRect *) alloc(nrect * sizeof (NhRect)) : 0; 106. for (i = 0; i < nrect; i++) { 107. if (rects[i].lx < reg->bounding_box.lx) 108. reg->bounding_box.lx = rects[i].lx; 109. if (rects[i].ly < reg->bounding_box.ly) 110. reg->bounding_box.ly = rects[i].ly; 111. if (rects[i].hx > reg->bounding_box.hx) 112. reg->bounding_box.hx = rects[i].hx; 113. if (rects[i].hy > reg->bounding_box.hy) 114. reg->bounding_box.hy = rects[i].hy; 115. reg->rects[i] = rects[i]; 116. } 117. reg->ttl = -1L; /* Defaults */ 118. reg->attach_2_u = FALSE; 119. reg->attach_2_m = 0; 120. /* reg->attach_2_o = NULL; */ 121. reg->enter_msg = (const char *) 0; 122. reg->leave_msg = (const char *) 0; 123. reg->expire_f = NO_CALLBACK; 124. reg->enter_f = NO_CALLBACK; 125. reg->can_enter_f = NO_CALLBACK; 126. reg->leave_f = NO_CALLBACK; 127. reg->can_leave_f = NO_CALLBACK; 128. reg->inside_f = NO_CALLBACK; 129. clear_hero_inside(reg); 130. clear_heros_fault(reg); 131. reg->n_monst = 0; 132. reg->max_monst = 0; 133. reg->monsters = (unsigned int *) 0; 134. reg->arg = zeroany; 135. return reg; 136. } 137. 138. /* 139. * Add rectangle to region. 140. */ 141. void 142. add_rect_to_reg(reg, rect) 143. NhRegion *reg; 144. NhRect *rect; 145. { 146. NhRect *tmp_rect; 147. 148. tmp_rect = (NhRect *) alloc((reg->nrects + 1) * sizeof (NhRect)); 149. if (reg->nrects > 0) { 150. (void) memcpy((genericptr_t) tmp_rect, (genericptr_t) reg->rects, 151. reg->nrects * sizeof (NhRect)); 152. free((genericptr_t) reg->rects); 153. } 154. tmp_rect[reg->nrects] = *rect; 155. reg->nrects++; 156. reg->rects = tmp_rect; 157. /* Update bounding box if needed */ 158. if (reg->bounding_box.lx > rect->lx) 159. reg->bounding_box.lx = rect->lx; 160. if (reg->bounding_box.ly > rect->ly) 161. reg->bounding_box.ly = rect->ly; 162. if (reg->bounding_box.hx < rect->hx) 163. reg->bounding_box.hx = rect->hx; 164. if (reg->bounding_box.hy < rect->hy) 165. reg->bounding_box.hy = rect->hy; 166. } 167. 168. /* 169. * Add a monster to the region 170. */ 171. void 172. add_mon_to_reg(reg, mon) 173. NhRegion *reg; 174. struct monst *mon; 175. { 176. int i; 177. unsigned *tmp_m; 178. 179. if (reg->max_monst <= reg->n_monst) { 180. tmp_m = (unsigned *) alloc(sizeof (unsigned) 181. * (reg->max_monst + MONST_INC)); 182. if (reg->max_monst > 0) { 183. for (i = 0; i < reg->max_monst; i++) 184. tmp_m[i] = reg->monsters[i]; 185. free((genericptr_t) reg->monsters); 186. } 187. reg->monsters = tmp_m; 188. reg->max_monst += MONST_INC; 189. } 190. reg->monsters[reg->n_monst++] = mon->m_id; 191. } 192. 193. /* 194. * Remove a monster from the region list (it left or died...) 195. */ 196. void 197. remove_mon_from_reg(reg, mon) 198. NhRegion *reg; 199. struct monst *mon; 200. { 201. register int i; 202. 203. for (i = 0; i < reg->n_monst; i++) 204. if (reg->monsters[i] == mon->m_id) { 205. reg->n_monst--; 206. reg->monsters[i] = reg->monsters[reg->n_monst]; 207. return; 208. } 209. } 210. 211. /* 212. * Check if a monster is inside the region. 213. * It's probably quicker to check with the region internal list 214. * than to check for coordinates. 215. */ 216. boolean 217. mon_in_region(reg, mon) 218. NhRegion *reg; 219. struct monst *mon; 220. { 221. int i; 222. 223. for (i = 0; i < reg->n_monst; i++) 224. if (reg->monsters[i] == mon->m_id) 225. return TRUE; 226. return FALSE; 227. } 228. 229. #if 0 230. /* not yet used */ 231. 232. /* 233. * Clone (make a standalone copy) the region. 234. */ 235. NhRegion * 236. clone_region(reg) 237. NhRegion *reg; 238. { 239. NhRegion *ret_reg; 240. 241. ret_reg = create_region(reg->rects, reg->nrects); 242. ret_reg->ttl = reg->ttl; 243. ret_reg->attach_2_u = reg->attach_2_u; 244. ret_reg->attach_2_m = reg->attach_2_m; 245. /* ret_reg->attach_2_o = reg->attach_2_o; */ 246. ret_reg->expire_f = reg->expire_f; 247. ret_reg->enter_f = reg->enter_f; 248. ret_reg->can_enter_f = reg->can_enter_f; 249. ret_reg->leave_f = reg->leave_f; 250. ret_reg->can_leave_f = reg->can_leave_f; 251. ret_reg->player_flags = reg->player_flags; /* set/clear_hero_inside,&c*/ 252. ret_reg->n_monst = reg->n_monst; 253. if (reg->n_monst > 0) { 254. ret_reg->monsters = (unsigned int *) 255. alloc((sizeof (unsigned)) * reg->n_monst); 256. (void) memcpy((genericptr_t) ret_reg->monsters, 257. (genericptr_t) reg->monsters, 258. sizeof (unsigned) * reg->n_monst); 259. } else 260. ret_reg->monsters = (unsigned int *) 0; 261. return ret_reg; 262. } 263. 264. #endif /*0*/ 265. 266. /* 267. * Free mem from region. 268. */ 269. void 270. free_region(reg) 271. NhRegion *reg; 272. { 273. if (reg) { 274. if (reg->rects) 275. free((genericptr_t) reg->rects); 276. if (reg->monsters) 277. free((genericptr_t) reg->monsters); 278. if (reg->enter_msg) 279. free((genericptr_t) reg->enter_msg); 280. if (reg->leave_msg) 281. free((genericptr_t) reg->leave_msg); 282. free((genericptr_t) reg); 283. } 284. } 285. 286. /* 287. * Add a region to the list. 288. * This actually activates the region. 289. */ 290. void 291. add_region(reg) 292. NhRegion *reg; 293. { 294. NhRegion **tmp_reg; 295. int i, j; 296. 297. if (max_regions <= n_regions) { 298. tmp_reg = regions; 299. regions = 300. (NhRegion **) alloc((max_regions + 10) * sizeof (NhRegion *)); 301. if (max_regions > 0) { 302. (void) memcpy((genericptr_t) regions, (genericptr_t) tmp_reg, 303. max_regions * sizeof (NhRegion *)); 304. free((genericptr_t) tmp_reg); 305. } 306. max_regions += 10; 307. } 308. regions[n_regions] = reg; 309. n_regions++; 310. /* Check for monsters inside the region */ 311. for (i = reg->bounding_box.lx; i <= reg->bounding_box.hx; i++) 312. for (j = reg->bounding_box.ly; j <= reg->bounding_box.hy; j++) { 313. /* Some regions can cross the level boundaries */ 314. if (!isok(i, j)) 315. continue; 316. if (MON_AT(i, j) && inside_region(reg, i, j)) 317. add_mon_to_reg(reg, level.monsters[i][j]); 318. if (reg->visible && cansee(i, j)) 319. newsym(i, j); 320. } 321. /* Check for player now... */ 322. if (inside_region(reg, u.ux, u.uy)) 323. set_hero_inside(reg); 324. else 325. clear_hero_inside(reg); 326. } 327. 328. /* 329. * Remove a region from the list & free it. 330. */ 331. void 332. remove_region(reg) 333. NhRegion *reg; 334. { 335. register int i, x, y; 336. 337. for (i = 0; i < n_regions; i++) 338. if (regions[i] == reg) 339. break; 340. if (i == n_regions) 341. return; 342. 343. /* remove region before potential newsym() calls, but don't free it yet */ 344. if (--n_regions != i) 345. regions[i] = regions[n_regions]; 346. regions[n_regions] = (NhRegion *) 0; 347. 348. /* Update screen if necessary */ 349. reg->ttl = -2L; /* for visible_region_at */ 350. if (reg->visible) 351. for (x = reg->bounding_box.lx; x <= reg->bounding_box.hx; x++) 352. for (y = reg->bounding_box.ly; y <= reg->bounding_box.hy; y++) 353. if (isok(x, y) && inside_region(reg, x, y) && cansee(x, y)) 354. newsym(x, y); 355. 356. free_region(reg); 357. } 358. 359. /* 360. * Remove all regions and clear all related data (This must be down 361. * when changing level, for instance). 362. */ 363. void 364. clear_regions() 365. { 366. register int i; 367. 368. for (i = 0; i < n_regions; i++) 369. free_region(regions[i]); 370. n_regions = 0; 371. if (max_regions > 0) 372. free((genericptr_t) regions); 373. max_regions = 0; 374. regions = (NhRegion **) 0; 375. } 376. 377. /* 378. * This function is called every turn. 379. * It makes the regions age, if necessary and calls the appropriate 380. * callbacks when needed. 381. */ 382. void 383. run_regions() 384. { 385. register int i, j, k; 386. int f_indx; 387. 388. /* End of life ? */ 389. /* Do it backward because the array will be modified */ 390. for (i = n_regions - 1; i >= 0; i--) { 391. if (regions[i]->ttl == 0L) { 392. if ((f_indx = regions[i]->expire_f) == NO_CALLBACK 393. || (*callbacks[f_indx])(regions[i], (genericptr_t) 0)) 394. remove_region(regions[i]); 395. } 396. } 397. 398. /* Process remaining regions */ 399. for (i = 0; i < n_regions; i++) { 400. /* Make the region age */ 401. if (regions[i]->ttl > 0L) 402. regions[i]->ttl--; 403. /* Check if player is inside region */ 404. f_indx = regions[i]->inside_f; 405. if (f_indx != NO_CALLBACK && hero_inside(regions[i])) 406. (void) (*callbacks[f_indx])(regions[i], (genericptr_t) 0); 407. /* Check if any monster is inside region */ 408. if (f_indx != NO_CALLBACK) { 409. for (j = 0; j < regions[i]->n_monst; j++) { 410. struct monst *mtmp = 411. find_mid(regions[i]->monsters[j], FM_FMON); 412. 413. if (!mtmp || DEADMONSTER(mtmp) 414. || (*callbacks[f_indx])(regions[i], mtmp)) { 415. /* The monster died, remove it from list */ 416. k = (regions[i]->n_monst -= 1); 417. regions[i]->monsters[j] = regions[i]->monsters[k]; 418. regions[i]->monsters[k] = 0; 419. --j; /* current slot has been reused; recheck it next */ 420. } 421. } 422. } 423. } 424. } 425. 426. /* 427. * check whether player enters/leaves one or more regions. 428. */ 429. boolean 430. in_out_region(x, y) 431. xchar x, y; 432. { 433. int i, f_indx; 434. 435. /* First check if hero can do the move */ 436. for (i = 0; i < n_regions; i++) { 437. if (regions[i]->attach_2_u) 438. continue; 439. if (inside_region(regions[i], x, y) 440. ? (!hero_inside(regions[i]) 441. && (f_indx = regions[i]->can_enter_f) != NO_CALLBACK) 442. : (hero_inside(regions[i]) 443. && (f_indx = regions[i]->can_leave_f) != NO_CALLBACK)) { 444. if (!(*callbacks[f_indx])(regions[i], (genericptr_t) 0)) 445. return FALSE; 446. } 447. } 448. 449. /* Callbacks for the regions hero does leave */ 450. for (i = 0; i < n_regions; i++) { 451. if (regions[i]->attach_2_u) 452. continue; 453. if (hero_inside(regions[i]) 454. && !inside_region(regions[i], x, y)) { 455. clear_hero_inside(regions[i]); 456. if (regions[i]->leave_msg != (const char *) 0) 457. pline1(regions[i]->leave_msg); 458. if ((f_indx = regions[i]->leave_f) != NO_CALLBACK) 459. (void) (*callbacks[f_indx])(regions[i], (genericptr_t) 0); 460. } 461. } 462. 463. /* Callbacks for the regions hero does enter */ 464. for (i = 0; i < n_regions; i++) { 465. if (regions[i]->attach_2_u) 466. continue; 467. if (!hero_inside(regions[i]) 468. && inside_region(regions[i], x, y)) { 469. set_hero_inside(regions[i]); 470. if (regions[i]->enter_msg != (const char *) 0) 471. pline1(regions[i]->enter_msg); 472. if ((f_indx = regions[i]->enter_f) != NO_CALLBACK) 473. (void) (*callbacks[f_indx])(regions[i], (genericptr_t) 0); 474. } 475. } 476. 477. return TRUE; 478. } 479. 480. /* 481. * check whether a monster enters/leaves one or more regions. 482. */ 483. boolean 484. m_in_out_region(mon, x, y) 485. struct monst *mon; 486. xchar x, y; 487. { 488. int i, f_indx; 489. 490. /* First check if mon can do the move */ 491. for (i = 0; i < n_regions; i++) { 492. if (regions[i]->attach_2_m == mon->m_id) 493. continue; 494. if (inside_region(regions[i], x, y) 495. ? (!mon_in_region(regions[i], mon) 496. && (f_indx = regions[i]->can_enter_f) != NO_CALLBACK) 497. : (mon_in_region(regions[i], mon) 498. && (f_indx = regions[i]->can_leave_f) != NO_CALLBACK)) { 499. if (!(*callbacks[f_indx])(regions[i], mon)) 500. return FALSE; 501. } 502. } 503. 504. /* Callbacks for the regions mon does leave */ 505. for (i = 0; i < n_regions; i++) { 506. if (regions[i]->attach_2_m == mon->m_id) 507. continue; 508. if (mon_in_region(regions[i], mon) 509. && !inside_region(regions[i], x, y)) { 510. remove_mon_from_reg(regions[i], mon); 511. if ((f_indx = regions[i]->leave_f) != NO_CALLBACK) 512. (void) (*callbacks[f_indx])(regions[i], mon); 513. } 514. } 515. 516. /* Callbacks for the regions mon does enter */ 517. for (i = 0; i < n_regions; i++) { 518. if (regions[i]->attach_2_m == mon->m_id) 519. continue; 520. if (!mon_in_region(regions[i], mon) 521. && inside_region(regions[i], x, y)) { 522. add_mon_to_reg(regions[i], mon); 523. if ((f_indx = regions[i]->enter_f) != NO_CALLBACK) 524. (void) (*callbacks[f_indx])(regions[i], mon); 525. } 526. } 527. 528. return TRUE; 529. } 530. 531. /* 532. * Checks player's regions after a teleport for instance. 533. */ 534. void 535. update_player_regions() 536. { 537. register int i; 538. 539. for (i = 0; i < n_regions; i++) 540. if (!regions[i]->attach_2_u && inside_region(regions[i], u.ux, u.uy)) 541. set_hero_inside(regions[i]); 542. else 543. clear_hero_inside(regions[i]); 544. } 545. 546. /* 547. * Ditto for a specified monster. 548. */ 549. void 550. update_monster_region(mon) 551. struct monst *mon; 552. { 553. register int i; 554. 555. for (i = 0; i < n_regions; i++) { 556. if (inside_region(regions[i], mon->mx, mon->my)) { 557. if (!mon_in_region(regions[i], mon)) 558. add_mon_to_reg(regions[i], mon); 559. } else { 560. if (mon_in_region(regions[i], mon)) 561. remove_mon_from_reg(regions[i], mon); 562. } 563. } 564. } 565. 566. #if 0 567. /* not yet used */ 568. 569. /* 570. * Change monster pointer in regions 571. * This happens, for instance, when a monster grows and 572. * need a new structure (internally that is). 573. */ 574. void 575. replace_mon_regions(monold, monnew) 576. struct monst *monold, *monnew; 577. { 578. register int i; 579. 580. for (i = 0; i < n_regions; i++) 581. if (mon_in_region(regions[i], monold)) { 582. remove_mon_from_reg(regions[i], monold); 583. add_mon_to_reg(regions[i], monnew); 584. } 585. } 586. 587. /* 588. * Remove monster from all regions it was in (ie monster just died) 589. */ 590. void 591. remove_mon_from_regions(mon) 592. struct monst *mon; 593. { 594. register int i; 595. 596. for (i = 0; i < n_regions; i++) 597. if (mon_in_region(regions[i], mon)) 598. remove_mon_from_reg(regions[i], mon); 599. } 600. 601. #endif /*0*/ 602. 603. /* 604. * Check if a spot is under a visible region (eg: gas cloud). 605. * Returns NULL if not, otherwise returns region. 606. */ 607. NhRegion * 608. visible_region_at(x, y) 609. xchar x, y; 610. { 611. register int i; 612. 613. for (i = 0; i < n_regions; i++) { 614. if (!regions[i]->visible || regions[i]->ttl == -2L) 615. continue; 616. if (inside_region(regions[i], x, y)) 617. return regions[i]; 618. } 619. return (NhRegion *) 0; 620. } 621. 622. void 623. show_region(reg, x, y) 624. NhRegion *reg; 625. xchar x, y; 626. { 627. show_glyph(x, y, reg->glyph); 628. } 629. 630. /** 631. * save_regions : 632. */ 633. void 634. save_regions(fd, mode) 635. int fd; 636. int mode; 637. { 638. int i, j; 639. unsigned n; 640. 641. if (!perform_bwrite(mode)) 642. goto skip_lots; 643. 644. bwrite(fd, (genericptr_t) &moves, sizeof(moves)); /* timestamp */ 645. bwrite(fd, (genericptr_t) &n_regions, sizeof(n_regions)); 646. for (i = 0; i < n_regions; i++) { 647. bwrite(fd, (genericptr_t) ®ions[i]->bounding_box, sizeof(NhRect)); 648. bwrite(fd, (genericptr_t) ®ions[i]->nrects, sizeof(short)); 649. for (j = 0; j < regions[i]->nrects; j++) 650. bwrite(fd, (genericptr_t) ®ions[i]->rects[j], sizeof(NhRect)); 651. bwrite(fd, (genericptr_t) ®ions[i]->attach_2_u, sizeof(boolean)); 652. n = 0; 653. bwrite(fd, (genericptr_t) ®ions[i]->attach_2_m, sizeof(unsigned)); 654. n = regions[i]->enter_msg != (const char *) 0 655. ? strlen(regions[i]->enter_msg) 656. : 0; 657. bwrite(fd, (genericptr_t) &n, sizeof n); 658. if (n > 0) 659. bwrite(fd, (genericptr_t) regions[i]->enter_msg, n); 660. n = regions[i]->leave_msg != (const char *) 0 661. ? strlen(regions[i]->leave_msg) 662. : 0; 663. bwrite(fd, (genericptr_t) &n, sizeof n); 664. if (n > 0) 665. bwrite(fd, (genericptr_t) regions[i]->leave_msg, n); 666. bwrite(fd, (genericptr_t) ®ions[i]->ttl, sizeof(long)); 667. bwrite(fd, (genericptr_t) ®ions[i]->expire_f, sizeof(short)); 668. bwrite(fd, (genericptr_t) ®ions[i]->can_enter_f, sizeof(short)); 669. bwrite(fd, (genericptr_t) ®ions[i]->enter_f, sizeof(short)); 670. bwrite(fd, (genericptr_t) ®ions[i]->can_leave_f, sizeof(short)); 671. bwrite(fd, (genericptr_t) ®ions[i]->leave_f, sizeof(short)); 672. bwrite(fd, (genericptr_t) ®ions[i]->inside_f, sizeof(short)); 673. bwrite(fd, (genericptr_t) ®ions[i]->player_flags, 674. sizeof(unsigned int)); 675. bwrite(fd, (genericptr_t) ®ions[i]->n_monst, sizeof(short)); 676. for (j = 0; j < regions[i]->n_monst; j++) 677. bwrite(fd, (genericptr_t) ®ions[i]->monsters[j], 678. sizeof(unsigned)); 679. bwrite(fd, (genericptr_t) ®ions[i]->visible, sizeof(boolean)); 680. bwrite(fd, (genericptr_t) ®ions[i]->glyph, sizeof(int)); 681. bwrite(fd, (genericptr_t) ®ions[i]->arg, sizeof(anything)); 682. } 683. 684. skip_lots: 685. if (release_data(mode)) 686. clear_regions(); 687. } 688. 689. void 690. rest_regions(fd, ghostly) 691. int fd; 692. boolean ghostly; /* If a bones file restore */ 693. { 694. int i, j; 695. unsigned n; 696. long tmstamp; 697. char *msg_buf; 698. 699. clear_regions(); /* Just for security */ 700. mread(fd, (genericptr_t) &tmstamp, sizeof(tmstamp)); 701. if (ghostly) 702. tmstamp = 0; 703. else 704. tmstamp = (moves - tmstamp); 705. mread(fd, (genericptr_t) &n_regions, sizeof(n_regions)); 706. max_regions = n_regions; 707. if (n_regions > 0) 708. regions = (NhRegion **) alloc(sizeof(NhRegion *) * n_regions); 709. for (i = 0; i < n_regions; i++) { 710. regions[i] = (NhRegion *) alloc(sizeof(NhRegion)); 711. mread(fd, (genericptr_t) ®ions[i]->bounding_box, sizeof(NhRect)); 712. mread(fd, (genericptr_t) ®ions[i]->nrects, sizeof(short)); 713. 714. if (regions[i]->nrects > 0) 715. regions[i]->rects = 716. (NhRect *) alloc(sizeof(NhRect) * regions[i]->nrects); 717. for (j = 0; j < regions[i]->nrects; j++) 718. mread(fd, (genericptr_t) ®ions[i]->rects[j], sizeof(NhRect)); 719. mread(fd, (genericptr_t) ®ions[i]->attach_2_u, sizeof(boolean)); 720. mread(fd, (genericptr_t) ®ions[i]->attach_2_m, sizeof(unsigned)); 721. 722. mread(fd, (genericptr_t) &n, sizeof n); 723. if (n > 0) { 724. msg_buf = (char *) alloc(n + 1); 725. mread(fd, (genericptr_t) msg_buf, n); 726. msg_buf[n] = '\0'; 727. regions[i]->enter_msg = (const char *) msg_buf; 728. } else 729. regions[i]->enter_msg = (const char *) 0; 730. 731. mread(fd, (genericptr_t) &n, sizeof n); 732. if (n > 0) { 733. msg_buf = (char *) alloc(n + 1); 734. mread(fd, (genericptr_t) msg_buf, n); 735. msg_buf[n] = '\0'; 736. regions[i]->leave_msg = (const char *) msg_buf; 737. } else 738. regions[i]->leave_msg = (const char *) 0; 739. 740. mread(fd, (genericptr_t) ®ions[i]->ttl, sizeof(long)); 741. /* check for expired region */ 742. if (regions[i]->ttl >= 0L) 743. regions[i]->ttl = 744. (regions[i]->ttl > tmstamp) ? regions[i]->ttl - tmstamp : 0L; 745. mread(fd, (genericptr_t) ®ions[i]->expire_f, sizeof(short)); 746. mread(fd, (genericptr_t) ®ions[i]->can_enter_f, sizeof(short)); 747. mread(fd, (genericptr_t) ®ions[i]->enter_f, sizeof(short)); 748. mread(fd, (genericptr_t) ®ions[i]->can_leave_f, sizeof(short)); 749. mread(fd, (genericptr_t) ®ions[i]->leave_f, sizeof(short)); 750. mread(fd, (genericptr_t) ®ions[i]->inside_f, sizeof(short)); 751. mread(fd, (genericptr_t) ®ions[i]->player_flags, 752. sizeof(unsigned int)); 753. if (ghostly) { /* settings pertained to old player */ 754. clear_hero_inside(regions[i]); 755. clear_heros_fault(regions[i]); 756. } 757. mread(fd, (genericptr_t) ®ions[i]->n_monst, sizeof(short)); 758. if (regions[i]->n_monst > 0) 759. regions[i]->monsters = 760. (unsigned *) alloc(sizeof(unsigned) * regions[i]->n_monst); 761. else 762. regions[i]->monsters = (unsigned int *) 0; 763. regions[i]->max_monst = regions[i]->n_monst; 764. for (j = 0; j < regions[i]->n_monst; j++) 765. mread(fd, (genericptr_t) ®ions[i]->monsters[j], 766. sizeof(unsigned)); 767. mread(fd, (genericptr_t) ®ions[i]->visible, sizeof(boolean)); 768. mread(fd, (genericptr_t) ®ions[i]->glyph, sizeof(int)); 769. mread(fd, (genericptr_t) ®ions[i]->arg, sizeof(anything)); 770. } 771. /* remove expired regions, do not trigger the expire_f callback (yet!); 772. also update monster lists if this data is coming from a bones file */ 773. for (i = n_regions - 1; i >= 0; i--) 774. if (regions[i]->ttl == 0L) 775. remove_region(regions[i]); 776. else if (ghostly && regions[i]->n_monst > 0) 777. reset_region_mids(regions[i]); 778. } 779. 780. /* to support '#stats' wizard-mode command */ 781. void 782. region_stats(hdrfmt, hdrbuf, count, size) 783. const char *hdrfmt; 784. char *hdrbuf; 785. long *count, *size; 786. { 787. NhRegion *rg; 788. int i; 789. 790. /* other stats formats take one parameter; this takes two */ 791. Sprintf(hdrbuf, hdrfmt, (long) sizeof (NhRegion), (long) sizeof (NhRect)); 792. *count = (long) n_regions; /* might be 0 even though max_regions isn't */ 793. *size = (long) max_regions * (long) sizeof (NhRegion); 794. for (i = 0; i < n_regions; ++i) { 795. rg = regions[i]; 796. *size += (long) rg->nrects * (long) sizeof (NhRect); 797. if (rg->enter_msg) 798. *size += (long) (strlen(rg->enter_msg) + 1); 799. if (rg->leave_msg) 800. *size += (long) (strlen(rg->leave_msg) + 1); 801. *size += (long) rg->max_monst * (long) sizeof *rg->monsters; 802. } 803. /* ? */ 804. } 805. 806. /* update monster IDs for region being loaded from bones; `ghostly' implied */ 807. STATIC_OVL void 808. reset_region_mids(reg) 809. NhRegion *reg; 810. { 811. int i = 0, n = reg->n_monst; 812. unsigned *mid_list = reg->monsters; 813. 814. while (i < n) 815. if (!lookup_id_mapping(mid_list[i], &mid_list[i])) { 816. /* shrink list to remove missing monster; order doesn't matter */ 817. mid_list[i] = mid_list[--n]; 818. } else { 819. /* move on to next monster */ 820. ++i; 821. } 822. reg->n_monst = n; 823. return; 824. } 825. 826. #if 0 827. /* not yet used */ 828. 829. /*--------------------------------------------------------------* 830. * * 831. * Create Region with just a message * 832. * * 833. *--------------------------------------------------------------*/ 834. 835. NhRegion * 836. create_msg_region(x, y, w, h, msg_enter, msg_leave) 837. xchar x, y; 838. xchar w, h; 839. const char *msg_enter; 840. const char *msg_leave; 841. { 842. NhRect tmprect; 843. NhRegion *reg = create_region((NhRect *) 0, 0); 844. 845. if (msg_enter) 846. reg->enter_msg = dupstr(msg_enter); 847. if (msg_leave) 848. reg->leave_msg = dupstr(msg_leave); 849. tmprect.lx = x; 850. tmprect.ly = y; 851. tmprect.hx = x + w; 852. tmprect.hy = y + h; 853. add_rect_to_reg(reg, &tmprect); 854. reg->ttl = -1L; 855. return reg; 856. } 857. 858. 859. /*--------------------------------------------------------------* 860. * * 861. * Force Field Related Cod * 862. * (unused yet) * 863. *--------------------------------------------------------------*/ 864. 865. boolean 866. enter_force_field(p1, p2) 867. genericptr_t p1; 868. genericptr_t p2; 869. { 870. struct monst *mtmp; 871. 872. if (p2 == (genericptr_t) 0) { /* That means the player */ 873. if (!Blind) 874. You("bump into %s. Ouch!", 875. Hallucination ? "an invisible tree" 876. : "some kind of invisible wall"); 877. else 878. pline("Ouch!"); 879. } else { 880. mtmp = (struct monst *) p2; 881. if (canseemon(mtmp)) 882. pline("%s bumps into %s!", Monnam(mtmp), something); 883. } 884. return FALSE; 885. } 886. 887. NhRegion * 888. create_force_field(x, y, radius, ttl) 889. xchar x, y; 890. int radius; 891. long ttl; 892. { 893. int i; 894. NhRegion *ff; 895. int nrect; 896. NhRect tmprect; 897. 898. ff = create_region((NhRect *) 0, 0); 899. nrect = radius; 900. tmprect.lx = x; 901. tmprect.hx = x; 902. tmprect.ly = y - (radius - 1); 903. tmprect.hy = y + (radius - 1); 904. for (i = 0; i < nrect; i++) { 905. add_rect_to_reg(ff, &tmprect); 906. tmprect.lx--; 907. tmprect.hx++; 908. tmprect.ly++; 909. tmprect.hy--; 910. } 911. ff->ttl = ttl; 912. if (!in_mklev && !context.mon_moving) 913. set_heros_fault(ff); /* assume player has created it */ 914. /* ff->can_enter_f = enter_force_field; */ 915. /* ff->can_leave_f = enter_force_field; */ 916. add_region(ff); 917. return ff; 918. } 919. 920. #endif /*0*/ 921. 922. /*--------------------------------------------------------------* 923. * * 924. * Gas cloud related code * 925. * * 926. *--------------------------------------------------------------*/ 927. 928. /* 929. * Here is an example of an expire function that may prolong 930. * region life after some mods... 931. */ 932. /*ARGSUSED*/ 933. boolean 934. expire_gas_cloud(p1, p2) 935. genericptr_t p1; 936. genericptr_t p2 UNUSED; 937. { 938. NhRegion *reg; 939. int damage; 940. 941. reg = (NhRegion *) p1; 942. damage = reg->arg.a_int; 943. 944. /* If it was a thick cloud, it dissipates a little first */ 945. if (damage >= 5) { 946. damage /= 2; /* It dissipates, let's do less damage */ 947. reg->arg = zeroany; 948. reg->arg.a_int = damage; 949. reg->ttl = 2L; /* Here's the trick : reset ttl */ 950. return FALSE; /* THEN return FALSE, means "still there" */ 951. } 952. return TRUE; /* OK, it's gone, you can free it! */ 953. } 954. 955. boolean 956. inside_gas_cloud(p1, p2) 957. genericptr_t p1; 958. genericptr_t p2; 959. { 960. NhRegion *reg; 961. struct monst *mtmp; 962. int dam; 963. 964. /* 965. * Gas clouds can't be targetted at water locations, but they can 966. * start next to water and spread over it. 967. */ 968. 969. reg = (NhRegion *) p1; 970. dam = reg->arg.a_int; 971. if (p2 == (genericptr_t) 0) { /* This means *YOU* Bozo! */ 972. if (u.uinvulnerable || nonliving(youmonst.data) || Breathless 973. || Underwater) 974. return FALSE; 975. if (!Blind) { 976. Your("%s sting.", makeplural(body_part(EYE))); 977. make_blinded(1L, FALSE); 978. } 979. if (!Poison_resistance) { 980. pline("%s is burning your %s!", Something, 981. makeplural(body_part(LUNG))); 982. You("cough and spit blood!"); 983. losehp(Maybe_Half_Phys(rnd(dam) + 5), "gas cloud", KILLED_BY_AN); 984. return FALSE; 985. } else { 986. You("cough!"); 987. return FALSE; 988. } 989. } else { /* A monster is inside the cloud */ 990. mtmp = (struct monst *) p2; 991. 992. /* Non living and non breathing monsters are not concerned; 993. adult green dragon is not affected by gas cloud, baby one is */ 994. if (!(nonliving(mtmp->data) || is_vampshifter(mtmp)) 995. && !breathless(mtmp->data) 996. /* not is_swimmer(); assume that non-fish are swimming on 997. the surface and breathing the air above it periodically 998. unless located at water spot on plane of water */ 999. && !((mtmp->data->mlet == S_EEL || Is_waterlevel(&u.uz)) 1000. && is_pool(mtmp->mx, mtmp->my)) 1001. /* exclude monsters with poison gas breath attack: 1002. adult green dragon and Chromatic Dragon (and iron golem, 1003. but nonliving() and breathless() tests also catch that) */ 1004. && !(attacktype_fordmg(mtmp->data, AT_BREA, AD_DRST) 1005. || attacktype_fordmg(mtmp->data, AT_BREA, AD_RBRE))) { 1006. if (cansee(mtmp->mx, mtmp->my)) 1007. pline("%s coughs!", Monnam(mtmp)); 1008. if (heros_fault(reg)) 1009. setmangry(mtmp, TRUE); 1010. if (haseyes(mtmp->data) && mtmp->mcansee) { 1011. mtmp->mblinded = 1; 1012. mtmp->mcansee = 0; 1013. } 1014. if (resists_poison(mtmp)) 1015. return FALSE; 1016. mtmp->mhp -= rnd(dam) + 5; 1017. if (DEADMONSTER(mtmp)) { 1018. if (heros_fault(reg)) 1019. killed(mtmp); 1020. else 1021. monkilled(mtmp, "gas cloud", AD_DRST); 1022. if (DEADMONSTER(mtmp)) { /* not lifesaved */ 1023. return TRUE; 1024. } 1025. } 1026. } 1027. } 1028. return FALSE; /* Monster is still alive */ 1029. } 1030. 1031. NhRegion * 1032. create_gas_cloud(x, y, radius, damage) 1033. xchar x, y; 1034. int radius; 1035. int damage; 1036. { 1037. NhRegion *cloud; 1038. int i, nrect; 1039. NhRect tmprect; 1040. 1041. cloud = create_region((NhRect *) 0, 0); 1042. nrect = radius; 1043. tmprect.lx = x; 1044. tmprect.hx = x; 1045. tmprect.ly = y - (radius - 1); 1046. tmprect.hy = y + (radius - 1); 1047. for (i = 0; i < nrect; i++) { 1048. add_rect_to_reg(cloud, &tmprect); 1049. tmprect.lx--; 1050. tmprect.hx++; 1051. tmprect.ly++; 1052. tmprect.hy--; 1053. } 1054. cloud->ttl = rn1(3, 4); 1055. if (!in_mklev && !context.mon_moving) 1056. set_heros_fault(cloud); /* assume player has created it */ 1057. cloud->inside_f = INSIDE_GAS_CLOUD; 1058. cloud->expire_f = EXPIRE_GAS_CLOUD; 1059. cloud->arg = zeroany; 1060. cloud->arg.a_int = damage; 1061. cloud->visible = TRUE; 1062. cloud->glyph = cmap_to_glyph(damage ? S_poisoncloud : S_cloud); 1063. add_region(cloud); 1064. return cloud; 1065. } 1066. 1067. /* for checking troubles during prayer; is hero at risk? */ 1068. boolean 1069. region_danger() 1070. { 1071. int i, f_indx, n = 0; 1072. 1073. for (i = 0; i < n_regions; i++) { 1074. /* only care about regions that hero is in */ 1075. if (!hero_inside(regions[i])) 1076. continue; 1077. f_indx = regions[i]->inside_f; 1078. /* the only type of region we understand is gas_cloud */ 1079. if (f_indx == INSIDE_GAS_CLOUD) { 1080. /* completely harmless if you don't need to breathe */ 1081. if (nonliving(youmonst.data) || Breathless) 1082. continue; 1083. /* minor inconvenience if you're poison resistant; 1084. not harmful enough to be a prayer-level trouble */ 1085. if (Poison_resistance) 1086. continue; 1087. ++n; 1088. } 1089. } 1090. return n ? TRUE : FALSE; 1091. } 1092. 1093. /* for fixing trouble at end of prayer; 1094. danger detected at start of prayer might have expired by now */ 1095. void 1096. region_safety() 1097. { 1098. NhRegion *r = 0; 1099. int i, f_indx, n = 0; 1100. 1101. for (i = 0; i < n_regions; i++) { 1102. /* only care about regions that hero is in */ 1103. if (!hero_inside(regions[i])) 1104. continue; 1105. f_indx = regions[i]->inside_f; 1106. /* the only type of region we understand is gas_cloud */ 1107. if (f_indx == INSIDE_GAS_CLOUD) { 1108. if (!n++ && regions[i]->ttl >= 0) 1109. r = regions[i]; 1110. } 1111. } 1112. 1113. if (n > 1 || (n == 1 && !r)) { 1114. /* multiple overlapping cloud regions or non-expiring one */ 1115. safe_teleds(FALSE); 1116. } else if (r) { 1117. remove_region(r); 1118. pline_The("gas cloud enveloping you dissipates."); 1119. } else { 1120. /* cloud dissipated on its own, so nothing needs to be done */ 1121. pline_The("gas cloud has dissipated."); 1122. } 1123. /* maybe cure blindness too */ 1124. if ((Blinded & TIMEOUT) == 1L) 1125. make_blinded(0L, TRUE); 1126. } 1127. 1128. /*region.c*/