home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / games / volume16 / nethck31 / part88 < prev    next >
Encoding:
Internet Message Format  |  1993-02-05  |  57.5 KB

  1. Path: uunet!news.tek.com!master!saab!billr
  2. From: billr@saab.CNA.TEK.COM (Bill Randle)
  3. Newsgroups: comp.sources.games
  4. Subject: v16i096:  nethack31 - display oriented dungeons & dragons (Ver. 3.1), Part88/108
  5. Message-ID: <4459@master.CNA.TEK.COM>
  6. Date: 5 Feb 93 19:22:12 GMT
  7. Sender: news@master.CNA.TEK.COM
  8. Lines: 2010
  9. Approved: billr@saab.CNA.TEK.COM
  10. Xref: uunet comp.sources.games:1647
  11.  
  12. Submitted-by: izchak@linc.cis.upenn.edu (Izchak Miller)
  13. Posting-number: Volume 16, Issue 96
  14. Archive-name: nethack31/Part88
  15. Supersedes: nethack3p9: Volume 10, Issue 46-102
  16. Environment: Amiga, Atari, Mac, MS-DOS, OS2, Unix, VMS, X11
  17.  
  18.  
  19.  
  20. #! /bin/sh
  21. # This is a shell archive.  Remove anything before this line, then unpack
  22. # it by saving it into a file and typing "sh file".  To overwrite existing
  23. # files, type "sh file -c".  You can also feed this as standard input via
  24. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  25. # will see the following message at the end:
  26. #        "End of archive 88 (of 108)."
  27. # Contents:  dat/Caveman.des src/weapon.c sys/amiga/splitter/splitter.c
  28. #   sys/share/random.c
  29. # Wrapped by billr@saab on Wed Jan 27 16:09:22 1993
  30. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  31. if test -f 'dat/Caveman.des' -a "${1}" != "-c" ; then 
  32.   echo shar: Will not clobber existing file \"'dat/Caveman.des'\"
  33. else
  34. echo shar: Extracting \"'dat/Caveman.des'\" \(11855 characters\)
  35. sed "s/^X//" >'dat/Caveman.des' <<'END_OF_FILE'
  36. X#    SCCS Id: @(#)Caveman.des    3.1    91/12/24
  37. X#    Copyright (c) 1989 by Jean-Christophe Collet
  38. X#    Copyright (c) 1991 by M. Stephenson
  39. X# NetHack may be freely redistributed.  See license for details.
  40. X#
  41. X#    The "start" level for the quest.
  42. X#
  43. X#    Here you meet your (besieged) class leader, Shaman Karnov
  44. X#    and receive your quest assignment.
  45. X#
  46. XMAZE: "C-start",' '
  47. XFLAGS: noteleport,hardfloor
  48. XGEOMETRY:center,center
  49. XMAP
  50. X                                                                            
  51. X  ......     ..........................       ...        ....  ......       
  52. X ......       ..........................     ........       ....    .....   
  53. X  ..BB      .............................    .........            ....  ..  
  54. X     ..    ......................              .......      ..     ....  .. 
  55. X     ..     ....................                     ..  .......    ..  ... 
  56. X   ..              S   BB                .....     .......   ....      .... 
  57. X    ..        ...  .   ..               ........  ..     ..   ..       ...  
  58. X     ..      ......     ..             ............       ..          ...   
  59. X       .      ....       ..             ........           ..  ...........  
  60. X  ...   ..     ..        .............                  ................... 
  61. X .....   .....            ...............................      ...........  
  62. X  .....B................            ...                               ...   
  63. X  .....     .  ..........        .... .      ...  ..........           ...  
  64. X   ...     ..          .............  ..    ...................        .... 
  65. X          BB       ..   .........      BB    ...  ..........  ..   ...  ... 
  66. X       ......    .....  B          ........         ..         .. ....  ... 
  67. X     ..........  ..........         ..... ...      .....        ........    
  68. X       ..  ...    .  .....         ....    ..       ...            ..       
  69. X                                                                            
  70. XENDMAP
  71. X# Random Monsters
  72. XRANDOM_MONSTERS: 'h', 'H'
  73. X# Dungeon Description
  74. XREGION:(00,00,75,19),unlit,"ordinary"
  75. XREGION:(13,01,40,05),lit,"temple",unfilled,true
  76. X# The occupied rooms.
  77. XREGION:(02,01,08,03),lit,"ordinary",unfilled,true
  78. XREGION:(01,11,06,14),lit,"ordinary",unfilled,true
  79. XREGION:(13,08,18,10),lit,"ordinary",unfilled,true
  80. XREGION:(05,17,14,18),lit,"ordinary",unfilled,true
  81. XREGION:(17,16,23,18),lit,"ordinary",unfilled,true
  82. XREGION:(35,16,44,18),lit,"ordinary",unfilled,true
  83. X# Stairs
  84. XSTAIR:(02,03),down
  85. X# Portal arrival point
  86. XBRANCH:(71,09,71,09),(0,0,0,0)
  87. X# Doors
  88. XDOOR:locked,(19,06)
  89. X# The temple altar (this will force a priest(ess) to be created)
  90. XALTAR:(36,02),law,shrine
  91. X# Shaman Karnov
  92. XMONSTER:'@',"Shaman Karnov",(35,02)
  93. X# The treasure of Shaman Karnov
  94. XOBJECT:'(',"chest",(34,02)
  95. X# neanderthal guards for the audience chamber
  96. XMONSTER:'@',"neanderthal",(20,03)
  97. XMONSTER:'@',"neanderthal",(20,02)
  98. XMONSTER:'@',"neanderthal",(20,01)
  99. XMONSTER:'@',"neanderthal",(21,03)
  100. XMONSTER:'@',"neanderthal",(21,02)
  101. XMONSTER:'@',"neanderthal",(21,01)
  102. XMONSTER:'@',"neanderthal",(22,01)
  103. XMONSTER:'@',"neanderthal",(26,09)
  104. X# Non diggable walls
  105. XNON_DIGGABLE:(00,00,75,19)
  106. X# Random traps
  107. XTRAP:"pit",(47,11)
  108. XTRAP:"pit",(57,10)
  109. XTRAP:random,random
  110. XTRAP:random,random
  111. XTRAP:random,random
  112. XTRAP:random,random
  113. X# Monsters on siege duty (in the outer caves).
  114. XMONSTER: 'h',"bugbear",(47,02),hostile
  115. XMONSTER: 'h',"bugbear",(48,03),hostile
  116. XMONSTER: 'h',"bugbear",(49,04),hostile
  117. XMONSTER: 'h',"bugbear",(67,03),hostile
  118. XMONSTER: 'h',"bugbear",(69,04),hostile
  119. XMONSTER: 'h',"bugbear",(51,13),hostile
  120. XMONSTER: 'h',"bugbear",(53,14),hostile
  121. XMONSTER: 'h',"bugbear",(55,15),hostile
  122. XMONSTER: 'h',"bugbear",(63,10),hostile
  123. XMONSTER: 'h',"bugbear",(65,09),hostile
  124. XMONSTER: 'h',"bugbear",(67,10),hostile
  125. XMONSTER: 'h',"bugbear",(69,11),hostile
  126. XWALLIFY
  127. X#
  128. X#    The "locate" level for the quest.
  129. X#
  130. X#    Here you have to find the lair of Tiamat to go
  131. X#    further towards your assigned quest.
  132. X#
  133. X
  134. XMAZE: "C-locate",' '
  135. XFLAGS: hardfloor
  136. XGEOMETRY:center,center
  137. XMAP
  138. X                                                                            
  139. X    .............                     ...........                           
  140. X   ...............                   .............                          
  141. X    .............                  ...............        ..........        
  142. X     ...........                    .............      ...............      
  143. X        ...                                    ...   ..................     
  144. X         ...                ..........          ... ..................      
  145. X          ...              ............          BBB...................     
  146. X           ...              ..........          ......................      
  147. X            .....                 ..      .....B........................    
  148. X  ....       ...............      .    ........B..........................  
  149. X ......     .. .............S..............         ..................      
  150. X  ....     ..                ...........             ...............        
  151. X     ..  ...                                    ....................        
  152. X      ....                                      BB...................       
  153. X         ..                 ..                 ..  ...............          
  154. X          ..   .......     ....  .....  ....  ..     .......   S            
  155. X           ............     ....... ..  .......       .....    ...  ....    
  156. X               .......       .....   ......                      .......    
  157. X                                                                            
  158. XENDMAP
  159. X# Random Monsters
  160. XRANDOM_MONSTERS: 'h', 'H'
  161. X# Dungeon Description
  162. XREGION:(00,00,75,19),unlit,"ordinary"
  163. XREGION:(52,06,73,15),lit,"ordinary",unfilled,true
  164. X# Doors
  165. XDOOR:locked,(28,11)
  166. X# Stairs
  167. XSTAIR:(04,03),up
  168. XSTAIR:(73,10),down
  169. X# Non diggable walls
  170. XNON_DIGGABLE:(00,00,75,19)
  171. X# Objects
  172. XOBJECT:random,random,random
  173. XOBJECT:random,random,random
  174. XOBJECT:random,random,random
  175. XOBJECT:random,random,random
  176. XOBJECT:random,random,random
  177. XOBJECT:random,random,random
  178. XOBJECT:random,random,random
  179. XOBJECT:random,random,random
  180. XOBJECT:random,random,random
  181. XOBJECT:random,random,random
  182. XOBJECT:random,random,random
  183. XOBJECT:random,random,random
  184. XOBJECT:random,random,random
  185. XOBJECT:random,random,random
  186. XOBJECT:random,random,random
  187. X# Random traps
  188. XTRAP:random,random
  189. XTRAP:random,random
  190. XTRAP:random,random
  191. XTRAP:random,random
  192. XTRAP:random,random
  193. XTRAP:random,random
  194. X# Random monsters.
  195. XMONSTER:'h',"bugbear",(02,10),hostile
  196. XMONSTER:'h',"bugbear",(03,11),hostile
  197. XMONSTER:'h',"bugbear",(04,12),hostile
  198. XMONSTER:'h',"bugbear",(02,11),hostile
  199. XMONSTER:'h',"bugbear",(16,16),hostile
  200. XMONSTER:'h',"bugbear",(17,17),hostile
  201. XMONSTER:'h',"bugbear",(18,18),hostile
  202. XMONSTER:'h',"bugbear",(19,16),hostile
  203. XMONSTER:'h',"bugbear",(30,06),hostile
  204. XMONSTER:'h',"bugbear",(31,07),hostile
  205. XMONSTER:'h',"bugbear",(32,08),hostile
  206. XMONSTER:'h',"bugbear",(33,06),hostile
  207. XMONSTER:'h',"bugbear",(34,07),hostile
  208. XMONSTER:'h',"bugbear",random,hostile
  209. XMONSTER:'h',"bugbear",random,hostile
  210. XMONSTER:'h',"bugbear",random,hostile
  211. XMONSTER:'h',"bugbear",random,hostile
  212. XMONSTER:'h',random,random,hostile
  213. XMONSTER:'H',random,random,hostile
  214. XMONSTER:'H',"hill giant",(03,12),hostile
  215. XMONSTER:'H',"hill giant",(20,17),hostile
  216. XMONSTER:'H',"hill giant",(35,08),hostile
  217. XMONSTER:'H',"hill giant",random,hostile
  218. XMONSTER:'H',"hill giant",random,hostile
  219. XMONSTER:'H',"hill giant",random,hostile
  220. XMONSTER:'H',"hill giant",random,hostile
  221. XMONSTER:'H',random,random,hostile
  222. XWALLIFY
  223. X
  224. X#
  225. X#    The "goal" level for the quest.
  226. X#
  227. X#    Here you meet Tiamat your nemesis monster.  You have to
  228. X#    defeat Tiamat in combat to gain the artifact you have
  229. X#    been assigned to retrieve.
  230. X#
  231. X
  232. XMAZE: "C-goal", ' '
  233. XGEOMETRY:center,center
  234. XMAP
  235. X                                                                            
  236. X                          .....................                             
  237. X                         .......................                            
  238. X                        .........................                           
  239. X                       ...........................                          
  240. X                      .............................                         
  241. X                     ...............................                        
  242. X                    .................................                       
  243. X                   ...................................                      
  244. X                  .....................................                     
  245. X                 .......................................                    
  246. X                  .....................................                     
  247. X                   ...................................                      
  248. X                    .................................                       
  249. X                     ...............................                        
  250. X                      .............................                         
  251. X                       ...........................                          
  252. X                        .........................                           
  253. X                         .......................                            
  254. X                                                                            
  255. XENDMAP
  256. X# Random Monsters
  257. XRANDOM_MONSTERS: 'h', 'H'
  258. X# Dungeon Description
  259. XREGION:(00,00,75,19),lit,"ordinary"
  260. X# Stairs
  261. XSTAIR:random,up
  262. X# Non diggable walls
  263. XNON_DIGGABLE:(00,00,75,19)
  264. X# Objects
  265. XOBJECT:')',"quarterstaff",(23,10),blessed,0,"The Sceptre of Might"
  266. XOBJECT:random,random,random
  267. XOBJECT:random,random,random
  268. XOBJECT:random,random,random
  269. XOBJECT:random,random,random
  270. XOBJECT:random,random,random
  271. XOBJECT:random,random,random
  272. XOBJECT:random,random,random
  273. XOBJECT:random,random,random
  274. XOBJECT:random,random,random
  275. XOBJECT:random,random,random
  276. XOBJECT:random,random,random
  277. XOBJECT:random,random,random
  278. XOBJECT:random,random,random
  279. XOBJECT:random,random,random
  280. X# monsters.
  281. XMONSTER:'D',"Chromatic Dragon",(23,10),hostile
  282. XWALLIFY
  283. X
  284. X#
  285. X#    The "fill" levels for the quest.
  286. X#
  287. X#    These levels are used to fill out any levels not occupied by specific
  288. X#    levels as defined above. "filla" is the upper filler, between the
  289. X#    start and locate levels, and "fillb" the lower between the locate
  290. X#    and goal levels.
  291. X#
  292. X
  293. XMAZE: "C-filla" , ' '
  294. XINIT_MAP: '.' , ' ' , true , true , random , true
  295. XNOMAP
  296. X# Random Monsters
  297. XRANDOM_MONSTERS: 'h', 'H'
  298. X#
  299. XSTAIR: random, up
  300. XSTAIR: random, down
  301. X#
  302. XOBJECT: random, random, random
  303. XOBJECT: random, random, random
  304. XOBJECT: random, random, random
  305. XOBJECT: random, random, random
  306. XOBJECT: random, random, random
  307. XOBJECT: random, random, random
  308. XOBJECT: random, random, random
  309. X#
  310. XTRAP: random, random
  311. XTRAP: random, random
  312. XTRAP: random, random
  313. XTRAP: random, random
  314. X#
  315. XMONSTER: 'h', "bugbear", random, hostile
  316. XMONSTER: 'h', "bugbear", random, hostile
  317. XMONSTER: 'h', "bugbear", random, hostile
  318. XMONSTER: 'h', "bugbear", random, hostile
  319. XMONSTER: 'h', "bugbear", random, hostile
  320. XMONSTER: 'h', random, random, hostile
  321. XMONSTER: 'H', "hill giant", random, hostile
  322. X
  323. XMAZE: "C-fillb" , ' '
  324. XINIT_MAP: '.' , ' ' , true , true , random , true
  325. XNOMAP
  326. X# Random Monsters
  327. XRANDOM_MONSTERS: 'h', 'H'
  328. X#
  329. XSTAIR: random, up
  330. XSTAIR: random, down
  331. X#
  332. XOBJECT: random, random, random
  333. XOBJECT: random, random, random
  334. XOBJECT: random, random, random
  335. XOBJECT: random, random, random
  336. XOBJECT: random, random, random
  337. XOBJECT: random, random, random
  338. XOBJECT: random, random, random
  339. XOBJECT: random, random, random
  340. XOBJECT: random, random, random
  341. XOBJECT: random, random, random
  342. XOBJECT: random, random, random
  343. XOBJECT: random, random, random
  344. X#
  345. XTRAP: random, random
  346. XTRAP: random, random
  347. XTRAP: random, random
  348. XTRAP: random, random
  349. X#
  350. XMONSTER: 'h', "bugbear", random, hostile
  351. XMONSTER: 'h', "bugbear", random, hostile
  352. XMONSTER: 'h', "bugbear", random, hostile
  353. XMONSTER: 'h', "bugbear", random, hostile
  354. XMONSTER: 'h', random, random, hostile
  355. XMONSTER: 'h', random, random, hostile
  356. XMONSTER: 'H', "hill giant", random, hostile
  357. XMONSTER: 'H', "hill giant", random, hostile
  358. END_OF_FILE
  359. if test 11855 -ne `wc -c <'dat/Caveman.des'`; then
  360.     echo shar: \"'dat/Caveman.des'\" unpacked with wrong size!
  361. fi
  362. # end of 'dat/Caveman.des'
  363. fi
  364. if test -f 'src/weapon.c' -a "${1}" != "-c" ; then 
  365.   echo shar: Will not clobber existing file \"'src/weapon.c'\"
  366. else
  367. echo shar: Extracting \"'src/weapon.c'\" \(14162 characters\)
  368. sed "s/^X//" >'src/weapon.c' <<'END_OF_FILE'
  369. X/*    SCCS Id: @(#)weapon.c    3.1    93/01/15    */
  370. X/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  371. X/* NetHack may be freely redistributed.  See license for details. */
  372. X
  373. X/*
  374. X *    This module contains code for calculation of "to hit" and damage
  375. X *    bonuses for any given weapon used, as well as weapons selection
  376. X *    code for monsters.
  377. X */
  378. X#include    "hack.h"
  379. X
  380. X#ifdef OVLB
  381. X
  382. Xstatic const char NEARDATA kebabable[] = { S_XORN, S_DRAGON, S_NAGA, S_GIANT, 0 };
  383. X
  384. X/*
  385. X *    hitval returns an integer representing the "to hit" bonuses
  386. X *    of "otmp" against the monster type "ptr".
  387. X */
  388. Xint
  389. Xhitval(otmp, ptr)
  390. Xstruct    obj *otmp;
  391. Xstruct    permonst *ptr;
  392. X{
  393. X    int    tmp = 0;
  394. X
  395. X    if (otmp->oclass == WEAPON_CLASS ||
  396. X        otmp->otyp == PICK_AXE || otmp->otyp == UNICORN_HORN)
  397. X        tmp += otmp->spe;
  398. X
  399. X/*    Put weapon specific "to hit" bonuses in below:        */
  400. X    tmp += objects[otmp->otyp].oc_hitbon;
  401. X
  402. X/*    Put weapon vs. monster type "to hit" bonuses in below:    */
  403. X
  404. X    /* Blessed weapons used against undead or demons */
  405. X    if(otmp->oclass == WEAPON_CLASS && otmp->blessed &&
  406. X       (is_demon(ptr) || is_undead(ptr))) tmp += 2;
  407. X
  408. X    if(otmp->otyp >= SPEAR && otmp->otyp <= JAVELIN &&
  409. X       index(kebabable, ptr->mlet)) tmp += 2;
  410. X
  411. X    /* Check specially named weapon "to hit" bonuses */
  412. X    if (otmp->oartifact) tmp += spec_abon(otmp, ptr);
  413. X    return tmp;
  414. X}
  415. X
  416. X/*
  417. X *    dmgval returns an integer representing the damage bonuses
  418. X *    of "otmp" against the monster type "ptr".
  419. X */
  420. Xint
  421. Xdmgval(otmp, ptr)
  422. Xstruct    obj *otmp;
  423. Xstruct    permonst *ptr;
  424. X{
  425. X    int    tmp = 0;
  426. X
  427. X    if(otmp->otyp == CREAM_PIE)    return(0);
  428. X
  429. X    if(ptr->msize >= MZ_HUMAN) {
  430. X        if(objects[otmp->otyp].oc_wldam)
  431. X        tmp = rnd(objects[otmp->otyp].oc_wldam);
  432. X        switch (otmp->otyp) {
  433. X        case CROSSBOW_BOLT:
  434. X        case MORNING_STAR:
  435. X        case PARTISAN:
  436. X        case ELVEN_BROADSWORD:
  437. X        case BROADSWORD:    tmp++; break;
  438. X
  439. X        case FLAIL:
  440. X        case RANSEUR:
  441. X        case VOULGE:        tmp += rnd(4); break;
  442. X
  443. X        case ACID_VENOM:
  444. X        case HALBERD:
  445. X        case SPETUM:        tmp += rnd(6); break;
  446. X
  447. X        case BATTLE_AXE:
  448. X        case BARDICHE:
  449. X        case TRIDENT:        tmp += d(2,4); break;
  450. X
  451. X        case TSURUGI:
  452. X        case DWARVISH_MATTOCK:
  453. X        case TWO_HANDED_SWORD:    tmp += d(2,6); break;
  454. X        }
  455. X    } else {
  456. X        if(objects[otmp->otyp].oc_wsdam)
  457. X        tmp = rnd(objects[otmp->otyp].oc_wsdam);
  458. X        switch (otmp->otyp) {
  459. X        case CROSSBOW_BOLT:
  460. X        case MACE:
  461. X        case WAR_HAMMER:
  462. X        case FLAIL:
  463. X        case SPETUM:
  464. X        case TRIDENT:        tmp++; break;
  465. X
  466. X        case BATTLE_AXE:
  467. X        case BARDICHE:
  468. X        case BILL_GUISARME:
  469. X        case GUISARME:
  470. X        case LUCERN_HAMMER:
  471. X        case MORNING_STAR:
  472. X        case RANSEUR:
  473. X        case BROADSWORD:
  474. X        case ELVEN_BROADSWORD:
  475. X        case VOULGE:        tmp += rnd(4); break;
  476. X
  477. X        case ACID_VENOM:    tmp += rnd(6); break;
  478. X        }
  479. X    }
  480. X    if (otmp->oclass == WEAPON_CLASS || otmp->otyp == PICK_AXE
  481. X                        || otmp->otyp == UNICORN_HORN)
  482. X        tmp += otmp->spe;
  483. X
  484. X    tmp -= otmp->oeroded;
  485. X
  486. X    if (objects[otmp->otyp].oc_material <= LEATHER && thick_skinned(ptr))
  487. X        /* thick skinned/scaled creatures don't feel it */
  488. X        tmp = 0;
  489. X    if (ptr == &mons[PM_SHADE] && objects[otmp->otyp].oc_material != SILVER)
  490. X        tmp = 0;
  491. X
  492. X/*    Put weapon vs. monster type damage bonuses in below:    */
  493. X    if(otmp->oclass == WEAPON_CLASS) {
  494. X        if (otmp->blessed && (is_undead(ptr) || is_demon(ptr)))
  495. X        tmp += rnd(4);
  496. X        if ((otmp->otyp == AXE || otmp->otyp == BATTLE_AXE)
  497. X        && is_wooden(ptr))
  498. X        tmp += rnd(4);
  499. X        if (objects[otmp->otyp].oc_material == SILVER && hates_silver(ptr))
  500. X        tmp += rnd(20);
  501. X    }
  502. X
  503. X    return(tmp);
  504. X}
  505. X
  506. Xvoid
  507. Xset_uasmon()        /* update the "uasmon" structure */
  508. X{
  509. X#ifdef POLYSELF
  510. X    if(u.umonnum >= 0) uasmon = &mons[u.umonnum];
  511. X    else {
  512. X#endif
  513. X
  514. X        uasmon = &playermon;
  515. X        playermon.mlevel = u.ulevel;
  516. X        playermon.ac = u.uac;
  517. X        playermon.mr = (u.ulevel > 8) ? 5 * (u.ulevel-7) : u.ulevel;
  518. X#ifdef POLYSELF
  519. X    }
  520. X#endif
  521. X    return;
  522. X}
  523. X
  524. X#endif /* OVLB */
  525. X#ifdef OVL0
  526. X
  527. X#define    Oselect(x)    if((otmp = oselect(mtmp, x))) return(otmp);
  528. X
  529. Xstatic struct obj * FDECL(oselect, (struct monst *,int));
  530. X
  531. Xstatic struct obj *
  532. Xoselect(mtmp, x)
  533. Xstruct monst *mtmp;
  534. Xint x;
  535. X{
  536. X    struct obj *otmp;
  537. X
  538. X    for(otmp=mtmp->minvent; otmp; otmp = otmp->nobj) {
  539. X        if (otmp->otyp == x && touch_artifact(otmp,mtmp))
  540. X            return otmp;
  541. X    }
  542. X    return (struct obj *)0;
  543. X}
  544. X
  545. Xstatic const int NEARDATA rwep[] =
  546. X    { DWARVISH_SPEAR, ELVEN_SPEAR, SPEAR, ORCISH_SPEAR, JAVELIN,
  547. X      SHURIKEN, SILVER_ARROW, ELVEN_ARROW, ARROW, ORCISH_ARROW,
  548. X      CROSSBOW_BOLT, ELVEN_DAGGER, DAGGER, ORCISH_DAGGER, KNIFE, ROCK,
  549. X      LOADSTONE, LUCKSTONE, DART, /* BOOMERANG, */ CREAM_PIE
  550. X      /* note: CREAM_PIE should NOT be #ifdef KOPS */
  551. X      };
  552. X
  553. Xstatic struct obj *propellor;
  554. X
  555. Xstruct obj *
  556. Xselect_rwep(mtmp)    /* select a ranged weapon for the monster */
  557. Xregister struct monst *mtmp;
  558. X{
  559. X    register struct obj *otmp;
  560. X    int i;
  561. X
  562. X#ifdef KOPS
  563. X    char mlet = mtmp->data->mlet;
  564. X#endif
  565. X
  566. X    propellor = &zeroobj;
  567. X#ifdef KOPS
  568. X    if(mlet == S_KOP)    /* pies are first choice for Kops */
  569. X        Oselect(CREAM_PIE);
  570. X#endif
  571. X    if(throws_rocks(mtmp->data))    /* ...boulders for giants */
  572. X        Oselect(BOULDER);
  573. X    /*
  574. X     * other than these two specific cases, always select the
  575. X     * most potent ranged weapon to hand.
  576. X     */
  577. X    for (i = 0; i < SIZE(rwep); i++) {
  578. X        int prop;
  579. X
  580. X        propellor = &zeroobj;
  581. X        /* shooting gems from slings; this goes just before the darts */
  582. X        if (rwep[i]==DART && !likes_gems(mtmp->data)
  583. X            && (propellor = m_carrying(mtmp, SLING))) {
  584. X        for(otmp=mtmp->minvent; otmp; otmp=otmp->nobj) {
  585. X            if(otmp->oclass==GEM_CLASS &&
  586. X                (otmp->otyp != LOADSTONE || !otmp->cursed))
  587. X            return(otmp);
  588. X        }
  589. X        }
  590. X        prop = (objects[rwep[i]]).w_propellor;
  591. X        if (prop > 0) {
  592. X        switch (prop) {
  593. X        case WP_BOW:
  594. X          propellor = (oselect(mtmp, ELVEN_BOW));
  595. X          if (!propellor) propellor = (oselect(mtmp, BOW));
  596. X          if (!propellor) propellor = (oselect(mtmp, ORCISH_BOW));
  597. X          break;
  598. X        case WP_SLING:
  599. X          propellor = (oselect(mtmp, SLING));
  600. X          break;
  601. X        case WP_CROSSBOW:
  602. X          propellor = (oselect(mtmp, CROSSBOW));
  603. X        }
  604. X#ifdef MUSE
  605. X        if ((otmp = MON_WEP(mtmp)) && otmp->cursed && otmp != propellor
  606. X                && mtmp->weapon_check == NO_WEAPON_WANTED)
  607. X            propellor = 0;
  608. X#endif
  609. X        }
  610. X        /* propellor = obj, propellor to use
  611. X         * propellor = &zeroobj, doesn't need a propellor
  612. X         * propellor = 0, needed one and didn't have one
  613. X         */
  614. X        if (propellor != 0) {
  615. X        /* Note: cannot use m_carrying for loadstones, since it will
  616. X         * always select the first object of a type, and maybe the
  617. X         * monster is carrying two but only the first is unthrowable.
  618. X         */
  619. X        if (rwep[i] != LOADSTONE) {
  620. X#ifdef MUSE
  621. X            /* Don't throw a cursed weapon-in-hand */
  622. X            if ((otmp = oselect(mtmp, rwep[i]))
  623. X                && (!otmp->cursed || otmp != MON_WEP(mtmp)))
  624. X                return(otmp);
  625. X#else
  626. X            Oselect(rwep[i]);
  627. X#endif
  628. X        } else for(otmp=mtmp->minvent; otmp; otmp=otmp->nobj) {
  629. X            if (otmp->otyp == LOADSTONE && !otmp->cursed)
  630. X            return otmp;
  631. X        }
  632. X        }
  633. X      }
  634. X
  635. X    /* failure */
  636. X    return (struct obj *)0;
  637. X}
  638. X
  639. X/* 0 = used by any monster; 1 = only used by strong monsters */
  640. Xstatic const struct hwep { short otyp, big; } NEARDATA hwep[] = {
  641. X      {TSURUGI,1}, {RUNESWORD,0},
  642. X      {DWARVISH_MATTOCK,1}, {TWO_HANDED_SWORD,1}, {BATTLE_AXE,1},
  643. X      {KATANA,0}, {UNICORN_HORN,1}, {CRYSKNIFE,0},
  644. X      {TRIDENT,0}, {LONG_SWORD,0}, {ELVEN_BROADSWORD,0}, {BROADSWORD,0},
  645. X      {LUCERN_HAMMER,1}, {SCIMITAR,1}, {SILVER_SABER,0}, {HALBERD,1},
  646. X      {PARTISAN,1}, {LANCE,1}, {FAUCHARD,1}, {BILL_GUISARME,1},
  647. X      {BEC_DE_CORBIN,1}, {GUISARME,1}, {RANSEUR,1}, {SPETUM,1},
  648. X      {VOULGE,1}, {BARDICHE,0}, {MORNING_STAR,0}, {GLAIVE,0},
  649. X      {ELVEN_SHORT_SWORD,0}, {DWARVISH_SHORT_SWORD,0}, {SHORT_SWORD,0},
  650. X      {ORCISH_SHORT_SWORD,0}, {MACE,0}, {AXE,0}, {DWARVISH_SPEAR,0},
  651. X      {ELVEN_SPEAR,0}, {SPEAR,0}, {ORCISH_SPEAR,0}, {FLAIL,0},
  652. X      {QUARTERSTAFF,1}, {JAVELIN,0}, {AKLYS,0}, {CLUB,0}, {PICK_AXE,0},
  653. X#ifdef KOPS
  654. X      {RUBBER_HOSE,0},
  655. X#endif /* KOPS */
  656. X      {WAR_HAMMER,0}, {ELVEN_DAGGER,0}, {DAGGER,0}, {ORCISH_DAGGER,0},
  657. X      {ATHAME,0}, {SCALPEL,0}, {KNIFE,0}, {WORM_TOOTH,0}, {BULLWHIP,0}
  658. X    };
  659. X
  660. Xstruct obj *
  661. Xselect_hwep(mtmp)    /* select a hand to hand weapon for the monster */
  662. Xregister struct monst *mtmp;
  663. X{
  664. X    register struct obj *otmp;
  665. X    register int i;
  666. X    register const struct hwep *hw;
  667. X    boolean strong = strongmonst(mtmp->data);
  668. X
  669. X    if(is_giant(mtmp->data))    /* giants just love to use clubs */
  670. X        Oselect(CLUB);
  671. X
  672. X    /* only strong monsters can wield big (esp. long) weapons */
  673. X    /* all monsters can wield the remaining weapons */
  674. X    for (i = 0, hw = hwep; i < SIZE(hwep); i++, hw++)
  675. X        if ((strong || !hw->big) &&
  676. X#ifdef MUSE
  677. X          (!objects[hw->otyp].oc_bimanual || !which_armor(mtmp, W_ARMS)) &&
  678. X#endif
  679. X    (objects[hw->otyp].oc_material != SILVER || !hates_silver(mtmp->data)))
  680. X        Oselect(hw->otyp);
  681. X
  682. X    /* failure */
  683. X    return (struct obj *)0;
  684. X}
  685. X
  686. X#ifdef MUSE
  687. X/* Called after polymorphing a monster, robbing it, etc....  Monsters
  688. X * otherwise never unwield stuff on their own.  Shouldn't print messages.
  689. X */
  690. Xvoid
  691. Xpossibly_unwield(mon)
  692. Xregister struct monst *mon;
  693. X{
  694. X    register struct obj *obj;
  695. X    struct obj *otmp, *backobj, *mw_tmp;
  696. X
  697. X    if (!(mw_tmp = MON_WEP(mon)))
  698. X        return;
  699. X    for(obj=mon->minvent; obj; obj=obj->nobj)
  700. X        if (obj == mw_tmp) break;
  701. X    if (!obj) { /* The weapon was stolen or destroyed */
  702. X        MON_NOWEP(mon);
  703. X        mon->weapon_check = NEED_WEAPON;
  704. X        return;
  705. X    }
  706. X    if (!attacktype(mon->data, AT_WEAP)) {
  707. X        MON_NOWEP(mon);
  708. X        mon->weapon_check = NO_WEAPON_WANTED;
  709. X        if (cansee(mon->mx, mon->my)) {
  710. X            pline("%s drops %s.", Monnam(mon),
  711. X                distant_name(obj, doname));
  712. X        }
  713. X        backobj = 0;
  714. X        for(otmp = mon->minvent; otmp; otmp = otmp->nobj) {
  715. X            /* flooreffects unnecessary, can't wield boulders */
  716. X            if (otmp == obj) {
  717. X                if (!backobj) mon->minvent = otmp->nobj;
  718. X                else backobj->nobj = otmp->nobj;
  719. X                place_object(otmp, mon->mx, mon->my);
  720. X                otmp->nobj = fobj;
  721. X                fobj = otmp;
  722. X                stackobj(fobj);
  723. X                if(cansee(mon->mx,mon->my))
  724. X                    newsym(mon->mx, mon->my);
  725. X                break;
  726. X            }
  727. X            backobj = otmp;
  728. X        }
  729. X        return;
  730. X    }
  731. X    /* The remaining case where there is a change is where a monster
  732. X     * is polymorphed into a stronger/weaker monster with a different
  733. X     * choice of weapons.  This has no parallel for players.  It can
  734. X     * be handled by waiting until mon_wield_item is actually called.
  735. X     * Though the monster still wields the wrong weapon until then,
  736. X     * this is OK since the player can't see it.
  737. X     * Possible problem: big monster with big cursed weapon gets
  738. X     * polymorphed into little monster.  But it's not quite clear how to
  739. X     * handle this anyway....
  740. X     */
  741. X    mon->weapon_check = NEED_WEAPON;
  742. X    return;
  743. X}
  744. X
  745. X/* Let a monster try to wield a weapon, based on mon->weapon_check.
  746. X * Returns 1 if the monster took time to do it, 0 if it did not.
  747. X */
  748. Xint
  749. Xmon_wield_item(mon)
  750. Xregister struct monst *mon;
  751. X{
  752. X    struct obj *obj;
  753. X
  754. X    /* This case actually should never happen */
  755. X    if (mon->weapon_check == NO_WEAPON_WANTED) return 0;
  756. X
  757. X    switch(mon->weapon_check) {
  758. X        case NEED_HTH_WEAPON:
  759. X            obj = select_hwep(mon);
  760. X            break;
  761. X        case NEED_RANGED_WEAPON:
  762. X            (void)select_rwep(mon);
  763. X            obj = propellor;
  764. X            break;
  765. X        case NEED_PICK_AXE:
  766. X            obj = m_carrying(mon, PICK_AXE);
  767. X            break;
  768. X        default: impossible("weapon_check %d for %s?",
  769. X                mon->weapon_check, mon_nam(mon));
  770. X            return 0;
  771. X    }
  772. X    if (obj && obj != &zeroobj) {
  773. X        struct obj *mw_tmp = MON_WEP(mon);
  774. X        if (mw_tmp == obj) { /* already wielding it */
  775. X            mon->weapon_check = NEED_WEAPON;
  776. X            return 0;
  777. X        }
  778. X        /* Actually, this isn't necessary--as soon as the monster
  779. X         * wields the weapon, the weapon welds itself, so the monster
  780. X         * can know it's cursed and needn't even bother trying.
  781. X         * Still....
  782. X         */
  783. X        if (mw_tmp && mw_tmp->cursed) {
  784. X            if (obj->otyp == PICK_AXE) {
  785. X            if (canseemon(mon)) {
  786. X                pline("Since %s weapon %s welded to %s hand,",
  787. X                s_suffix(mon_nam(mon)),
  788. X                (mw_tmp->quan == 1L) ? "is" : "are",
  789. X                humanoid(mon->data)
  790. X                    ? (mon->female ? "her" : "his")
  791. X                    : "its");
  792. X                pline("%s cannot wield that %s.",
  793. X                mon_nam(mon), xname(obj));
  794. X                mw_tmp->bknown = 1;
  795. X            }
  796. X            } else {
  797. X            if (canseemon(mon)) {
  798. X                pline("%s tries to wield %s.", Monnam(mon),
  799. X                doname(obj));
  800. X                pline("%s %s %s welded to %s hand!",
  801. X                s_suffix(Monnam(mon)), xname(mw_tmp),
  802. X                (mw_tmp->quan == 1L) ? "is" : "are",
  803. X                humanoid(mon->data)
  804. X                    ? (mon->female ? "her" : "his")
  805. X                    : "its");
  806. X                mw_tmp->bknown = 1;
  807. X            }
  808. X            }
  809. X            mon->weapon_check = NO_WEAPON_WANTED;
  810. X            return 1;
  811. X        }
  812. X        mon->mw = obj;        /* wield obj */
  813. X        mon->weapon_check = NEED_WEAPON;
  814. X        if (canseemon(mon)) {
  815. X            pline("%s wields %s!", Monnam(mon), doname(obj));
  816. X            if (obj->cursed) {
  817. X                pline("%s %s to %s hand!",
  818. X                    The(xname(obj)),
  819. X                    (obj->quan == 1L) ? "welds itself"
  820. X                        : "weld themselves",
  821. X                    s_suffix(mon_nam(mon)));
  822. X                obj->bknown = 1;
  823. X            }
  824. X        }
  825. X        return 1;
  826. X    }
  827. X    mon->weapon_check = NEED_WEAPON;
  828. X    return 0;
  829. X}
  830. X
  831. X/* rearrange a monster's inventory so that wielded weapon is first */
  832. Xvoid
  833. Xsort_mwep(mon)
  834. Xstruct monst *mon;
  835. X{
  836. X    struct obj *otmp, *prev, *mw_tmp = MON_WEP(mon);
  837. X
  838. X    if (!mw_tmp) return;
  839. X    for (otmp = mon->minvent, prev = 0; otmp; otmp = otmp->nobj) {
  840. X        if (otmp == mw_tmp)  break;
  841. X        prev = otmp;
  842. X    }
  843. X    if (!otmp) {
  844. X        MON_NOWEP(mon);
  845. X    } else if (prev) {
  846. X        prev->nobj = otmp->nobj;
  847. X        otmp->nobj = mon->minvent;
  848. X        mon->minvent = otmp;
  849. X    }
  850. X}
  851. X#endif
  852. X
  853. Xint
  854. Xabon() {    /* attack bonus for strength & dexterity */
  855. X    int    sbon;
  856. X    register int    str = ACURR(A_STR), dex = ACURR(A_DEX);
  857. X
  858. X#ifdef POLYSELF
  859. X    if (u.umonnum >= 0) return(adj_lev(&mons[u.umonnum])-3);
  860. X#endif
  861. X    if (str < 6) sbon = -2;
  862. X    else if (str < 8) sbon = -1;
  863. X    else if (str < 17) sbon = 0;
  864. X    else if (str < 69) sbon = 1;    /* up to 18/50 */
  865. X    else if (str < 118) sbon = 2;
  866. X    else sbon = 3;
  867. X/*
  868. X *    Temporary kludge - make it a bit easier for a low level character
  869. X *               to hit until we tune the game a little better.
  870. X */
  871. X    sbon += (u.ulevel < 3) ? 1 : 0;
  872. X
  873. X    if (dex < 4) return(sbon-3);
  874. X    else if (dex < 6) return(sbon-2);
  875. X    else if (dex < 8) return(sbon-1);
  876. X    else if (dex < 14) return(sbon);
  877. X    else return(sbon + dex-14);
  878. X}
  879. X
  880. X#endif /* OVL0 */
  881. X#ifdef OVL1
  882. X
  883. Xint
  884. Xdbon() {    /* damage bonus for strength */
  885. X    register int    str = ACURR(A_STR);
  886. X
  887. X#ifdef POLYSELF
  888. X    if (u.umonnum >= 0) return(0);
  889. X#endif
  890. X
  891. X    if (str < 6) return(-1);
  892. X    else if (str < 16) return(0);
  893. X    else if (str < 18) return(1);
  894. X    else if (str == 18) return(2);        /* up to 18 */
  895. X    else if (str < 94) return(3);        /* up to 18/75 */
  896. X    else if (str < 109) return(4);        /* up to 18/90 */
  897. X    else if (str < 118) return(5);        /* up to 18/99 */
  898. X    else return(6);
  899. X}
  900. X
  901. X#endif /* OVL1 */
  902. X
  903. X/*weapon.c*/
  904. END_OF_FILE
  905. if test 14162 -ne `wc -c <'src/weapon.c'`; then
  906.     echo shar: \"'src/weapon.c'\" unpacked with wrong size!
  907. fi
  908. # end of 'src/weapon.c'
  909. fi
  910. if test -f 'sys/amiga/splitter/splitter.c' -a "${1}" != "-c" ; then 
  911.   echo shar: Will not clobber existing file \"'sys/amiga/splitter/splitter.c'\"
  912. else
  913. echo shar: Extracting \"'sys/amiga/splitter/splitter.c'\" \(13609 characters\)
  914. sed "s/^X//" >'sys/amiga/splitter/splitter.c' <<'END_OF_FILE'
  915. X/*    SCCS Id: @(#)splitter.c        3.1   93/01/08
  916. X/*    Copyright (c) Kenneth Lorber, Bethesda, Maryland, 1992, 1993 */
  917. X/* NetHack may be freely redistributed.  See license for details. */
  918. X
  919. X#define SOUT    /* split output files */
  920. X#define SPLITSIZE (800 * 1024)        /* somewhat < one floppy */
  921. X
  922. X#include <stdio.h>
  923. X#include <stdlib.h>
  924. X#include <string.h>
  925. X#include <fcntl.h>
  926. X
  927. X#include <proto/exec.h>
  928. X#include <exec/types.h>
  929. X#include <exec/nodes.h>
  930. X#include <exec/lists.h>
  931. X
  932. X#include "split.h"
  933. X#include "amiout.h"
  934. X#include "arg.h"
  935. X
  936. Xint main(int,char **);
  937. X
  938. Xchar *code_proto="%n.c%C";
  939. Xchar *data_proto="%n.d%D";
  940. Xchar *dir_proto="%n.dir";
  941. X
  942. Xchar trace[MAXTRACEVAR];        /* debugging info */
  943. Xchar *basename;
  944. Xint datacount;    /* for ssubst - should be replaced */
  945. Xint codecount;
  946. Xint data_file_count=0;    /* actual maxima */
  947. Xint code_file_count=0;
  948. Xstruct hheader hheader;
  949. Xstruct shunk (*hlist)[];
  950. Xchar buf[80];
  951. Xint wsf_count;
  952. X
  953. Xmain(argc,argv)
  954. X    int argc;
  955. X    char **argv;
  956. X{
  957. X    int cur_arg;
  958. X
  959. X    arg_init("C:D:d:t:T",argc,argv);
  960. X    while((cur_arg=arg_next())!=ARG_DONE){
  961. X        switch(cur_arg){
  962. X        case 'C':    /* code prototype */
  963. X            code_proto=strdup(argarg);break;
  964. X        case 'D':    /* data prototype */
  965. X            data_proto=strdup(argarg);break;
  966. X        case 'd':    /* directions prototype */
  967. X            dir_proto=strdup(argarg);break;
  968. X        case 't':    /* trace (debug) */
  969. X            {
  970. X            int dtype=0,dlevel=0;    /* rude defaults */
  971. X            sscanf(argarg,"%d=%d",&dtype,&dlevel);
  972. X            if(dtype<0 || dtype>=MAXTRACEVAR){
  973. X                fprintf(stderr,"-t: bad trace num ignored\n");
  974. X            }else{
  975. X                trace[dtype]=dlevel?dlevel:1;
  976. X            }
  977. X            break;
  978. X            }
  979. X        case 'T':    /* trace everything */
  980. X            {
  981. X            int dtype;
  982. X            for(dtype=0;dtype<MAXTRACEVAR;dtype++)trace[dtype]=255;
  983. X            }
  984. X            break;
  985. X        default:
  986. X            fprintf(stderr,"Unrecognized option.\n");
  987. X            /* FALLTHROUGH */
  988. X        case ARG_ERROR:
  989. X            panic("Error processing arguments.");
  990. X        case ARG_FREE:
  991. X            basename=strdup(argarg);
  992. X            read_load_file(basename);break;
  993. X        }
  994. X    }
  995. X    renumber();
  996. X    out_start(code_proto);
  997. X    write_header();
  998. X    write_code_file();
  999. X    out_stop();
  1000. X    out_start(data_proto);
  1001. X    write_data_file();
  1002. X    out_stop();
  1003. X    write_dir_file();
  1004. X    exit(0);
  1005. X}
  1006. X
  1007. Xchar *
  1008. Xssubst(buf,pat)
  1009. X    char *buf;
  1010. X    const char *pat;
  1011. X{
  1012. X    char *buf1=buf;
  1013. X
  1014. X    while(*pat){
  1015. X        if(*pat!='%'){
  1016. X            *buf++=*pat++;
  1017. X        } else {
  1018. X            pat++;
  1019. X            switch(*pat++){
  1020. X            case '%': *buf++='%';break;
  1021. X            case 'n': strcpy(buf,basename);buf=eos(buf);break;
  1022. X            case 'D': sprintf(buf,"%02d",datacount);buf=eos(buf);
  1023. X                  break;
  1024. X            case 'C': sprintf(buf,"%02d",codecount);buf=eos(buf);
  1025. X                  break;
  1026. X            default:  panic("pattern substitution error");
  1027. X            }
  1028. X        }
  1029. X    }
  1030. X    *buf='\0';
  1031. X    return buf1;
  1032. X}
  1033. X
  1034. Xvoid
  1035. Xpanic(s)
  1036. X    char *s;
  1037. X{
  1038. X    fprintf(stderr,"\npanic: %s\n",s);
  1039. X    exit(1);
  1040. X}
  1041. X
  1042. Xchar *
  1043. Xeos(s)
  1044. X    char *s;
  1045. X{
  1046. X    while(*s)s++;
  1047. X    return s;
  1048. X}
  1049. X
  1050. X/* input routines */
  1051. X
  1052. X    /* macro for reading the next long.  If e==EOF_OK, caller MUST check
  1053. X     * for EOF condition via hreadval or assure it can't occur */
  1054. Xstatic int hreadval=0;        /* macro internal temporary */
  1055. X#define EOF_OK    1
  1056. X#define EOF_BAD    0
  1057. X#define READLONG(e)    \
  1058. X    ((4!=(hreadval=read(f->fd,&(READLONGx),4)))        \
  1059. X    ?((0==hreadval && (e)                    \
  1060. X        ?0                        \
  1061. X        :rderror()))                    \
  1062. X    :READLONGx)
  1063. Xstatic long READLONGx;
  1064. X#define READSHORT(e)    \
  1065. X    ((2!=(hreadval=read(f->fd,&(READSHORTx),2)))        \
  1066. X    ?((0==hreadval && (e)                    \
  1067. X        ?0                        \
  1068. X        :rderror()))                    \
  1069. X    :READSHORTx)
  1070. Xstatic short READSHORTx;
  1071. X
  1072. X#define LONGLEN(x)    (strlen(x)+3 >>2)    /* # longs for a string */
  1073. X
  1074. Xvoid
  1075. Xread_load_file(name)
  1076. X    char *name;
  1077. X{
  1078. X    int t;
  1079. X    int hc;
  1080. X    file *f=NewFile(name);
  1081. X
  1082. X        /* read HUNK_HEADER */
  1083. X    t=READLONG(EOF_BAD);if(t!=HUNK_HEADER)panic("no HUNK_HEADER");
  1084. X    t=READLONG(EOF_BAD);if(t)while(t--)READLONG(EOF_BAD); /* eat any name */
  1085. X    hheader.hcount=READLONG(EOF_BAD);
  1086. X    hheader.first=READLONG(EOF_BAD);
  1087. X    hheader.last=READLONG(EOF_BAD);
  1088. X    if(hheader.hcount !=(hheader.last-hheader.first+1))panic("can't count");
  1089. X    hheader.sizes=calloc(hheader.hcount,sizeof(int*));
  1090. X    for(t=0;t<hheader.hcount;t++)
  1091. X        (*hheader.sizes)[t]=READLONG(EOF_BAD);
  1092. X
  1093. X    hlist=calloc(hheader.hcount,sizeof(struct shunk));
  1094. X    for(hc=0;hc<hheader.hcount;hc++){
  1095. X        struct shunk *th = &(*hlist)[hc];
  1096. X                /* read each hunk */
  1097. X        th->h=ReadHunk(f);
  1098. X    }
  1099. X    close(f->fd);
  1100. X}
  1101. X
  1102. X/* write routines */
  1103. X#define S_CODE    0
  1104. X#define S_DATA    1
  1105. X
  1106. Xvoid
  1107. Xwrite_header(){
  1108. X    int x;
  1109. X    int target=0;
  1110. X
  1111. X    owrite_long(HUNK_HEADER);
  1112. X    owrite_long(0);
  1113. X    owrite_long(hheader.hcount);
  1114. X    owrite_long(hheader.first);
  1115. X    owrite_long(hheader.last);
  1116. X
  1117. X    for(x=0;x<hheader.hcount;x++){
  1118. X        hunk *hp = (*hlist)[x].h;
  1119. X        if(hp->hunknum==target){
  1120. X            owrite_long((*hheader.sizes)[x]);
  1121. X            target++;
  1122. X        }
  1123. X    }
  1124. X    for(x=0;x<hheader.hcount;x++){
  1125. X        hunk *hp = (*hlist)[x].h;
  1126. X        if(hp->hunknum==target){
  1127. X            owrite_long((*hheader.sizes)[x]);
  1128. X            target++;
  1129. X        }
  1130. X    }
  1131. X    if(target!=hheader.hcount)panic("lost hunks?");
  1132. X}
  1133. X
  1134. Xvoid
  1135. Xwrite_code_file(){
  1136. X    code_file_count=write_split_file(S_CODE)-1;
  1137. X}
  1138. X
  1139. Xvoid
  1140. Xwrite_data_file(){
  1141. X    data_file_count=write_split_file(S_DATA)-1;
  1142. X}
  1143. X
  1144. Xvoid
  1145. Xwrite_dir_file(){
  1146. X    int x;
  1147. X    FILE *fp=fopen(ssubst(buf,dir_proto),"w");
  1148. X
  1149. X    /*fprintf(fp,"%d %d\n",code_file_count,data_file_count);*/
  1150. X    for(x=0;x<=code_file_count;x++){
  1151. X        codecount=x;
  1152. X        fprintf(fp,"C%s\n",ssubst(buf,code_proto));
  1153. X    }
  1154. X    for(x=0;x<=data_file_count;x++){
  1155. X        datacount=x;
  1156. X        fprintf(fp,"D%s\n",ssubst(buf,data_proto));
  1157. X    }
  1158. X    fclose(fp);
  1159. X}
  1160. X
  1161. X/* BUGFIX: 9/23/92: see HT() above */
  1162. X#define HT(x)    ((x) & ~MEM_OBJ_EXTEND)
  1163. X
  1164. Xint
  1165. Xwrite_split_file(fl)
  1166. X    int fl;
  1167. X{
  1168. X    int hc;
  1169. X    for(hc=0;hc<hheader.hcount;hc++){
  1170. X        hunk *hp = (*hlist)[hc].h;
  1171. X        if(fl==S_CODE && HT(hp->rb->id)==HUNK_CODE){
  1172. X            wsf_hunk(hp);
  1173. X        } else if(fl==S_DATA && HT(hp->rb->id)==HUNK_DATA){
  1174. X            wsf_hunk(hp);
  1175. X        } else if(fl==S_DATA && HT(hp->rb->id)==HUNK_BSS){
  1176. X            wsf_hunk(hp);
  1177. X        }
  1178. X    }
  1179. X    return wsf_count;
  1180. X}
  1181. X
  1182. X/* BUGFIX: 9/23/92: see HT() below */
  1183. Xvoid
  1184. Xwsf_hunk(hp)
  1185. X    hunk *hp;
  1186. X{
  1187. X    listlist *el;
  1188. X
  1189. X    switch(HT(hp->rb->id)){
  1190. X    case HUNK_CODE:
  1191. X    case HUNK_DATA:
  1192. X        owrite(hp->rb->b,(2+hp->rb->b[1])*sizeof(long));
  1193. X        break;
  1194. X    case HUNK_BSS:
  1195. X        owrite(hp->rb->b,2*sizeof(long));
  1196. X        break;
  1197. X    default:panic("wsf_hunk: bad type");
  1198. X    }
  1199. X    foreach(el,&(hp->reloc),(listlist*)){
  1200. X        write_lreloc(hp,el);
  1201. X    }
  1202. X    owrite_long(HUNK_END);
  1203. X}
  1204. X
  1205. Xvoid
  1206. Xwrite_lreloc(hp,ll)
  1207. X    hunk *hp;listlist *ll;
  1208. X    {
  1209. X    block *bp;
  1210. X    owrite_long(HUNK_RELOC32);
  1211. X    foreach(bp,&(ll->list),(block*)){
  1212. X        owrite_long(bp->b[0]);
  1213. X        owrite_long(((*hlist)[bp->b[1]]).h->hunknum);
  1214. X        owrite(&(bp->b[2]),bp->b[0]*sizeof(long));
  1215. X    }
  1216. X    owrite_long(0);
  1217. X}
  1218. X
  1219. Xvoid
  1220. Xrenumber()
  1221. X{
  1222. X    int n;
  1223. X    n=renumber2(S_CODE,0);
  1224. X    renumber2(S_DATA,n);
  1225. X}
  1226. X
  1227. X/* BUGFIX 9/23/92: hp->rb->id must be wrapped with a bit stripper to ignore
  1228. X * memory type bits still in that longword.
  1229. X */
  1230. X
  1231. Xrenumber2(fl,n)
  1232. X    int fl;
  1233. X    int n;
  1234. X{
  1235. X    int hc;
  1236. X    for(hc=0;hc<hheader.hcount;hc++){
  1237. X        hunk *hp = (*hlist)[hc].h;
  1238. X        if(fl==S_CODE && HT(hp->rb->id)==HUNK_CODE){
  1239. X            hp->hunknum=n++;
  1240. X        } else if(fl==S_DATA && HT(hp->rb->id)==HUNK_DATA){
  1241. X            hp->hunknum=n++;
  1242. X        } else if(fl==S_DATA && HT(hp->rb->id)==HUNK_BSS){
  1243. X            hp->hunknum=n++;
  1244. X        }
  1245. X    }
  1246. X    return n;
  1247. X}
  1248. X
  1249. X/* output package */
  1250. X#ifndef SOUT
  1251. X/* NB - this version does NOT cope with multiple output files per type */
  1252. Xint ofile;
  1253. X
  1254. Xvoid
  1255. Xout_start(prot)
  1256. X    char *prot;
  1257. X{
  1258. X    datacount=codecount=0;
  1259. X    ofile=open(ssubst(buf,prot),O_WRONLY|O_CREAT|O_TRUNC);
  1260. X    if(ofile<0)panic("can't open output file");
  1261. X}
  1262. X
  1263. Xvoid
  1264. Xout_stop(){
  1265. X    close(ofile);
  1266. X}
  1267. X
  1268. Xvoid
  1269. Xowrite_long(literal)
  1270. X    long literal;
  1271. X{
  1272. X    long x=literal;
  1273. X    owrite(&x,sizeof(x));
  1274. X}
  1275. X
  1276. Xvoid
  1277. Xowrite(where,len)
  1278. X    void *where;
  1279. X    long len;
  1280. X{
  1281. X    write(ofile,where,len);
  1282. X}
  1283. X#else /* SOUT */
  1284. Xint ofile=0;
  1285. Xint osize;
  1286. Xchar *oprot;
  1287. Xvoid
  1288. Xout_start(prot)
  1289. X    char *prot;
  1290. X{
  1291. X    datacount=codecount=wsf_count=0;
  1292. X    oprot=prot;
  1293. X    new_file();
  1294. X}
  1295. X
  1296. Xvoid
  1297. Xout_stop(){
  1298. X    close(ofile);
  1299. X    ofile=0;
  1300. X}
  1301. X
  1302. Xvoid
  1303. Xowrite_long(literal)
  1304. X    long literal;
  1305. X{
  1306. X    long x=literal;
  1307. X    if((osize+sizeof(x))>SPLITSIZE)new_file();
  1308. X    owrite(&x,sizeof(x));
  1309. X    osize += sizeof(x);
  1310. X}
  1311. X
  1312. Xvoid
  1313. Xowrite(where,len)
  1314. X    void *where;
  1315. X    long len;
  1316. X{
  1317. X    if((osize+len)>SPLITSIZE)new_file();
  1318. X    write(ofile,where,len);
  1319. X    osize += len;
  1320. X}
  1321. X
  1322. Xvoid
  1323. Xnew_file(){
  1324. X    if(ofile)close(ofile);
  1325. X    ofile=open(ssubst(buf,oprot),O_WRONLY|O_CREAT|O_TRUNC);
  1326. X    if(ofile<0)panic("can't open output file");
  1327. X    wsf_count++,datacount++,codecount++;
  1328. X    osize=0;
  1329. X}
  1330. X#endif /* SOUT */
  1331. X
  1332. Xstruct Node *Head(l)
  1333. X    struct List *l;
  1334. X    {
  1335. X    if(!l)panic("Head(NULL)\n");
  1336. X    return l->lh_Head->ln_Succ?l->lh_Head:0;
  1337. X}
  1338. Xstruct Node *Tail(l)
  1339. X    struct List *l;
  1340. X{
  1341. X    if(!l)panic("Tail(NULL)\n");
  1342. X    return (l->lh_TailPred==(NODE_P)l)?0:l->lh_TailPred;
  1343. X}
  1344. Xstruct Node *Next(n)
  1345. X    struct Node *n;
  1346. X{
  1347. X    if(!n)printf("Warning: Next(NULL)\n");
  1348. X    return n?(n->ln_Succ->ln_Succ?n->ln_Succ:0):0;
  1349. X}
  1350. Xstruct Node *Prev(n)
  1351. X    struct Node *n;
  1352. X{
  1353. X    if(!n)printf("Warning: Prev(NULL)\n");
  1354. X    return n?(n->ln_Pred->ln_Pred?n->ln_Pred:0):0;
  1355. X}
  1356. Xstruct List *_fortemp;    /* scratch for foreach macro */
  1357. X
  1358. Xvoid
  1359. Xdump_after_read(struct List *root){
  1360. X    file *f;
  1361. X    foreach(f,root,(file *)){
  1362. X        punit *p;
  1363. X        printf("FILE '%s'\n",f->name);
  1364. X        foreach(p,&(f->punits),(punit *)){
  1365. X        hunk *h;
  1366. X        print_text_block("\tPUNIT %.*s\n",p->unit_header);
  1367. X        if(p->libsize){
  1368. X        printf("\tlibsize=%08x\n",p->libsize);
  1369. X        } else {
  1370. X        /* */
  1371. X        }
  1372. X        foreach(h,&(p->hunks),(hunk *)){
  1373. X        print_text_block("\t\tHUNK %.*s",h->name);
  1374. X        printf(" @%08x\n",h);
  1375. X        print_bin_block(h->rb);
  1376. X        printf("\t\t\tCode Reloc\n");
  1377. X        printf("\t\t\tData Reloc\n");
  1378. X        if(h->merge)printf("\t\t\tmerge(%08x)\n",h->merge);
  1379. X        if(h->hunkstart)printf("\t\t\thunkstart\n");
  1380. X        if(h->hunkchain)printf("\t\t\thunkchain\n");
  1381. X        if(h->hunkgone)printf("\t\t\thunkgone\n");
  1382. X        printf("\t\t\toverlay(%08x) hunknum(%08x) offset(%08x)\n",
  1383. X          h->overlay,h->hunknum,h->hunkoffset);
  1384. X        }
  1385. X        }
  1386. X    }
  1387. X}
  1388. X
  1389. Xvoid
  1390. Xprint_text_block(char *fmt,block *b){
  1391. X    if(!b){
  1392. X        printf(fmt,10,"(no block)");
  1393. X    } else {
  1394. X        if(b->sw){
  1395. X            printf(fmt,13,"(swapped out)");
  1396. X        } else {
  1397. X            if(!(b->b[1]) || !*(char*)&(b->b[2])){
  1398. X                printf(fmt,6,"(null)");
  1399. X            } else {
  1400. X                printf(fmt,b->b[1]*4,&(b->b[2]));
  1401. X            }
  1402. X        }
  1403. X    }
  1404. X}
  1405. X
  1406. Xvoid
  1407. Xprint_bin_block(block *b){
  1408. X    if(b->sw){
  1409. X        printf("\t\t\t(swapped out)\n");
  1410. X    } else {
  1411. X        printf("\t\t\tid1=%08x id2=%08x len=%08x\n",
  1412. X          b->id,b->b[0],b->b[1]);
  1413. X    }
  1414. X}
  1415. X
  1416. X/*
  1417. X * read routines
  1418. X */
  1419. X
  1420. X/*
  1421. X * ReadSimpleBlock
  1422. X * If the given id is recognized as a simple block (id, length, data),
  1423. X * allocate and fill in a block structure.  Include the id in the block.
  1424. X */
  1425. Xblock *ReadSimpleBlock(f,id)
  1426. X    file *f;
  1427. X    long id;
  1428. X    {
  1429. X    long len;
  1430. X    long hid;
  1431. X    block *b;
  1432. X
  1433. X    hid=id & 0x0fffffff;
  1434. X    if(    hid !=HUNK_UNIT && hid != HUNK_NAME && hid != HUNK_CODE &&
  1435. X        hid != HUNK_DATA && hid != HUNK_BSS && hid != HUNK_DEBUG
  1436. X      ){
  1437. X        printf("%08x\n",id);
  1438. X        panic("ReadSImpleBlock");
  1439. X    }
  1440. X
  1441. X    len=READLONG(EOF_BAD);
  1442. X    b=NewBlock();
  1443. X    b->id=id;
  1444. X    b->sw=0;
  1445. X    b->b=NewData((hid==HUNK_BSS)?2:len+2);
  1446. X    b->b[0]=id;
  1447. X    b->b[1]=len;
  1448. X    if(hid != HUNK_BSS)read(f->fd,&(b->b[2]),len*4);
  1449. X    return(b);
  1450. X}
  1451. X
  1452. X/*
  1453. X * TossSimpleBlock
  1454. X * Skip past something we don't need.
  1455. X */
  1456. Xint TossSimpleBlock(f)
  1457. X    file *f;
  1458. X    {
  1459. X    long len=READLONG(EOF_BAD);
  1460. X    if(len)if( lseek(f->fd,len*4,1) == -1)panic("Toss failed\n");
  1461. X    return(len);
  1462. X}
  1463. X
  1464. X/*
  1465. X * ReadHunk
  1466. X * Read an entire hunk, building lists of each block type in the given hunk
  1467. X * structure.  If we are listing, do the listing as we read so we can see
  1468. X * where things die if we hit a type code we don't recognize.
  1469. X */
  1470. Xhunk *ReadHunk(f)
  1471. X    file *f;
  1472. X    {
  1473. X    long id;
  1474. X    hunk *h=NewHunk();
  1475. X    while(1){
  1476. X        id=READLONG(EOF_OK);
  1477. X        switch(id & 0x0fffffff){    /* ignore memory type bits */
  1478. X        case 0: return 0;        /* EOF - not good test */
  1479. X        case HUNK_RELOC32:
  1480. X            LIST{printf("Reloc32:\n");}
  1481. X            ReadReloc(f,id,&h->reloc);break;
  1482. X        case HUNK_CODE:
  1483. X            h->rb=ReadSimpleBlock(f,id);
  1484. X            LIST{printf("Code size %d\n",block_size(h->rb)*4);};
  1485. X            break;
  1486. X        case HUNK_DATA:
  1487. X            h->rb=ReadSimpleBlock(f,id);
  1488. X            LIST{printf("Data size %d\n",block_size(h->rb)*4);};
  1489. X            break;
  1490. X        case HUNK_BSS:
  1491. X            h->rb=ReadSimpleBlock(f,id);
  1492. X            LIST{printf("Bss size %d\n",block_size(h->rb)*4);};
  1493. X            break;
  1494. X        case HUNK_SYMBOL:
  1495. X            while(TossSimpleBlock(f))READLONG(EOF_BAD);
  1496. X            LIST{printf("Symbols skipped\n");};
  1497. X            break;
  1498. X        case HUNK_DEBUG:
  1499. X            (void)TossSimpleBlock(f);
  1500. X            LIST{printf("Debug hunk skipped\n");};
  1501. X            break;
  1502. X        case HUNK_END:    LIST{printf("End of hunk\n");};return h;
  1503. X        case HUNK_BREAK:LIST{printf("End of overlay\n");};break;
  1504. X        default:
  1505. X            printf("Lost id=0x%x\n",id);exit(2);
  1506. X        }
  1507. X    }
  1508. X    return 0;
  1509. X}
  1510. X
  1511. X/*
  1512. X * ReadReloc
  1513. X * Read a relocation block and build a linked list of the sections.
  1514. X * If we are listing, do that now.
  1515. X */
  1516. Xvoid ReadReloc(f,id,ls)
  1517. X    file *f;
  1518. X    long id;
  1519. X    struct List *ls;
  1520. X    {
  1521. X    long len;
  1522. X    block *cur;
  1523. X    listlist *blist=NewListList();
  1524. X    AddTail(ls,blist);
  1525. X    blist->id=id;
  1526. X    len=READLONG(EOF_BAD);
  1527. X    while(len){
  1528. X        cur=NewBlock();
  1529. X        cur->b=NewData(len+2);
  1530. X        read(f->fd,&(cur->b[1]),len*4+4);
  1531. X        cur->b[0]=len;
  1532. X        LIST{printf("\thunk #%d - %d items\n",cur->b[1],len);}
  1533. X        AddTail(&blist->list,cur);
  1534. X        /*ReadLong(len);*/
  1535. X        len=READLONG(EOF_BAD);
  1536. X    }
  1537. X}
  1538. X
  1539. Xint rderror(){
  1540. X    panic("read error\n");
  1541. X    return 0;    /* just to make it quiet - NOTREACHED */
  1542. X}
  1543. X
  1544. Xlong block_size(blk)
  1545. X    block *blk;
  1546. X{
  1547. X    return(blk->b[1]);
  1548. X}
  1549. X
  1550. X/* Allocation routines - if this was C++ then this code would be buried in the
  1551. X * constructors.  Doing it this way means we can re-write the allocation later
  1552. X * to allocate things we'll need lots of in larger blocks to avoid the time and
  1553. X * space penalties of malloc. */
  1554. Xfile *NewFile(fname)
  1555. X    char *fname;
  1556. X    {
  1557. X    file *ret=calloc(sizeof(file),1);
  1558. X    NewList(&ret->punits);
  1559. X    ret->name=strdup(fname);
  1560. X    ret->fd= open(fname,O_RDONLY);
  1561. X    return(ret);
  1562. X}
  1563. X
  1564. Xpunit *NewPunit(){
  1565. X    punit *ret=calloc(sizeof(punit),1);
  1566. X    NewList(&ret->hunks);
  1567. X    return(ret);
  1568. X}
  1569. X
  1570. Xhunk *NewHunk(){
  1571. X    hunk *ret=calloc(sizeof(hunk),1);
  1572. X    NewList(&ret->reloc);
  1573. X    NewList(&ret->dreloc);
  1574. X    NewList(&ret->extsym);
  1575. X    ret->overlay=UNASSIGNED_HUNK;
  1576. X    return(ret);
  1577. X}
  1578. X
  1579. Xblock *NewBlock()
  1580. X    {
  1581. X    return calloc(sizeof(block),1);
  1582. X}
  1583. X
  1584. Xlistlist *NewListList(){
  1585. X    listlist *ret=calloc(sizeof(listlist),1);
  1586. X    NewList(&ret->list);
  1587. X    return(ret);
  1588. X}
  1589. X
  1590. Xlong *NewData(longs)
  1591. X    long longs;    
  1592. X    {
  1593. X    return(malloc(longs*4));
  1594. X}
  1595. END_OF_FILE
  1596. if test 13609 -ne `wc -c <'sys/amiga/splitter/splitter.c'`; then
  1597.     echo shar: \"'sys/amiga/splitter/splitter.c'\" unpacked with wrong size!
  1598. fi
  1599. # end of 'sys/amiga/splitter/splitter.c'
  1600. fi
  1601. if test -f 'sys/share/random.c' -a "${1}" != "-c" ; then 
  1602.   echo shar: Will not clobber existing file \"'sys/share/random.c'\"
  1603. else
  1604. echo shar: Extracting \"'sys/share/random.c'\" \(13686 characters\)
  1605. sed "s/^X//" >'sys/share/random.c' <<'END_OF_FILE'
  1606. X/*
  1607. X * Copyright (c) 1983 Regents of the University of California.
  1608. X * All rights reserved.
  1609. X *
  1610. X * Redistribution and use in source and binary forms are permitted
  1611. X * provided that the above copyright notice and this paragraph are
  1612. X * duplicated in all such forms and that any documentation,
  1613. X * advertising materials, and other materials related to such
  1614. X * distribution and use acknowledge that the software was developed
  1615. X * by the University of California, Berkeley.  The name of the
  1616. X * University may not be used to endorse or promote products derived
  1617. X * from this software without specific prior written permission.
  1618. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  1619. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  1620. X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  1621. X */
  1622. X
  1623. X/* Several minor changes were made for the NetHack distribution to satisfy
  1624. X * non-BSD compilers (by definition BSD compilers do not need to compile
  1625. X * this file for NetHack).  These changes consisted of:
  1626. X *    - changing the sccsid conditions to nested ifdefs from defined()s
  1627. X *    to accommodate stupid preprocessors
  1628. X *    - giving srandom() type void instead of allowing it to default to int
  1629. X *    - making the first return in initstate() return a value consistent
  1630. X *    with its type (instead of no value)
  1631. X *    - ANSI function prototyping in extern.h - therefore include hack.h
  1632. X *    instead of stdio.h and remove separate declaration of random() from
  1633. X *    the beginning of function srandom
  1634. X *    - moving sccsid after hack.h to allow precompiled headers, which
  1635. X *    means the defined()s would be ok again...
  1636. X *    - change fprintf(stderr, "x(%d)y\n", z) to impossible("x(%d)y", z)
  1637. X *    - remove useless variable `j' from srandom()
  1638. X */
  1639. X
  1640. X#include "hack.h"
  1641. X
  1642. X#ifdef LIBC_SCCS
  1643. X# ifndef lint
  1644. Xstatic char sccsid[] = "@(#)random.c    5.5 (Berkeley) 7/6/88";
  1645. X# endif
  1646. X#endif /* LIBC_SCCS and not lint */
  1647. X
  1648. X/*
  1649. X * random.c:
  1650. X * An improved random number generation package.  In addition to the standard
  1651. X * rand()/srand() like interface, this package also has a special state info
  1652. X * interface.  The initstate() routine is called with a seed, an array of
  1653. X * bytes, and a count of how many bytes are being passed in; this array is then
  1654. X * initialized to contain information for random number generation with that
  1655. X * much state information.  Good sizes for the amount of state information are
  1656. X * 32, 64, 128, and 256 bytes.  The state can be switched by calling the
  1657. X * setstate() routine with the same array as was initiallized with initstate().
  1658. X * By default, the package runs with 128 bytes of state information and
  1659. X * generates far better random numbers than a linear congruential generator.
  1660. X * If the amount of state information is less than 32 bytes, a simple linear
  1661. X * congruential R.N.G. is used.
  1662. X * Internally, the state information is treated as an array of longs; the
  1663. X * zeroeth element of the array is the type of R.N.G. being used (small
  1664. X * integer); the remainder of the array is the state information for the
  1665. X * R.N.G.  Thus, 32 bytes of state information will give 7 longs worth of
  1666. X * state information, which will allow a degree seven polynomial.  (Note: the 
  1667. X * zeroeth word of state information also has some other information stored
  1668. X * in it -- see setstate() for details).
  1669. X * The random number generation technique is a linear feedback shift register
  1670. X * approach, employing trinomials (since there are fewer terms to sum up that
  1671. X * way).  In this approach, the least significant bit of all the numbers in
  1672. X * the state table will act as a linear feedback shift register, and will have
  1673. X * period 2^deg - 1 (where deg is the degree of the polynomial being used,
  1674. X * assuming that the polynomial is irreducible and primitive).  The higher
  1675. X * order bits will have longer periods, since their values are also influenced
  1676. X * by pseudo-random carries out of the lower bits.  The total period of the
  1677. X * generator is approximately deg*(2**deg - 1); thus doubling the amount of
  1678. X * state information has a vast influence on the period of the generator.
  1679. X * Note: the deg*(2**deg - 1) is an approximation only good for large deg,
  1680. X * when the period of the shift register is the dominant factor.  With deg
  1681. X * equal to seven, the period is actually much longer than the 7*(2**7 - 1)
  1682. X * predicted by this formula.
  1683. X */
  1684. X
  1685. X
  1686. X
  1687. X/*
  1688. X * For each of the currently supported random number generators, we have a
  1689. X * break value on the amount of state information (you need at least this
  1690. X * many bytes of state info to support this random number generator), a degree
  1691. X * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
  1692. X * the separation between the two lower order coefficients of the trinomial.
  1693. X */
  1694. X
  1695. X#define        TYPE_0        0        /* linear congruential */
  1696. X#define        BREAK_0        8
  1697. X#define        DEG_0        0
  1698. X#define        SEP_0        0
  1699. X
  1700. X#define        TYPE_1        1        /* x**7 + x**3 + 1 */
  1701. X#define        BREAK_1        32
  1702. X#define        DEG_1        7
  1703. X#define        SEP_1        3
  1704. X
  1705. X#define        TYPE_2        2        /* x**15 + x + 1 */
  1706. X#define        BREAK_2        64
  1707. X#define        DEG_2        15
  1708. X#define        SEP_2        1
  1709. X
  1710. X#define        TYPE_3        3        /* x**31 + x**3 + 1 */
  1711. X#define        BREAK_3        128
  1712. X#define        DEG_3        31
  1713. X#define        SEP_3        3
  1714. X
  1715. X#define        TYPE_4        4        /* x**63 + x + 1 */
  1716. X#define        BREAK_4        256
  1717. X#define        DEG_4        63
  1718. X#define        SEP_4        1
  1719. X
  1720. X
  1721. X/*
  1722. X * Array versions of the above information to make code run faster -- relies
  1723. X * on fact that TYPE_i == i.
  1724. X */
  1725. X
  1726. X#define        MAX_TYPES    5        /* max number of types above */
  1727. X
  1728. Xstatic  int        degrees[ MAX_TYPES ]    = { DEG_0, DEG_1, DEG_2,
  1729. X                                DEG_3, DEG_4 };
  1730. X
  1731. Xstatic  int        seps[ MAX_TYPES ]    = { SEP_0, SEP_1, SEP_2,
  1732. X                                SEP_3, SEP_4 };
  1733. X
  1734. X
  1735. X
  1736. X/*
  1737. X * Initially, everything is set up as if from :
  1738. X *        initstate( 1, &randtbl, 128 );
  1739. X * Note that this initialization takes advantage of the fact that srandom()
  1740. X * advances the front and rear pointers 10*rand_deg times, and hence the
  1741. X * rear pointer which starts at 0 will also end up at zero; thus the zeroeth
  1742. X * element of the state information, which contains info about the current
  1743. X * position of the rear pointer is just
  1744. X *    MAX_TYPES*(rptr - state) + TYPE_3 == TYPE_3.
  1745. X */
  1746. X
  1747. Xstatic  long        randtbl[ DEG_3 + 1 ]    = { TYPE_3,
  1748. X                0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 
  1749. X                0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 
  1750. X                0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 
  1751. X                0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 
  1752. X                0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7, 
  1753. X                0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 
  1754. X                0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 
  1755. X                    0xf5ad9d0e, 0x8999220b, 0x27fb47b9 };
  1756. X
  1757. X/*
  1758. X * fptr and rptr are two pointers into the state info, a front and a rear
  1759. X * pointer.  These two pointers are always rand_sep places aparts, as they cycle
  1760. X * cyclically through the state information.  (Yes, this does mean we could get
  1761. X * away with just one pointer, but the code for random() is more efficient this
  1762. X * way).  The pointers are left positioned as they would be from the call
  1763. X *            initstate( 1, randtbl, 128 )
  1764. X * (The position of the rear pointer, rptr, is really 0 (as explained above
  1765. X * in the initialization of randtbl) because the state table pointer is set
  1766. X * to point to randtbl[1] (as explained below).
  1767. X */
  1768. X
  1769. Xstatic  long        *fptr            = &randtbl[ SEP_3 + 1 ];
  1770. Xstatic  long        *rptr            = &randtbl[ 1 ];
  1771. X
  1772. X
  1773. X
  1774. X/*
  1775. X * The following things are the pointer to the state information table,
  1776. X * the type of the current generator, the degree of the current polynomial
  1777. X * being used, and the separation between the two pointers.
  1778. X * Note that for efficiency of random(), we remember the first location of
  1779. X * the state information, not the zeroeth.  Hence it is valid to access
  1780. X * state[-1], which is used to store the type of the R.N.G.
  1781. X * Also, we remember the last location, since this is more efficient than
  1782. X * indexing every time to find the address of the last element to see if
  1783. X * the front and rear pointers have wrapped.
  1784. X */
  1785. X
  1786. Xstatic  long        *state            = &randtbl[ 1 ];
  1787. X
  1788. Xstatic  int        rand_type        = TYPE_3;
  1789. Xstatic  int        rand_deg        = DEG_3;
  1790. Xstatic  int        rand_sep        = SEP_3;
  1791. X
  1792. Xstatic  long        *end_ptr        = &randtbl[ DEG_3 + 1 ];
  1793. X
  1794. X
  1795. X
  1796. X/*
  1797. X * srandom:
  1798. X * Initialize the random number generator based on the given seed.  If the
  1799. X * type is the trivial no-state-information type, just remember the seed.
  1800. X * Otherwise, initializes state[] based on the given "seed" via a linear
  1801. X * congruential generator.  Then, the pointers are set to known locations
  1802. X * that are exactly rand_sep places apart.  Lastly, it cycles the state
  1803. X * information a given number of times to get rid of any initial dependencies
  1804. X * introduced by the L.C.R.N.G.
  1805. X * Note that the initialization of randtbl[] for default usage relies on
  1806. X * values produced by this routine.
  1807. X */
  1808. X
  1809. Xvoid
  1810. Xsrandom( x )
  1811. X
  1812. X    unsigned        x;
  1813. X{
  1814. X        register  int        i;
  1815. X
  1816. X    if(  rand_type  ==  TYPE_0  )  {
  1817. X        state[ 0 ] = x;
  1818. X    }
  1819. X    else  {
  1820. X        state[ 0 ] = x;
  1821. X        for( i = 1; i < rand_deg; i++ )  {
  1822. X        state[i] = 1103515245*state[i - 1] + 12345;
  1823. X        }
  1824. X        fptr = &state[ rand_sep ];
  1825. X        rptr = &state[ 0 ];
  1826. X        for( i = 0; i < 10*rand_deg; i++ )  random();
  1827. X    }
  1828. X}
  1829. X
  1830. X
  1831. X
  1832. X/*
  1833. X * initstate:
  1834. X * Initialize the state information in the given array of n bytes for
  1835. X * future random number generation.  Based on the number of bytes we
  1836. X * are given, and the break values for the different R.N.G.'s, we choose
  1837. X * the best (largest) one we can and set things up for it.  srandom() is
  1838. X * then called to initialize the state information.
  1839. X * Note that on return from srandom(), we set state[-1] to be the type
  1840. X * multiplexed with the current value of the rear pointer; this is so
  1841. X * successive calls to initstate() won't lose this information and will
  1842. X * be able to restart with setstate().
  1843. X * Note: the first thing we do is save the current state, if any, just like
  1844. X * setstate() so that it doesn't matter when initstate is called.
  1845. X * Returns a pointer to the old state.
  1846. X */
  1847. X
  1848. Xchar  *
  1849. Xinitstate( seed, arg_state, n )
  1850. X
  1851. X    unsigned        seed;            /* seed for R. N. G. */
  1852. X    char        *arg_state;        /* pointer to state array */
  1853. X    int            n;            /* # bytes of state info */
  1854. X{
  1855. X    register  char        *ostate        = (char *)( &state[ -1 ] );
  1856. X
  1857. X    if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
  1858. X    else  state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type;
  1859. X    if(  n  <  BREAK_1  )  {
  1860. X        if(  n  <  BREAK_0  )  {
  1861. X        impossible(
  1862. X "initstate: not enough state (%d bytes) with which to do jack; ignored.", n);
  1863. X        return (char *)0;
  1864. X        }
  1865. X        rand_type = TYPE_0;
  1866. X        rand_deg = DEG_0;
  1867. X        rand_sep = SEP_0;
  1868. X    }
  1869. X    else  {
  1870. X        if(  n  <  BREAK_2  )  {
  1871. X        rand_type = TYPE_1;
  1872. X        rand_deg = DEG_1;
  1873. X        rand_sep = SEP_1;
  1874. X        }
  1875. X        else  {
  1876. X        if(  n  <  BREAK_3  )  {
  1877. X            rand_type = TYPE_2;
  1878. X            rand_deg = DEG_2;
  1879. X            rand_sep = SEP_2;
  1880. X        }
  1881. X        else  {
  1882. X            if(  n  <  BREAK_4  )  {
  1883. X            rand_type = TYPE_3;
  1884. X            rand_deg = DEG_3;
  1885. X            rand_sep = SEP_3;
  1886. X            }
  1887. X            else  {
  1888. X            rand_type = TYPE_4;
  1889. X            rand_deg = DEG_4;
  1890. X            rand_sep = SEP_4;
  1891. X            }
  1892. X        }
  1893. X        }
  1894. X    }
  1895. X    state = &(  ( (long *)arg_state )[1]  );    /* first location */
  1896. X    end_ptr = &state[ rand_deg ];    /* must set end_ptr before srandom */
  1897. X    srandom( seed );
  1898. X    if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
  1899. X    else  state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type;
  1900. X    return( ostate );
  1901. X}
  1902. X
  1903. X
  1904. X
  1905. X/*
  1906. X * setstate:
  1907. X * Restore the state from the given state array.
  1908. X * Note: it is important that we also remember the locations of the pointers
  1909. X * in the current state information, and restore the locations of the pointers
  1910. X * from the old state information.  This is done by multiplexing the pointer
  1911. X * location into the zeroeth word of the state information.
  1912. X * Note that due to the order in which things are done, it is OK to call
  1913. X * setstate() with the same state as the current state.
  1914. X * Returns a pointer to the old state information.
  1915. X */
  1916. X
  1917. Xchar  *
  1918. Xsetstate( arg_state )
  1919. X
  1920. X    char        *arg_state;
  1921. X{
  1922. X    register  long        *new_state    = (long *)arg_state;
  1923. X    register  int        type        = new_state[0]%MAX_TYPES;
  1924. X    register  int        rear        = new_state[0]/MAX_TYPES;
  1925. X    char            *ostate        = (char *)( &state[ -1 ] );
  1926. X
  1927. X    if(  rand_type  ==  TYPE_0  )  state[ -1 ] = rand_type;
  1928. X    else  state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type;
  1929. X    switch(  type  )  {
  1930. X        case  TYPE_0:
  1931. X        case  TYPE_1:
  1932. X        case  TYPE_2:
  1933. X        case  TYPE_3:
  1934. X        case  TYPE_4:
  1935. X        rand_type = type;
  1936. X        rand_deg = degrees[ type ];
  1937. X        rand_sep = seps[ type ];
  1938. X        break;
  1939. X
  1940. X        default:
  1941. X  impossible("setstate: state info has been munged (%d); not changed.", type);
  1942. X        break;
  1943. X    }
  1944. X    state = &new_state[ 1 ];
  1945. X    if(  rand_type  !=  TYPE_0  )  {
  1946. X        rptr = &state[ rear ];
  1947. X        fptr = &state[ (rear + rand_sep)%rand_deg ];
  1948. X    }
  1949. X    end_ptr = &state[ rand_deg ];        /* set end_ptr too */
  1950. X    return( ostate );
  1951. X}
  1952. X
  1953. X
  1954. X
  1955. X/*
  1956. X * random:
  1957. X * If we are using the trivial TYPE_0 R.N.G., just do the old linear
  1958. X * congruential bit.  Otherwise, we do our fancy trinomial stuff, which is the
  1959. X * same in all ther other cases due to all the global variables that have been
  1960. X * set up.  The basic operation is to add the number at the rear pointer into
  1961. X * the one at the front pointer.  Then both pointers are advanced to the next
  1962. X * location cyclically in the table.  The value returned is the sum generated,
  1963. X * reduced to 31 bits by throwing away the "least random" low bit.
  1964. X * Note: the code takes advantage of the fact that both the front and
  1965. X * rear pointers can't wrap on the same call by not testing the rear
  1966. X * pointer if the front one has wrapped.
  1967. X * Returns a 31-bit random number.
  1968. X */
  1969. X
  1970. Xlong
  1971. Xrandom()
  1972. X{
  1973. X    long        i;
  1974. X    
  1975. X    if(  rand_type  ==  TYPE_0  )  {
  1976. X        i = state[0] = ( state[0]*1103515245 + 12345 )&0x7fffffff;
  1977. X    }
  1978. X    else  {
  1979. X        *fptr += *rptr;
  1980. X        i = (*fptr >> 1)&0x7fffffff;    /* chucking least random bit */
  1981. X        if(  ++fptr  >=  end_ptr  )  {
  1982. X        fptr = state;
  1983. X        ++rptr;
  1984. X        }
  1985. X        else  {
  1986. X        if(  ++rptr  >=  end_ptr  )  rptr = state;
  1987. X        }
  1988. X    }
  1989. X    return( i );
  1990. X}
  1991. X
  1992. END_OF_FILE
  1993. if test 13686 -ne `wc -c <'sys/share/random.c'`; then
  1994.     echo shar: \"'sys/share/random.c'\" unpacked with wrong size!
  1995. fi
  1996. # end of 'sys/share/random.c'
  1997. fi
  1998. echo shar: End of archive 88 \(of 108\).
  1999. cp /dev/null ark88isdone
  2000. MISSING=""
  2001. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \
  2002. 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 \
  2003. 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 \
  2004. 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 \
  2005. 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 \
  2006. 101 102 103 104 105 106 107 108 ; do
  2007.     if test ! -f ark${I}isdone ; then
  2008.     MISSING="${MISSING} ${I}"
  2009.     fi
  2010. done
  2011. if test "${MISSING}" = "" ; then
  2012.     echo You have unpacked all 108 archives.
  2013.     echo "Now execute 'rebuild.sh'"
  2014.     rm -f ark10[0-8]isdone ark[1-9]isdone ark[1-9][0-9]isdone
  2015. else
  2016.     echo You still need to unpack the following archives:
  2017.     echo "        " ${MISSING}
  2018. fi
  2019. ##  End of shell archive.
  2020. exit 0
  2021.