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