PDA

View Full Version : Dom3 Perl Scripts


DrPraetorious
October 18th, 2006, 02:08 AM
This thread is hoped-for as a clearing house for useful dominions 3 perl-scripts.

Short-term plans call for a script to convert .csv files (a format accessible from all major spreadsheet programs) into modscript.

Here's a short one I just wrote that automatically selects semi-reasonable start locations on dom3 random maps. Usage:

StartFinder.pl [neighbors] [distance] < foo.map

StartFinder.pl [neighbors] [distance] < foo.map >> copy of foo.map

neighbors = (exactly) how many neighbors each start site will have. ATM the only terrain it cares about is that sea hexes don't count as neighbors for starts in land hexes, but this is easy to change (if people are worried about being stuck next to heavy cavalry hexes, and so forth.)

distance = minimum distance between start sites. The type of the route (through water, etc.) is ignored.

Disclaimer: This is NOT the most efficient way to manipulate a matrix like this! If you are tempted to adapt this code to do multivariate calculus or something, seek help immediately!

[spooiler] <font class="small">Code:</font><hr /><pre>
#!/usr/bin/perl
#Arguments are -
# minimum # of neighbors
# desired distance between start sites

$d_neigh = $ARGV[0];
$d_dist = $ARGV[1];
$max_sea = $ARGV[2];

while (&lt;STDIN&gt;)
{
chomp $_;
@line = split (/ /);

if (/terrain/){
if ($line[2] % 8 &gt;= 4){
$type[$line[1]] = -1;
}
else {
$type[$line[1]] = 0;
}
$dist{$line[1]}{$line[1]} = 0;
if ($line[1] &gt; $numprov){$numprov = $line[1];}
}

if (/neighbour/){
$adj{$line[1]}{$line[2]} = 1;
$adj{$line[2]}{$line[1]} = 1;
$dist{$line[1]}{$line[2]} = 1;
$dist{$line[2]}{$line[1]} = 1;
}
}

# Okay, now we need to know which provinces are eligible start-sites.
for ($i = 1; $i &lt;= $numprov; $i++){
$elig = 0; $same = 0;
for $j (keys %{$adj{$i}}){
if ($type[$i] &lt;= $type[$j]) {$elig++;}
if ($type[$i] == $type[$j]) {$same++;}
}
# print "$i,$elig\n";
if ($elig == $d_neigh){
$elig_list{$i} = 1;
}
if ($same == 0 &amp;&amp; $type[$i] == -1){print "--$i is a single province lake!\n";}
if ($same == 0 &amp;&amp; $type[$i] == 0){print "--$i is a single province island!\n";}
}

# Now we need to extend the adjacency list for each province out to $d_dist.

for ($i = 1; $i &lt;= $numprov; $i++){
for $j (keys %{$adj{$i}}){
for $k (keys %{$adj{$j}}){
if (!exists $dist{$i}{$k}){
$dist{$i}{$k} = 2;
$zone{2}{$i}{$k} = 1;
}
}}}


for ($l = 3; $l &lt;= $d_dist; $l++)
{
for ($i = 1; $i &lt;= $numprov; $i++){
for $j (keys %{$zone{$l-1}{$i}}){
for $k (keys %{$adj{$j}}){
if (!exists $dist{$i}{$k}){
$dist{$i}{$k} = $l;
$zone{$l}{$i}{$k} = 1;
}
}}}
}

for ($i = 1; $i &lt;= $numprov; $i++){
if (exists $elig_list{$i}){
for $j (keys %{$dist{$i}})
{
if (exists $elig_list{$j}){
$clashes{$i}++;
$clashlist{$i}{$j} = 1;
}
}
}
}


@startpriority = sort {$clashes{$a} &lt;=&gt; $clashes{$b}} keys %elig_list;

for $i (@startpriority){
if (!exists $excluded{$i}){
print "#start $i\n";
if ($type[$i] == -1){print "-- sea start\n";}
for $j (keys %{$clashlist{$i}}){
$excluded{$j} = 1;
}
}
}
</pre><hr />[/spoiler]

Gandalf Parker
October 18th, 2006, 12:47 PM
Since you are arraying neighbors Id like one that can find islands and ponds on a map. That would use arrays of neighbor and terrain (water with all neighbors land equals pond, land with all neighbors water equals island).

DrPraetorious
October 18th, 2006, 06:06 PM
I believe I've added the desired functionality - let me know if that is what you meant. Is there a way in the version of BB-code these forums use to collapse something so that it doesn't eat the entire page? I suppose I should probably make the script an attachment instead of just pasteing it inline.

Gandalf Parker
October 18th, 2006, 06:48 PM
I was hoping for a seperate program. Something I could run against a .map and it would tell me islands and lakes

DrPraetorious
October 18th, 2006, 09:21 PM
Oh http://forum.shrapnelgames.com/images/smilies/happy.gif

Attached.

Nerfix
October 19th, 2006, 02:14 PM
Perl hmmm, is it hard to learn?

Leif_-
October 19th, 2006, 02:31 PM
Nerfix said:
Perl hmmm, is it hard to learn?



Not really. Soul-endangering to learn, maybe, but not hard.

Nerfix
October 19th, 2006, 02:48 PM
Perfect. Ftgahn!

Gandalf Parker
October 19th, 2006, 03:33 PM
Perl is kindof a cross between basic and C+. With the best of both (altho "best" is an opinion) http://forum.shrapnelgames.com/images/smilies/happy.gif

Leif_-
October 19th, 2006, 04:17 PM
Gandalf Parker said:
Perl is kindof a cross between basic and C+. With the best of both (altho "best" is an opinion) http://forum.shrapnelgames.com/images/smilies/happy.gif



And the structure of neither.

thejeff
October 20th, 2006, 09:35 AM
Yeah, it has the structure of line noise.

By far my favorite for any kind of complex scripting or text processing though.

Gandalf Parker
October 20th, 2006, 09:52 AM
Dont ask me. I still do all my windows desktop and linux internet server programming in Basic.

Nerfix
October 20th, 2006, 09:53 AM
BASIC is wonderful.