PDA

View Full Version : Can you think of how to improve this?


evocation
08-02-2003, 03:26 AM
Try running this code in perl, making sure you have an ANSI driver installed first...ANSI.SYS might work.

This code has been tested on ActivePERL under WINDOWS ME and works fine. I am only after advice on how to improve the speed.


here is the first part of the code:

#!c:\perl\bin\perl.exe

use Win32::Console; #Windows specific

sub drawscreen
{

print "\e[1;1H"; # ANSI Escape Seq - requires Windows 95 ANSI.SYS driver to be installed

my $a;
my $b;
$a = 0;
$b = 0;

while($a<16)
{

while($b<64)
{

if($s_map[$a][$b] eq "empty")
{

print " ";

}
else
{

print " ";

}

$b++;

}

print "\n";
$a++;
$b = 0;

}

print "\e[17;1" . "H";
print "\e[K";
print " EN: ";
print "\e[17;6" . "H";
print "$ps_energy";

print "\e[17;11" . "H";
print " HP: ";
print "\e[17;16" . "H";
print int($ps_hpnow) . "/$ps_hpmax";

print "\e[17;23" . "H";
print " AC: ";
print "\e[17;28" . "H";
print "$ps_ac";

print "\e[17;33" . "H";
print " DC: ";
print "\e[17;38" . "H";
print "$ps_dc";

print "\e[17;43" . "H";
print " Level: ";
print "\e[17;51" . "H";
print "$s_level/$s_xp";

print "\e[19;1" . "H";

foreach (@x_message)
{

print "> $_\n";

}

print "\e[$s_atrow;$s_atcol" . "H";
print "@";
print "\e[$s_atrow;$s_atcol" . "H";

}

sub Win32ReadKey
{

return(undef) if(!$win32_keybrd->GetEvents);
my @event;
@event = $win32_keybrd->Input();
return(undef) if(!defined($event[0]) || ($event[0] ne "1") || !$event[1] || !$event[5]);
return(chr($event[5]));

}

# Define globals / defaults

$|=1;

$win32_keybrd;
$win32_mod = "Win32::Console";
die if (eval "use $win32_mod");
$win32_keybrd = Win32::Console->new(Win32::Console->STD_INPUT_HANDLE);

$x_points=16;
@x_message;
$x_message[0]="";$x_message[1]="";$x_message[2]="";$x_message[3]="";$x_message[4]="";

$ps_ac=50;
$ps_dc=0;
$ps_energy=500;
$ps_hpmax=10;
$ps_hpnow=10;
$ps_metab=0.01;

$ps_agility=10;
$ps_endurance=10;
$ps_perception=10;
$ps_strength=10;

$ps_agility_max=18;
$ps_endurance_max=18;
$ps_perception_max=18;
$ps_strength_max=18;

$ps_agility_min=3;
$ps_endurance_min=3;
$ps_perception_min=3;
$ps_strength_min=3;

$s_level=1;
$s_turn=0;
$s_xp=0;

$s_atcol = "1";
$s_atrow = "1";
@s_map;


# Load game from file or create raw map


{

my $a;
my $b;
$a = 0;
$b = 0;

while($a<16)
{

while($b<64)
{

$s_map[$a][$b] = "empty";
$b++;

}

$a++;
$b = 0;

}

}

# Input game character stats

{

my $x_select;
$x_select=1;
CHARAA:
print "\e[2J";

print "\e[2;4H";
print " Character points:";
print "\e[2;28H";
print $x_points;

print "\e[4;4H";
print "a. Agility:";
print "\e[4;28H";
print $ps_agility;

print "\e[5;4H";
print "b. Endurance:";
print "\e[5;28H";
print $ps_endurance;

print "\e[6;4H";
print "c. Perception:";
print "\e[6;28H";
print $ps_perception;

print "\e[7;4H";
print "d. Strength:";
print "\e[7;28H";
print $ps_strength;

if($x_select == 1) { print "\e[4;2H" . "\e[7m" . "x" . "\e[0m" . "\e[4;4H"; }
if($x_select == 2) { print "\e[5;2H" . "\e[7m" . "x" . "\e[0m" . "\e[5;4H"; }
if($x_select == 3) { print "\e[6;2H" . "\e[7m" . "x" . "\e[0m" . "\e[6;4H"; }
if($x_select == 4) { print "\e[7;2H" . "\e[7m" . "x" . "\e[0m" . "\e[7;4H"; }

if($x_points == 0) { print "\e[24;4H" . "Press space to continue"; }
CHARAB:
$keypress;
$keypress="";

for(;;)
{

$keypress=Win32ReadKey();
last unless($keypress eq "");

}

if($keypress == 8)
{

if($x_select == 1)
{

if($ps_agility < $ps_agility_max && $x_points > 0)
{

$ps_agility+=1;
$x_points--;
goto CHARAA;

}
else
{

goto CHARAB;

}

}
if($x_select == 2)
{

if($ps_endurance < $ps_endurance_max && $x_points > 0)
{

$ps_endurance+=1;
$x_points--;
goto CHARAA;

}
else
{

goto CHARAB;

}

}
if($x_select == 3)
{

if($ps_perception < $ps_perception_max && $x_points > 0)
{

$ps_perception+=1;
$x_points--;
goto CHARAA;

}
else
{

goto CHARAB;

}

}
if($x_select == 4)
{

if($ps_strength < $ps_strength_max && $x_points > 0)
{

$ps_strength+=1;
$x_points--;
goto CHARAA;

}
else
{

goto CHARAB;

}

}

}

elsif($keypress == 2)
{

if($x_select == 1)
{

if($ps_agility > $ps_agility_min)
{

$ps_agility-=1;
$x_points++;
goto CHARAA;

}
else
{

goto CHARAB;

}

}
if($x_select == 2)
{

if($ps_endurance > $ps_endurance_min)
{

$ps_endurance-=1;
$x_points++;
goto CHARAA;

}
else
{

goto CHARAB;

}

}
if($x_select == 3)
{

if($ps_perception > $ps_perception_min)
{

$ps_perception-=1;
$x_points++;
goto CHARAA;

}
else
{

goto CHARAB;

}

}
if($x_select == 4)
{

if($ps_strength > $ps_strength_min)
{

$ps_strength-=1;
$x_points++;
goto CHARAA;

}
else
{

goto CHARAB;

}

}

}

elsif($keypress eq "a")
{

$x_select = 1;
goto CHARAA;

}
elsif($keypress eq "b")
{

$x_select = 2;
goto CHARAA;

}
elsif($keypress eq "c")
{

$x_select = 3;
goto CHARAA;

}
elsif($keypress eq "d")
{

$x_select = 4;
goto CHARAA;

}
elsif($keypress eq " " && $x_points == 0)
{

goto GAMERU;

}
else
{

goto CHARAB;

}

}

evocation
08-02-2003, 03:28 AM
add this second part to the end of the first...


# Game Cycle


GAMERU:
print "\e[2J";

{
WPOINA:
&drawscreen;
WPOINB:
$keypress="";

for(;;)
{

$keypress=Win32ReadKey();
last unless($keypress eq "");

}

if($keypress eq "5" || $keypress eq ".")
{

goto WPOINC;

}

if($keypress eq "8")
{

if($s_atrow < 2)
{

goto WPOINB;

}
else
{

$s_atrow -= 1;
goto WPOINC;

}

}

if($keypress eq "2")
{

if($s_atrow > 15)
{

goto WPOINB;

}
else
{

$s_atrow += 1;
goto WPOINC;

}

}

if($keypress eq "6")
{

if($s_atcol > 63)
{

goto WPOINB;

}
else
{

$s_atcol += 1;
goto WPOINC;

}

}

if($keypress eq "4")
{

if($s_atcol < 2)
{

goto WPOINB;

}
else
{

$s_atcol -= 1;
goto WPOINC;

}

}

if($keypress eq "7")
{

if($s_atrow < 2)
{

goto WPOINB;

}
if($s_atcol < 2)
{

goto WPOINB;

}
else
{

$s_atrow -= 1;
$s_atcol -= 1;
goto WPOINC;

}

}

if($keypress eq "3")
{

if($s_atrow > 15)
{

goto WPOINB;

}
if($s_atcol > 63)
{

goto WPOINB;

}
else
{

$s_atrow += 1;
$s_atcol += 1;
goto WPOINC;

}

}

if($keypress eq "9")
{

if($s_atrow < 2)
{

goto WPOINB;

}
if($s_atcol > 63)
{

goto WPOINB;

}
else
{

$s_atrow -= 1;
$s_atcol += 1;
goto WPOINC;

}

}

if($keypress eq "1")
{

if($s_atrow > 15)
{

goto WPOINB;

}
if($s_atcol < 2)
{

goto WPOINB;

}
else
{

$s_atrow += 1;
$s_atcol -= 1;
goto WPOINC;

}

}

goto WPOINB;
WPOINC:
$ps_energy--;
$s_turn++;

if($ps_hpnow < $ps_hpmax)
{

$ps_hpnow += $ps_metab;

}

##shift @x_message;
##push @x_message, "";

goto WPOINA;

}

jamessan
08-02-2003, 12:40 PM
--edit-- Obviously I need to remember how to read the origninal post before I reply.

EscapeCharacter
08-03-2003, 05:39 AM
less gotos, youll thank me later

Smerdyakov
08-03-2003, 12:56 PM
jamessan, it looks like you didn't read the message. He says it works fine and he is just looking for optimization suggestions.

evocation, why are you using a "scripting language" like Perl if you are interested in blazing fast speed?

inkedmn
08-03-2003, 08:31 PM
Originally posted by Smerdyakov
evocation, why are you using a "scripting language" like Perl if you are interested in blazing fast speed?

ok, looks like YOU didn't read the message. he's not looking for "blazing fast speed", he's wanting to make it faster than it is (if possible).

Smerdyakov
08-03-2003, 11:08 PM
That was meant to be exaggeration for effect, to highlight how ridiculous it is to care about speed of Perl programs.

inkedmn
08-04-2003, 10:38 PM
how is it ridiculous? speed is a valid concern for just about any application, not just those written in C* or Assembly.

Smerdyakov
08-04-2003, 10:39 PM
It's ridiculous because the most obvious thing to do is not use Perl.