CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
I want to make a perl-based MUD

 



zansafer
New User

Jun 3, 2010, 11:31 AM

Post #1 of 2 (582 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:

Code
 #!/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)


zansafer
New User

Jun 4, 2010, 9:09 AM

Post #2 of 2 (547 views)
Re: [zansafer] I want to make a perl-based MUD [In reply to] Can't Post

The thing I noticed while playing around with this is once I use the handler <$client>, everything then waits..the below test script proves this. I would expect to see a dot every second unless I type something...but what it actually does is when $line = <$client> is called...it waits for input. So, Im starting to think I need a different angle on this OR there may be some special way for $line to just grab the contents of handler <$client> without just waiting, and every loop look for $line to have value. Can anyone offer some education here? I very much appreciate it!

example


Code
  

$alive_counter = 0;
$a = 0;
while($a == 0){
print $client ".";
if(defined($line = <$client>){ ##Once this is called, it waits - unexpected
$alive_counter = 0;
print $client "You typed $line\r\n";
}
$alive_counter++;
if($alive_counter > 10){
print $client "You are disconnected\r\n";
$a = 1;
}
sleep 1;
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives