Skip to content

Commit 91f807f

Browse files
committed
Implement reading Stop, Work, Buy/Sell, Garrison, Gather Point, Repair, Unload, Wall actions
1 parent f29d1c6 commit 91f807f

12 files changed

+591
-61
lines changed

src/Analyzers/BodyAnalyzer.php

Lines changed: 194 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,30 @@ protected function run()
274274
$this->readUnits($unitCount)
275275
));
276276
break;
277+
case self::COMMAND_STOP:
278+
$count = ord($this->body[$this->position++]);
279+
$this->push(new Actions\StopAction(
280+
$this->rec,
281+
$this->currentTime,
282+
$this->readUnits($count)
283+
));
284+
break;
285+
case self::COMMAND_WORK:
286+
$this->position += 3;
287+
$targetId = $this->readBody('l', 4);
288+
$count = ord($this->body[$this->position++]);
289+
$this->position += 3;
290+
$x = $this->readBody('f', 4);
291+
$y = $this->readBody('f', 4);
292+
$this->push(new Actions\WorkAction(
293+
$this->rec,
294+
$this->currentTime,
295+
$targetId,
296+
$x,
297+
$y,
298+
$this->readUnits($count)
299+
));
300+
break;
277301
case self::COMMAND_MOVE:
278302
$playerId = ord($this->body[$this->position++]);
279303
$this->position += 2;
@@ -288,6 +312,24 @@ protected function run()
288312
$this->readUnits($count)
289313
));
290314
break;
315+
case self::COMMAND_CREATE:
316+
$this->position++;
317+
$objectCategory = $this->readBody('v', 2);
318+
$playerId = ord($this->body[$this->position++]);
319+
$this->position += 3;
320+
$x = $this->readBody('f', 4);
321+
$y = $this->readBody('f', 4);
322+
$z = $this->readBody('f', 4);
323+
$this->push(new Actions\CreateAction(
324+
$this->rec,
325+
$this->currentTime,
326+
$playerId,
327+
$objectCategory,
328+
$x,
329+
$y,
330+
$z
331+
));
332+
break;
291333
case self::COMMAND_UNIT_AI_STATE:
292334
$numUnits = ord($this->body[$this->position++]);
293335
$stance = ord($this->body[$this->position++]);
@@ -300,9 +342,9 @@ protected function run()
300342
break;
301343
// player resign
302344
case self::COMMAND_RESIGN:
303-
$playerIndex = ord($this->body[$this->position]);
304-
$playerNumber = ord($this->body[$this->position + 1]);
305-
$dropped = ord($this->body[$this->position + 2]);
345+
$playerIndex = ord($this->body[$this->position++]);
346+
$playerNumber = ord($this->body[$this->position++]);
347+
$dropped = ord($this->body[$this->position++]);
306348

307349
$this->push(new Actions\ResignAction(
308350
$this->rec,
@@ -312,14 +354,27 @@ protected function run()
312354
$dropped
313355
));
314356

315-
$this->position += 3;
316357
$player = $playersByIndex[$playerIndex];
317358
if ($player && $player->resignTime === 0) {
318359
$player->resignTime = $this->currentTime;
319360
$message = sprintf('%s resigned', $player->name);
320361
$this->chatMessages[] = new ChatMessage($this->currentTime, null, $message);
321362
}
322363
break;
364+
case self::COMMAND_FORM_FORMATION:
365+
$count = ord($this->body[$this->position++]);
366+
$playerId = ord($this->body[$this->position++]);
367+
$this->position += 1;
368+
$formation = $this->readBody('l', 4);
369+
370+
$this->push(new Actions\FormFormationAction(
371+
$this->rec,
372+
$this->currentTime,
373+
$playerId,
374+
$formation,
375+
$this->readUnits($count)
376+
));
377+
break;
323378
// researches
324379
case self::COMMAND_RESEARCH:
325380
$this->position += 3;
@@ -387,11 +442,44 @@ protected function run()
387442
case self::COMMAND_GAME:
388443
$this->processGameAction();
389444
break;
445+
case self::COMMAND_BUILD_WALL:
446+
$count = ord($this->body[$this->position++]);
447+
$playerId = ord($this->body[$this->position++]);
448+
$x1 = ord($this->body[$this->position++]);
449+
$y1 = ord($this->body[$this->position++]);
450+
$x2 = ord($this->body[$this->position++]);
451+
$y2 = ord($this->body[$this->position++]);
452+
$this->position += 1; // Padding
453+
$objectType = $this->readBody('v', 2);
454+
$this->position += 2; // Padding
455+
$this->position += 4; // Always -1
456+
457+
$this->push(new Actions\BuildWallAction(
458+
$this->rec,
459+
$this->currentTime,
460+
$playerId,
461+
$objectType,
462+
[$x1, $y1],
463+
[$x2, $y2],
464+
$this->readUnits($count)
465+
));
466+
break;
467+
case self::COMMAND_CANCEL_BUILD:
468+
$this->position += 3;
469+
$objectId = $this->readBody('l', 4);
470+
$playerId = $this->readBody('l', 4);
471+
$this->push(new Actions\CancelBuildAction(
472+
$this->rec,
473+
$this->currentTime,
474+
$playerId,
475+
$objectId
476+
));
477+
break;
390478
// AI trains unit
391479
case self::COMMAND_MAKE:
392-
$this->position += 9;
393-
$playerId = -1;
394-
$objectId = -1;
480+
$this->position += 3;
481+
$objectId = $this->readBody('l', 4);
482+
$playerId = $this->readBody('v', 2);
395483
$unitType = $this->readBody('v', 2);
396484

397485
$this->push(new Actions\MakeAction(
@@ -470,6 +558,105 @@ protected function run()
470558
$this->position += 8;
471559
}
472560
break;
561+
case self::COMMAND_REPAIR:
562+
$count = ord($this->body[$this->position++]);
563+
$this->position += 2;
564+
$targetId = $this->readBody('l', 4);
565+
$this->push(new Actions\RepairAction(
566+
$this->rec,
567+
$this->currentTime,
568+
$targetId,
569+
$this->readUnits($count)
570+
));
571+
break;
572+
case self::COMMAND_UNLOAD:
573+
$count = ord($this->body[$this->position++]);
574+
$this->position += 2;
575+
$x = $this->readBody('f', 4);
576+
$y = $this->readBody('f', 4);
577+
$flag = ord($this->body[$this->position++]);
578+
$this->position += 3;
579+
$unitType = $this->readBody('l', 4);
580+
581+
$this->push(new Actions\UnloadAction(
582+
$this->rec,
583+
$this->currentTime,
584+
$x,
585+
$y,
586+
$flag,
587+
$unitType,
588+
$this->readUnits($count)
589+
));
590+
break;
591+
case self::COMMAND_UNIT_ORDER:
592+
$count = ord($this->body[$this->position++]);
593+
$this->position += 2;
594+
$targetId = $this->readBody('l', 4);
595+
$action = ord($this->body[$this->position++]);
596+
$this->position += 3;
597+
$x = $this->readBody('f', 4);
598+
$y = $this->readBody('f', 4);
599+
$parameter = $this->readBody('l', 4);
600+
601+
$this->push(new Actions\UnitOrderAction(
602+
$this->rec,
603+
$this->currentTime,
604+
$x,
605+
$y,
606+
$targetId,
607+
$action,
608+
$parameter,
609+
$this->readUnits($count)
610+
));
611+
break;
612+
case self::COMMAND_SET_GATHER_POINT:
613+
$count = ord($this->body[$this->position++]);
614+
$this->position += 2;
615+
$targetId = $this->readBody('l', 4);
616+
$targetType = $this->readBody('l', 4);
617+
$x = $this->readBody('f', 4);
618+
$y = $this->readBody('f', 4);
619+
620+
$this->push(new Actions\SetGatherPointAction(
621+
$this->rec,
622+
$this->currentTime,
623+
$targetId,
624+
$targetType,
625+
$x,
626+
$y,
627+
$this->readUnits($count)
628+
));
629+
break;
630+
case self::COMMAND_SELL_COMMODITY:
631+
$playerId = ord($this->body[$this->position++]);
632+
$resourceType = ord($this->body[$this->position++]);
633+
$amount = ord($this->body[$this->position++]);
634+
$marketId = $this->readBody('l', 4);
635+
636+
$this->push(new Actions\SellCommodityAction(
637+
$this->rec,
638+
$this->currentTime,
639+
$playerId,
640+
$resourceType,
641+
$amount,
642+
$marketId
643+
));
644+
break;
645+
case self::COMMAND_BUY_COMMODITY:
646+
$playerId = ord($this->body[$this->position++]);
647+
$resourceType = ord($this->body[$this->position++]);
648+
$amount = ord($this->body[$this->position++]);
649+
$marketId = $this->readBody('l', 4);
650+
651+
$this->push(new Actions\BuyCommodityAction(
652+
$this->rec,
653+
$this->currentTime,
654+
$playerId,
655+
$resourceType,
656+
$amount,
657+
$marketId
658+
));
659+
break;
473660
// multiplayer postgame data in UP1.4 RC2+
474661
case self::COMMAND_POSTGAME:
475662
$this->postGameData = $this->read(PostgameDataAnalyzer::class);

src/Model/Actions/BuildWallAction.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,46 @@ class BuildWallAction extends Action
1717
const ID = 0x69;
1818

1919
// BuildWall(num=%d, x1=%d, y1=%d, x2=%d, y2=%d, pId=%d, oId=%d, qId=%d)
20-
private $num;
21-
private $x1;
22-
private $y1;
23-
private $x2;
24-
private $y2;
25-
private $playerId;
26-
private $objectId;
20+
public $playerId;
21+
public $objectId;
22+
public $from;
23+
public $to;
24+
public $units;
2725

2826
/**
2927
* Create a ...
3028
*
3129
* @param \RecAnalyst\RecordedGame $rec Recorded game instance.
3230
* @param int $time Recorded game instance.
3331
*/
34-
public function __construct(RecordedGame $rec, $time)
32+
public function __construct(RecordedGame $rec, $time, $playerId, $objectType, $from, $to, $builders)
3533
{
3634
parent::__construct($rec, $time);
35+
36+
$this->playerId = $playerId;
37+
$this->objectType = $objectType;
38+
$this->from = $from;
39+
$this->to = $to;
40+
$this->builders = $builders;
41+
}
42+
43+
/**
44+
* Get a string representation of the action.
45+
*
46+
* @return string
47+
*/
48+
public function __toString()
49+
{
50+
return sprintf(
51+
'BuildWall(playerId=%d, objectType=%d, from={%d,%d}, to={%d,%d}, builders[%d]={%s})',
52+
$this->playerId,
53+
$this->objectType,
54+
$this->from[0],
55+
$this->from[1],
56+
$this->to[0],
57+
$this->to[1],
58+
count($this->builders),
59+
implode(', ', $this->builders)
60+
);
3761
}
3862
}

src/Model/Actions/BuyCommodityAction.php

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,63 @@ class BuyCommodityAction extends Action
1616
*/
1717
const ID = 0x7b;
1818

19-
// BuySellAttr(uId=%d, pId=%d, attr=%d, amt=%d)
20-
private $unitId;
21-
private $playerId;
22-
private $attribute;
23-
private $amount;
19+
/**
20+
* Player who is buying resources.
21+
*
22+
* @var int
23+
*/
24+
public $playerId;
25+
26+
/**
27+
* Resource type to buy.
28+
*
29+
* @var int
30+
*/
31+
public $resourceType;
32+
33+
/**
34+
* Amount of the resource to buy, in hundreds.
35+
*
36+
* @var int
37+
*/
38+
public $amount;
39+
40+
/**
41+
* Market to buy the resources at.
42+
*
43+
* @var int
44+
*/
45+
public $objectId;
2446

2547
/**
2648
* Create a ...
2749
*
2850
* @param \RecAnalyst\RecordedGame $rec Recorded game instance.
2951
* @param int $time Recorded game instance.
3052
*/
31-
public function __construct(RecordedGame $rec, $time)
53+
public function __construct(RecordedGame $rec, $time, $playerId, $resourceType, $amount, $objectId)
3254
{
3355
parent::__construct($rec, $time);
56+
57+
$this->playerId = $playerId;
58+
$this->resourceType = $resourceType;
59+
$this->amount = $amount;
60+
$this->objectId = $objectId;
61+
}
62+
63+
/**
64+
* Get a string representation of the action.
65+
*
66+
* @return string
67+
*/
68+
public function __toString()
69+
{
70+
return sprintf(
71+
'BuyCommodity(playerId=%d, resourceType=%d, amount=%d, objectId=%d)',
72+
$this->playerId,
73+
$this->resourceType,
74+
$this->amount,
75+
$this->objectId
76+
);
3477
}
3578
}

0 commit comments

Comments
 (0)