Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add trigger info analyzer #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions src/Analyzers/ScenarioTriggersAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace RecAnalyst\Analyzers;

use RecAnalyst\Model\Trigger;
use RecAnalyst\Model\TriggerEffect;
use RecAnalyst\Model\TriggerCondition;

class ScenarioTriggersAnalyzer extends Analyzer
{
public function run()
{
$triggers = [];

$numTriggers = $this->readHeader('l', 4);
for ($i = 0; $i < $numTriggers; $i += 1) {
$triggers[] = $this->readTrigger();
}

// Trigger order
$this->position += $numTriggers * 4;

return $triggers;
}

/**
* Read information about a single trigger.
*
* @return \RecAnalyst\Model\Trigger
*/
protected function readTrigger()
{
$trigger = new Trigger();
$trigger->enabled = $this->readHeader('l', 4) !== 0;
$trigger->looping = $this->header[$this->position] !== "\0";
$trigger->objective = $this->header[$this->position + 1] !== "\0";
$this->position += 2;
$trigger->descOrder = $this->readHeader('l', 4);
$this->position += 4;
$this->position += 4;
$descriptionLength = $this->readHeader('l', 4);
if ($descriptionLength > 0) {
$trigger->description = rtrim($this->readHeaderRaw($descriptionLength), "\0");
}
$nameLength = $this->readHeader('l', 4);
if ($nameLength > 0) {
$trigger->name = rtrim($this->readHeaderRaw($nameLength), "\0");
}
$numEffects = $this->readHeader('l', 4);
for ($i = 0; $i < $numEffects; $i += 1) {
$effect = $this->readTriggerEffect();
$trigger->addEffect($effect);
}

// effects order
$this->position += $numEffects * 4;

$numConditions = $this->readHeader('l', 4);
for ($i = 0; $i < $numConditions; $i += 1) {
$condition = $this->readTriggerCondition();
$trigger->addCondition($condition);
}
// condition order
$this->position += $numConditions * 4;

return $trigger;
}


/**
* Read information about a trigger effect.
*
* @return \RecAnalyst\Model\TriggerEffect
*/
protected function readTriggerEffect()
{
$effect = new TriggerEffect();
$effect->type = $this->readHeader('l', 4);
$effect->check = $this->readHeader('l', 4);
$effect->setAiGoal = $this->readHeader('l', 4);
$effect->amount = $this->readHeader('l', 4);
$effect->resource = $this->readHeader('l', 4);
$effect->diplomacy = $this->readHeader('l', 4);
$numSelectedObjects = $this->readHeader('l', 4);
$effect->unitLocation = $this->readHeader('l', 4);
$effect->unitType = $this->readHeader('l', 4);
$effect->playerSource = $this->readHeader('l', 4);
$effect->playerTarget = $this->readHeader('l', 4);
$effect->technology = $this->readHeader('l', 4);
$effect->textId = $this->readHeader('l', 4);
$effect->soundId = $this->readHeader('l', 4);
$effect->displayTime = $this->readHeader('l', 4);
$effect->triggerIndex = $this->readHeader('l', 4);
$effect->location = [
$this->readHeader('l', 4),
$this->readHeader('l', 4),
];
$effect->area = [
[$this->readHeader('l', 4), $this->readHeader('l', 4)],
[$this->readHeader('l', 4), $this->readHeader('l', 4)],
];
$effect->unitGroup = $this->readHeader('l', 4);
$effect->objectType = $this->readHeader('l', 4);
$effect->instructionPanel = $this->readHeader('l', 4);
if ($this->version->isHDPatch4) {
// HD Edition: trigger effects can change unit attack stances.
// I'd think HD could've just reused an other field here for
// backwards compatibility, but what do I know!
$effect->stance = $this->readHeader('l', 4);
}
$textLength = $this->readHeader('l', 4);
if ($textLength > 0) {
$effect->text = rtrim($this->readHeaderRaw($textLength), "\0");
}
$soundFileNameLength = $this->readHeader('l', 4);
if ($soundFileNameLength > 0) {
$effect->soundFileName = rtrim($this->readHeaderRaw($soundFileNameLength), "\0");
}
for ($i = 0; $i < $numSelectedObjects; $i++) {
$effect->unitIds[] = $this->readHeader('l', 4);
}

return $effect;
}

/**
* Read information about a trigger condition.
*
* @return \RecAnalyst\Model\TriggerCondition
*/
protected function readTriggerCondition()
{
$condition = new TriggerCondition();
$condition->type = $this->readHeader('l', 4);
$condition->check = $this->readHeader('l', 4);
$condition->amount = $this->readHeader('l', 4);
$condition->resource = $this->readHeader('l', 4);
$condition->unitObject = $this->readHeader('l', 4);
$condition->unitLocation = $this->readHeader('l', 4);
$condition->unitType = $this->readHeader('l', 4);
$condition->player = $this->readHeader('l', 4);
$condition->technology = $this->readHeader('l', 4);
$condition->timer = $this->readHeader('l', 4);
$this->position += 4;
$condition->area = [
[$this->readHeader('l', 4), $this->readHeader('l', 4)],
[$this->readHeader('l', 4), $this->readHeader('l', 4)],
];
$condition->unitGroup = $this->readHeader('l', 4);
$this->position += 4;
$condition->aiSignal = $this->readHeader('l', 4);
if ($this->version->isHDPatch4) {
$condition->reverse = $this->readHeader('l', 4);
$condition->unknown2 = $this->readHeader('l', 4);
}

return $condition;
}
}
96 changes: 96 additions & 0 deletions src/Model/Trigger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace RecAnalyst\Model;

/**
* Represents a trigger.
*/
class Trigger
{
/**
* List of trigger conditions.
*
* @var \RecAnalyst\Model\TriggerCondition[]
*/
private $conditions = [];
/**
* List of trigger effects.
*
* @var \RecAnalyst\Model\TriggerEffect[]
*/
private $effects = [];

/**
* True if the trigger is enabled, false if disabled.
*
* @var bool
*/
public $enabled;

/**
* True if the trigger is looping, false otherwise.
*
* @var bool
*/
public $looping;

/**
* True if the trigger is part of the scenario objective, false otherwise.
*
* @var bool
*/
public $objective;

/**
* [Todo]
*
* @var int
*/
public $descOrder;

/**
* Trigger description.
*
* @var string
*/
public $description;

/**
* Trigger name.
*
* @var string
*/
public $name;

/**
* Add a condition to this trigger.
*
* @param \RecAnalyst\Model\TriggerCondition $condition Condition.
* @return void
*/
public function addCondition(TriggerCondition $condition)
{
$this->conditions[] = $condition;
}

/**
* Add an effect to this trigger.
*
* @param \RecAnalyst\Model\TriggerEffect $condition Condition.
* @return void
*/
public function addEffect(TriggerEffect $effect)
{
$this->effects[] = $effect;
}

public function conditions()
{
return $this->conditions;
}

public function effects()
{
return $this->effects;
}
}
100 changes: 100 additions & 0 deletions src/Model/TriggerCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace RecAnalyst\Model;

/**
* Represents a trigger condition.
*/
class TriggerCondition
{
/**
* [Todo]
*
* @var int
*/
public $type;

/**
* [Todo]
*
* @var int
*/
public $check;

/**
* [Todo]
*
* @var int
*/
public $amount;

/**
* [Todo]
*
* @var int
*/
public $resource;

/**
* [Todo]
*
* @var int
*/
public $unitObject;

/**
* [Todo]
*
* @var int
*/
public $unitLocation;

/**
* [Todo]
*
* @var int
*/
public $unitType;

/**
* [Todo]
*
* @var int
*/
public $player;

/**
* [Todo]
*
* @var int
*/
public $technology;

/**
* [Todo]
*
* @var int
*/
public $timer;

/**
* [Todo]
*
* @var int[][]
*/
public $area;

/**
* [Todo]
*
* @var int
*/
public $unitGroup;

/**
* [Todo]
*
* @var int
*/
public $aiSignal;
}
Loading