
zansafer
New User
Jun 3, 2010, 11:31 AM
Post #1 of 2
(418 views)
|
|
I want to make a perl-based MUD
|
Can't Post
|
|
Im not new to PERL, but Im very new to using TCP or any kind of networking using PERL. I usually use PERL for parsing logs and such(aka make my life easier). I really want to create my own MUD from scratch, for both personal enjoyment and learning - Im finding the hardest part is understanding where to start. I started using IO:SOCKET and created the below script that allows multiple users to connect and roam around a 4 room map. This is just a tester to figure out how to get 2 or more users connected with separate sessions - that piece works! So my next task to solve is, how to combine the multiusers into teh same environment - Ex. if user 1 walks into teh room where users 2 is, I want user 2's screen to display 'SoandSo entered the room'. Problem is with my script, it only processes and prints to the client when the client has entered input. Has anyone out there created a MUD before that might be able to push me in a direction. I think once I figure out how to create the enviroment where users can interact with each other and world events, the rest will just be the game itself which will be the fun part. Thanks for helping! The sample script Im playing with - its not really the way its going to look - just trying to figure out the TCP connections stuff: #!/bin/perl use IO::Socket; use Switch; $| = 1; $socket = 8000; my $sock = IO::Socket::INET->new( Localhost => 'localhost', Proto => 'tcp', Listen => 1, Reuse => 1, LocalPort => $socket, ); print "Could not create socket: port $socket\n" unless $sock; while ($client=$sock->accept()) { if( my $pid = fork) { print "Child Spawned : $pid"; push(@childs, $pid); } elsif( !defined $pid) { die "Cannot fork $!\n" } else { $client->autoflush(1); $host=$client->peerhost(); print "Connection received from: ", $host . "\n"; $current_room = 1; @Exits = ('E','S'); print $client "Welcome!\n"; while (defined ($line = <$client>)){ ### AT this point, script only reacts when user gives input..need to figure this out $line =~ s/\r\n//g; #remove the newline stuff $line =~ s/\W//g; #remove garbage the TCP initialization creates if(grep {m|$line|} @Exits){ switch($current_room){ case 1 {if($line eq 'E'){ $current_room = 2 } elsif($line eq 'S'){ $current_room = 4 }; print $client "1 - " . $current_room;} case 2 {if($line eq 'W'){ $current_room = 1 } elsif($line eq 'S'){ $current_room = 3 }; print $client "2 - " . $current_room;} case 3 {if($line eq 'N'){ $current_room = 2 } elsif($line eq 'W'){ $current_room = 4 }; print $client "3 - " . $current_room;} case 4 {if($line eq 'N'){ $current_room = 1 } elsif($line eq 'E'){ $current_room = 3 }; print $client "4 - " . $current_room;} else {print $client "Huhhh?!?\n"} } switch($current_room){ case 1 {print $client "Your in room 1. Exits [East, South]\r\n"; @Exits = ('E','S')} case 2 {print $client "Your in room 2. Exits [West, South]\r\n"; @Exits = ('W','S')} case 3 {print $client "Your in room 3. Exits [North, West]\r\n"; @Exits = ('N','W')} case 4 {print $client "Your in room 4. Exits [North, East]\r\n"; @Exits = ('N','E')} else {print "Huh?\n" } } } else { print $client "$line is an unrecognized direction!\r\n"; print $client "Valid directions:\r\n"; foreach(@Exits){ print $client "$_\r\n"; } } } exit(0); } }
(This post was edited by zansafer on Jun 3, 2010, 1:19 PM)
|