Skip to content

Instantly share code, notes, and snippets.

@Xymph
Last active February 16, 2022 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xymph/5aab927475f1a8e3eadb87e9eb59a920 to your computer and use it in GitHub Desktop.
Save Xymph/5aab927475f1a8e3eadb87e9eb59a920 to your computer and use it in GitHub Desktop.
Searches an extracted TEXTMAP lump (from a Doom UDMF map) for the vertices belonging to a specified sector number and computes its approximate center position, allowing to hover to that coordinate in the layout in an editor supporting UDMF (which DeePsea doesn't)
#!/usr/bin/php
<?php
// TEXTMAP sector position script for DoomWiki.org, by Frans P. de Vries (Xymph)
$usage = "Usage: {$argv[0]} [-v] TEXTMAP-file sector-id\n";
// check input parameters
$verbose = false;
if (isset($argv[1]) && $argv[1] == '-v') {
$verbose = true;
unset($argv[1]);
$argc--;
$argv = array_merge($argv);
}
if ($argc != 3) {
echo $usage;
exit(1);
}
// load TEXTMAP file
if (($text = @file_get_contents($argv[1])) === false) {
echo $argv[1] . ": unable to read\n";
exit(1);
}
$sectid = intval($argv[2]);
// initialize stats
$stats = array(
'vertex' => 0,
'linedef' => 0,
'sidedef' => 0,
'sector' => 0,
);
// process all stats types
foreach ($stats as $type => $stat) {
$stats[$type] = preg_match_all("/^{$type}[^{=]+{[^}]+}/ms", $text, $matches);
if ($type == 'vertex')
$vertexes = $matches[0];
else if ($type == 'linedef')
$linedefs = $matches[0];
else if ($type == 'sidedef')
$sidedefs = $matches[0];
else if ($type == 'sector')
$sectors = $matches[0];
}
//print_r($vertexes);
//print_r($linedefs);
//print_r($sidedefs);
//print_r($sectors);
echo $sectors[$sectid]."\n\n";
$posx = $posy = array();
// find sidedefs belonging to sector
$sides = array();
foreach ($sidedefs as $sidid => $sidedef) {
if (preg_match("/sector *= *\b$sectid\b/", $sidedef))
$sides[] = $sidid;
}
if ($verbose)
echo "Sidedefs: ".print_r($sides,true);
// find linedefs & vertexes belonging to sidedefs
$lines = $verts = array();
foreach ($sides as $sideid) {
foreach ($linedefs as $linid => $linedef) {
if (preg_match("/side\w+ *= *\b$sideid\b/", $linedef)) {
$lines[] = $linid;
if (preg_match("/v1 *= *\b(\d+)\b/", $linedef, $match))
$verts[] = $match[1];
if (preg_match("/v2 *= *\b(\d+)\b/", $linedef, $match))
$verts[] = $match[1];
}
}
}
if ($verbose) {
echo "Linedefs: ".print_r($lines,true);
echo "Vertexes: ".print_r($verts,true);
}
// find vertex positions
foreach ($verts as $vert) {
//print_r($vertexes[$vert]);
if (preg_match("/x *= *(-?\d+)\.\d+/", $vertexes[$vert], $match))
$posx[] = $match[1];
if (preg_match("/y *= *(-?\d+)\.\d+/", $vertexes[$vert], $match))
$posy[] = $match[1];
}
if ($verbose) {
echo "PosX: ".print_r($posx,true);
echo "PosY: ".print_r($posy,true);
}
// compute average of X & Y position
echo "approx. position: x = ".round(array_sum($posx)/count($posx)).
", y = ".round(array_sum($posy)/count($posy))."\n";
// vim:set noexpandtab tabstop=2 softtabstop=2 shiftwidth=2:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment