0 | |
#/usr/local/bin/perl -w
|
|
0 |
#!/usr/bin/perl -w
|
1 | 1 |
|
2 | 2 |
# noit o' mnain worb - fungeoid language based on brownian motion
|
3 | |
# v1.1 Jul 19 2000 Chris Pressey, Cat's Eye Technologies
|
4 | |
|
5 | |
# Copyright (c)2000, Cat's Eye Technologies.
|
|
3 |
# v2007.1123 Chris Pressey, Cat's Eye Technologies
|
|
4 |
|
|
5 |
# Copyright (c)2000-2007, Chris Pressey, Cat's Eye Technologies.
|
6 | 6 |
# All rights reserved.
|
7 | |
#
|
|
7 |
#
|
8 | 8 |
# Redistribution and use in source and binary forms, with or without
|
9 | 9 |
# modification, are permitted provided that the following conditions
|
10 | 10 |
# are met:
|
11 | |
#
|
12 | |
# Redistributions of source code must retain the above copyright
|
13 | |
# notice, this list of conditions and the following disclaimer.
|
14 | |
#
|
15 | |
# Redistributions in binary form must reproduce the above copyright
|
16 | |
# notice, this list of conditions and the following disclaimer in
|
17 | |
# the documentation and/or other materials provided with the
|
18 | |
# distribution.
|
19 | |
#
|
20 | |
# Neither the name of Cat's Eye Technologies nor the names of its
|
21 | |
# contributors may be used to endorse or promote products derived
|
22 | |
# from this software without specific prior written permission.
|
23 | |
#
|
24 | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
25 | |
# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
26 | |
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
27 | |
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
28 | |
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
|
29 | |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
30 | |
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
31 | |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
32 | |
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
33 | |
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
34 | |
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
35 | |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
36 | |
# POSSIBILITY OF SUCH DAMAGE.
|
37 | |
|
38 | |
# usage: [perl] worb[.pl] worb-playfield-file
|
|
11 |
#
|
|
12 |
# 1. Redistributions of source code must retain the above copyright
|
|
13 |
# notices, this list of conditions and the following disclaimer.
|
|
14 |
# 2. Redistributions in binary form must reproduce the above copyright
|
|
15 |
# notices, this list of conditions, and the following disclaimer in
|
|
16 |
# the documentation and/or other materials provided with the
|
|
17 |
# distribution.
|
|
18 |
# 3. Neither the names of the copyright holders nor the names of their
|
|
19 |
# contributors may be used to endorse or promote products derived
|
|
20 |
# from this software without specific prior written permission.
|
|
21 |
#
|
|
22 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
23 |
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES INCLUDING, BUT NOT
|
|
24 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
25 |
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
26 |
# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
27 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
28 |
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
29 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
30 |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
31 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
32 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
33 |
# POSSIBILITY OF SUCH DAMAGE.
|
|
34 |
|
|
35 |
# usage: [perl] worb[.pl] [-delay ms] worb-playfield-file
|
39 | 36 |
# requirements: ANSI terminal emulation, for animation.
|
40 | 37 |
|
41 | 38 |
# history: v1.0 Jul 5 2000 - original release.
|
|
43 | 40 |
# optimized display routine for ANSI
|
44 | 41 |
# optimized is_bobule_at (cached)
|
45 | 42 |
# relicensed & released on web site
|
|
43 |
# v2007.1123 - adapted to use Console::Virtual
|
|
44 |
# - added strict qw(vars refs subs)
|
|
45 |
# - added delay in ms cmdline option
|
|
46 |
# - fixed hashbang line
|
|
47 |
# - updated BSD license (no "REGENTS")
|
|
48 |
|
|
49 |
use strict qw(vars refs subs);
|
|
50 |
|
|
51 |
# This allows us to keep Console::Virtual in same directory as script
|
|
52 |
BEGIN { use File::Basename; push @INC, dirname($0); }
|
|
53 |
|
|
54 |
# Uncomment these lines to use specific display/input/color drivers.
|
|
55 |
# BEGIN { $Console::Virtual::setup{display} = 'ANSI'; }
|
|
56 |
# BEGIN { $Console::Virtual::setup{input} = 'Teletype'; }
|
|
57 |
# BEGIN { $Console::Virtual::setup{color} = 'ANSI16'; }
|
|
58 |
|
|
59 |
use Console::Virtual 2007.1122
|
|
60 |
qw(getkey display gotoxy clrscr clreol
|
|
61 |
normal inverse bold update_display color);
|
|
62 |
|
|
63 |
# This lets us do sub-second sleeps, if Time::HiRes is available.
|
|
64 |
my $sleep = sub($) { sleep(shift); };
|
|
65 |
my $found_time_hires = 0;
|
|
66 |
foreach my $c (@INC)
|
|
67 |
{
|
|
68 |
$found_time_hires = 1 if -r "$c/Time/HiRes.pm";
|
|
69 |
}
|
|
70 |
if ($found_time_hires) {
|
|
71 |
require Time::HiRes;
|
|
72 |
$sleep = sub($) { Time::HiRes::sleep(shift); };
|
|
73 |
}
|
46 | 74 |
|
47 | 75 |
### GLOBALS ###
|
48 | 76 |
|
49 | |
@bobule = ();
|
50 | |
@source = ();
|
51 | |
@sink = ();
|
52 | |
@playfield = ();
|
53 | |
|
54 | |
@bobule_at_cache = ();
|
55 | |
|
56 | |
$x = 0; $y = 0;
|
|
77 |
my @bobule = ();
|
|
78 |
my @source = ();
|
|
79 |
my @sink = ();
|
|
80 |
my @playfield = ();
|
|
81 |
|
|
82 |
my @bobule_at_cache = ();
|
|
83 |
|
|
84 |
my $x = 0;
|
|
85 |
my $y = 0;
|
|
86 |
|
|
87 |
my $maxx = 1;
|
|
88 |
my $maxy = 1;
|
|
89 |
|
|
90 |
my $delay = 100;
|
57 | 91 |
|
58 | 92 |
### SUBS ###
|
59 | 93 |
|
60 | 94 |
sub draw_playfield
|
61 | 95 |
{
|
62 | |
printf "%c[1;1H", 27; # gotoxy 1,1
|
63 | 96 |
my $i; my $j; my $p;
|
64 | 97 |
for($j = 0; $j <= $maxy; $j++)
|
65 | 98 |
{
|
|
99 |
gotoxy(1, $j+1);
|
66 | 100 |
for($i = 0; $i <= $maxx; $i++)
|
67 | 101 |
{
|
68 | 102 |
if ($p = is_bobule_at($i,$j))
|
69 | 103 |
{
|
70 | |
if ($p == 1) { print '.'; }
|
71 | |
elsif ($p >= 2 and $p <= 3) { print 'o'; }
|
72 | |
elsif ($p >= 4 and $p <= 6) { print 'O'; }
|
73 | |
elsif ($p >= 7 and $p <= 10) { print '0'; }
|
74 | |
else { print '@'; }
|
|
104 |
if ($p == 1) { display '.'; }
|
|
105 |
elsif ($p >= 2 and $p <= 3) { display 'o'; }
|
|
106 |
elsif ($p >= 4 and $p <= 6) { display 'O'; }
|
|
107 |
elsif ($p >= 7 and $p <= 10) { display '0'; }
|
|
108 |
else { display '@'; }
|
75 | 109 |
} else
|
76 | 110 |
{
|
77 | |
print $playfield[$i][$j];
|
|
111 |
display($playfield[$i][$j] or ' ');
|
78 | 112 |
}
|
79 | 113 |
}
|
80 | |
print "\n";
|
81 | 114 |
}
|
82 | 115 |
}
|
83 | 116 |
|
|
108 | 141 |
|
109 | 142 |
### MAIN ###
|
110 | 143 |
|
|
144 |
while ($ARGV[0] =~ /^\-\-?(.*?)$/)
|
|
145 |
{
|
|
146 |
my $opt = $1;
|
|
147 |
shift @ARGV;
|
|
148 |
if ($opt eq 'delay')
|
|
149 |
{
|
|
150 |
$delay = 0+shift @ARGV;
|
|
151 |
}
|
|
152 |
else
|
|
153 |
{
|
|
154 |
die "Unknown command-line option --$opt";
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
my $line;
|
|
159 |
|
111 | 160 |
open PLAYFIELD, $ARGV[0];
|
112 | 161 |
while(defined($line = <PLAYFIELD>))
|
113 | 162 |
{
|
|
138 | 187 |
}
|
139 | 188 |
close PLAYFIELD;
|
140 | 189 |
|
141 | |
printf "%c[2J", 27; # clear screen
|
142 | |
|
|
190 |
clrscr();
|
|
191 |
color('white', 'black');
|
143 | 192 |
draw_playfield();
|
144 | |
|
145 | |
$start_time = time();
|
146 | |
$tick = 1;
|
147 | |
while(1)
|
|
193 |
update_display();
|
|
194 |
|
|
195 |
my $new_x;
|
|
196 |
my $new_y;
|
|
197 |
|
|
198 |
while (1)
|
148 | 199 |
{
|
149 | 200 |
my $bobule; my $pole;
|
150 | 201 |
foreach $bobule (@bobule)
|
|
153 | 204 |
if ($bobule->[2] == 2 or $bobule->[2] == 4 or $bobule->[2] == 7 or $bobule->[2] == 11)
|
154 | 205 |
{
|
155 | 206 |
my $p = $bobule->[2];
|
156 | |
printf "%c[%d;%dH", 27, $bobule->[1]+1, $bobule->[0]+1;
|
157 | |
if ($p == 2) { print 'o'; }
|
158 | |
elsif ($p == 4) { print 'O'; }
|
159 | |
elsif ($p == 7) { print '0'; }
|
160 | |
elsif ($p == 11) { print '@'; }
|
|
207 |
gotoxy($bobule->[0]+1, $bobule->[1]+1);
|
|
208 |
if ($p == 2) { display 'o'; }
|
|
209 |
elsif ($p == 4) { display 'O'; }
|
|
210 |
elsif ($p == 7) { display '0'; }
|
|
211 |
elsif ($p == 11) { display '@'; }
|
161 | 212 |
}
|
162 | 213 |
$new_x = $bobule->[0] + int(rand(1) * 3)-1;
|
163 | 214 |
$new_y = $bobule->[1] + int(rand(1) * 3)-1;
|
|
168 | 219 |
next if $playfield[$new_x][$new_y] eq 'v' and $bobule->[1] > $new_y;
|
169 | 220 |
next if $new_x == $bobule->[0] and $new_y == $bobule->[1];
|
170 | 221 |
print chr(7) if $playfield[$new_x][$new_y] eq '!';
|
171 | |
printf "%c[%d;%dH%s", 27, $bobule->[1]+1, $bobule->[0]+1,
|
172 | |
$playfield[$bobule->[0]][$bobule->[1]];
|
|
222 |
gotoxy($bobule->[0]+1, $bobule->[1]+1);
|
|
223 |
display $playfield[$bobule->[0]][$bobule->[1]];
|
173 | 224 |
$bobule_at_cache[$bobule->[0]][$bobule->[1]] = 0;
|
174 | 225 |
$bobule->[0] = $new_x;
|
175 | 226 |
$bobule->[1] = $new_y;
|
176 | 227 |
$bobule_at_cache[$bobule->[0]][$bobule->[1]] = 1;
|
177 | |
printf "%c[%d;%dH.", 27, $bobule->[1]+1, $bobule->[0]+1;
|
|
228 |
gotoxy($bobule->[0]+1, $bobule->[1]+1);
|
|
229 |
display '.';
|
178 | 230 |
$bobule->[2] = 1;
|
179 | 231 |
}
|
180 | 232 |
foreach $pole (@source)
|
|
194 | 246 |
$bobule[$q] = $bobule[$#bobule]; pop @bobule;
|
195 | 247 |
}
|
196 | 248 |
}
|
197 | |
# track_time();
|
198 | |
}
|
199 | |
|
200 | |
sub track_time
|
201 | |
{
|
202 | |
$tick++;
|
203 | |
if ($tick > 1000)
|
204 | |
{
|
205 | |
$total_time = time() - $start_time;
|
206 | |
$fps = int(1000 / $total_time);
|
207 | |
die "Total time: $total_time seconds, approx fps: $fps\n";
|
208 | |
}
|
|
249 |
update_display();
|
|
250 |
&$sleep($delay / 1000);
|
209 | 251 |
}
|
210 | 252 |
|
211 | 253 |
### END ###
|