<?php
function regex_trigger_tag($code = '')
{
//$code_orig = base64_encode($code);
$code = explode("\n", $code);
$lut_types = array(
'actions' => 'action.gif',
'ai' => 'ai.gif',
'animation' => 'animation.gif',
'camera' => 'camera.gif',
'cinematic' => 'camera.gif',
'conditions' => 'condition.gif',
'countdown' => 'timer.gif',
//'custom' => 'general.gif',
'destructible' => 'destructible.gif',
'dialog' => 'dialog.gif',
'else' => 'action.gif',
'environment' => 'environment.gif',
'events' => 'event.gif',
'for' => 'if.gif',
'game' => 'game.gif',
'hero' => 'hero.gif',
'if - conditions' => 'condition.gif',
'if' => 'if.gif',
'item' => 'item.gif',
'leaderboard' => 'quest.gif',
'loop' => 'action.gif',
//'map' => 'general.gif',
'melee' => 'melee.gif',
'multiboard' => 'quest.gif',
'player group' => 'playergroup.gif',
'player' => 'player.gif',
'quest' => 'quest.gif',
'selection' => 'selection',
'set' => 'set.gif',
'special' => 'animation.gif',
'then' => 'action.gif',
'time' => 'timer.gif',
//'trigger' => 'general.gif',
'unit group' => 'unitgroup.gif',
'unit' => 'unit.gif',
'visibility' => 'visibility.gif',
'wait' => 'wait.gif',
'--------' => 'comment.gif'
);
$image_default = 'general.gif';
$image_cond = 'if.gif';
$image_start = 'start.gif';
$image_tee = 'tree_tee.gif';
$image_vert = 'tree_vert.gif';
$image_el = 'tree_el.gif';
$image_blank = 'tree_spacer.gif';
//$image_prefix = $this->ipsclass->vars['img_url'] . '/trigger/';
$image_prefix = 'images/';
// Start an output buffer to catch all printed lines
// which happens to be a bit faster than concatenating onto a string.
ob_start();
print '<!--trigcode-->';
// Loop through the lines
$type_count = count($lut_types);
$is_start = true;
$is_cond = false;
for ($i = 0, $ic = count($code); $i < $ic; $i++)
{
// Get indentation depth (fast divide by 4)
$depth = strspn($code[$i], ' ') >> 2;
// Check for an empty line
if (($line = trim($code[$i])) == '')
{
continue;
}
// Check for first line
if ($depth == 0 && $is_start)
{
// Print the image
print '<!--trigim--><img src="' . $image_prefix . $image_start . '" alt="" /> <!--trigeim-->' . htmlspecialchars($line) . "<br />\n";
$is_start = false;
continue;
}
// Parse line type
$image = ($is_cond) ? $image_cond : $image_default;
reset($lut_types);
foreach ($lut_types as $key => $value)
{
if ($key == strtolower(substr($line, 0, strlen($key))))
{
// Found the correct line type
$image = $value;
// Conditions block?
$is_cond = ($key == 'conditions' || $key == 'if - conditions');
break;
}
}
// Print tree
for ($j = 0; $j < $depth; $j++)
{
print '<!--trigind--><img src="' . $image_prefix . 'tree_spacer.gif" alt="" /><!--trigeind-->';
}
// Print the image and code
print '<!--trigim--><img src="' . $image_prefix . $image . '" alt="" /> <!--trigeim-->' . htmlspecialchars($line) . "<br />\n";
}
print '<!--trigecode-->';
// Return contents of the output buffer
return ob_get_clean();
}
// Parses trigger HTML back into code
// By: PiRho (July 14, 2006)
// terralith@yahoo.com
function regex_trigger_revert($code)
{
// Replace tree/spacer images with spaces
$code = preg_replace('#<!--trigind-->.*?<!--trigeind-->#', ' ', $code);
// Remove extra images
return preg_replace('#<!--trigim-->.*?<!--trigeim-->#', '', str_replace('<br />', '', $code));
}
?>