View Full Version : tool request (terrain math)
Gandalf Parker
March 18th, 2007, 02:05 PM
I COULD look at the how the terrain tools that have already been done managed the math, but I thought Id ask instead. For scenario creation could someone whip out a tool that will take a range of province numbers and mark them all nostart?
Yes I know the #nostart command but Id rather avoid making the .map file that large if I can avoid it.
Yes I know the #specstart command but that wont divide the map between AI territory and human player territory.
YaBasic preferred but anything that will run on linux or windows will work.
For ease of scenario planning I am presently choosing maps which look good for scenarios based on a "the whole top of the map" or "the whole bottom of the map". But IF someone really wanted to.... having the utility do something like 20% of the right side, or 20% of the left side, might be nice (not necessary if its a hassle)
Gandalf Parker
DrPraetorious
March 18th, 2007, 03:07 PM
Linux *or* windows or linux *and* windows?
I've touched this up for legibility. First, copy these scripts into textfiles (hence, "scriptfile").
<font class="small">Code:</font><hr /><pre>
BEGIN{low=ARGV[2]; high=ARGV[3]; mask=ARGV[4]; ARGC-=3;}
{pmode = 1;}
/#terrain/ && ($2 >= low && $2 <= high){print $1 " " $2 " " or($3,mask); pmode = 0;}
(pmode == 1){print;}
</pre><hr />
usage: awk -f <scriptfile> <mapfilename> <min> <max> <mask>
ex: awk -f makemask.awk 390_mp.map 1 12 1
The output will be a version of 390_mp.map where provinces 1-12 are now large.
You can use this to set any province mask you want on any range of provinces you want - I forget what the bitmask is for nostarts but you can use this to add ocean, for example.
If you want to strip out a bitmask, use this:
<font class="small">Code:</font><hr /><pre>
BEGIN{low=ARGV[2]; high=ARGV[3]; mask=ARGV[4]; ARGC-=3;}
{pmode = 1;}
/#terrain/ && ($2 >= low && $2 <= high){print $1 " " $2 " " and($3,compl(mask)); pmode = 0;}
(pmode == 1){print;}
</pre><hr />
Identical usage.
Gandalf Parker
March 18th, 2007, 03:23 PM
wow, thats cool. I didnt think to mention awk or sed.
That will work fine. Thanks.
Gandalf Parker
March 18th, 2007, 03:25 PM
Does it check to see if the bit is already set?
DrPraetorious
March 18th, 2007, 03:54 PM
Oh, yeah, both of those are awk scripts. You could do it with sed as well, of course, but I don't know the bitwise operators in sed.
No, it doesn't check - but it's a bitwise or, so nothing should change if the bit is set already (or isn't set already, depending.)
If you add something like:
<font class="small">Code:</font><hr /><pre>
if (and($3,mask)==mask){print mask " already set at " $2;}
</pre><hr />
It'll check bitmasks as well.
Kaljamaha
March 18th, 2007, 04:59 PM
I see the problem is already fixed, but here goes anyway...
I whipped up a small Perl script for this, I thought it would make a fun exercise (I haven't done Perl for some time). It became a lot bigger than I originally thought because I added some functionality to it. I wrote it in windows, so if you want to use it in Unix open it with nano or pico or something to change the line endings.
Just run the thing, it'll tell you what to do. I even tested the thing, so it shouldn't contain too many bugs. http://forum.shrapnelgames.com/images/smilies/wink.gif
K.
Gandalf Parker
April 15th, 2007, 01:17 PM
I love both the Awk and the Perl versions of this. And reading them I SHOULD be able to figure out my next request but I wasnt able to. (I HATE bitmath)
The problem is that the nostarts are hidden in the terrain bits now. If I generate a map for a 10 player game, then everyone joins, then it tries to generate the game. Only after everyone has joined will it blow up because there is no way to fit that many players on the map.
Can someone whip me out a simple script (yabasic, bash, awk, sed, perl) which will tell me how many nostarts I have. All it has to do is count them so I know how many players to limit the game to.
Gandalf Parker
DrPraetorious
April 15th, 2007, 03:43 PM
Change "mask" to the actual bitmask you want.
<font class="small">Code:</font><hr /><pre>
awk '/#terrain/ && (and($3,mask)==mask){print; i++;} END{print i " start locations";}' map.mp
</pre><hr />
I'm at my folks place so I can't check to see if it works.
Gandalf Parker
April 15th, 2007, 04:52 PM
It works great. Thanks again. http://forum.shrapnelgames.com/images/smilies/happy.gif
Gandalf Parker
May 1st, 2007, 03:40 PM
OK I thought we had this but so far I found a perl script that doesnt seem to work for me, and an online one that I cant use in lynx.
I want to package up some of the wrapped maps from PH's map generator but I notice that it has no nostarts. Can I get something (basic, awk, sed, whatever) that will nostart any province with fewer than 4 neighbors?
DrPraetorious
May 1st, 2007, 03:48 PM
I have a script that does that at home, I'll attach it when I get back from work.
Do you want to require anything of the four neighbours - like, they all have to be the same land/sea, or don't count wastelands, anything like that?
Gandalf Parker
May 1st, 2007, 04:28 PM
I would like them to be the same land/sea.
Thanks for asking.
edited: come to think of it. If its not too much bother Id rather nostart anyplace that would get stuck with neighbors of all the same bonuses.
water: 3 or more water neighbors.
land: 4 or more land neighbors. None of them water. And not all the same terrain (in other words refuse a mountain terrain surrounded by mountains or a swamp terrain surrounded by swamps
Id like the water to be 4 or more water and no land neighbors but so far Im having trouble getting PHs mapgen to make oceans
Edi
May 1st, 2007, 04:43 PM
Kaljamaha's script will output a text file that will tell you which province has how many neighbors, which provinces are startable and which are not and the output is tab-separated, so you can copy-paste it to a spreadsheet and examine at your leisure. It basically gives you a full bore terrain analysis and neighbor analysis out of a map file.
lch
May 1st, 2007, 04:54 PM
Gandalf Parker said: I want to package up some of the wrapped maps from PH's map generator but I notice that it has no nostarts. Can I get something (basic, awk, sed, whatever) that will nostart any province with fewer than 4 neighbors?
There are dozens of scripts for this...
DireAussie (http://users.on.net/~dfidge/cgi-bin/dom3map.pl) has a web page which does this, Jack (http://dominions.realites.org/) has that aswell and with source code (http://dominions.realites.org/JTmap.rar) available, too. There's a Python script in the Urrapand map (http://www.maladjustite.com/urraparrand.zip) which does this, although it would have to be changed a little so that the map file name becomes a parameter instead of being hard-coded. Finally, DrP has a done a perl script (http://www.shrapnelcommunity.com/threads/showflat.php?Number=486359#Post504573) for this aswell.
Gandalf Parker
May 1st, 2007, 05:59 PM
Great list. http://forum.shrapnelgames.com/images/smilies/happy.gif
I think you will see my name in the credits of most of those. But Im hoping for something that will work in linux, text mode, and can be automated as much as possible.
DireAussies doesnt seem to work in lynx (text browser)
Jacks online version is great and does work in lynx (I was surprised). I hadnt realized that he had added starting provinces. But it would need to be done manually I think. I might do it for the great big maps that I plan to offer very few of.
Jacks code seems to be windowsy.
The python code in the urrapand map does actually look to be workable. Thanks for mentioning it.
DrP's code was the first one I tried and didnt seem to get a result. Im sure its a fluke with my system.
I had understood that Ballbarian did one also but I havent found it yet.
So far it looks like Urrapand's python code is my best bet
DrPraetorious
May 1st, 2007, 09:15 PM
GP, this is the script I happened to have lying around - I can modify it to check that the neighbours have a diversity of bitmasks, but this might do your for your purposes.
Arguments are:
* Minimum # of neighbors (same land/sea)
* Minimum # of neighbors (same land/sea and NOT swamp, wasteland or deep sea)
* Bitmask to apply to provinces that don't meet the criterion (presumably you want the bitmask for nostart but I forget what that is.)
Made some slight modifications, had a few beers after work can easily make other changes in the morning if this doesn't suit.
e.g.:
$ perl GandStart.pl.txt 4 3 128 < 390mp_new.map
Gandalf Parker
May 1st, 2007, 10:35 PM
DrP, I get an error trying to grab that file.
DrPraetorious
May 1st, 2007, 11:20 PM
Weird.
Well, it's not long.
<font class="small">Code:</font><hr /><pre>
#!/usr/bin/perl
#Arguments are -
# minimum # of neighbors, not counting non-matching.
# minimum # of neighbours, not counting non-matching, wasteland, deep sea or swamp
# bitmask you want to apply.
$d_neigh = $ARGV[0];
$d_neigh2 = $ARGV[1];
$apply = $ARGV[2];
$forced = 0;
while (<STDIN>)
{
chomp $_;
@line = split (/ /);
$pmode = 1;
if (/^#terrain/){
$terrain[$line[1]] = $line[2];
if ($line[2] % 8 >= 4){
$type[$line[1]] = -1;
}
else {
$type[$line[1]] = 1;
}
$cool[$line[1]] = $type[$line[1]];
if ($line[2] % 128 >= 64 || $line[2] % 64 >= 32 || $line[2] % 4096 >= 2048){
$elig[$line[1]] = 2;
}
# Upshot
# - implies sea.
# + implies land.
# 2 implies a wasteland, swamp or deep sea.
$mask[$line[1]] = $line[2];
if ($line[1] > $numprov){$numprov = $line[1];}
$pmode = 0;
}
if (/^#neighbour/){
$adj{$line[1]}{$line[2]} = 1;
$adj{$line[2]}{$line[1]} = 1;
}
if ($pmode == 1){
print "$_\n";
}
}
# Okay, now we need to know which provinces are eligible start-sites.
for ($i = 1; $i <= $numprov; $i++){
$total = 0; $same = 0; $bad = 0;
for $j (keys %{$adj{$i}}){
if ($cool[$i] * $cool[$i] == 1) {$same++;}
if ($cool[$i] * $cool[$j] * $elig[$j] == 2) {$bad++;}
$total++;
}
# print "$i,$elig\n";
if ($same == 0 && $type[$i] == -1){print "-- $i is a single province lake!\n";}
if ($same == 0 && $type[$i] == 0){print "-- $i is a single province island!\n";}
if ($total == 0){print "-- $i is an isolated province!\n";}
if ($same < $d_neigh){$mask[$i] = $mask[$i] | $apply;}
if ($same - $bad < $d_neigh2){$mask[$i] = $mask[$i] | $apply;}
print "#terrain $i $mask[$i]\n";
}
</pre><hr />
Gandalf Parker
May 2nd, 2007, 12:22 AM
That works great. Thanks. http://forum.shrapnelgames.com/images/smilies/laugh.gif
vBulletin® v3.8.1, Copyright ©2000-2025, Jelsoft Enterprises Ltd.