This is done by total beginner and just for practicing some basic stuff, so it might look quite stupid. Still decide to publish here just to get some feedback. I'll start practice curses next, so maybe get something useful done someday, if I have time and find understandable tutorials.
CODE
#!/usr/bin/perl -w
use strict;
use Term::ReadKey;
use Switch;
@ARGV == 3 or die "Usage: map.pl continent x y\n";
my ($continent, $max_x, $max_y) = &select_continent($ARGV[0]);
my $x = $ARGV[1];
my $y = $ARGV[2];
&screen;
sub screen_properties{
if ($x < 1 || $x > $max_x || $y < 1 || $y > $max_y) {
die "Invalid cordinates. Minimimum is 1x1 and maximum for ",$continent," is ",$max_x,"x",$max_y,"\n";
}
my ($cols,$rows) = GetTerminalSize();
my $offset = sprintf("%0.f", $x - ($cols / 2));
my $length = $cols - 1;
my $start = sprintf("%0.f", $y - ($rows / 2));
my $end = $y + ($rows - ($y - $start)) - 6;
if ($length > $max_x) {
$length = $max_x;
our $space_l = sprintf("%0.f", ($cols - $max_x) / 2);
our $space_r = $cols - $max_x - $space_l - 1;
}
else {
our $space_l = 0;
our $space_r = 0;
}
if ($start != abs($start)) {$end += abs($start)}
if ($end > $max_y) {
$start -= ($end - $max_y);
$end = $max_y;
}
if ($offset < 1) {$offset = 1}
if ($offset + $length > $max_x) {$offset -= ($offset + $length - $max_x)}
return ($cols,$rows,$offset,$length,$start,$end);
}
sub screen {
our ($cols,$rows,$offset,$length,$start,$end) = &screen_properties();
system ("clear");
&print_line("top");
my $count = 1;
open(MAP_FILE, $continent) or die "For some reason I can't find map file\n";
while (<MAP_FILE>) {
if ($count >= $start && $count <= $end) {
&print_line("left");
my $map_line = substr $_, $offset, $length;
my $line_length = length($map_line);
for(my $round=1; $round <= $line_length; $round++) {
my $char_to_print = substr $map_line, $round, 1;
my $test = $offset + $round + 1;
if ($count == $y && $x == $test) {$char_to_print = "*"}
&print_colored_char($char_to_print);
}
&print_line("right");
}
if ($count >= $end) {
&print_line("bottom");
print $continent,": ",$max_x," x ", $max_y, "\nScreen: ",$cols," x ",$rows, "\n";
exit;
}
$count++;
}
}
sub print_colored_char {
switch ($_[0]) {
case ["f","F","v"] {print "\033[0;1;32m"}
case ["j"] {print "\033[0;0;32m"}
case ["~","r","R","l"] {print "\033[0;0;34m"}
case ["S"] {print "\033[0;1;36m"}
case ["z"] {print "\033[0;1;33m"}
case ["."] {print "\033[0;0;32m"}
case ["^"] {print "\033[0;1;35m"}
case ["h","H"] {print "\033[0;0;35m"}
case [","] {print "\033[0;1;33m"}
case ["-","|","+","/","\\","="] {print "\033[0;1;30m"}
case ["!"] {print "\033[0;0;37m"}
case ["?","*"] {print "\033[0;1;37m"}
case ["C","#"] {print "\033[0;1;30m"}
case ["L","@"] {print "\033[0;1;31m"}
case ["x","s","V"] {print "\033[0;0;31m"}
case ["d","b","y"] {print "\033[0;0;33m"}
else {print "\033[0m"}
}
print $_[0];
}
sub print_line {
#my ($cols,$rows) = GetTerminalSize();
print "\033[41;1;1m";
switch ($_[0])
{
case ["top"]
{
for(my $round=1; $round <= $::cols; $round++) {print " "}
print "\033[0m\n";
}
case ["left"]
{
print " \033[0m";
for(my $round=1; $round <= $::space_l; $round++) {print " "}
}
case ["right"]
{
print "\033[0m";
for(my $round=1; $round <= $::space_r; $round++) {print " "};
print "\033[41;1;1m \033[0m\n";
}
case ["bottom"]
{
for(my $round=1; $round <= $::cols; $round++) {print " "}
print "\033[0m\n";
}
}
}
sub select_continent {
switch ($_[0]) {
case ["laenor","lae"] {return ("laenor","827","781")}
case ["rothikgen","roth"] {return ("rothikgen","480","480")}
case ["lucentium","luc"] {return ("lucentium","700","500")}
case ["desolathya","deso"] {return ("desolathya","540","530")}
case ["furnachia","furn"] {return ("furnachia","440","480")}
case ["renardy","ren"] {return ("renardy","168","86")}
else {die "No such continent\nUse [lae]nor, [roth]ikgen, [luc]entium, [furn]achia, [deso]lathya or [ren]ardy\n"}
}
}
use strict;
use Term::ReadKey;
use Switch;
@ARGV == 3 or die "Usage: map.pl continent x y\n";
my ($continent, $max_x, $max_y) = &select_continent($ARGV[0]);
my $x = $ARGV[1];
my $y = $ARGV[2];
&screen;
sub screen_properties{
if ($x < 1 || $x > $max_x || $y < 1 || $y > $max_y) {
die "Invalid cordinates. Minimimum is 1x1 and maximum for ",$continent," is ",$max_x,"x",$max_y,"\n";
}
my ($cols,$rows) = GetTerminalSize();
my $offset = sprintf("%0.f", $x - ($cols / 2));
my $length = $cols - 1;
my $start = sprintf("%0.f", $y - ($rows / 2));
my $end = $y + ($rows - ($y - $start)) - 6;
if ($length > $max_x) {
$length = $max_x;
our $space_l = sprintf("%0.f", ($cols - $max_x) / 2);
our $space_r = $cols - $max_x - $space_l - 1;
}
else {
our $space_l = 0;
our $space_r = 0;
}
if ($start != abs($start)) {$end += abs($start)}
if ($end > $max_y) {
$start -= ($end - $max_y);
$end = $max_y;
}
if ($offset < 1) {$offset = 1}
if ($offset + $length > $max_x) {$offset -= ($offset + $length - $max_x)}
return ($cols,$rows,$offset,$length,$start,$end);
}
sub screen {
our ($cols,$rows,$offset,$length,$start,$end) = &screen_properties();
system ("clear");
&print_line("top");
my $count = 1;
open(MAP_FILE, $continent) or die "For some reason I can't find map file\n";
while (<MAP_FILE>) {
if ($count >= $start && $count <= $end) {
&print_line("left");
my $map_line = substr $_, $offset, $length;
my $line_length = length($map_line);
for(my $round=1; $round <= $line_length; $round++) {
my $char_to_print = substr $map_line, $round, 1;
my $test = $offset + $round + 1;
if ($count == $y && $x == $test) {$char_to_print = "*"}
&print_colored_char($char_to_print);
}
&print_line("right");
}
if ($count >= $end) {
&print_line("bottom");
print $continent,": ",$max_x," x ", $max_y, "\nScreen: ",$cols," x ",$rows, "\n";
exit;
}
$count++;
}
}
sub print_colored_char {
switch ($_[0]) {
case ["f","F","v"] {print "\033[0;1;32m"}
case ["j"] {print "\033[0;0;32m"}
case ["~","r","R","l"] {print "\033[0;0;34m"}
case ["S"] {print "\033[0;1;36m"}
case ["z"] {print "\033[0;1;33m"}
case ["."] {print "\033[0;0;32m"}
case ["^"] {print "\033[0;1;35m"}
case ["h","H"] {print "\033[0;0;35m"}
case [","] {print "\033[0;1;33m"}
case ["-","|","+","/","\\","="] {print "\033[0;1;30m"}
case ["!"] {print "\033[0;0;37m"}
case ["?","*"] {print "\033[0;1;37m"}
case ["C","#"] {print "\033[0;1;30m"}
case ["L","@"] {print "\033[0;1;31m"}
case ["x","s","V"] {print "\033[0;0;31m"}
case ["d","b","y"] {print "\033[0;0;33m"}
else {print "\033[0m"}
}
print $_[0];
}
sub print_line {
#my ($cols,$rows) = GetTerminalSize();
print "\033[41;1;1m";
switch ($_[0])
{
case ["top"]
{
for(my $round=1; $round <= $::cols; $round++) {print " "}
print "\033[0m\n";
}
case ["left"]
{
print " \033[0m";
for(my $round=1; $round <= $::space_l; $round++) {print " "}
}
case ["right"]
{
print "\033[0m";
for(my $round=1; $round <= $::space_r; $round++) {print " "};
print "\033[41;1;1m \033[0m\n";
}
case ["bottom"]
{
for(my $round=1; $round <= $::cols; $round++) {print " "}
print "\033[0m\n";
}
}
}
sub select_continent {
switch ($_[0]) {
case ["laenor","lae"] {return ("laenor","827","781")}
case ["rothikgen","roth"] {return ("rothikgen","480","480")}
case ["lucentium","luc"] {return ("lucentium","700","500")}
case ["desolathya","deso"] {return ("desolathya","540","530")}
case ["furnachia","furn"] {return ("furnachia","440","480")}
case ["renardy","ren"] {return ("renardy","168","86")}
else {die "No such continent\nUse [lae]nor, [roth]ikgen, [luc]entium, [furn]achia, [deso]lathya or [ren]ardy\n"}
}
}