Jack_Trowell |
January 14th, 2007 07:20 AM |
Re: Simple Terrain Calculator...
For those that prefer Perl, here's my similar function (from my perl dom library project).
The function return an array with the list of all types.
Quote:
# extract list of terrain types from the numeric code
sub get_terrain_types_from_code($); # function prototype, prevent warning when the function try to call itself recursively in strict mode
sub get_terrain_types_from_code($)
{
my $terrain_code = shift;
if ($terrain_code !~ /^[0-9]+$/)
{
error("[get_terrain_types_from_code] terrain code must be numeric value ('$terrain_code')");
}
my @terrain_types = ();
# terrain code is the sum of the successives individual terrain type code values
if ($terrain_code >= 4194304) # "edgemount" 4194304
{
# code >= 4194304 means that province type include "edgemount"
push (@terrain_types, "edgemount");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 4194304));
}
elsif ($terrain_code >= 2097152) # "priestsite" 2097152
{
# code >= 2097152 means that province type include "priestsite"
push (@terrain_types, "priestsite");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 2097152));
}
elsif ($terrain_code >= 1048576) # "bloodsite" 1048576
{
# now you should know how it works ... ^_^
push (@terrain_types, "bloodsite");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 1048576));
}
elsif ($terrain_code >= 524288) # "naturesite" 524288
{
# add the type to the list
push (@terrain_types, "naturesite");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 524288));
}
elsif ($terrain_code >= 262144) # "deathsite" 262144
{
# add the type to the list
push (@terrain_types, "deathsite");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 262144));
}
elsif ($terrain_code >= 131072) # "astralsite" 131072
{
# add the type to the list
push (@terrain_types, "astralsite");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 131072));
}
elsif ($terrain_code >= 65536) # "earthsite" 65536
{
# add the type to the list
push (@terrain_types, "earthsite");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 65536));
}
elsif ($terrain_code >= 32768) # "watersite" 32768
{
# add the type to the list
push (@terrain_types, "watersite");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 32768));
}
elsif ($terrain_code >= 16384) # "airsite" 16384
{
# add the type to the list
push (@terrain_types, "airsite");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 16384));
}
elsif ($terrain_code >= 8192) # "firesite" 8192
{
# add the type to the list
push (@terrain_types, "firesite");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 8192));
}
elsif ($terrain_code >= 4096) # "cave" 4096
{
# add the type to the list (at last : a true terrain type !)
push (@terrain_types, "cave");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 4096));
}
elsif ($terrain_code >= 2048) # "deep" 2048
{
# add the type to the list
push (@terrain_types, "deep");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 2048));
}
elsif ($terrain_code >= 1024) # "manysites" 1024
{
# add the type to the list
push (@terrain_types, "manysites");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 1024));
}
elsif ($terrain_code >= 512) # "nostart" 512
{
# add the type to the list
push (@terrain_types, "nostart");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 512));
}
elsif ($terrain_code >= 256) # "farm" 256
{
# add the type to the list
push (@terrain_types, "farm");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 256));
}
elsif ($terrain_code >= 128) # "forest" 128
{
# add the type to the list
push (@terrain_types, "forest");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 128));
}
elsif ($terrain_code >= 64) # "waste" 64
{
# add the type to the list
push (@terrain_types, "waste");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 64));
}
elsif ($terrain_code >= 32) # "swamp" 32
{
# add the type to the list
push (@terrain_types, "swamp");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 32));
}
elsif ($terrain_code >= 16) # "mountain" 16
{
# add the type to the list
push (@terrain_types, "mountain");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 16));
}
elsif ($terrain_code >= 8) # "somewater" 8
{
# add the type to the list
push (@terrain_types, "somewater");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 8));
}
elsif ($terrain_code >= 4) # "sea" 4
{
# add the type to the list
push (@terrain_types, "sea");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 4));
}
elsif ($terrain_code >= 2) # "large" 2
{
# add the type to the list
push (@terrain_types, "large");
# now get the other types (if any) by recursive call
push (@terrain_types, get_terrain_types_from_code($terrain_code - 2));
}
elsif ($terrain_code >= 1) # "small" 1
{
# add the type to the list
push (@terrain_types, "small");
# no other type, we're done
}
return @terrain_types;
}
|
|