function terrainGen($mapDoc, $terrainDoc) { $terrainArr = json_decode($terrainDoc->hexStr->hexEncodedStr); $map = $mapDoc->map; $this->terrain->mapUrl = $mapUrl = $map->mapUrl; $this->terrain->maxCol = $maxCol = $map->numX; $this->terrain->maxRow = $maxRow = $map->numY; $this->terrain->mapWidth = $map->mapWidth; // $this->mapData->setData($maxCol, $maxRow, $mapUrl); \Wargame\Hexagon::setMinMax(); // $this->terrain->setMaxHex(); $a = $map->a; $b = $map->b; $c = $map->c; $this->terrain->a = $a; $this->terrain->b = $b; $this->terrain->c = $c; $this->terrain->originY = $b * 3 - $map->y; $xOff = ($a + $c) * 2 - ($c / 2 + $a); $this->terrain->originX = $xOff - $map->x; // $this->terrain->addTerrainFeature("town", "town", "t", 0, 0, 1, false); }
function bfsCommunication($goal, $bias, $attackingForceId = false, $maxHex = false) { $goalArray = array(); if (is_array($goal)) { foreach ($goal as $key => $val) { $goalArray[$val] = true; } } else { $goalArray[$goal] = true; } if ($attackingForceId !== false) { $defendingForceId = $this->force->Enemy($attackingForceId); } else { $attackingForceId = $this->force->attackingForceId; $defendingForceId = $this->force->defendingForceId; } $cnt = 0; while (count($this->moveQueue) > 0) { $cnt++; $hexPath = array_shift($this->moveQueue); $hexNum = $hexPath->name; if ($maxHex !== false) { $movePoints = $hexPath->pointsLeft; } if (!$hexNum) { continue; } if ($goalArray[$hexNum]) { return true; } if (!isset($this->moves->{$hexNum})) { /* first time here */ $this->moves->{$hexNum} = $hexPath; } else { /* invalid hex */ if ($this->moves->{$hexNum}->isValid === false) { continue; } /* been here, done that */ continue; } /* @var MapHex $mapHex */ $mapHex = $this->mapData->getHex($hexNum); if ($mapHex->isOccupied($attackingForceId)) { $this->moves->{$hexNum}->isOccupied = true; } if ($mapHex->isOccupied($defendingForceId)) { $this->moves->{$hexNum}->isValid = false; continue; } if ($maxHex !== false) { $this->moves->{$hexNum}->pointsLeft = $movePoints; } $this->moves->{$hexNum}->pathToHere = $hexPath->pathToHere; if ($this->moves->{$hexNum}->isZoc == NULL) { $this->moves->{$hexNum}->isZoc = $this->force->mapHexIsZOC($mapHex, $defendingForceId); } if ($this->moves->{$hexNum}->isZoc) { if (!$this->moves->{$hexNum}->isOccupied) { continue; } } $path = $hexPath->pathToHere; $path[] = $hexNum; for ($i = 1; $i <= 6; $i++) { $newHexNum = $mapHex->neighbors[$i - 1]; $gnuHex = Hexagon::getHexPartXY($newHexNum); if (!$gnuHex) { continue; } if ($this->terrain->terrainIsHexSide($hexNum, $newHexNum, "blocked")) { continue; } if ($this->terrain->terrainIsXY($gnuHex[0], $gnuHex[1], "offmap")) { continue; } if ($this->terrain->terrainIsXY($gnuHex[0], $gnuHex[1], "blocked")) { continue; } $newMapHex = $this->mapData->getHex($newHexNum); if ($newMapHex->isOccupied($defendingForceId)) { continue; } if ($maxHex !== false && $movePoints - 1 < 0) { continue; } $head = false; if ($bias[$i]) { $head = true; } if (isset($this->moves->{$newHexNum})) { if ($maxHex !== false) { if ($this->moves->{$newHexNum}->pointsLeft > $movePoints - 1) { continue; } } else { continue; } } $newPath = new HexPath(); $newPath->name = $newHexNum; $newPath->pathToHere = $path; if ($maxHex !== false) { $newPath->pointsLeft = $movePoints - 1; } if ($head) { array_unshift($this->moveQueue, $newPath); } else { $this->moveQueue[] = $newPath; } } } return false; }
function terrainGen($mapDoc, $terrainDoc) { $terrainArr = json_decode($terrainDoc->hexStr->hexEncodedStr); $map = $mapDoc->map; $this->terrain->mapUrl = $mapUrl = $map->mapUrl; $this->terrain->maxCol = $maxCol = $map->numX; $this->terrain->maxRow = $maxRow = $map->numY; $this->terrain->mapWidth = $map->mapWidth; $this->mapData->setData($maxCol, $maxRow, $mapUrl); Hexagon::setMinMax(); $this->terrain->setMaxHex(); $a = $map->a; $b = $map->b; $c = $map->c; $this->terrain->a = $a; $this->terrain->b = $b; $this->terrain->c = $c; $this->terrain->originY = $b * 3 - $map->y; $xOff = ($a + $c) * 2 - ($c / 2 + $a); $this->terrain->originX = $xOff - $map->x; // code, name, displayName, letter, entranceCost, traverseCost, combatEffect, is Exclusive $this->terrain->addTerrainFeature("offmap", "offmap", "o", 1, 0, 0, true); $this->terrain->addTerrainFeature("clear", "", "c", 1, 0, 0, true); $this->terrain->addTerrainFeature("road", "road", "r", 0.5, 0, 0, false); $this->terrain->addTerrainFeature("town", "town", "t", 0.5, 0, 0, true, true); $this->terrain->addTerrainFeature("forest", "forest", "f", 2, 0, 1, true, true); $this->terrain->addTerrainFeature("hill", "hill", "h", 2, 0, 0, true, true); $this->terrain->addTerrainFeature("river", "river", "v", 0, 1, 0, false); $this->terrain->addTerrainFeature("trail", "trail", "r", 1, 0, 0, false); $this->terrain->addTerrainFeature("blocked", "blocked", "b", 1, 0, 0, true); $this->terrain->addTerrainFeature("blocksnonroad", "blocksnonroad", "b", 1, 0, 0, false); $this->terrain->addTerrainFeature("slope", "slope", "s", 0, 1, 0, false); $this->terrain->addTerrainFeature("elevation", "elevation", "e", 0, 0, 0, false); $this->terrain->addTerrainFeature("elevation2", "elevation2", "e", 0, 0, 0, false); $this->terrain->addTerrainFeature("crest", "crest", "c", 0, 1, 0, false, true); for ($col = 100; $col <= $maxCol * 100; $col += 100) { for ($row = 1; $row <= $maxRow; $row++) { $this->terrain->addTerrain($row + $col, LOWER_LEFT_HEXSIDE, "clear"); $this->terrain->addTerrain($row + $col, UPPER_LEFT_HEXSIDE, "clear"); $this->terrain->addTerrain($row + $col, BOTTOM_HEXSIDE, "clear"); $this->terrain->addTerrain($row + $col, HEXAGON_CENTER, "clear"); } } foreach ($terrainArr as $terrain) { foreach ($terrain->type as $terrainType) { $name = $terrainType->name; $matches = []; if (preg_match("/SpecialHex/", $name)) { $this->terrain->addSpecialHex($terrain->number, $name); } else { if (preg_match("/^ReinforceZone(.*)\$/", $name, $matches)) { $this->terrain->addReinforceZone($terrain->number, $matches[1]); } else { $tNum = sprintf("%04d", $terrain->number); $this->terrain->addTerrain($tNum, $terrain->hexpartType, strtolower($name)); } } } } }
function equals(Hexagon $hexagon) { $isEqual = false; if ($this->x == $hexagon->getX() && $this->y == $hexagon->getY()) { $isEqual = true; } return $isEqual; }
function isExit($hexagon) { $isExit = false; if (is_object($hexagon)) { $X = $hexagon->getX(); $Y = $hexagon->getY(); } else { list($X, $Y) = Hexagon::getHexPartXY($hexagon); } $hexpart = new Hexpart($X, $Y); $terrainCode = $this->getTerrainCode($hexpart); if ($this->terrainIs($hexpart, "offmap") == true) { $isExit = true; } return $isExit; }
static function transformChanges($doc, $last_seq, $user) { global $mode_name, $phase_name; $battle = Battle::battleFromDoc($doc); $val = print_r($doc->wargame->force, true); file_put_contents('/tmp/loggy', $val, FILE_APPEND); $val = print_r($doc->wargame->force, true); file_put_contents('/tmp/loggy', $val, FILE_APPEND); $chatsIndex = 0; /* remove me */ $click = $doc->_rev; $matches = array(); preg_match("/^([0-9]+)-/", $click, $matches); $click = $matches[1]; // $games = $doc->games; $chats = array_slice($doc->chats, $chatsIndex); $chatsIndex = count($doc->chats); // $users = $doc->users; // $clock = $doc->clock; $players = $doc->wargame->players; $player = array_search($user, $players); if ($player === false) { $player = 0; } $force = $doc->wargame->force; $wargame = $doc->wargame; $gameName = $doc->gameName; $gameRules = $wargame->gameRules; $gameRules->option = false; $fogDeploy = false; if (!empty($wargame->scenario->fogDeploy) && $doc->playerStatus == "multi") { $fogDeploy = true; } // $revs = $doc->_revs_info; // \Wargame\Battle::loadGame($gameName, $doc->wargame->arg); //Battle::getHeader(); if (isset($doc->wargame->mapViewer)) { $playerData = $doc->wargame->mapViewer[$player]; } else { $playerData = $doc->wargame->mapData[0]; } $mapGrid = new MapGrid($playerData); $mapUnits = array(); $moveRules = $doc->wargame->moveRules; $combatRules = $doc->wargame->combatRules; $combats = isset($combatRules->combats) ? $combatRules->combats : false; if (!$combats) { $combats = isset($combatRules->combatsToResolve) ? $combatRules->combatsToResolve : false; } if ($playerData->trueRows && $combats) { foreach ($combats as $combat) { foreach ($combat->thetas as $theta) { foreach ($theta as $key => $val) { $theta->{$key} += 90; } } } } $combats = isset($combatRules->resolvedCombats) ? $combatRules->resolvedCombats : false; if ($playerData->trueRows && $combats) { foreach ($combats as $combat) { foreach ($combat->thetas as $theta) { foreach ($theta as $key => $val) { $theta->{$key} += 90; } } } } $units = $battle->force->units; $attackingId = $doc->wargame->gameRules->attackingForceId; foreach ($units as $unit) { // $unit = static::buildUnit($unit); if (is_object($unit->hexagon)) { // $unit->hexagon->parent = $unit->parent; } else { $unit->hexagon = new Hexagon($unit->hexagon); } // $unit->hexagon->parent = $unit->parent; $mapGrid->setHexagonXY($unit->hexagon->x, $unit->hexagon->y); $mapUnit = $unit->fetchData(); $mapUnit->x = $mapGrid->getPixelX(); $mapUnit->y = $mapGrid->getPixelY(); if ($fogDeploy && ($gameRules->phase == RED_DEPLOY_PHASE || $gameRules->phase == BLUE_DEPLOY_PHASE) && $unit->forceId !== $player) { if ($unit->hexagon->parent == "gameImages") { $mapUnit = new stdClass(); } } $mapUnits[] = $mapUnit; } $turn = $doc->wargame->gameRules->turn; // foreach ($units as $i => $unit) { // $u = new StdClass(); // $u->status = $unit->status; // $u->moveAmountUsed = $unit->moveAmountUsed; // $u->maxMove = $unit->maxMove; // $u->forceId = $unit->forceId; // $u->forceMarch = $unit->forceMarch; // $u->isDisrupted = $unit->isDisrupted; // $u->disruptLen = $unit->disruptLen; // $u->isImproved = $unit->isImproved; // if ($unit->reinforceTurn > $turn) { // $u->reinforceTurn = $unit->reinforceTurn; // } // $units[$i] = $u; // } if ($fogDeploy) { if ($gameRules->phase == BLUE_DEPLOY_PHASE && $player === RED_FORCE) { $moveRules->moves = new stdClass(); } if ($gameRules->phase == RED_DEPLOY_PHASE && $player === BLUE_FORCE) { $moveRules->moves = new stdClass(); } } if ($moveRules->moves) { foreach ($moveRules->moves as $k => $move) { $hex = new Hexagon($k); $mapGrid->setHexagonXY($hex->getX(), $hex->getY()); $n = new stdClass(); $moveRules->moves->{$k}->pixX = $mapGrid->getPixelX(); $moveRules->moves->{$k}->pixY = $mapGrid->getPixelY(); $pointsLeft = sprintf("%.2f", $moveRules->moves->{$k}->pointsLeft); $pointsLeft = preg_replace("/\\.0*\$/", '', $pointsLeft); $pointsLeft = preg_replace("/(\\.[1-9]*)0*/", "\$1", $pointsLeft); $moveRules->moves->{$k}->pointsLeft = $pointsLeft; unset($moveRules->moves->{$k}->isValid); } if (false && $moveRules->path) { foreach ($moveRules->path as $hexName) { $hex = new Hexagon($hexName); $mapGrid->setHexagonXY($hex->x, $hex->y); $path = new stdClass(); $path->pixX = $mapGrid->getPixelX(); $path->pixY = $mapGrid->getPixelY(); $moveRules->hexPath[] = $path; } } } $force->units = []; $gameRules = $wargame->gameRules; $gameRules->phase_name = $phase_name; $gameRules->mode_name = $mode_name; if (isset($force->exchangeAmount)) { $gameRules->exchangeAmount = $force->exchangeAmount; } $newSpecialHexes = new stdClass(); $phaseClicks = $gameRules->phaseClicks; if ($doc->wargame->mapData->specialHexes) { $specialHexes = $doc->wargame->mapData->specialHexes; foreach ($specialHexes as $k => $v) { $hex = new Hexagon($k); $mapGrid->setHexagonXY($hex->x, $hex->y); $path = new stdClass(); $newSpecialHexes->{"x" . intval($mapGrid->getPixelX()) . "y" . intval($mapGrid->getPixelY())} = $v; } } $newMapSymbols = new stdClass(); if ($doc->wargame->mapData->mapSymbols) { $mapSymbols = $doc->wargame->mapData->mapSymbols; foreach ($mapSymbols as $k => $v) { $hex = new Hexagon($k); $mapGrid->setHexagonXY($hex->x, $hex->y); $newMapSymbols->{"x" . intval($mapGrid->getPixelX()) . "y" . intval($mapGrid->getPixelY())} = $v; } } $mapSymbols = $newMapSymbols; $sentBreadcrumbs = new stdClass(); if ($doc->wargame->mapData->breadcrumbs) { $breadcrumbs = $doc->wargame->mapData->breadcrumbs; $breadcrumbKey = "/{$turn}" . "t" . $attackingId . "a/"; foreach ($breadcrumbs as $key => $crumbs) { if (!preg_match($breadcrumbKey, $key)) { continue; } $matches = array(); preg_match("/m(\\d*)\$/", $key, $matches); if (strlen($matches[1]) < 1) { continue; } $unitId = $matches[1]; if (!isset($sentBreadcrumbs->{$unitId})) { $sentBreadcrumbs->{$unitId} = []; } $sentMoves = $sentBreadcrumbs->{$unitId}; foreach ($crumbs as $crumb) { if (!isset($crumb->type)) { $type = "move"; } else { $type = $crumb->type; } switch ($type) { case "move": if ($crumb->fromHex === "0000") { continue; } $fromHex = new Hexagon($crumb->fromHex); $mapGrid->setHexagonXY($fromHex->x, $fromHex->y); $crumb->fromX = intval($mapGrid->getPixelX()); $crumb->fromY = intval($mapGrid->getPixelY()); $toHex = new Hexagon($crumb->toHex); $mapGrid->setHexagonXY($toHex->x, $toHex->y); $crumb->toX = intval($mapGrid->getPixelX()); $crumb->toY = intval($mapGrid->getPixelY()); break; case "combatResult": if ($crumb->hex) { $hex = new Hexagon($crumb->hex); $mapGrid->setHexagonXY($hex->x, $hex->y); $crumb->hexX = intval($mapGrid->getPixelX()); $crumb->hexY = intval($mapGrid->getPixelY()); } break; } $sentMoves[] = $crumb; } $sentBreadcrumbs->{$unitId} = $sentMoves; } } $specialHexes = $newSpecialHexes; $newSpecialHexesChanges = new stdClass(); if (!empty($doc->wargame->mapData->specialHexesChanges)) { $specialHexesChanges = $doc->wargame->mapData->specialHexesChanges; foreach ($specialHexesChanges as $k => $v) { $hex = new Hexagon($k); $mapGrid->setHexagonXY($hex->x, $hex->y); $path = new stdClass(); $newSpecialHexesChanges->{"x" . intval($mapGrid->getPixelX()) . "y" . intval($mapGrid->getPixelY())} = $v; } } $newSpecialHexesVictory = new stdClass(); if (!empty($doc->wargame->mapData->specialHexesVictory)) { $specialHexesVictory = $doc->wargame->mapData->specialHexesVictory; foreach ($specialHexesVictory as $k => $v) { $hex = new Hexagon($k); $mapGrid->setHexagonXY($hex->x, $hex->y); $path = new stdClass(); $newSpecialHexesVictory->{"x" . intval($mapGrid->getPixelX()) . "y" . intval($mapGrid->getPixelY())} = $v; } } $vp = $doc->wargame->victory->victoryPoints; $victory = $doc->wargame->victory; $flashMessages = $gameRules->flashMessages; if (count($flashMessages)) { foreach ($flashMessages as $key => $mess) { $match = []; if (preg_match("/@hex (\\d*)/", $mess, $match)) { $hex = new Hexagon($match[1]); $mapGrid->setHexagonXY($hex->x, $hex->y); $flashMessages[$key] = "@hex x" . intval($mapGrid->getPixelX()) . "y" . intval($mapGrid->getPixelY()); } } } $specialHexesChanges = $newSpecialHexesChanges; $specialHexesVictory = $newSpecialHexesVictory; $gameRules->playerStatus = $doc->playerStatus; $mapViewer = $playerData; $clock = 'love'; $wargameId = $doc->_id; // $clock = "The turn is " . $gameRules->turn . ". The Phase is " . $phase_name[$gameRules->phase] . ". The mode is " . $mode_name[$gameRules->mode]; $transform = compact("wargameId", "mapSymbols", "mapViewer", "sentBreadcrumbs", "phaseClicks", "click", "revs", "vp", "flashMessages", "specialHexesVictory", "specialHexes", "specialHexesChanges", "combatRules", 'force', 'seq', 'chats', 'chatsIndex', 'last_seq', 'users', 'games', 'clock', 'mapUnits', 'moveRules', 'gameRules', "victory"); $transform = $battle->postTransform($battle, $transform); return $transform; }
function bfsRoadTrace($goal, $bias, $attackingForceId = false, $maxHex = false) { $goalArray = array(); if (is_array($goal)) { foreach ($goal as $key => $val) { $goalArray[$val] = true; } } else { $goalArray[$goal] = true; } if ($attackingForceId !== false) { $defendingForceId = $this->force->Enemy($attackingForceId); } else { $attackingForceId = $this->force->attackingForceId; $defendingForceId = $this->force->defendingForceId; } $cnt = 0; while (count($this->moveQueue) > 0) { $cnt++; $hexPath = array_shift($this->moveQueue); $hexNum = $hexPath->name; if ($maxHex !== false) { $movePoints = $hexPath->pointsLeft; } if (!$hexNum) { continue; } if (!isset($this->moves->{$hexNum})) { /* first time here */ $this->moves->{$hexNum} = $hexPath; } else { /* invalid hex */ if ($this->moves->{$hexNum}->isValid === false) { continue; } /* been here, done that */ continue; } /* @var MapHex $mapHex */ $mapHex = $this->mapData->getHex($hexNum); if ($mapHex->isOccupied($attackingForceId)) { $this->moves->{$hexNum}->isOccupied = true; } if ($mapHex->isOccupied($defendingForceId)) { $this->moves->{$hexNum}->isValid = false; continue; } if ($maxHex !== false) { $this->moves->{$hexNum}->pointsLeft = $movePoints; } $this->moves->{$hexNum}->pathToHere = $hexPath->pathToHere; if ($this->moves->{$hexNum}->isZoc == NULL) { $this->moves->{$hexNum}->isZoc = $this->force->mapHexIsZOC($mapHex, $defendingForceId); } if ($this->moves->{$hexNum}->isZoc) { if (!$this->moves->{$hexNum}->isOccupied) { unset($this->moves->{$hexNum}); // $this->moves->$hexNum->isValid = false; continue; } } $path = $hexPath->pathToHere; $path[] = $hexNum; for ($i = 1; $i <= 6; $i++) { $newHexNum = $mapHex->neighbors[$i - 1]; $gnuHex = Hexagon::getHexPartXY($newHexNum); if (!$gnuHex) { continue; } if (!($this->terrain->terrainIsHexSide($hexNum, $newHexNum, "road") || $this->terrain->terrainIsHexSide($hexNum, $newHexNum, "trail") || $this->terrain->terrainIsHexSide($hexNum, $newHexNum, "secondaryroad"))) { continue; } if ($this->terrain->terrainIsXY($gnuHex[0], $gnuHex[1], "offmap")) { continue; } $newMapHex = $this->mapData->getHex($newHexNum); if ($newMapHex->isOccupied($defendingForceId)) { continue; } /* * TODO order is important in if statement check if doing zoc zoc move first then if just one hex move. * Then check if oneHex and firstHex */ if ($maxHex !== false && $movePoints - 1 < 0) { continue; } $head = false; if (!empty($bias[$i])) { $head = true; } if (isset($this->moves->{$newHexNum})) { if ($maxHex !== false) { if ($this->moves->{$newHexNum}->pointsLeft > $movePoints - 1) { continue; } } else { continue; } } $newPath = new HexPath(); $newPath->name = $newHexNum; $newPath->pathToHere = $path; if ($maxHex !== false) { $newPath->pointsLeft = $movePoints - 1; } if ($head) { array_unshift($this->moveQueue, $newPath); } else { $this->moveQueue[] = $newPath; } } } return false; }