forked from quentinnuk/perlmud-3.0-TH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathth_mud.pm
8497 lines (7956 loc) · 277 KB
/
th_mud.pm
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# -*- mode: perl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
# vim:filetype=perl:et:sw=4:ts=4:sts=4
package th_mud;
use strict;
use Text::Wrap qw(wrap $columns);
use Exporter 'import';
our @EXPORT = qw(do_mud load_mud);
use List::Util 'shuffle';
use parent;
my $maintainer = "jadawin"; # local system user who maintains the MUD
#Should users be allowed to create objects and rooms by default?
#Set this to 0 if you prefer not.
my $allowBuild = 0;
#Should users be allowed to @emit things without their
#name prefixed? Set this to zero if you prefer not.
my $allowEmit = 0;
#File locations for the database, the login screen banner,
#the Message of the Day, and the help file.
my $dbFile = "data/mud/mud.db";
my $personaFile = "data/mud/mud.pf";
my $welcomeFile = "data/mud/welcome.txt";
my $motdFile = "data/mud/motd.txt";
my $helpFile = "data/mud/help.txt";
my $filePrefix = "data/mud/";
#If you uncomment this, set a password
#my $newsPassword = "";
my $newsPassword = "a8b7";
#Idle timeout. This hangs up on users if they do not
#enter at least one command in the time interval
#(specified in seconds; 3600 is an hour).
my $idleTimeout = 86400;
#Time to wait between (brief) attempts at closing
#sockets we don't need anymore. The longer this is,
#the fewer pauses the mud will experience.
my $fdClosureInterval = 30;
#Interval between automatic backups of the database, in seconds (1 hour).
#Note: stale topics are also sent home during this pass.
my $dumpinterval = 3600;
#Seconds until a topic is considered stale.
my $topicStaleTime = 3000;
#Version of PerlMUD.
my $perlMudVersion = 3.0;
#Max size of output buffer before flushing takes place
my $flushOutput = 32768;
#If the client sends the 'smartclient' command prior to sending the
#connect command, then this prefix is sent in front of each line of
#topic-specific output for the life of the connection. The @emit command
#cannot spoof this.
my $topicPrefix = "[{}]";
#Nothing below here should require changes to set up the mud
our $mudReloadFlag;
#Protocols
my $tinyp = 0;
#Object types
my $room = 1;
my $player = 2;
my $exit = 3;
my $thing = 4;
my $topic = 5;
my $synonym = 6;
my $action = 7;
my $demon = 8;
#Special IDs
my $none = -1;
my $home = -2;
my $nowhere = -3;
#Flag values
#Can't be seen; or description only, contents invisible
my $dark = 1;
#Gender
my $male = 2;
my $female = 4;
my $herm = 6;
#Name of location visible in who list
my $public = 8;
#Gives off light flag
my $bright = 16;
#OK to link to
my $linkok = 32;
#OK to jump to
my $jumpok = 64;
#OK for anyone to build here
my $buildok = 128;
#Claimable by anyone who passes the lock
#(Not yet implemented)
my $claimok = 256;
#Goes home when dropped
my $sticky = 512;
#Part of a puzzle; a teleport or home command
#from this location drops all objects carried.
my $puzzle = 1024;
#If true, this location can be set home (@link)
#for an object by anyone.
my $abode = 2048;
#If true for a room, this location is "grand central station":
#players can see things, hear people speak, etc., but arrivals and
#departures go unnoticed.
my $grand = 4096;
#If true for an object, any person can "sign" the object,
#appending a string of up to 60 characters to its description.
my $book = 8192;
#This player is a wizard. #1 is always a wizard.
my $wizard = 16384;
#This player hates automatic speech and wants more abbreviations.
my $expert = 32768;
#This player wants to know who @emits things.
#Only an issue if $allowEmit is set.
my $spy = 65536;
#This player is allowed to build things. Set for new
#players if $allowBuild is set. Only a wizard can change
#this flag after that.
my $builder = 131072;
#If the book flag is set, and the once flag is also set, then
#any subsequent signature replaces all previous signatures
#by the same individual.
my $once = 262144;
# there is water here
my $water = 524288;
# there is oil here
my $oil = 1048576;
# if a mortal player enters they die
my $death = 2097152;
# objects dropped here add to score by object value
my $sanctuary = 4194304;
# If a player is in here, they cannot be seen from outside by mortals
my $hideaway = 8388608;
# If an object is in here, it cannot be seen by mortal players.
my $hide = 16777216;
# Only one player or mobile can be in this room at a time.
my $small = 33554432;
# This means that the room cannot be looked into from an adjacent room
my $nolook = 67108864;
# If a wiz is in a silent room, then they receives no status messages
my $silent = 134217728;
# you cannot pick up an object with this flag
my $noget = 268435456;
# you can always see the contents of this object even if not prop 0
my $transparent = 536870912;
# objects can be removed even if not at prop 0
my $opened = 1073741824;
# there will be no indication that this object is a container
my $disguised = 2147483648;
# need a 64bit arch for these flags
# if you carry this object you cannot be summoned
my $nosummon = 4294967296;
# impossible to pick up even for wizards (eg tide or rain)
my $fixed = 8589934592;
# will never be assigned to "it"
my $noit = 17179869184;
# illnesses
my $blind = 34359738368;
my $deaf = 68719476736;
my $dumb = 137438953472;
my $paralysed = 274877906944;
my $asleep = 549755813888;
my $destroyed = 1099511627776;
# demon flags
my $dEnabled = 1;
my $dGlobal = 2;
my $dAlways = 4;
#For flag setting
my %flags = (
"dark", $dark,
"male", $male,
"female", $female,
"public", $public,
"bright", $bright,
"linkok", $linkok,
"jumpok", $jumpok,
"buildok", $buildok,
"claimok", $claimok,
"link_ok", $linkok,
"jump_ok", $jumpok,
"build_ok", $buildok,
"claim_ok", $claimok,
"link-ok", $linkok,
"jump-ok", $jumpok,
"build-ok", $buildok,
"claim-ok", $claimok,
"sticky", $sticky,
"puzzle", $puzzle,
"abode", $abode,
"grand", $grand,
"book", $book,
"wizard", $wizard,
"expert", $expert,
"spy", $spy,
"builder", $builder,
"once", $once,
"water", $water,
"oil", $oil,
"death", $death,
"sanctuary", $sanctuary,
"hideaway", $hideaway,
"hide", $hide,
"small", $small,
"nolook", $nolook,
"no-look", $nolook,
"silent", $silent,
"noget", $noget,
"no-get", $noget,
"transparent", $transparent,
"opened", $opened,
"disguised", $disguised,
"no-summon", $nosummon,
"fixed", $fixed,
"noit", $noit,
"no-it", $noit,
"blind", $blind,
"deaf", $deaf,
"dumb", $dumb,
"paralysed", $paralysed,
"asleep", $asleep,
"destroyed", $destroyed
);
my %flagsProper = (
"dark", $dark,
"male", $male,
"female", $female,
"public", $public,
"bright", $bright,
"linkok", $linkok,
"jumpok", $jumpok,
"buildok", $buildok,
"claimok", $claimok,
"sticky", $sticky,
"puzzle", $puzzle,
"abode", $abode,
"grand", $grand,
"book", $book,
"wizard", $wizard,
"expert", $expert,
"spy", $spy,
"builder", $builder,
"once", $once,
"water", $water,
"oil", $oil,
"death", $death,
"sanctuary", $sanctuary,
"hideaway", $hideaway,
"hide", $hide,
"small", $small,
"nolook", $nolook,
"silent", $silent,
"noget", $noget,
"transparent", $transparent,
"opened", $opened,
"disguised", $disguised,
"no-summon", $nosummon,
"fixed", $fixed,
"noit", $noit,
"blind", $blind,
"deaf", $deaf,
"dumb", $dumb,
"paralysed", $paralysed,
"asleep", $asleep,
"destroyed", $destroyed
);
my @flagNames = (
"dark",
"male",
"female",
"public",
"bright",
"linkok",
"jumpok",
"buildok",
"claimok",
"sticky",
"puzzle",
"abode",
"grand",
"book",
"wizard",
"expert",
"spy",
"builder",
"once",
"water",
"oil",
"death",
"sanctuary",
"hideaway",
"hide",
"small",
"nolook",
"silent",
"noget",
"transparent",
"opened",
"disguised",
"no-summon",
"fixed",
"noit",
"blind",
"deaf",
"dumb",
"paralysed",
"asleep",
"destroyed"
);
# the levels - must be in order 0 to 10 as below
my %levelNames = (
"male", ["novice",
"warrior",
"hero",
"champion",
"superhero",
"enchanter",
"sorcerer",
"necromancer",
"legend",
"wizard",
"arch-wizard"],
"female", ["novice",
"warrior",
"heroine",
"champion",
"superheroine",
"enchantress",
"sorceress",
"necromancess",
"legend",
"witch",
"arch-witch"]
);
#Set these up in a particular order so that we can
#say that, for instance, abbreviations of 'whisper'
#should beat abbreviations of 'who'.
my @commandsProperOrder = (
"\@wall", \&shout,
"shout", \&shout,
# "say", \&say,
"emote", \&emote,
"\@dig", \&dig,
# "\@doing", \&doing,
"\@create", \&create,
"\@stats", \&stats,
"\@rooms", \&rooms,
"\@gag", \&gag,
"\@ungag", \&ungag,
# "l", \&look,
# "score", \&mud_score,
# "qs", \&quickScore,
# "read", \&look,
# "exits", \&exits,
"\@examine", \&examine,
# "i", \&inven,
# "drop", \&drop,
# "get", \&get,
# "home", \&home,
# "whisper", \&whisper,
# "tell", \&whisper,
# "who", \&who,
# "sign", \&sign,
# "write", \&sign,
# "unsign", \&unsign,
"\@help", \&help,
# "motd", \&motd,
# "welcome", \&welcome,
"\@set", \&setFlag,
"\@describe", \&setDescription,
# "page", \&page,
"\@name", \&name,
"\@chown", \&chown,
# "\@pcreate", \&pcreate, #debug not needed on TH
"\@teleport", \&teleport,
"\@go", \&teleport,
"\@link", \&link,
"\@open", \&open,
"\@fail", \&setFail,
"\@ofail", \&setOfail,
"\@success", \&setSuccess,
"\@osuccess", \&setOsuccess,
"\@odrop", \&setOdrop,
"\@lock", \&setLock,
"\@boot", \&boot,
"\@find", \&find,
"\@emit", \&emit,
"\@topic", \&createTopic,
"\@join", \&joinTopic,
"\@leave", \&leaveTopic,
# "last", \&last,
# "\@tz", \&tz, #debug not needed on TH
# "\@24", \&twentyfour,
# "\@12", \&twelve,
"\@prop", \&setProp,
"\@recycle", \&recycle,
"\@purge", \&purgeObj,
"\@toad", \&toad,
"\@shutdown", \&mud_shutdown,
"\@reset", \&reload,
"\@dump", \&dump
);
my @invalidMsgs=(
"I'm not sure I understand you fully.",
"What?",
"I don't understand that.",
"I don't see what you mean.",
"It's all double dutch to me mate!");
#Set up commands table (now in order of precedence)
#3.0: make sure to empty it again if we're reloading
our %commandsTable;
our %commandsProper;
# initialise synonym table
our %synonymTable;
our ($lastdump, $lastFdClosure, $now) = (time, time, time);
our $initialized = time;
our (@activeFds);
our ($fdClosureNew, $fdClosureTimedOut, @fdClosureList);
our (@objects, %playerIds, $commandLogging);
our @demonsTable;
our %fightsTable;
our $mudPlayers; # keep a count for scoring
our $mud_closed = 0;
$mudReloadFlag = 1;
our $debugMode = 0;
#debug default to peace for now
our $worldPeace = 1;
sub load_mud
{
$mud_closed = 1; # in case anything bad happens
$mudPlayers=0;
#debug default to peace for now
$worldPeace = 1;
my($i);
# define the core builder commands
for ($i = 0; ($i < int(@commandsProperOrder)); $i += 2) {
$commandsProper{$commandsProperOrder[$i]} =
$commandsProperOrder[$i + 1];
}
# kill any residual timers just in case
&TH::kill_timer(\&mud_housekeeping, 0); # stop the housekeeping
&TH::kill_timer(\&mud_creature, 0); # stops all creature daemons
&TH::kill_timer(\&mud_xdemon, 0); # stops all other daemons
for (my $i = 0; ($i <= $#activeFds); $i++) { # stop autowhos
next if ($activeFds[$i]{"id"}==$none); # fd not in use
if (defined $objects[$activeFds[$i]{"id"}]{"autowho"}) {
delete $objects[$activeFds[$i]{"id"}]{"autowho"};
&TH::kill_timer(\&mud_autowho, $activeFds[$i]{"fd"}->{"fd"});
}
}
@objects = (); # initialise the objects
%playerIds = (); # init the player table;
%synonymTable = (); # init synonyms;
@demonsTable = (); # init the demons table;
%fightsTable = {}; # init fightsTable;
#debug a return of 0 here isnt causing an exception
if (!&restore()) {
&TH::xlog('Mud: FATAL: Can\'t start the mud with this database.');
return 0;
}
$TH::data->{mud} = {}; # nobody in mud
&TH::set_timer(1,\&mud_housekeeping,0,0); # start housekeeping every second
&TH::xlog("Mud: initialized");
$mud_closed = 0; # open it up
#(re)initialization code ends here
}
sub restore
{
#debug broken databases dont kill the mud
&TH::xlog('Mud: loading data');
return 0 unless (restore_db($dbFile)); #debug these arent failing on 0
return 0 unless (restore_db($personaFile));
&TH::xlog('Mud: initialising game');
# process synonyms, actions and mobiles
for my $id (0..$#objects) {
if ($objects[$id]{"type"}==$action) { # build commandTable
my $s = $objects[$id]{"name"};
if (!exists($commandsTable{$s})) {
$commandsTable{$s} = "$id";
} else {
$commandsTable{$s} .= ",$id";
}
}
if ($objects[$id]{"type"}==$synonym) { # build synonymTable from synonym objects
my $synonym = $objects[$id]{"name"};
$synonymTable{"$synonym"} = $objects[$id]{"action"}; # synonym to action map
}
if ($objects[$id]{"type"}==$demon) { # build demon table
my $demonid = int(abs($objects[$id]{"name"})); # must be numeric
$demonsTable[$demonid]{"id"}=$id; # demon to object map
$demonsTable[$demonid]{"flags"}=$objects[$id]{"flags"}; # demon run state
if ($demonsTable[$demonid]{"flags"} & $dEnabled) {
# launch at load
&mud_demon(0,$demonid,"","","");
}
}
if (($objects[$id]{"type"}==$thing) && ($objects[$id]{"speed"}>0)) { # add mobile to timers
&TH::set_timer($objects[$id]{"speed"},\&mud_creature,$id,0);
}
}
return 1;
}
sub restore_db
{
my ($dbFile) = @_;
my($id, $dbVersion, $dbVersionKnown);
if (!open(IN, $dbFile)) {
&TH::xlog('Mud: FATAL: Unable to read from ' . $dbFile);
return 0;
# debug
# in future this should just flag mud unavailable in TH rather than exit 0
}
$dbVersionKnown = 0;
while($id = <IN>) {
if (!$dbVersionKnown) {
$dbVersionKnown = 1;
if (!($id =~ /^\d+\.\d+\s*$/)) {
$dbVersion = 0.0;
} else {
$dbVersion = $id;
if ($dbVersion > $perlMudVersion) {
&TH::xlog ( 'Mud: FATAL: database '. $dbVersion . ' not supported');
close(IN);
return 0;
}
next;
}
}
chomp $id;
if ($id eq "<END>") { # empty formatted db
last;
}
if ($dbVersion >= 2.1) {
# New style database, hurrah
while (1) {
my($attribute, $value, $line);
$line = <IN>;
if ($line eq "") {
#Uh-oh
&TH::xlog ('Mud: FATAL: Database ' . $dbFile . ' is truncated!');
return 0;
}
chomp $line;
if ($line eq "<END>") {
last;
}
# Get the attribute and the value
($attribute, $value) = split(/ /, $line, 2);
# Unescape endlines
$value =~ s/\\n/\r\n/g;
# But a slash preceding one of those
# means an escaped LF is truly wanted
$value =~ s/\\\r\n/\\n/g;
if ((($attribute eq 'action') || ($attribute eq 'name')) && ($value=~/.+\|.+/)) { # multi value attribute
my @vals = split(/\|/,$value);
@vals = shuffle(@vals); # randomise order
$value = join('|',@vals); # re-assemble
}
$objects[$id]{$attribute} = $value;
}
if ((exists $objects[$id]{"id"}) && ($id!=1)) { # arch-wiz obj 1 can be duplicated but nothing else
&TH::xlog('Mud: FATAL: Database ' . $dbFile . ' duplicate object ' . $id. '!');
return 0;
}
$objects[$id]{"id"} = $id;
if ($id == 1) {
$objects[1]{"flags"} |= $wizard;
}
if ($objects[$id]{"type"} == $player) {
my($n);
$n = $objects[$id]{"name"};
$n =~ tr/A-Z/a-z/;
$playerIds{$n} = $id;
}
if ($objects[$id]{"home"}=~/.+\|.+/) { # multi value attribute
my @vals = split(/\|/,$objects[$id]{"home"});
my $loc = $vals[rand @vals]; # pick a random location from possible homes for the current location
$objects[$id]{"location"}=$loc;
# now put it in the location
if (length($objects[$loc]{"contents"}) > 0) {
$objects[$loc]{"contents"} .= "," . $id;
} else {
$objects[$loc]{"contents"} = $id;
}
}
# GOTCHA: $none and 0 are different
$objects[$id]{"activeFd"} = $none;
} else {
&TH::xlog('Mud: FATAL: invalid database ' . $dbFile . ', not loaded');
return 0;
}
}
close(IN);
}
sub do_mud
{
my ( $conn ) = @_;
if ($mud_closed) {
&TH::th_println("Some powerful magic prevents your entering The Land. Try again later, please.");
return;
}
$TH::data->{mud} = {} if !defined $TH::data->{mud};
my $mud = $TH::data->{mud};
my $user = &TH::get_user($conn);
my $udata = $conn->{udata};
if ($mud->{users}->{$user} == 1) {
&TH::th_println("You were already playing MUD and have been kicked out.");
$user =~ tr/A-Z/a-z/;
if (exists($playerIds{$user})) {
my $id = $playerIds{$user};
forceClosePlayer( $id, 1); # forces exit to shell
}
delete $mud->{users}->{$user};
return;
}
$mud->{users}->{$user} = 1;
$conn->{in_mud} = 1;
$conn->{interrupt_sub} = \& do_mud_quit;
# functional TH equivalent of acceptTinyp in PerlMUD source
my($aindex, $found);
$found = 0;
for ($aindex = 0; ($aindex <= $#activeFds); $aindex++) {
if ($activeFds[$aindex]{"fd"} eq $none) {
$activeFds[$aindex]{"protocol"} = $tinyp;
$activeFds[$aindex]{"fd"} = $conn;
$activeFds[$aindex]{"id"} = $user; # this is case sensitive in other parts of perlmud
$found = 1;
last;
}
}
if (!$found) {
$aindex = $#activeFds + 1;
$activeFds[$aindex]{"protocol"} = $tinyp;
$activeFds[$aindex]{"fd"} = $conn;
$activeFds[$aindex]{"id"} = $user; # this is case sensitive in other parts of perlmud
}
&sendActiveFdFile($aindex, $welcomeFile);
my($id, $n);
$n = $user;
$n =~ tr/A-Z/a-z/; # user is translated into lowercase only - #debug
&mud_tellActiveFd($aindex, "This mud last reloaded " . &timeFormat(time - $initialized) . ' ago.');
&TH::th_println();
if (!exists($playerIds{$n})) {
# player is new to mud, so create a player record
$id = pcreate(1,'pcreate',$n,$none); # object 1 is always the arch-wizard so can pcreate
}
$id = $playerIds{$n};
$activeFds[$aindex]{"id"} = $id;
&mud_login($id, $aindex);
return \& mud_loop;
}
sub mud_login
{
my($id, $aindex) = @_;
$now = time;
&addContents(int($objects[$id]{"home"}), $id); # put me in my home
if (!($objects[$objects[$id]{"location"}]{"flags"} & $grand) && !($objects[$id]{"flags"} & $dark)) { # grand or invis
&tellRoom($objects[$id]{"location"}, $none,
playerName($id) . " has just arrived.");
}
$mudPlayers+=1;
my $level = $levelNames{(($objects[$id]{"flags"} & $female) ? "female" : "male")}[$objects[$id]{"level"}];
if ($objects[$id]{"played"}>0) {
&mud_tellActiveFd($aindex, "Hello again, " . playerName($id) . "!");
&mud_tellActiveFd($aindex, "Your last game was " . &timeFormat($now - $objects[$id]{"last"}) . " ago.");
$objects[$id]{"flags"} &= ~$asleep; # invert asleep if set
$objects[$id]{"flags"} &= ~$deaf; # invert deaf if set
$objects[$id]{"flags"} &= ~$dumb; # invert dumb if set
$objects[$id]{"flags"} &= ~$blind; # invert blind if set
$objects[$id]{"flags"} &= ~$paralysed; # invert paralysed if set
# new stamina point for every 60 seconds outside game
my $kips=int((time - $objects[$id]{"last"})/60);
my $y = $objects[$id]{"stamina"} + $kips;
$objects[$id]{"stamina"} = ($objects[$id]{"maxstamina"}, $y)[$y<$objects[$id]{"maxstamina"}];
} else {
my $msg = "Welcome, " . ucfirst($objects[$id]{"name"}) . "! You are born a bouncing baby " . (($objects[$id]{"flags"} & $female) ? "girl" : "boy") . "!";
&mud_tellActiveFd($aindex, $msg);
}
&tellWizards(playerName($id) . " has entered MUD having played " . $objects[$id]{"played"} . " times before.");
$objects[$id]{"activeFd"} = $aindex;
$objects[$id]{"on"} = $now;
$objects[$id]{"last"} = $now;
$objects[$id]{"lastPing"} = $now;
$objects[$id]{"played"}+=1;
$objects[$id]{"prompt"}="*";
$objects[$id]{"prompt"}="(*)" if ($objects[$id]{"flags"} & $dark); # invis
#debug do TH scores for these I think
&mud_tellActiveFd($aindex, "Well done! You've managed 10 games so far without getting killed!") if ($objects[$id]{"played"}==10);
&mud_tellActiveFd($aindex, "Congratulations! Your 100th game!") if ($objects[$id]{"played"}==100);
&mud_tellActiveFd($aindex, "Amazing! This is your 1000th game! What dedication!") if ($objects[$id]{"played"}==1000);
&mud_tellActiveFd($aindex, "Yes, you DO get a message congratulating you on your 10000th game!") if ($objects[$id]{"played"}==10000);
&sendFile($id, $motdFile);
&mud_command($id, "look");
}
sub pcreate
{
my($me, $arg, $arg1, $arg2) = @_;
if (!&wizardTest($me)) {
&tellPlayer($me, "Sorry, only a wizard can do that.");
return;
}
if (($arg1 eq "") || ($arg2 eq "")) {
&tellPlayer($me, "Syntax: \@pcreate name = password");
return;
}
if (substr($arg1, 0, 1) eq "#") {
&tellPlayer($me, "Sorry, names cannot begin with #.");
return;
}
$_ = $arg1;
if (/\s/) {
&tellPlayer($me, "Sorry, names cannot contain spaces.");
return;
}
my($n);
$n = $arg1;
$n =~ tr/A-Z/a-z/;
if (exists($playerIds{$n})) {
&tellPlayer($me, "Sorry, that name is taken.");
return;
}
my $id=$none;
$id = &addObject($me, $arg1, $player);
$playerIds{$n} = $id;
$objects[$id]{"owner"} = $id;
$objects[$id]{"last"} = time; # set last game to now
$objects[$id]{"location"} = $nowhere; # create in nowhere
$objects[$id]{"home"} = 0; # set home to room 0
$objects[$id]{"score"} = 0;
$objects[$id]{"played"} = 0;
$objects[$id]{"maxstamina"} = rollChar();
$objects[$id]{"strength"} = rollChar();
$objects[$id]{"dexterity"} = rollChar();
$objects[$id]{"stamina"} = $objects[$id]{"maxstamina"};
$objects[$id]{"level"} = 0; # start as a novice
if ($allowBuild) {
$objects[$id]{"flags"} = ($builder);
}
$objects[$id]{"flags"} |= (int(rand(2))? $male : $female); # pick a random sex
return $id;
}
sub rollChar
{
# roll persona value attribute on 5d20
my $sum=0;
for (my $i=0; ($i<=4); $i++) {
$sum+=int(rand(20)+1); # 5d20
}
return ($sum > 90 ? 100 : $sum+10); # max 100, min 15
}
sub weighContents
{
# yield the total weight of all contents of type (usually thing)
my ($what, $type) = @_;
my $weight = 0;
my @list = split(/,/,$objects[$what]{"contents"});
# debug this should explode containers in containers but doesnt
foreach my $o (@list) {
if ($objects[$o]{"type"} == $type) {
$weight += $objects[$o]{"weight"};
}
}
return $weight;
}
sub maxObj
{
# returns max number of objects $what can contain
# returns 2 if there is no dexterity
#debug needs to handle bags etc in future using "extraobj"?
my ($what) = @_;
return int($objects[$what]{"dexterity"}/10+2);
}
sub howMany
{
# retruns how many $type objs $what contains
my ($what,$type) = @_;
my @list = split(/,/,$objects[$what]{"contents"});
my $count=0;
foreach my $o (@list) {
if ($objects[$o]{"type"} == $type) {
$count++;
}
}
return int($count);
}
sub startFight
{
my($me, $id) = @_;
# $me starts fight with $id
$fightsTable{$me}{$id}=$me;
$fightsTable{$id}{$me}=$me;
print "startFight [$me] vs [$id]\n" if ($debugMode);
}
sub endFight
{
my($me, $id) = @_;
# $me ends fight with $id
delete $fightsTable{$me}{$id};
delete $fightsTable{$id}{$me};
print "endFight [$me] vs [$id]\n" if ($debugMode);
}
sub stopFighting
{
# stops all fights in progress
#debug to be worked out - something should allow for flee
#debug see stop.fighting(fleeing) in MUD4.BCL and K.IFFY in MUD8.BCL
if (keys %fightsTable>0) {
# handle fights in progress
foreach my $x (keys %fightsTable) {
foreach my $y (keys %{$fightsTable{$x}}) {
# take a fight turn
my $xname = ($objects[$x]{"type"}==$player) ? &playerName($x) : "The " . $objects[$x]{"name"};
my $yname = ($objects[$y]{"type"}==$player) ? &playerName($y) : "The " . $objects[$y]{"name"};
&tellPlayer($y,"You can no longer fight " . $xname . "!") if ($objects[$y]{"type"}==$player);
#debug drop everything for player $y
&tellPlayer($x,"You can no longer fight " . $yname . "!") if ($objects[$x]{"type"}==$player);
#debug drop everything for player $x
&endFight($x,$y);
}
}
}
}
sub fightCheck
{
my ($me,$id) = @_;
return $none if !(defined $fightsTable{$me}); # no fight in me
if (defined $id) {
if (defined $fightsTable{$me}{$id}) {
return $fightsTable{$me}{$id}; # return the fight initiator
} else {
return $none;
}
}
my @combat = ();
foreach my $f (keys %{$fightsTable{$me}}) { # fight check
push (@combat,$f);
}
return \@combat; # return an array of who is being fought
}
sub addContents
{
my($addto, $add) = @_;
# adds item id $add to container $addto
# Whatever you do, don't let any commas get in here
$add =~ s/,//g;
if (length($objects[$addto]{"contents"}) > 0) {
$objects[$addto]{"contents"} .= "," . $add;
} else {
$objects[$addto]{"contents"} = $add;
}
$objects[$add]{"location"} = $addto;
}
sub addObject
{
my($maker, $name, $type) = @_;
my($id);
my($found);
$found = 0;
for ($id = 0; ($id <= $#objects); $id++) {
if ($objects[$id]{"type"} == $none) {
$found = 1;
last;
}
}
if (!$found) {
$id = $#objects + 1;
}
$objects[$id]{"name"} = $name;
$objects[$id]{"type"} = $type;
$objects[$id]{"activeFd"} = $none;
$objects[$id]{"owner"} = $maker;
&tellPlayer($maker, $objects[$id]{"name"} .
" has been created as #" . $id . ".");
return $id;
}
sub sendFile
{
my($id, $fname) = @_;
if ($objects[$id]{"activeFd"} ne $none) {
&sendActiveFdFile($objects[$id]{"activeFd"}, $fname);
}
}
sub sendActiveFdFile
{
my($i, $fname) = @_;
if (!open(IN, $fname)) {