Skip to content

Commit

Permalink
update mapviewer to return unique map hash for
Browse files Browse the repository at this point in the history
  • Loading branch information
HarpyWar committed Mar 27, 2019
1 parent d28e922 commit d642cda
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
64 changes: 44 additions & 20 deletions inc/NFK/MapViewer/MapViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use NFK\MapViewer\Type\TLocationText;
use NFK\MapViewer\Type\TMapEntry;
use NFK\MapViewer\Type\TMapObj;
use NFK\MapViewer\MapObject\SimpleObject;

use NFK\MapViewer\GD\Graphics;

Expand Down Expand Up @@ -354,8 +355,10 @@ public function LoadMap()
}



// generate and return map binary string
public function GetMapBytes()
// $strip_dynamic_data return malformed map data to get a correct map hash from demo
public function GetMapBytes($strip_dynamic_data = false)
{
$writer = new StreamWriter();

Expand Down Expand Up @@ -384,29 +387,44 @@ public function GetMapBytes()
// write bricks
for ($y = 0; $y < $this->Header->MapSizeY; $y++)
for ($x = 0; $x < $this->Header->MapSizeX; $x++)
$writer->putByte( isset($this->Bricks[$x][$y]) ? $this->Bricks[$x][$y] : 0 );

// write objects
foreach($this->Objects as $tmapobj)
{
$brick = ( isset($this->Bricks[$x]) && isset($this->Bricks[$x][$y]) )
? $this->Bricks[$x][$y]
: 0;

if ($strip_dynamic_data)
{
if ($brick == SimpleObject::RedRespawn() || $brick == SimpleObject::BlueRespawn())
$brick = SimpleObject::Respawn();
}

$writer->putByte($brick);
}

if (!$strip_dynamic_data)
{
$writer->putBool($tmapobj->active);
// write objects
foreach($this->Objects as $tmapobj)
{
$writer->putBool($tmapobj->active);
$writer->putByte(0x03); // byte alignment

$writer->putWord($tmapobj->x);
$writer->putWord($tmapobj->y);
$writer->putWord($tmapobj->length);
$writer->putWord($tmapobj->dir);
$writer->putWord($tmapobj->wait);
$writer->putWord($tmapobj->targetname);
$writer->putWord($tmapobj->target);
$writer->putWord($tmapobj->orient);
$writer->putWord($tmapobj->nowanim);
$writer->putWord($tmapobj->special);

$writer->putWord($tmapobj->x);
$writer->putWord($tmapobj->y);
$writer->putWord($tmapobj->length);
$writer->putWord($tmapobj->dir);
$writer->putWord($tmapobj->wait);
$writer->putWord($tmapobj->targetname);
$writer->putWord($tmapobj->target);
$writer->putWord($tmapobj->orient);
$writer->putWord($tmapobj->nowanim);
$writer->putWord($tmapobj->special);

$writer->putByte($tmapobj->objtype);
$writer->putByte($tmapobj->objtype);
$writer->putByte(0x03); // byte alignment
}
}

// write map palette
if ( $this->graphics->getResource('i_custom_palette') )
{
Expand All @@ -426,7 +444,7 @@ public function GetMapBytes()

$writer->putString($pal_bz);
}

// write locations
if ($this->Locations && count($this->Locations) > 0)
{
Expand Down Expand Up @@ -455,6 +473,12 @@ public function GetMapBytes()
return $writer->stream();
}

// return map hash without dynamic info, to get the same unique hash for a map from demos
public function GetHash()
{
return md5( $this->GetMapBytes(true) );
}




Expand Down
2 changes: 1 addition & 1 deletion mods/demomap.mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// read demo map
$nfkMap = new MapViewer($file);
$nfkMap->LoadMap();
$hash = md5($nfkMap->GetMapBytes());
$hash = $nfkMap->GetHash();

$imageFile = ($isThumb ? MAPS_THUMBS : MAPS_IMAGES) . $hash . ($isThumb ? '.jpg' : '.png');

Expand Down

0 comments on commit d642cda

Please sign in to comment.