Skip to content

Commit

Permalink
Implement reading Stop, Work, Buy/Sell, Garrison, Gather Point, Repai…
Browse files Browse the repository at this point in the history
…r, Unload, Wall actions
  • Loading branch information
goto-bus-stop committed Dec 10, 2016
1 parent 60fe938 commit 723b223
Show file tree
Hide file tree
Showing 12 changed files with 591 additions and 61 deletions.
201 changes: 194 additions & 7 deletions src/Analyzers/BodyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,30 @@ protected function run()
$this->readUnits($unitCount)
));
break;
case self::COMMAND_STOP:
$count = ord($this->body[$this->position++]);
$this->push(new Actions\StopAction(
$this->rec,
$this->currentTime,
$this->readUnits($count)
));
break;
case self::COMMAND_WORK:
$this->position += 3;
$targetId = $this->readBody('l', 4);
$count = ord($this->body[$this->position++]);
$this->position += 3;
$x = $this->readBody('f', 4);
$y = $this->readBody('f', 4);
$this->push(new Actions\WorkAction(
$this->rec,
$this->currentTime,
$targetId,
$x,
$y,
$this->readUnits($count)
));
break;
case self::COMMAND_MOVE:
$playerId = ord($this->body[$this->position++]);
$this->position += 2;
Expand All @@ -272,6 +296,24 @@ protected function run()
$this->readUnits($count)
));
break;
case self::COMMAND_CREATE:
$this->position++;
$objectCategory = $this->readBody('v', 2);
$playerId = ord($this->body[$this->position++]);
$this->position += 3;
$x = $this->readBody('f', 4);
$y = $this->readBody('f', 4);
$z = $this->readBody('f', 4);
$this->push(new Actions\CreateAction(
$this->rec,
$this->currentTime,
$playerId,
$objectCategory,
$x,
$y,
$z
));
break;
case self::COMMAND_UNIT_AI_STATE:
$numUnits = ord($this->body[$this->position++]);
$stance = ord($this->body[$this->position++]);
Expand All @@ -284,9 +326,9 @@ protected function run()
break;
// player resign
case self::COMMAND_RESIGN:
$playerIndex = ord($this->body[$this->position]);
$playerNumber = ord($this->body[$this->position + 1]);
$dropped = ord($this->body[$this->position + 2]);
$playerIndex = ord($this->body[$this->position++]);
$playerNumber = ord($this->body[$this->position++]);
$dropped = ord($this->body[$this->position++]);

$this->push(new Actions\ResignAction(
$this->rec,
Expand All @@ -296,14 +338,27 @@ protected function run()
$dropped
));

$this->position += 3;
$player = $playersByIndex[$playerIndex];
if ($player && $player->resignTime === 0) {
$player->resignTime = $this->currentTime;
$message = sprintf('%s resigned', $player->name);
$this->chatMessages[] = new ChatMessage($this->currentTime, null, $message);
}
break;
case self::COMMAND_FORM_FORMATION:
$count = ord($this->body[$this->position++]);
$playerId = ord($this->body[$this->position++]);
$this->position += 1;
$formation = $this->readBody('l', 4);

$this->push(new Actions\FormFormationAction(
$this->rec,
$this->currentTime,
$playerId,
$formation,
$this->readUnits($count)
));
break;
// researches
case self::COMMAND_RESEARCH:
$this->position += 3;
Expand Down Expand Up @@ -371,11 +426,44 @@ protected function run()
case self::COMMAND_GAME:
$this->processGameAction();
break;
case self::COMMAND_BUILD_WALL:
$count = ord($this->body[$this->position++]);
$playerId = ord($this->body[$this->position++]);
$x1 = ord($this->body[$this->position++]);
$y1 = ord($this->body[$this->position++]);
$x2 = ord($this->body[$this->position++]);
$y2 = ord($this->body[$this->position++]);
$this->position += 1; // Padding
$objectType = $this->readBody('v', 2);
$this->position += 2; // Padding
$this->position += 4; // Always -1

$this->push(new Actions\BuildWallAction(
$this->rec,
$this->currentTime,
$playerId,
$objectType,
[$x1, $y1],
[$x2, $y2],
$this->readUnits($count)
));
break;
case self::COMMAND_CANCEL_BUILD:
$this->position += 3;
$objectId = $this->readBody('l', 4);
$playerId = $this->readBody('l', 4);
$this->push(new Actions\CancelBuildAction(
$this->rec,
$this->currentTime,
$playerId,
$objectId
));
break;
// AI trains unit
case self::COMMAND_MAKE:
$this->position += 9;
$playerId = -1;
$objectId = -1;
$this->position += 3;
$objectId = $this->readBody('l', 4);
$playerId = $this->readBody('v', 2);
$unitType = $this->readBody('v', 2);

$this->push(new Actions\MakeAction(
Expand Down Expand Up @@ -454,6 +542,105 @@ protected function run()
$this->position += 8;
}
break;
case self::COMMAND_REPAIR:
$count = ord($this->body[$this->position++]);
$this->position += 2;
$targetId = $this->readBody('l', 4);
$this->push(new Actions\RepairAction(
$this->rec,
$this->currentTime,
$targetId,
$this->readUnits($count)
));
break;
case self::COMMAND_UNLOAD:
$count = ord($this->body[$this->position++]);
$this->position += 2;
$x = $this->readBody('f', 4);
$y = $this->readBody('f', 4);
$flag = ord($this->body[$this->position++]);
$this->position += 3;
$unitType = $this->readBody('l', 4);

$this->push(new Actions\UnloadAction(
$this->rec,
$this->currentTime,
$x,
$y,
$flag,
$unitType,
$this->readUnits($count)
));
break;
case self::COMMAND_UNIT_ORDER:
$count = ord($this->body[$this->position++]);
$this->position += 2;
$targetId = $this->readBody('l', 4);
$action = ord($this->body[$this->position++]);
$this->position += 3;
$x = $this->readBody('f', 4);
$y = $this->readBody('f', 4);
$parameter = $this->readBody('l', 4);

$this->push(new Actions\UnitOrderAction(
$this->rec,
$this->currentTime,
$x,
$y,
$targetId,
$action,
$parameter,
$this->readUnits($count)
));
break;
case self::COMMAND_SET_GATHER_POINT:
$count = ord($this->body[$this->position++]);
$this->position += 2;
$targetId = $this->readBody('l', 4);
$targetType = $this->readBody('l', 4);
$x = $this->readBody('f', 4);
$y = $this->readBody('f', 4);

$this->push(new Actions\SetGatherPointAction(
$this->rec,
$this->currentTime,
$targetId,
$targetType,
$x,
$y,
$this->readUnits($count)
));
break;
case self::COMMAND_SELL_COMMODITY:
$playerId = ord($this->body[$this->position++]);
$resourceType = ord($this->body[$this->position++]);
$amount = ord($this->body[$this->position++]);
$marketId = $this->readBody('l', 4);

$this->push(new Actions\SellCommodityAction(
$this->rec,
$this->currentTime,
$playerId,
$resourceType,
$amount,
$marketId
));
break;
case self::COMMAND_BUY_COMMODITY:
$playerId = ord($this->body[$this->position++]);
$resourceType = ord($this->body[$this->position++]);
$amount = ord($this->body[$this->position++]);
$marketId = $this->readBody('l', 4);

$this->push(new Actions\BuyCommodityAction(
$this->rec,
$this->currentTime,
$playerId,
$resourceType,
$amount,
$marketId
));
break;
// multiplayer postgame data in UP1.4 RC2+
case self::COMMAND_POSTGAME:
$this->postGameData = $this->read(PostgameDataAnalyzer::class);
Expand Down
40 changes: 32 additions & 8 deletions src/Model/Actions/BuildWallAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,46 @@ class BuildWallAction extends Action
const ID = 0x69;

// BuildWall(num=%d, x1=%d, y1=%d, x2=%d, y2=%d, pId=%d, oId=%d, qId=%d)
private $num;
private $x1;
private $y1;
private $x2;
private $y2;
private $playerId;
private $objectId;
public $playerId;
public $objectId;
public $from;
public $to;
public $units;

/**
* Create a ...
*
* @param \RecAnalyst\RecordedGame $rec Recorded game instance.
* @param int $time Recorded game instance.
*/
public function __construct(RecordedGame $rec, $time)
public function __construct(RecordedGame $rec, $time, $playerId, $objectType, $from, $to, $builders)
{
parent::__construct($rec, $time);

$this->playerId = $playerId;
$this->objectType = $objectType;
$this->from = $from;
$this->to = $to;
$this->builders = $builders;
}

/**
* Get a string representation of the action.
*
* @return string
*/
public function __toString()
{
return sprintf(
'BuildWall(playerId=%d, objectType=%d, from={%d,%d}, to={%d,%d}, builders[%d]={%s})',
$this->playerId,
$this->objectType,
$this->from[0],
$this->from[1],
$this->to[0],
$this->to[1],
count($this->builders),
implode(', ', $this->builders)
);
}
}
55 changes: 49 additions & 6 deletions src/Model/Actions/BuyCommodityAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,63 @@ class BuyCommodityAction extends Action
*/
const ID = 0x7b;

// BuySellAttr(uId=%d, pId=%d, attr=%d, amt=%d)
private $unitId;
private $playerId;
private $attribute;
private $amount;
/**
* Player who is buying resources.
*
* @var int
*/
public $playerId;

/**
* Resource type to buy.
*
* @var int
*/
public $resourceType;

/**
* Amount of the resource to buy, in hundreds.
*
* @var int
*/
public $amount;

/**
* Market to buy the resources at.
*
* @var int
*/
public $objectId;

/**
* Create a ...
*
* @param \RecAnalyst\RecordedGame $rec Recorded game instance.
* @param int $time Recorded game instance.
*/
public function __construct(RecordedGame $rec, $time)
public function __construct(RecordedGame $rec, $time, $playerId, $resourceType, $amount, $objectId)
{
parent::__construct($rec, $time);

$this->playerId = $playerId;
$this->resourceType = $resourceType;
$this->amount = $amount;
$this->objectId = $objectId;
}

/**
* Get a string representation of the action.
*
* @return string
*/
public function __toString()
{
return sprintf(
'BuyCommodity(playerId=%d, resourceType=%d, amount=%d, objectId=%d)',
$this->playerId,
$this->resourceType,
$this->amount,
$this->objectId
);
}
}
Loading

0 comments on commit 723b223

Please sign in to comment.