Exemplo n.º 1
0
 public static function trainAnts($ants)
 {
     while (!empty($ants)) {
         $name = key($ants);
         $shift = array_shift($ants);
         if (empty($shift)) {
             continue;
         }
         $ant = Ants::getAntByName($name)->fetch_all()[0];
         Ants::tryTrain($ant, $shift);
         $userID = User::getUserById($_SESSION['id'])['iduser'];
         $db = mysqli_connect("localhost", "root", "1234", "ant_rpg");
         $result = mysqli_query($db, "INSERT INTO train_ants (ant_id,user_id,quantity,trained) VALUES ('" . $ant[0] . "','" . $userID . "','" . $shift . "',0)");
     }
 }
Exemplo n.º 2
0
 public static function run($bot)
 {
     global $argv, $argc;
     $ants = new Ants();
     $map_data = array();
     $round = 0;
     if (in_array("--debug", $argv)) {
         $ants->debugFlag = true;
     }
     if (in_array("--visTool", $argv)) {
         $ants->visTool = true;
     }
     if ($ants->debugFlag) {
         unlink($_SERVER['PWD'] . "/debug_ants.log");
     }
     while (true) {
         $current_line = fgets(STDIN, 1024);
         $current_line = trim($current_line);
         if ($current_line === 'ready') {
             $ants->setup($map_data);
             $ants->finishTurn();
             $map_data = array();
         } elseif ($current_line === 'go') {
             $round++;
             $ants->currentTurn = $round;
             $ants->roundStart = microtime(true);
             if ($round == 1) {
                 $bot->doSetup($ants);
             }
             $ants->update($map_data);
             $bot->doTurn($ants);
             $ants->finishTurn();
             $map_data = array();
         } else {
             $map_data[] = $current_line;
         }
     }
 }
Exemplo n.º 3
0
                    $closest_distance = $dist;
                    $closest_target = array($tRow, $tCol);
                }
            }
            if ($closest_target === null) {
                # no target found, mark ant as not moving so we don't run into it
                $destinations[] = $ant;
                continue;
            }
            $directions = $ants->direction($aRow, $aCol, $closest_target[0], $closest_target[1]);
            shuffle($directions);
            foreach ($directions as $direction) {
                list($nRow, $nCol) = $ants->destination($aRow, $aCol, $direction);
                $nKey = $nRow . '_' . $nCol;
                if ($ants->unoccupied($nRow, $nCol) and !isset($destinations[$nKey])) {
                    $destinations[$nKey] = 1;
                    $ants->issueOrder($aRow, $aCol, $direction);
                    continue 2;
                }
            }
            //ant can't move
            $destinations[$aRow . '_' . $aCol] = 1;
        }
    }
}
/**
 * Don't run bot when unit-testing
 */
if (!defined('PHPUnit_MAIN_METHOD')) {
    Ants::run(new HunterBot());
}
Exemplo n.º 4
0
<?php

/**
 * Created by PhpStorm.
 * User: Hristo
 * Date: 10/4/2015
 * Time: 11:37
 */
$db = mysqli_connect("localhost", "root", "1234", "ant_rpg");
$request = mysqli_query($db, "SELECT rt.id,rt.return_start,rt.distance,rt.return_to,tfb.ant_id,tfb.quantity FROM return_troops rt JOIN troops_for_battle tfb ON rt.tfb_id=tfb.id\n WHERE returned = 0");
while ($row = $request->fetch_assoc()) {
    $now = date_create();
    $time = $row['return_start'];
    $timeToAdd = $row['distance'] * 60;
    $time = date("Y-M-d H:i:s", strtotime($time) + $timeToAdd);
    $ng = $now->getTimestamp();
    $newNow = date("Y-M-d H:i:s", strtotime($ng) + ($ng + 3600));
    if ($newNow > $time) {
        Ants::updateAntsPlus($row['ant_id'], $row['quantity'], $row['return_to']);
        mysqli_query($db, "UPDATE return_troops SET returned = 1 WHERE id=" . $row['id']);
    }
}
Exemplo n.º 5
0
<?php

require_once 'Ants.php';
class RandomBot
{
    public function doTurn($ants)
    {
        $destinations = array();
        foreach ($ants->myAnts as $ant) {
            list($aRow, $aCol) = $ant;
            // try all directions randomly until one is passable and not occupied
            $directions = array_keys($ants->AIM);
            shuffle($directions);
            foreach ($directions as $direction) {
                list($nRow, $nCol) = $ants->destination($aRow, $aCol, $direction);
                $nKey = $nRow . '_' . $nCol;
                if (!isset($destinations[$nKey]) && $ants->passable($nRow, $nCol)) {
                    $ants->issueOrder($aRow, $aCol, $direction);
                    $destinations[$nKey] = 1;
                    continue 2;
                }
            }
            //ant can't move
            $destinations[$aRow . '_' . $aCol] = 1;
        }
    }
}
Ants::run(new RandomBot());
Exemplo n.º 6
0
                $this->diffusion_map[$scent[0]][$scent[1]]['food'] += $boost;
            }
        }
        foreach ($this->enemy_hills as $hill) {
            $hill_boost = 10000 + rand(0, 2500);
            list($hill_row, $hill_col) = $hill;
            // Diffuse Food Scent in a radius of 10 units
            $scentable = $this->getScentableTiles($hill_row, $hill_col, 20);
            foreach ($scentable as $scent) {
                $distance = $this->ants->distance($hill_row, $hill_col, $scent[0], $scent[1]);
                if ($distance == 0) {
                    $boost = $hill_boost;
                } else {
                    $boost = $hill_boost / $distance;
                }
                if ($this->diffusion_map[$scent[0]][$scent[1]]['hill'] > $boost) {
                    continue;
                }
                $this->diffusion_map[$scent[0]][$scent[1]]['hill'] = $boost;
            }
        }
        $end = microtime(true);
        $this->debug("Diffusion Time: " . ($end - $start) . "\n");
    }
}
/**
 * Don't run bot when unit-testing
 */
if (!defined('PHPUnit_MAIN_METHOD')) {
    Ants::run(new MyBot());
}
Exemplo n.º 7
0
 public static function run($bot)
 {
     $ants = new Ants();
     $map_data = array();
     while (true) {
         $current_line = fgets(STDIN, 1024);
         $current_line = trim($current_line);
         if ($current_line === 'ready') {
             $ants->setup($map_data);
             $ants->finishTurn();
             $map_data = array();
         } elseif ($current_line === 'go') {
             $ants->update($map_data);
             $bot->doTurn($ants);
             $ants->finishTurn();
             $map_data = array();
         } else {
             $map_data[] = $current_line;
         }
     }
 }
Exemplo n.º 8
0
 public static function startBattle($battleId, $attackerId, $defenderId)
 {
     $db = mysqli_connect("localhost", "root", "1234", "ant_rpg");
     $attackerRequest = mysqli_query($db, "SELECT tfb.quantity,a.ant_id,a.attack FROM troops_for_battle tfb\n      \tJOIN ant a\n\t    \tON tfb.ant_id=a.ant_id\n        WHERE tfb.battleid = " . $battleId);
     $defenderRequest = mysqli_query($db, "SELECT ua.quantity,a.attack,a.ant_id FROM user_ants ua JOIN ant a ON ua.ant_id=a.ant_id\n        WHERE user_id = " . $defenderId);
     $attackerATT = 0;
     while ($row = $attackerRequest->fetch_assoc()) {
         $attackerATT = $attackerATT + $row['quantity'] * $row['attack'];
     }
     $defendATT = 0;
     $attacker = User::getUserById($attackerId)['username'];
     $defender = User::getUserById($defenderId)['username'];
     while ($row = $defenderRequest->fetch_assoc()) {
         $defendATT = $defendATT + $row['quantity'] * $row['attack'];
     }
     if ($attackerATT > $defendATT) {
         $diff = $defendATT;
         $attackerAnts = Ants::getAntsByUserId($attackerId);
         for ($i = 0; $i < 2; $i++) {
             if ($i == count($attackerAnts) - 1) {
                 $attackerAnts[$i] = $attackerAnts[$i] - $diff;
             } else {
                 if ($diff > $attackerAnts[$i]) {
                     $diff = $diff - $attackerAnts[$i];
                     $attackerAnts[$i] = 0;
                 }
             }
             Ants::updateTroops($i + 1, $attackerAnts[$i], $battleId);
         }
         BattleReport::sendReport($attackerId, "You have won the attack versus player " . $defender . " and lost " . $defendATT . " troops", $defendATT);
         BattleReport::sendReport($defenderId, "You lost defence versus " . $attacker . " and lost " . $defendATT . " troops", $defendATT);
         Action::returnArmy($battleId);
         Ants::UpdateAntsToZero($defenderId);
     } elseif ($attackerATT == $defendATT) {
         BattleReport::sendReport($attackerId, "Draw you lost all your troops versus " . $defender, $defendATT);
         BattleReport::sendReport($attackerId, "Draw you lost all your troops versus " . $attacker, $defendATT);
         Ants::UpdateAntsToZero($defenderId);
     } else {
         $diff = $attackerATT;
         $defenderAnts = Ants::getAntsByUserId($defenderId);
         for ($i = 0; $i < 2; $i++) {
             if ($i == count($defenderAnts) - 1) {
                 $defenderAnts[$i] = $defenderAnts[$i] - $diff;
             } else {
                 if ($diff > $defenderAnts[$i]) {
                     $diff = $diff - $defenderAnts[$i];
                     $defenderAnts[$i] = 0;
                 }
             }
             Ants::updateAntsTo($i + 1, $defenderAnts[$i], $defenderId);
         }
         BattleReport::sendReport($defenderId, "You have won the defence versus player " . $attacker . " and lost " . $attackerATT . " troops", $attackerATT);
         BattleReport::sendReport($attackerId, "You lost the attack versus " . $defender . " and lost " . $attackerATT . " troops", $attackerATT);
     }
     mysqli_query($db, "UPDATE attack SET battled=1 WHERE idattack=" . $battleId);
 }
Exemplo n.º 9
0
            // send ants following a wall, keeping it on their left
            if (isset($this->antsLefty[$aKey])) {
                $direction = $this->antsLefty[$aKey];
                $directions = array($ants->LEFT[$direction], $direction, $ants->RIGHT[$direction], $ants->BEHIND[$direction]);
                // try 4 directions in order, attempting to turn left at corners
                foreach ($directions as $newDirection) {
                    list($nRow, $nCol) = $ants->destination($aRow, $aCol, $newDirection);
                    $nKey = $nRow . '_' . $nCol;
                    if ($ants->passable($nRow, $nCol)) {
                        if ($ants->unoccupied($nRow, $nCol) && !isset($destinations[$nKey])) {
                            $ants->issueOrder($aRow, $aCol, $newDirection);
                            $newLefty[$nKey] = $newDirection;
                            $destinations[$nKey] = 1;
                            break;
                        } else {
                            # have ant wait until it is clear
                            $newStraight[$aKey] = $ants->RIGHT[$direction];
                            $destinations[$aKey] = 1;
                            break;
                        }
                    }
                }
            }
        }
        # reset lists
        $this->antsStraight = $newStraight;
        $this->antsLefty = $newLefty;
    }
}
Ants::run(new LeftyBot());