Skip to content

Commit c7fbc37

Browse files
committed
Implement reading Back To Work and Patrol actions.
1 parent 91f807f commit c7fbc37

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

src/Analyzers/BodyAnalyzer.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,31 @@ protected function run()
361361
$this->chatMessages[] = new ChatMessage($this->currentTime, null, $message);
362362
}
363363
break;
364+
case self::COMMAND_PATROL:
365+
$start = $this->position - 1;
366+
$numUnits = ord($this->body[$this->position++]);
367+
$numWaypoints = ord($this->body[$this->position++]);
368+
$this->position++;
369+
$waypoints = [];
370+
// bytes for 10 waypoints are allocated. however, not
371+
// all of them are used, so we skip reading the ones
372+
// that are certainly empty.
373+
for ($i = 0; $i < $numWaypoints; $i++) {
374+
$waypoints[$i][0] = $this->readBody('f', 4);
375+
}
376+
$this->position += (10 - $numWaypoints) * 4;
377+
for ($i = 0; $i < $numWaypoints; $i++) {
378+
$waypoints[$i][1] = $this->readBody('f', 4);
379+
}
380+
$this->position += (10 - $numWaypoints) * 4;
381+
382+
$this->push(new Actions\PatrolAction(
383+
$this->rec,
384+
$this->currentTime,
385+
$this->readUnits($numUnits),
386+
$waypoints
387+
));
388+
break;
364389
case self::COMMAND_FORM_FORMATION:
365390
$count = ord($this->body[$this->position++]);
366391
$playerId = ord($this->body[$this->position++]);
@@ -657,6 +682,15 @@ protected function run()
657682
$marketId
658683
));
659684
break;
685+
case self::COMMAND_GO_BACK_TO_WORK:
686+
$this->position += 3;
687+
$unitId = $this->readBody('l', 4);
688+
689+
$this->push(new Actions\GoBackToWorkAction(
690+
$this->rec,
691+
$this->currentTime,
692+
$unitId
693+
));
660694
// multiplayer postgame data in UP1.4 RC2+
661695
case self::COMMAND_POSTGAME:
662696
$this->postGameData = $this->read(PostgameDataAnalyzer::class);

src/Model/Actions/GoBackToWorkAction.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,34 @@ class GoBackToWorkAction extends Action
1616
*/
1717
const ID = 0x80;
1818

19-
// GoBackToWork(uId=%d)
20-
private $unitId;
19+
/**
20+
* ID of the building that should ungarrison its units and send them back
21+
* to work.
22+
*
23+
* @var int
24+
*/
25+
public $unitId;
2126

2227
/**
2328
* Create a ...
2429
*
2530
* @param \RecAnalyst\RecordedGame $rec Recorded game instance.
2631
* @param int $time Recorded game instance.
2732
*/
28-
public function __construct(RecordedGame $rec, $time)
33+
public function __construct(RecordedGame $rec, $time, $unitId)
2934
{
3035
parent::__construct($rec, $time);
36+
37+
$this->unitId = $unitId;
38+
}
39+
40+
/**
41+
* Get a string representation of the action.
42+
*
43+
* @return string
44+
*/
45+
public function __toString()
46+
{
47+
return sprintf('GoBackToWork(unitId=%d)', $this->unitId);
3148
}
3249
}

src/Model/Actions/PatrolAction.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,19 @@ class PatrolAction extends Action
1616
*/
1717
const ID = 0x15;
1818

19-
// Patrol(num=%d, waypoints[%d]={%s})
19+
/**
20+
* IDs of the units to be patrolled.
21+
*
22+
* @var int[]
23+
*/
2024
private $units = [];
25+
26+
/**
27+
* Array of waypoints to pass by while patrolling. Each waypoint is a pair
28+
* of coordinates [$x, $y].
29+
*
30+
* @var float[][]
31+
*/
2132
private $waypoints = [];
2233

2334
/**
@@ -33,4 +44,22 @@ public function __construct(RecordedGame $rec, $time, $units, $waypoints)
3344
$this->units = $units;
3445
$this->waypoints = $waypoints;
3546
}
47+
48+
/**
49+
* Get a string representation of the action.
50+
*
51+
* @return string
52+
*/
53+
public function __toString()
54+
{
55+
return sprintf(
56+
'Patrol(units[%d]={%s}, waypoints[%d]={%s})',
57+
count($this->units),
58+
implode(', ', $this->units),
59+
count($this->waypoints),
60+
implode(', ', array_map(function ($w) {
61+
return vsprintf('{%.2f, %.2f}', $w);
62+
}, $this->waypoints))
63+
);
64+
}
3665
}

0 commit comments

Comments
 (0)