Skip to content

Instantly share code, notes, and snippets.

@Nixtren
Last active October 12, 2016 14:48
Show Gist options
  • Save Nixtren/9793d67b89a7ff47ce74 to your computer and use it in GitHub Desktop.
Save Nixtren/9793d67b89a7ff47ce74 to your computer and use it in GitHub Desktop.
Nixtren MTA to SA-MP Map Converter
<?php
/*
Nixtren MTA to SA-MP Map Converter
Converts objects and remove world objects
Licensed under GPLv3 - https://github.com/Nixtren
GitHub Gist: https://gist.github.com/Nixtren/9793d67b89a7ff47ce74
Live demo: https://nixtren.net/samp/mapconverter/
This script was originally written for private use, so code elegance was not in mind.
Please don't use this for study purposes, as it doesn't follow any quality coding standards.
*/
error_reporting(E_ALL);
if(isset($_POST['map'])) $map = $_POST['map'];
else die("Error: Missing map parameter");
if(isset($_POST['streamDistance'])) $streamDistance = $_POST['streamDistance'];
else /*$streamDistance = "STREAMER_OBJECT_SD";*/ $streamDistance = "200.0";
$drawDistance = "0.0"; //$drawDistance = "STREAMER_OBJECT_DD";
if(!is_numeric($streamDistance)) die("Error: Parameter streamDistance is not numeric");
$separator = "\n";
$line = strtok($map, $separator);
$mapObjects = "<map edf:definitions=\"editor_main\">\n";
$mapRWO = "<map edf:definitions=\"editor_main\">\n";
$objectCount = 0;
$removeWorldObjectCount = 0;
while ($line !== false) // For each line
{
if (strpos($line,'<object') !== false) { $mapObjects .= $line; $objectCount++; }
if (strpos($line,'<removeWorldObject') !== false) { $mapRWO .= $line; $removeWorldObjectCount++; }
$line = strtok($separator);
}
if ($objectCount == 0 && $removeWorldObjectCount == 0) die ("Error: Map is empty");
$mapObjects .= "</map>";
$mapRWO .= "</map>";
$objXML_Objects = new xml2Array();
$objXML_RWO = new xml2Array();
$mapObjectsArray = $objXML_Objects->parse($mapObjects);
$mapRWOArray = $objXML_RWO->parse($mapRWO);
$objects_output = "// OnGameModeInit\n";
$rwo_objects_output = "// OnPlayerConnect\n";
for ($i = 0; $i < $objectCount; $i++)
{
$objectid = $mapObjectsArray[0]['children'][$i]['attrs']['ID'];
$modelid = $mapObjectsArray[0]['children'][$i]['attrs']['MODEL'];
$PosX = $mapObjectsArray[0]['children'][$i]['attrs']['POSX'];
$PosY = $mapObjectsArray[0]['children'][$i]['attrs']['POSY'];
$PosZ = $mapObjectsArray[0]['children'][$i]['attrs']['POSZ'];
$RotX = $mapObjectsArray[0]['children'][$i]['attrs']['ROTX'];
$RotY = $mapObjectsArray[0]['children'][$i]['attrs']['ROTY'];
$RotZ = $mapObjectsArray[0]['children'][$i]['attrs']['ROTZ'];
$objects_output .= "CreateDynamicObject($modelid, $PosX, $PosY, $PosZ, $RotX, $RotY, $RotZ, -1, -1, -1, $streamDistance, $drawDistance); // $objectid\n";
}
for ($i = 0; $i < $removeWorldObjectCount; $i++)
{
$objectid = $mapRWOArray[0]['children'][$i]['attrs']['ID'];
$modelid = $mapRWOArray[0]['children'][$i]['attrs']['MODEL'];
$lodid = $mapRWOArray[0]['children'][$i]['attrs']['LODMODEL'];
$radius = $mapRWOArray[0]['children'][$i]['attrs']['RADIUS'];
$PosX = $mapRWOArray[0]['children'][$i]['attrs']['POSX'];
$PosY = $mapRWOArray[0]['children'][$i]['attrs']['POSY'];
$PosZ = $mapRWOArray[0]['children'][$i]['attrs']['POSZ'];
$rwo_objects_output .= "RemoveBuildingForPlayer(playerid, $modelid, $PosX, $PosY, $PosZ, $radius); // $objectid\n";
if($lodid != 0) $rwo_objects_output .= "RemoveBuildingForPlayer(playerid, $lodid, $PosX, $PosY, $PosZ, $radius); // (LOD) $objectid\n";
}
class xml2Array // Taken from somewhere in the internet
{
var $arrOutput = array();
var $resParser;
var $strXmlData;
function parse($strInputXML)
{
$this->resParser = xml_parser_create();
xml_set_object($this->resParser, $this);
xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
xml_set_character_data_handler($this->resParser, "tagData");
$this->strXmlData = xml_parse($this->resParser, $strInputXML);
if (!$this->strXmlData)
{
//die();
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->resParser)), xml_get_current_line_number($this->resParser)));
}
xml_parser_free($this->resParser);
return $this->arrOutput;
}
function tagOpen($parser, $name, $attrs)
{
$tag = array(
"name" => $name,
"attrs" => $attrs
);
array_push($this->arrOutput, $tag);
}
function tagData($parser, $tagData)
{
if (trim($tagData))
{
if (isset($this->arrOutput[count($this->arrOutput) - 1]['tagData']))
{
$this->arrOutput[count($this->arrOutput) - 1]['tagData'] .= $tagData;
}
else
{
$this->arrOutput[count($this->arrOutput) - 1]['tagData'] = $tagData;
}
}
}
function tagClosed($parser, $name)
{
$this->arrOutput[count($this->arrOutput) - 2]['children'][] = $this->arrOutput[count($this->arrOutput) - 1];
array_pop($this->arrOutput);
}
}
?>
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="//nixtren.net/samp/mapconverter/css/bootstrap.css?source=gist" rel="stylesheet" type="text/css">
<title>Nixtren MTA to SA-MP Map Converter</title>
</head><body>
<div class="section">
<div class="container">
<div class="row">
<div class="col-md-6">
<p>CreateDynamicObject (Objects)</p>
<textarea class="form-control" rows="20"><?php echo htmlentities($objects_output); ?></textarea>
</div>
<div class="col-md-6">
<p>RemoveBuildingForPlayer (Remove World Objects)</p>
<textarea class="form-control" rows="20"><?php echo htmlentities($rwo_objects_output); ?></textarea>
</div>
</div>
</div>
</div>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment