ularn_integ.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. #include <io.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "patchlevel.h"
  5. #include "action.h"
  6. #include "diag.h"
  7. #include "dungeon.h"
  8. #include "dungeon_obj.h"
  9. #include "getopt.h"
  10. #include "header.h"
  11. #include "help.h"
  12. #include "itm.h"
  13. #include "monster.h"
  14. #include "object.h"
  15. #include "player.h"
  16. #include "potion.h"
  17. #include "savegame.h"
  18. #include "scores.h"
  19. #include "scroll.h"
  20. #include "show.h"
  21. #include "spell.h"
  22. #include "ularn_ask.h"
  23. #include "ularn_game.h"
  24. #include "ularn_win.h"
  25. #define BUFSZ 256 /* for getlin buffers */
  26. #define MAX_CMDLINE_PARAM 255
  27. static char copyright[] =
  28. "\nVLarn: based on Phil Cordier's Ularn, and Noah Morgan's Larn.\n"
  29. " Updated by Jeffrey H. Johnson, Josh Brandt, and David Richerby\n"
  30. " Rewrite and Windows & Amiga graphic conversion by Julian Olds";
  31. static char cmdhelp[] = "\
  32. Cmd line format: Vlarn [-sicnh] [-o <optsfile>] [-d #] [-r]\n\
  33. -s show the scoreboard\n\
  34. -i show scoreboard with inventories\n\
  35. -c create new scoreboard (wizard only)\n\
  36. -n suppress welcome message on starting game\n\
  37. -h print this help text\n\
  38. -o <optsfile> specify .Vlarnopts file to be used instead of \"~/.Vlarnopts\"\n\
  39. -d # specify level of difficulty (example: Vlarn -d 5)\n\
  40. -r restore checkpoint (.ckp) file\n";
  41. static char *optstring = "sicnhro:d:";
  42. static short viewflag;
  43. static char *_get_cmd_arg(char *pCmdLine) {
  44. static char *pArgs = NULL;
  45. char *pRetArg;
  46. if (!pCmdLine && !pArgs)
  47. return NULL;
  48. if (!pArgs)
  49. pArgs = pCmdLine;
  50. /* skip whitespace */
  51. for (pRetArg = pArgs; *pRetArg && isspace(*pRetArg); pRetArg++)
  52. ;
  53. {
  54. if (!*pRetArg) {
  55. /* Hit end of string, so return */
  56. pArgs = NULL;
  57. return NULL;
  58. }
  59. }
  60. /* check for quote */
  61. if (*pRetArg == '"') {
  62. pRetArg++;
  63. pArgs = strchr(pRetArg, '"');
  64. } else
  65. /* skip to whitespace */
  66. for (pArgs = pRetArg; *pArgs && !isspace(*pArgs); pArgs++)
  67. ;
  68. if (pArgs && *pArgs) {
  69. char *p;
  70. p = pArgs;
  71. pArgs = pArgs++;
  72. *p = 0;
  73. } else
  74. pArgs = NULL;
  75. return pRetArg;
  76. }
  77. //
  78. // Perform once off initialisation and parse the command parameters.
  79. //
  80. void ULarnSetup(int argc, char *argv[]) {
  81. int i;
  82. int hard;
  83. int restore_ckp;
  84. int set_optfile;
  85. int reset_scores;
  86. int show_scores;
  87. char buf[BUFSIZ];
  88. char *home;
  89. /* clear the loginname and logname */
  90. loginname[0] = 0;
  91. logname[0] = 0;
  92. set_display(DISPLAY_TEXT);
  93. ClearText();
  94. nap(100);
  95. home = ".";
  96. /* set the save directory to the home directory by default */
  97. strcpy(savedir, home);
  98. /* initialize dungeon storage */
  99. init_cells();
  100. /* set the initial clock and initialise the random number generator*/
  101. newgame();
  102. hard = -1;
  103. restore_ckp = 0;
  104. set_optfile = 0;
  105. reset_scores = 0;
  106. show_scores = 0;
  107. /*
  108. * now process the command line arguments
  109. */
  110. opterr = 0;
  111. while ((i = getopt(argc, argv, optstring)) != -1) {
  112. switch (i) {
  113. case 's':
  114. show_scores = 1;
  115. break;
  116. case 'i':
  117. show_scores = 2;
  118. break;
  119. case 'c':
  120. reset_scores = 1;
  121. break;
  122. case 'n':
  123. nowelcome = 1;
  124. break;
  125. case 'd':
  126. /* specify hardness */
  127. hard = atoi(optarg);
  128. if (hard > 100)
  129. hard = 100;
  130. if (hard < 0) {
  131. Printf("Difficulty level must be > 0\n");
  132. Print(cmdhelp);
  133. get_prompt_input("\n\nPress space to exit", " ", 0);
  134. exit(1);
  135. }
  136. break;
  137. case 'h':
  138. /* print out command line arguments */
  139. Printf("%s", copyright);
  140. Printf("\n\n Version %s.%s (%s)", LARN_VERSION, LARN_PATCHLEVEL,
  141. LARN_DATE);
  142. Printf("\n\n%s", cmdhelp);
  143. get_prompt_input("\n\nPress space to exit", " ", 0);
  144. exit(0);
  145. case 'o':
  146. /* specify a vlarn.opt filename */
  147. strcpy(optsfile, optarg);
  148. set_optfile = 1;
  149. break;
  150. case 'r':
  151. /* restore checkpointed game */
  152. restore_ckp = 1;
  153. break;
  154. case '?':
  155. default:
  156. if (!opterr)
  157. Printf("Unknown option <%s>\n", argv[optind - 1]);
  158. Printf("%s\n", copyright);
  159. Printf("Version %s.%s (%s)\n", LARN_VERSION, LARN_PATCHLEVEL, LARN_DATE);
  160. Printf("%s", cmdhelp);
  161. exit(1);
  162. } /* end switch */
  163. } /* end while */
  164. /* Options filename was not specified on the command line, so set default */
  165. if (!set_optfile) {
  166. sprintf(buf, "%s/%s", home, optsfile);
  167. strcpy(optsfile, buf);
  168. }
  169. /* read the options file if there is one */
  170. read_options();
  171. /*
  172. * Process scorefile initialisation
  173. */
  174. /* the vlarn scoreboard filename */
  175. sprintf(scorefile, "%s/%s", libdir, SCORENAME);
  176. if (reset_scores) {
  177. /* anyone with password can create scoreboard */
  178. Print("Preparing to initialize the scoreboard.\n");
  179. if (getpassword() != 0) {
  180. if (makeboard() == -1) {
  181. Print("Scoreboard creation failed!!\n");
  182. exit(1);
  183. }
  184. showscores();
  185. Print("\nScoreboard initialized.\n");
  186. }
  187. exit(0);
  188. }
  189. if (show_scores == 1) {
  190. showscores();
  191. endgame();
  192. } else if (show_scores == 2) {
  193. showallscores();
  194. endgame();
  195. }
  196. /* now make scoreboard if it is not there (don't clear) */
  197. if (access(scorefile, 0) == -1) {
  198. /* score file not there */
  199. if (makeboard() == -1) {
  200. Printf("I can't create the scoreboard.\n");
  201. Printf("Check permissions on %s\n", libdir);
  202. endgame();
  203. }
  204. }
  205. /*
  206. * Get the user name and id.
  207. * For OS without usernames, use the logname, if it is specified.
  208. */
  209. strcpy(loginname, logname);
  210. GetUser(loginname, &userid);
  211. if (logname[0] == 0)
  212. /* no logname specified, so use loginname */
  213. strcpy(logname, loginname);
  214. /*
  215. * Set the file names
  216. */
  217. /* the vlarn on-line help file */
  218. sprintf(helpfile, "%s/%s", libdir, HELPNAME);
  219. /* the pre-made cave level data file */
  220. sprintf(larnlevels, "%s/%s", libdir, LEVELSNAME);
  221. /* the fortune data file name */
  222. sprintf(fortfile, "%s/%s", libdir, FORTSNAME);
  223. /* save file name in home directory */
  224. sprintf(savefilename, "%s/ularn_%s.sav", savedir, loginname);
  225. /* the checkpoint file */
  226. sprintf(ckpfile, "%s/ularn_%s.ckp", home, loginname);
  227. /* set up the desired difficulty */
  228. sethard(hard);
  229. if (restore_ckp) {
  230. if (access(ckpfile, 0) == -1) {
  231. printf("Cannot find checkpoint file %s\n", ckpfile);
  232. exit(1);
  233. }
  234. printf("Restoring...");
  235. fflush(stdout);
  236. restorflag = 1;
  237. hitflag = 1;
  238. restoregame(ckpfile);
  239. }
  240. /* restore game if need to, and haven't restored ckpfile */
  241. if (!restorflag && (access(savefilename, 0) == 0)) {
  242. restorflag = 1;
  243. hitflag = 1;
  244. Print("Restoring...");
  245. restoregame(savefilename);
  246. }
  247. /* create new game */
  248. if (restorflag == 0) {
  249. /* make the character that will play */
  250. makeplayer();
  251. /* make the dungeon */
  252. newcavelevel(0);
  253. if (nowelcome == 0)
  254. /* welcome the player to the game */
  255. welcome();
  256. }
  257. set_display(DISPLAY_MAP);
  258. showplayer();
  259. yrepcount = 0;
  260. hit2flag = 0;
  261. }
  262. /*
  263. * parse()
  264. *
  265. * execute a command
  266. */
  267. void parse(ActionType Action) {
  268. int i, j;
  269. int flag;
  270. switch (Action) {
  271. case ACTION_DIAG:
  272. if (wizard)
  273. diag();
  274. yrepcount = 0;
  275. return;
  276. case ACTION_MOVE_WEST:
  277. moveplayer(4);
  278. return;
  279. case ACTION_RUN_WEST:
  280. run(4);
  281. return;
  282. case ACTION_MOVE_EAST:
  283. moveplayer(2);
  284. return;
  285. case ACTION_RUN_EAST:
  286. run(2);
  287. return;
  288. case ACTION_MOVE_SOUTH:
  289. moveplayer(1);
  290. return;
  291. case ACTION_RUN_SOUTH:
  292. run(1);
  293. return;
  294. case ACTION_MOVE_NORTH:
  295. moveplayer(3);
  296. return;
  297. case ACTION_RUN_NORTH:
  298. run(3);
  299. return;
  300. case ACTION_MOVE_NORTHEAST:
  301. moveplayer(5);
  302. return;
  303. case ACTION_RUN_NORTHEAST:
  304. run(5);
  305. return;
  306. case ACTION_MOVE_NORTHWEST:
  307. moveplayer(6);
  308. return;
  309. case ACTION_RUN_NORTHWEST:
  310. run(6);
  311. return;
  312. case ACTION_MOVE_SOUTHEAST:
  313. moveplayer(7);
  314. return;
  315. case ACTION_RUN_SOUTHEAST:
  316. run(7);
  317. return;
  318. case ACTION_MOVE_SOUTHWEST:
  319. moveplayer(8);
  320. return;
  321. case ACTION_RUN_SOUTHWEST:
  322. run(8);
  323. return;
  324. case ACTION_WAIT:
  325. if (yrepcount)
  326. viewflag = 1;
  327. return;
  328. case ACTION_NONE:
  329. yrepcount = 0;
  330. nomove = 1;
  331. return;
  332. case ACTION_WIELD:
  333. yrepcount = 0;
  334. wield();
  335. return;
  336. case ACTION_WEAR:
  337. yrepcount = 0;
  338. wear();
  339. return;
  340. case ACTION_READ:
  341. yrepcount = 0;
  342. if (c[BLINDCOUNT])
  343. Print("\nYou can't read anything when you're blind!");
  344. else if (c[TIMESTOP] == 0)
  345. readscr();
  346. return;
  347. case ACTION_QUAFF:
  348. yrepcount = 0;
  349. if (c[TIMESTOP] == 0)
  350. quaff();
  351. return;
  352. case ACTION_DROP:
  353. yrepcount = 0;
  354. if (c[TIMESTOP] == 0)
  355. dropobj();
  356. return;
  357. case ACTION_CAST_SPELL:
  358. yrepcount = 0;
  359. cast();
  360. return;
  361. case ACTION_CLOSE_DOOR:
  362. yrepcount = 0;
  363. closedoor();
  364. return;
  365. case ACTION_INVENTORY:
  366. yrepcount = 0;
  367. nomove = 1;
  368. showstr();
  369. return;
  370. case ACTION_EAT_COOKIE:
  371. yrepcount = 0;
  372. if (c[TIMESTOP] == 0)
  373. eatcookie();
  374. return;
  375. case ACTION_LIST_SPELLS:
  376. yrepcount = 0;
  377. seemagic(0);
  378. nomove = 1;
  379. return;
  380. case ACTION_HELP:
  381. yrepcount = 0;
  382. help();
  383. nomove = 1;
  384. return;
  385. case ACTION_SAVE:
  386. ClearText();
  387. Print("Saving . . .");
  388. if (savegame(savefilename) == -1)
  389. Print("\nSave game failed.\n");
  390. wizard = 1; /* so not show scores */
  391. died(DIED_SUSPENDED, 0);
  392. return;
  393. case ACTION_TELEPORT:
  394. yrepcount = 0;
  395. if (wizard) {
  396. int t;
  397. Print("\nWhich level do you wish to teleport to? ");
  398. t = (int)get_num_input(20);
  399. if (t > VBOTTOM || t < 0) {
  400. Print(" sorry!");
  401. return;
  402. }
  403. playerx = (char)rnd(MAXX - 2);
  404. playery = (char)rnd(MAXY - 2);
  405. newcavelevel(t);
  406. positionplayer();
  407. draws(0, MAXX, 0, MAXY);
  408. UpdateStatusAndEffects();
  409. return;
  410. }
  411. if (c[LEVEL] >= INNATE_TELEPORT_LEVEL) {
  412. oteleport(1);
  413. return;
  414. }
  415. Print("\nYou don't know how to teleport yet.");
  416. return;
  417. case ACTION_IDENTIFY_TRAPS:
  418. flag = 0;
  419. yrepcount = 0;
  420. Printc('\n');
  421. for (j = playery - 1; j < playery + 2; j++) {
  422. if (j < 0)
  423. j = 0;
  424. if (j >= MAXY)
  425. break;
  426. for (i = playerx - 1; i < playerx + 2; i++) {
  427. if (i < 0)
  428. i = 0;
  429. if (i >= MAXX)
  430. break;
  431. switch (item[i][j]) {
  432. case OTRAPDOOR:
  433. case ODARTRAP:
  434. case OTRAPARROW:
  435. case OTELEPORTER:
  436. case OELEVATORUP:
  437. case OELEVATORDOWN:
  438. Print("\nIt's ");
  439. Print(objectname[item[i][j]]);
  440. flag++;
  441. }
  442. }
  443. }
  444. if (flag == 0)
  445. Print("\nNo traps are visible.");
  446. return;
  447. case ACTION_BECOME_CREATOR:
  448. yrepcount = 0;
  449. nomove = 1;
  450. if (!wizard)
  451. if (getpassword() == 0)
  452. return;
  453. raiseexperience(370 * 1000000);
  454. recalc();
  455. UpdateStatus();
  456. drawscreen();
  457. return;
  458. case ACTION_CREATE_ITEM:
  459. yrepcount = 0;
  460. if (wizard)
  461. do_create();
  462. return;
  463. case ACTION_TOGGLE_WIZARD:
  464. yrepcount = 0;
  465. nomove = 1;
  466. if (wizard) {
  467. Print("You are no longer a wizard.\n");
  468. wizard = 0;
  469. return;
  470. }
  471. if (getpassword()) {
  472. Print("You are now a wizard.\n");
  473. wizard = 1;
  474. } else
  475. Print("Sorry.\n");
  476. return;
  477. case ACTION_DEBUG_MODE:
  478. yrepcount = 0;
  479. nomove = 1;
  480. if (!wizard) {
  481. if (getpassword() == 0)
  482. return;
  483. wizard = 1;
  484. }
  485. for (i = 0; i < 6; i++)
  486. c[i] = 70;
  487. iven[0] = iven[1] = 0;
  488. take(OPROTRING, 50);
  489. take(OLANCE, 25);
  490. for (i = 0; i < IVENSIZE; i++) {
  491. if (iven[i] == OLANCE && ivenarg[i] == 25) {
  492. c[WIELD] = i;
  493. break;
  494. }
  495. }
  496. c[LANCEDEATH] = 1;
  497. c[WEAR] = c[SHIELD] = -1;
  498. raiseexperience(370 * 1000000);
  499. c[AWARENESS] += 25000;
  500. {
  501. int i, j;
  502. for (i = 0; i < MAXY; i++)
  503. for (j = 0; j < MAXX; j++)
  504. know[j][i] = 1;
  505. for (i = 0; i < SPELL_COUNT; i++)
  506. spelknow[i] = 1;
  507. for (i = 0; i < MAXSCROLL; i++)
  508. potionknown[i] = 1;
  509. for (i = 0; i < MAXPOTION; i++)
  510. potionknown[i] = 1;
  511. }
  512. for (i = 0; i < MAXSCROLL; i++) {
  513. if (strlen(scrollname[i]) > 2) {
  514. item[i][0] = OSCROLL;
  515. iarg[i][0] = (short)i;
  516. }
  517. }
  518. for (i = 0; i < MAXPOTION; i++) {
  519. /* no null items */
  520. if (strlen(potionname[i]) > 2) {
  521. item[(MAXX - 1) - i][0] = OPOTION;
  522. iarg[(MAXX - 1) - i][0] = (short)(i);
  523. }
  524. }
  525. j = 1;
  526. for (i = 1; i < MAXY; i++) {
  527. item[0][i] = (char)j;
  528. iarg[0][i] = (short)0;
  529. j++;
  530. }
  531. for (i = 1; i < MAXX; i++) {
  532. item[i][MAXY - 1] = (char)j;
  533. iarg[i][MAXY - 1] = (short)0;
  534. j++;
  535. }
  536. for (i = 1; i < MAXY - 1; i++) {
  537. item[MAXX - 1][i] = (char)j;
  538. iarg[MAXX - 1][i] = 0;
  539. j++;
  540. }
  541. c[GOLD] += 250000;
  542. recalc();
  543. UpdateStatus();
  544. drawscreen();
  545. return;
  546. case ACTION_REMOVE_ARMOUR:
  547. yrepcount = 0;
  548. if (c[SHIELD] != -1) {
  549. c[SHIELD] = -1;
  550. Print("\nYour shield is off.");
  551. recalc();
  552. UpdateStatus();
  553. } else {
  554. if (c[WEAR] != -1) {
  555. c[WEAR] = -1;
  556. Print("\nYour armor is off.");
  557. recalc();
  558. UpdateStatus();
  559. } else
  560. Print("\nYou aren't wearing anything.");
  561. }
  562. return;
  563. case ACTION_PACK_WEIGHT:
  564. Printf("\nThe stuff you are carrying presently weighs %d pound%s.",
  565. (long)packweight(), plural(packweight()));
  566. nomove = 1;
  567. yrepcount = 0;
  568. return;
  569. case ACTION_VERSION:
  570. yrepcount = 0;
  571. Printf(
  572. "\nThe Addiction of VLarn -- Version %s.%s (%s)\nDifficulty level %d",
  573. LARN_VERSION, LARN_PATCHLEVEL, LARN_DATE, (long)c[HARDGAME]);
  574. if (wizard)
  575. Print(" (WIZARD)");
  576. nomove = 1;
  577. if (cheat)
  578. Print(" (Cheater)");
  579. Print(copyright);
  580. return;
  581. case ACTION_QUIT:
  582. yrepcount = 0;
  583. quit();
  584. nomove = 1;
  585. return;
  586. case ACTION_REDRAW_SCREEN:
  587. yrepcount = 0;
  588. drawscreen();
  589. nomove = 1;
  590. return;
  591. case ACTION_SHOW_TAX:
  592. if (outstanding_taxes > 0)
  593. Printf("\nYou presently owe %d gp in taxes.", (long)outstanding_taxes);
  594. else
  595. Print("\nYou do not owe any taxes.");
  596. return;
  597. default:
  598. Print("HELP! unknown command\n");
  599. break;
  600. }
  601. }
  602. int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
  603. LPSTR lpcmdline, int ncmdshow) {
  604. ActionType Action;
  605. int argc;
  606. char *argv[MAX_CMDLINE_PARAM];
  607. TCHAR *p;
  608. TCHAR wbuf[BUFSZ];
  609. char buf[BUFSZ];
  610. size_t len;
  611. /*
  612. * get command line parameters
  613. */
  614. p = _get_cmd_arg(GetCommandLine());
  615. p = _get_cmd_arg(NULL); /* skip first paramter - command name */
  616. for (argc = 1; p && argc < MAX_CMDLINE_PARAM; argc++) {
  617. len = strlen(p);
  618. if (len > 0)
  619. argv[argc] = strdup(p);
  620. else
  621. argv[argc] = "";
  622. p = _get_cmd_arg(NULL);
  623. }
  624. GetModuleFileName(NULL, wbuf, BUFSZ);
  625. argv[0] = strdup(strncpy(wbuf, buf, BUFSZ));
  626. //
  627. // Setup the display interface
  628. //
  629. if (!init_app(hinstance))
  630. return 0;
  631. ULarnSetup(argc, argv);
  632. do {
  633. if (dropflag == 0) {
  634. lookforobject(); /* see if there is an object here*/
  635. } else {
  636. dropflag = 0; /* don't show it just dropped an item */
  637. }
  638. if (hitflag == 0) {
  639. if (c[HASTEMONST])
  640. movemonst();
  641. movemonst();
  642. }
  643. if (viewflag == 0)
  644. showcell(playerx, playery);
  645. else {
  646. viewflag = 0; /* show stuff around player */
  647. }
  648. hitflag = 0;
  649. hit3flag = 0;
  650. nomove = 1;
  651. /* get commands and make moves */
  652. while (nomove) {
  653. if (hit3flag)
  654. flushall();
  655. nomove = 0;
  656. Action = get_normal_input();
  657. parse(Action); /* may reset nomove=1 */
  658. }
  659. /* regenerate hp and spells */
  660. regen();
  661. if (c[TIMESTOP] == 0) {
  662. rmst--;
  663. if (rmst <= 0) {
  664. rmst = (char)(120 - (level << 2));
  665. fillmonst(makemonst(level));
  666. }
  667. }
  668. } while (Action != ACTION_QUIT);
  669. /*
  670. * tidyup and exit
  671. */
  672. endgame();
  673. /* not actually reachable */
  674. return 0;
  675. }