Guest User

Untitled

a guest
Sep 30th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.33 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * 2Moons
  5. * Copyright (C) 2011 Slaver
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package 2Moons
  21. * @author Slaver <slaver7@gmail.com>
  22. * @copyright 2011 Slaver <slaver7@gmail.com> (Fork/2Moons)
  23. * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
  24. * @version 0.1 (2011-07-03)
  25. * @link http://code.google.com/p/2moons/
  26. */
  27.  
  28. define('MICRO', microtime(true));
  29. set_time_limit(0);
  30.  
  31. # Includes
  32. require_once(ROOT_PATH.'includes/classes/class.BuildFunctions.php');
  33. require_once(ROOT_PATH.'includes/classes/ArrayUtil.class.php');
  34. require_once(ROOT_PATH.'includes/classes/class.PlanetRessUpdate.php');
  35. require_once(ROOT_PATH.'includes/pages/game/class.ShowResearchPage.php');
  36. require_once(ROOT_PATH.'includes/pages/game/class.ShowShipyardPage.php');
  37. require_once(ROOT_PATH.'includes/pages/game/class.ShowBuildingsPage.php');
  38. require_once(ROOT_PATH.'includes/classes/class.FleetFunctions.php');
  39.  
  40. # Action Classes
  41. class FleetActions extends FleetFunctions
  42. {
  43. function __construct(){}
  44. }
  45.  
  46. class BuildActions extends ShowBuildingsPage
  47. {
  48. function __construct(){}
  49. }
  50.  
  51. class ResearchActions extends ShowResearchPage
  52. {
  53. function __construct(){}
  54. }
  55.  
  56. class ShipyardActions extends ShowShipyardPage
  57. {
  58. function __construct(){}
  59. }
  60.  
  61. $BUILD = new BuildActions();
  62. $RESEARCH = new ResearchActions();
  63. $SHIPYARD = new ShipyardActions();
  64. $FLEET = new FleetActions();
  65.  
  66. class Bot
  67. {
  68. const VERSION = '0.1';
  69.  
  70. # BOTS RUNTIME Settings
  71.  
  72. const BOTS_MAX_PER_CALL = 1000;
  73. const BOTS_MAX_TRY_COLONIZE = 10;
  74.  
  75. # BOTS QUEUE Settings
  76. const BOTS_MAX_ELEMENTS_BUILDS = 3;
  77. const BOTS_MAX_ELEMENTS_TECH = 1;
  78. const BOTS_MAX_ELEMENTS_SHIPYARD = 10;
  79.  
  80. # BOTS QUEUE Settings
  81. const BOTS_MAX_TRY_BUILDS_ECO = 5;
  82. const BOTS_MAX_TRY_BUILDS_SPECIAL = 7;
  83. const BOTS_MAX_TRY_BUILDS_STORAGE = 3;
  84. const BOTS_MAX_TRY_TECH = 10;
  85. const BOTS_MAX_TRY_SHIPYARD = 10;
  86.  
  87. # BOTS Online times
  88. const BOTS_ACTIVE_FROM = 0;
  89. const BOTS_ACTIVE_TO = 24;
  90.  
  91. # BOTS Online times
  92. const BOTS_ALLOW_SAVE = 0;
  93. const BOTS_SAVE_FROM = 22;
  94. const BOTS_SAVE_TO = 6;
  95.  
  96. # CHANCE IN PERCENT
  97. const CHANCE_BUILD = 100;
  98. const CHANCE_BUILD_ECO = 40; # Chance of Mines
  99. const CHANCE_BUILD_SPECIAL = 40; # Another Builds
  100. const CHANCE_BUILD_NEXT = 100;
  101.  
  102. const CHANCE_TECH = 100;
  103. const CHANCE_TECH_BASIC = 40;
  104. const CHANCE_TECH_ECO = 40;
  105. const CHANCE_TECH_SPEED = 40;
  106. const CHANCE_TECH_SPECIAL = 40;
  107. const CHANCE_TECH_NEXT = 100;
  108.  
  109. const CHANCE_SHIPYARD = 100;
  110. const CHANCE_SHIPYARD_FLEET_BASIC = 30;
  111. const CHANCE_SHIPYARD_FLEET_ATTACK = 25;
  112. const CHANCE_SHIPYARD_DEF_BASIC = 30;
  113. const CHANCE_SHIPYARD_DEF_SHIELD = 15;
  114. const CHANCE_SHIPYARD_NEXT = 100;
  115.  
  116. const CHANCE_EXPO_START = 100;
  117. const CHANCE_EXPO_NORMAL = 100;
  118. const CHANCE_EXPO_DM = 100;
  119.  
  120. const CHANCE_FLEETS_COLONIZE = 100;
  121. const CHANCE_FLEETS_RES_TO_OWN_MOON = 100;
  122. const CHANCE_FLEETS_RES_TO_PLANET = 100;
  123.  
  124. const CHANCE_FLEETS_SAVE = 100;
  125. const CHANCE_FLEETS_SAVE_TF = 100;
  126. const CHANCE_FLEETS_SAVE_BUILD = 100;
  127. const CHANCE_FLEETS_SAVE_TRANSPORT = 100;
  128. const CHANCE_FLEETS_SAVE_JUMPGATE = 100;
  129.  
  130.  
  131. /* Buildable IDs
  132. Syntax: array(BuildID => Chance)
  133. */
  134.  
  135. public static $MineIDs = array(
  136. 1 => 50,
  137. 2 => 30,
  138. 3 => 20,
  139. );
  140.  
  141. public static $EnergyIDs = array(
  142. 4 => 90,
  143. 12 => 10,
  144. );
  145.  
  146. public static $StorageIDs = array(
  147. 22 => 50,
  148. 23 => 30,
  149. 24 => 20,
  150. );
  151.  
  152. public static $SpecialIDs = array(
  153. 6 => 8,
  154. 14 => 20,
  155. 15 => 10,
  156. 21 => 17,
  157. 31 => 16,
  158. 33 => 11,
  159. 34 => 10,
  160. 44 => 8,
  161. );
  162.  
  163. public static $MoonIDs = array(
  164. 14 => 20,
  165. 21 => 17,
  166. 31 => 16,
  167. 34 => 10,
  168. 41 => 10,
  169. 42 => 7,
  170. 43 => 1,
  171. 44 => 8,
  172. );
  173.  
  174. public static $TechBasicIDs = array(
  175. 106 => 12,
  176. 108 => 20,
  177. 109 => 20,
  178. 110 => 20,
  179. 111 => 4,
  180. 113 => 12,
  181. 114 => 9,
  182. 124 => 8,
  183. );
  184.  
  185. public static $TechSpeedIDs = array(
  186. 115 => 8,
  187. 117 => 8,
  188. 118 => 11,
  189. );
  190.  
  191. public static $TechEcoIDs = array(
  192. 131 => 20,
  193. 132 => 20,
  194. 133 => 20,
  195. );
  196.  
  197. public static $TechSpecialIDs = array(
  198. 120 => 12,
  199. 121 => 7,
  200. 122 => 5,
  201. 123 => 7,
  202. 199 => 1
  203. );
  204.  
  205. public static $FleetBasicIDs = array(
  206. 202 => 200,
  207. 203 => 150,
  208. 208 => 1,
  209. 209 => 500,
  210. 210 => 20,
  211. 212 => 300,
  212. 217 => 150,
  213. 219 => 150,
  214. 220 => 150,
  215. );
  216.  
  217. public static $FleetAttackIDs = array(
  218. 204 => 345,
  219. 205 => 100,
  220. 206 => 30,
  221. 207 => 500,
  222. 211 => 200,
  223. 213 => 100,
  224. 214 => 50,
  225. 215 => 150,
  226. 218 => 200,
  227. );
  228.  
  229. public static $DefBasicIDs = array(
  230. 401 => 150,
  231. 402 => 150,
  232. 403 => 110,
  233. 404 => 70,
  234. 406 => 50,
  235. 502 => 50,
  236. );
  237.  
  238. public static $DefShieldIDs = array(
  239. 407 => 1,
  240. 408 => 1,
  241. );
  242.  
  243. static public function playBot($Bot)
  244. {
  245. $bot = new self($Bot);
  246. $bot->Play();
  247. }
  248.  
  249. static function getBots()
  250. {
  251. $Hour = date('G', TIMESTAMP);
  252. if(self::BOTS_ACTIVE_FROM > $Hour || self::BOTS_ACTIVE_TO <= $Hour)
  253. return false;
  254.  
  255. self::log("Query Started");
  256. $Bots = self::getDB()->query("SELECT DISTINCT SQL_BIG_RESULT b.id as bot_id, b.player, b.last_time, b.every_time, b.last_planet, u.* FROM ".BOTS." b INNER JOIN ".USERS." u ON u.`id` = b.player /* WHERE b.`last_time` + b.`every_time` * 60 < ".TIMESTAMP." */ GROUP BY b.id ORDER BY RAND() LIMIT ".self::BOTS_MAX_PER_CALL.";");
  257. self::log("Query Ready. Found: ".self::getDB()->numRows($Bots));
  258. if(self::getDB()->numRows($Bots) != 0) {
  259. while($Bot = self::getDB()->fetchArray($Bots)) {
  260. self::playBot($Bot);
  261. }
  262. }
  263. }
  264.  
  265. static function getDB()
  266. {
  267. return $GLOBALS['DATABASE'];
  268. }
  269.  
  270. static function getName()
  271. {
  272. $Name = file(ROOT_PATH.'botnames.txt');
  273. return $Name[array_rand($Name)];
  274. }
  275.  
  276. function __construct($Bot)
  277. {
  278. $this->USER = $Bot;
  279. $this->USER['factor'] = getFactors($this->USER);
  280. $this->COUNT = array('PLANETS' => 0, 'MOONS' => 0);
  281. }
  282.  
  283. static function log($LOG){
  284. if (function_exists('logging')) {
  285. logging('bot', $LOG);
  286. } else {
  287. $handle = fopen(ROOT_PATH.'includes/'.date('Y_m_d').'_bot.log', 'a+');
  288. if ($handle !== false) {
  289. fwrite($handle, '['.date('H:i:s').'] '.$LOG."\n");
  290. fflush($handle);
  291. fclose($handle);
  292. }
  293. }
  294. }
  295.  
  296. function randomNum($Min, $Max)
  297. {
  298. return mt_rand($Min, $Max);
  299. }
  300.  
  301. function setGlobal()
  302. {
  303. $GLOBALS['USER'] = $this->USER;
  304. $GLOBALS['PLANET'] = $this->PLANET;
  305. }
  306.  
  307. function getGlobal()
  308. {
  309. $this->USER = $GLOBALS['USER'];
  310. $this->PLANET = $GLOBALS['PLANET'];
  311. }
  312.  
  313. function saveBot()
  314. {
  315. self::log("Save Bot");
  316. self::getDB()->query("UPDATE ".USERS." u, ".BOTS." b SET
  317. u.`onlinetime` = ".TIMESTAMP.",
  318. u.`user_lastip` = '127.0.0.1',
  319. b.`last_time` = ".TIMESTAMP.",
  320. b.`last_planet` = ".$this->PLANET['id']."
  321. WHERE
  322. u.`id` = ".$this->USER['id']." AND
  323. b.`id` = ".$this->USER['bot_id'].";");
  324. }
  325.  
  326. function getPlanet()
  327. {
  328. $this->PLANET = self::getDB()->uniquequery("SELECT * FROM ".PLANETS." WHERE `id` > ".$this->USER['last_planet']." AND `id_owner` = ".$this->USER['id']." ORDER BY `id` LIMIT 1;");
  329. if(empty($this->PLANET))
  330. $this->PLANET = self::getDB()->uniquequery("SELECT * FROM `".PLANETS."` WHERE `id` = '".$this->USER['id_planet']."';");
  331. }
  332.  
  333. function getPlanetResource($ID)
  334. {
  335. return $this->PLANET[$GLOBALS['resource'][$ID]];
  336. }
  337.  
  338. function getUserResource($ID)
  339. {
  340. return $this->USER[$GLOBALS['resource'][$ID]];
  341. }
  342.  
  343. function getChance($Cache)
  344. {
  345. return $this->randomNum(1, 100) <= $Cache;
  346. }
  347.  
  348. function getChanceValue($Value)
  349. {
  350. return $this->randomNum(1, $Value);
  351. }
  352.  
  353. function getRandomList($List)
  354. {
  355. return array_rand($List);
  356. }
  357.  
  358. function getMultiplyRandomList($Chances, $Lists)
  359. {
  360. $Chance = array();
  361. foreach($Chances as $Key => $Value)
  362. $Chance[$Key] = $this->getChanceValue($Value);
  363.  
  364. $MaxChance = max($Chance);
  365.  
  366. return $Lists[array_search($MaxChance, $Chance)];
  367. }
  368.  
  369. function Play()
  370. {
  371. $this->getPlanet();
  372. $PlanetRess = new ResourceUpdate();
  373. list($this->USER, $this->PLANET) = $PlanetRess->CalcResource($this->USER, $this->PLANET);
  374. if($this->getChance(self::CHANCE_BUILD))
  375. {
  376. self::log("Success Chance: Build");
  377. if($this->getChance(self::CHANCE_BUILD_ECO)) {
  378. $this->BuildEco();
  379. }
  380.  
  381. if($this->getPlanetResource(4) <= 5 && $this->getChance(self::CHANCE_BUILD_SPECIAL)) {
  382. $this->BuildSpecialBuildings();
  383. }
  384. }
  385.  
  386. self::log("Lab Level: ".$this->getPlanetResource(31));
  387.  
  388. if($this->getPlanetResource(31) > 0 && $this->getChance(self::CHANCE_TECH))
  389. {
  390. self::log("Success Chance: Tech");
  391. $this->ResearchTechs();
  392. }
  393.  
  394. self::log("Roboter Level: ".$this->getPlanetResource(14));
  395. self::log("Shipyard Level: ".$this->getPlanetResource(21));
  396.  
  397. if($this->getPlanetResource(21) > 0 && $this->getChance(self::CHANCE_SHIPYARD))
  398. {
  399. self::log("Success Chance: Shipyard");
  400. $this->BuildShipyard();
  401. }
  402.  
  403. if($this->getChance(self::CHANCE_FLEETS_COLONIZE))
  404. {
  405. if($this->Colonizeable()) {
  406. $this->Colonize();
  407. }
  408. }
  409.  
  410. /* if(self::BOTS_ALLOW_SAVE && (self::BOTS_SAVE_FROM <= $Hour || self::BOTS_SAVE_TO > $Hour) && $this->getChance(self::CHANCE_FLEETS_SAVE)) {
  411. $this->SaveFleets();
  412. } */
  413.  
  414. $PlanetRess->SavePlanetToDB($this->USER, $this->PLANET);
  415. $this->saveBot();
  416. }
  417.  
  418. /** BUILD FUNCTIONS **/
  419.  
  420. function AddBuildingToQueue($Element) {
  421. $this->LOG("Build: ".$Element);
  422. $this->setGlobal();
  423. BuildFunctions::addBuildingToQueue($Element);
  424. $this->getGlobal();
  425. }
  426.  
  427. function BuildEco()
  428. {
  429. global $resource;
  430. $this->BuildStores();
  431.  
  432. self::log("Buildtype: Eco");
  433. $ActualCount = $this->GetBuildQueueInfo();
  434. $EnergyChance = 1;
  435. if (abs($this->PLANET['energy_used']) > 0) {
  436. $EnergyChance = $this->PLANET['energy'] / abs($this->PLANET['energy_used']);
  437. }
  438. $Chance = 20 + $EnergyChance * 60;
  439. $Try = 1;
  440.  
  441. do {
  442. if($ActualCount >= self::BOTS_MAX_ELEMENTS_BUILDS || $Try >= self::BOTS_MAX_TRY_BUILDS_ECO)
  443. break;
  444.  
  445. $Try++;
  446.  
  447. if($this->getChance($Chance))
  448. $List = self::$MineIDs;
  449. else
  450. $List = self::$EnergyIDs;
  451.  
  452. $Element = $this->getRandomList($List);
  453.  
  454. if(
  455. $this->getChance($List[$Element]) ||
  456. ($Element == 12 && $this->getPlanetResource(4) < self::$EnergyIDs[4] - 10) ||
  457. !BuildFunctions::IsTechnologieAccessible($this->USER, $this->PLANET, $Element) ||
  458. !BuildFunctions::IsElementBuyable($this->USER, $this->PLANET, $Element)
  459. ) continue;
  460.  
  461. $this->AddBuildingToQueue($Element);
  462. $ActualCount++;
  463. } while($this->getChance(self::CHANCE_BUILD_NEXT));
  464. }
  465.  
  466. function BuildSpecialBuildings()
  467. {
  468. global $resource;
  469. $ActualCount = $this->GetBuildQueueInfo();
  470. self::log("Buildtype: Special");
  471.  
  472. $Try = 1;
  473.  
  474. do {
  475. if($ActualCount >= self::BOTS_MAX_ELEMENTS_BUILDS || $Try >= self::BOTS_MAX_TRY_BUILDS_SPECIAL)
  476. break;
  477.  
  478. $Try++;
  479.  
  480. $Element = $this->getRandomList(self::$SpecialIDs);
  481.  
  482. if(
  483. $this->getChance(self::$SpecialIDs[$Element]) ||
  484. !BuildFunctions::IsTechnologieAccessible($this->USER, $this->PLANET, $Element) ||
  485. !BuildFunctions::IsElementBuyable($this->USER, $this->PLANET, $Element)
  486. ) continue;
  487.  
  488. $this->AddBuildingToQueue($Element);
  489. $ActualCount++;
  490. } while($this->getChance(self::CHANCE_BUILD_NEXT));
  491. }
  492.  
  493. function BuildStores()
  494. {
  495. global $resource;
  496.  
  497. $ActualCount = $this->GetBuildQueueInfo();
  498. self::log("Buildtype: Storage");
  499.  
  500. $RessourceWrapper = array(
  501. 22 => 'metal',
  502. 23 => 'crystal',
  503. 24 => 'deuterium'
  504. );
  505.  
  506. $Try = 1;
  507.  
  508. do {
  509. self::BOTS_MAX_ELEMENTS_BUILDS;
  510. self::BOTS_MAX_TRY_BUILDS_STORAGE;
  511.  
  512.  
  513. if($ActualCount >= self::BOTS_MAX_ELEMENTS_BUILDS || $Try >= self::BOTS_MAX_TRY_BUILDS_STORAGE) {
  514. break;
  515. }
  516.  
  517. $Try++;
  518.  
  519. $Element = $this->getRandomList(self::$StorageIDs);
  520.  
  521. if(
  522. $this->getChance(self::$StorageIDs[$Element]) ||
  523. $this->PLANET[$RessourceWrapper[$Element]] >= $this->PLANET[$RessourceWrapper[$Element].'_max'] * 0.95 ||
  524. !BuildFunctions::IsTechnologieAccessible($this->USER, $this->PLANET, $Element) ||
  525. !BuildFunctions::IsElementBuyable($this->USER, $this->PLANET, $Element)
  526. ) {
  527. continue;
  528. }
  529.  
  530. $this->AddBuildingToQueue($Element);
  531. $ActualCount++;
  532. } while($this->getChance(self::CHANCE_TECH_NEXT));
  533. }
  534.  
  535. function GetBuildQueueInfo()
  536. {
  537. $CurrentQueue = unserialize($this->PLANET['b_building_id']);
  538.  
  539. if (!empty($CurrentQueue))
  540. return count($CurrentQueue);
  541. else
  542. return 0;
  543. }
  544.  
  545. /** TECH BUILD FUNCTIONS **/
  546.  
  547. function ResearchTechs()
  548. {
  549. global $resource, $RESEARCH;
  550. if (!$this->CheckLabSettingsInQueue()) {
  551. return false;
  552. }
  553.  
  554. $ActualCount = $this->GetTechQueueInfo();
  555.  
  556. $Try = 1;
  557.  
  558. do {
  559. if($ActualCount >= self::BOTS_MAX_ELEMENTS_TECH || $Try >= self::BOTS_MAX_TRY_TECH)
  560. break;
  561.  
  562. $Try++;
  563.  
  564. $List = $this->getMultiplyRandomList(array(
  565. self::CHANCE_TECH_BASIC,
  566. self::CHANCE_TECH_ECO,
  567. self::CHANCE_TECH_SPEED,
  568. self::CHANCE_TECH_SPECIAL,
  569. ), array(
  570. self::$TechBasicIDs,
  571. self::$TechEcoIDs,
  572. self::$TechSpeedIDs,
  573. self::$TechSpecialIDs,
  574. ));
  575.  
  576. $Element = $this->getRandomList($List);
  577.  
  578.  
  579. if(
  580. $this->getChance($List[$Element]) ||
  581. !BuildFunctions::IsTechnologieAccessible($this->USER, $this->PLANET, $Element) ||
  582. !BuildFunctions::IsElementBuyable($this->USER, $this->PLANET, $Element)
  583. ) {
  584. continue;
  585. }
  586.  
  587. $this->ResearchBuild($Element);
  588. $ActualCount++;
  589. } while($this->getChance(self::CHANCE_BUILD_NEXT));
  590.  
  591. }
  592.  
  593. function CheckLabSettingsInQueue()
  594. {
  595. global $RESEARCH;
  596. $this->setGlobal();
  597. return BuildFunctions::CheckLabSettingsInQueue();
  598. }
  599.  
  600. function ResearchBuild($Element)
  601. {
  602. global $RESEARCH;
  603. $this->setGlobal();
  604. $this->LOG("Tech: ".$Element);
  605. BuildFunctions::addResearchToQueue($Element);
  606. //$GLOBALS['RESEARCH']->AddBuildingToQueue($Element);
  607. $this->getGlobal();
  608. }
  609.  
  610. function GetTechQueueInfo()
  611. {
  612. $CurrentQueue = unserialize($this->USER['b_tech_queue']);
  613.  
  614. if (!empty($CurrentQueue))
  615. return count($CurrentQueue);
  616. else
  617. return 0;
  618. }
  619.  
  620. /** FLEET BUILD FUNCTIONS **/
  621.  
  622. function BuildShipyard()
  623. {
  624. global $resource, $reslist, $SHIPYARD;
  625. $ActualCount = $this->GetHangerQueueInfo();
  626.  
  627. $Try = 1;
  628.  
  629. do {
  630. if($ActualCount >= self::BOTS_MAX_ELEMENTS_SHIPYARD || $Try >= self::BOTS_MAX_TRY_SHIPYARD)
  631. break;
  632.  
  633. $Try++;
  634.  
  635. $List = $this->getMultiplyRandomList(array(
  636. self::CHANCE_SHIPYARD_FLEET_BASIC,
  637. self::CHANCE_SHIPYARD_FLEET_ATTACK,
  638. self::CHANCE_SHIPYARD_DEF_BASIC,
  639. self::CHANCE_SHIPYARD_DEF_SHIELD,
  640. ), array(
  641. self::$FleetBasicIDs,
  642. self::$FleetAttackIDs,
  643. self::$DefBasicIDs,
  644. self::$DefShieldIDs,
  645. ));
  646.  
  647. $Element = $this->getRandomList($List);
  648. if(!$this->getChance($List[$Element]) || !BuildFunctions::IsTechnologieAccessible($this->USER, $this->PLANET, $Element)) {
  649. continue;
  650. }
  651.  
  652. $MaxElements = $this->GetMaxConstructibleElements($this->USER, $this->PLANET, $Element);
  653.  
  654. $Mode = $this->randomNum(0, 20);
  655. $Count = 1;
  656.  
  657. if($Mode === 20)
  658. $Count = $this->randomNum(0, 10);
  659. elseif($Mode >= 18)
  660. $Count = $this->randomNum(10, 20);
  661. elseif($Mode >= 16)
  662. $Count = $this->randomNum(20, 30);
  663. elseif($Mode >= 13)
  664. $Count = $this->randomNum(30, 40);
  665. elseif($Mode >= 9)
  666. $Count = $this->randomNum(40, 60);
  667. elseif($Mode >= 6)
  668. $Count = $this->randomNum(60, 70);
  669. elseif($Mode >= 4)
  670. $Count = $this->randomNum(70, 80);
  671. elseif($Mode >= 2)
  672. $Count = $this->randomNum(80, 90);
  673. elseif($Mode >= 1)
  674. $Count = $this->randomNum(90, 100);
  675.  
  676. $MaxFactor = array();
  677. $MaxFactor[] = round(3600 / BuildFunctions::GetBuildingTime($this->USER, $this->PLANET, $Element));
  678. $MaxFactor[] = $MaxElements * $Count;
  679.  
  680. $this->HangarBuild($Element, min($MaxFactor));
  681. $ActualCount++;
  682. } while($this->getChance(self::CHANCE_SHIPYARD_NEXT));
  683. }
  684.  
  685. function GetMaxConstructibleElements($USER, $PLANET, $Element)
  686. {
  687. global $SHIPYARD;
  688. $this->setGlobal();
  689. return BuildFunctions::getMaxConstructibleElements($USER, $PLANET, $Element);
  690. }
  691.  
  692. function HangarBuild($Element, $Count)
  693. {
  694. global $SHIPYARD;
  695. $this->setGlobal();
  696. BuildFunctions::addShipsToQueue(array($Element => $Count));
  697. $this->getGlobal();
  698. }
  699.  
  700. function GetHangerQueueInfo()
  701. {
  702. $CurrentQueue = unserialize($this->PLANET['b_hangar_id']);
  703.  
  704. if (!empty($CurrentQueue))
  705. return count($CurrentQueue);
  706. else
  707. return 0;
  708. }
  709.  
  710. /** FLEET FUNCTIONS **/
  711.  
  712. function SendFleet($FleetData)
  713. {
  714. $FleetData = array_merge(array(
  715. 'mission' => 3,
  716. 'galaxy' => 0,
  717. 'system' => 0,
  718. 'planet' => 0,
  719. 'planettype' => 0,
  720. 'fleetgroup' => 0,
  721. 'speed' => 10,
  722. 'metal' => 0,
  723. 'crystal' => 0,
  724. 'deuterium' => 0,
  725. 'holdingtime' => 0,
  726. 'fleet' => array(),
  727. ), $FleetData);
  728.  
  729. $mission = $FleetData['mission'];
  730. $galaxy = $FleetData['galaxy'];
  731. $system = $FleetData['system'];
  732. $planet = $FleetData['planet'];
  733. $planettype = $FleetData['planettype'];
  734. $fleet_group = $FleetData['fleetgroup'];
  735. $GenFleetSpeed = $FleetData['speed'];
  736. $TransportMetal = $FleetData['metal'];
  737. $TransportCrystal = $FleetData['crystal'];
  738. $TransportDeuterium = $FleetData['deuterium'];
  739. $holdingtime = $FleetData['holdingtime'];
  740. $rawfleetarray = $FleetData['fleet'];
  741.  
  742. if ($planettype != 1 && $planettype != 3)
  743. return false;
  744.  
  745. if ($this->PLANET['galaxy'] == $galaxy && $this->PLANET['system'] == $system && $this->PLANET['planet'] == $planet && $this->PLANET['planet_type'] == $planettype)
  746. return false;
  747.  
  748. if ($galaxy > $CONF['max_galaxy'] || $galaxy < 1 || $system > $CONF['max_system'] || $system < 1 || $planet > ($CONF['max_planets'] + 1) || $planet < 1)
  749. return false;
  750.  
  751. if (empty($mission))
  752. return false;
  753.  
  754. $ActualFleets = parent::GetCurrentFleets($USER['id']);
  755.  
  756. if (parent::GetMaxFleetSlots($USER) <= $ActualFleets)
  757. return false;
  758.  
  759. $fleet_group_mr = 0;
  760. if(!empty($fleet_group) && $mission == 2)
  761. {
  762. $aks_count_mr = $db->uniquequery("SELECT COUNT(*) as state FROM ".AKS." WHERE `id` = '".$fleet_group."' AND `eingeladen` LIKE '%".$USER['id']."%';");
  763. if ($aks_count_mr['state'] > 0)
  764. $fleet_group_mr = $fleet_group;
  765. else
  766. $mission = 1;
  767. }
  768.  
  769. $ActualFleets = parent::GetCurrentFleets($USER['id']);
  770.  
  771. $TargetPlanet = $db->uniquequery("SELECT `id`, `id_owner`,`destruyed`,`ally_deposit` FROM ".PLANETS." WHERE `universe` = '".$UNI."' AND `galaxy` = '".$galaxy."' AND `system` = '".$system."' AND `planet` = '".$planet."' AND `planet_type` = '".($planettype == 2 ? 1 : $planettype)."';");
  772.  
  773. if (($mission != 15 && $TargetPlanet["destruyed"] != 0) || ($mission != 15 && $mission != 7 && empty($TargetPlanet['id_owner'])))
  774. return false;
  775.  
  776. $MyDBRec = $USER;
  777.  
  778. $FleetArray = parent::GetFleetArray($rawfleetarray);
  779.  
  780. if (!is_array($FleetArray))
  781. return false;
  782.  
  783. $FleetStorage = 0;
  784. $FleetShipCount = 0;
  785. $fleet_array = "";
  786. $FleetSubQRY = "";
  787.  
  788. foreach ($FleetArray as $Ship => $Count)
  789. {
  790. if ($Count > $this->PLANET[$resource[$Ship]] || $Count < 0)
  791. return false;
  792.  
  793. $FleetStorage += $pricelist[$Ship]["capacity"] * $Count;
  794. $FleetShipCount += $Count;
  795. $fleet_array .= $Ship .",". $Count .";";
  796. $FleetSubQRY .= "`".$resource[$Ship] . "` = `".$resource[$Ship]."` - '".floattostring($Count)."', ";
  797. }
  798.  
  799. $error = 0;
  800. $fleetmission = $mission;
  801.  
  802. $YourPlanet = false;
  803. $UsedPlanet = false;
  804.  
  805. if ($mission == 11)
  806. {
  807. $maxexpde = parent::GetCurrentFleets($USER['id'], 11);
  808.  
  809. if ($maxexpde >= $CONF['max_dm_missions'])
  810. return false;
  811. }
  812. elseif ($mission == 15)
  813. {
  814. $MaxExpedition = $USER[$resource[124]];
  815.  
  816. if ($MaxExpedition == 0)
  817. return false;
  818.  
  819. $ExpeditionEnCours = parent::GetCurrentFleets($USER['id'], 15);
  820. $EnvoiMaxExpedition = floor(sqrt($MaxExpedition));
  821.  
  822. if ($ExpeditionEnCours >= $EnvoiMaxExpedition)
  823. return false;
  824. }
  825.  
  826. $YourPlanet = (isset($TargetPlanet['id_owner']) && $TargetPlanet['id_owner'] == $USER['id']) ? true : false;
  827. $UsedPlanet = (isset($TargetPlanet['id_owner'])) ? true : false;
  828.  
  829. $HeDBRec = ($YourPlanet) ? $MyDBRec : GetUserByID($TargetPlanet['id_owner'], array('id','onlinetime','ally_id', 'urlaubs_modus', 'banaday', 'authattack'));
  830.  
  831. if ($HeDBRec['urlaubs_modus'] && $mission != 8)
  832. return false;
  833.  
  834. if(!$YourPlanet && ($mission == 1 || $mission == 2 || $mission == 5 || $mission == 6 || $mission == 9))
  835. {
  836. if($CONF['adm_attack'] == 1 && $UsedPlanet['authattack'] > $USER['authlevel'])
  837. return false;
  838.  
  839. $UserPoints = $USER;
  840. $User2Points = $db->uniquequery("SELECT `total_points` FROM ".STATPOINTS." WHERE `stat_type` = '1' AND `id_owner` = '".$HeDBRec['id']."';");
  841.  
  842. $IsNoobProtec = CheckNoobProtec($UserPoints, $User2Points, $HeDBRec);
  843.  
  844. if ($IsNoobProtec['NoobPlayer'])
  845. return false;
  846. elseif ($IsNoobProtec['StrongPlayer'])
  847. return false;
  848. }
  849.  
  850. if ($mission == 5)
  851. {
  852.  
  853. if ($TargetPlanet['ally_deposit'] < 1)
  854. return false;
  855.  
  856. $buddy = $db->uniquequery("SELECT COUNT(*) as state FROM ".BUDDY." WHERE `active` = '1' AND (`owner` = '".$HeDBRec['id']."' AND `sender` = '".$MyDBRec['id']."') OR (`owner` = '".$MyDBRec['id']."' AND `sender` = '".$HeDBRec['id']."');");
  857.  
  858. if($HeDBRec['ally_id'] != $MyDBRec['ally_id'] && $buddy['state'] == 0)
  859. return false;
  860. }
  861. if(!parent::CheckUserSpeed($GenFleetSpeed) || !array_key_exists($mission, parent::GetAvailableMissions(array('CurrentUser' => $USER,'galaxy' => $galaxy, 'system' => $system, 'planet' => $planet, 'planettype' => $planettype, 'IsAKS' => $fleet_group, 'Ship' => $FleetArray))))
  862. return false;
  863.  
  864.  
  865. $MaxFleetSpeed = parent::GetFleetMaxSpeed($FleetArray, $USER);
  866. $SpeedFactor = parent::GetGameSpeedFactor();
  867. $distance = parent::GetTargetDistance($this->PLANET['galaxy'], $galaxy, $this->PLANET['system'], $system, $this->PLANET['planet'], $planet);
  868. $duration = parent::GetMissionDuration($GenFleetSpeed, $MaxFleetSpeed, $distance, $SpeedFactor, $USER);
  869. $consumption = parent::GetFleetConsumption($FleetArray, $duration, $distance, $MaxFleetSpeed, $USER, $SpeedFactor);
  870.  
  871. $fleet['start_time'] = $duration + TIMESTAMP;
  872.  
  873. if ($mission == 15)
  874. {
  875. $StayDuration = (max($holdingtime, 1) * 3600) / $CONF['halt_speed'];
  876. $StayTime = $fleet['start_time'] + $StayDuration;
  877. }
  878. elseif ($mission == 5)
  879. {
  880. $StayDuration = $holdingtime * 3600;
  881. $StayTime = $fleet['start_time'] + $StayDuration;
  882. }
  883. elseif ($mission == 11)
  884. {
  885. $StayDuration = 3600 / $CONF['halt_speed'];
  886. $StayTime = $fleet['start_time'] + $StayDuration;
  887. }
  888. else
  889. {
  890. $StayDuration = 0;
  891. $StayTime = 0;
  892. }
  893.  
  894. $fleet['end_time'] = $StayDuration + (2 * $duration) + TIMESTAMP;
  895.  
  896.  
  897. $FleetStorage -= $consumption;
  898.  
  899. $PlanetRess = new ResourceUpdate();
  900. $PlanetRess->CalcResource();
  901.  
  902. $TransportMetal = min($TransportMetal, $this->PLANET['metal']);
  903. $TransportCrystal = min($TransportCrystal, $this->PLANET['crystal']);
  904. $TransportDeuterium = min($TransportDeuterium, ($this->PLANET['deuterium'] - $consumption));
  905.  
  906. $StorageNeeded = $TransportMetal + $TransportCrystal + $TransportDeuterium;
  907.  
  908. $StockMetal = $this->PLANET['metal'];
  909. $StockCrystal = $this->PLANET['crystal'];
  910. $StockDeuterium = $this->PLANET['deuterium'];
  911. $StockDeuterium -= $consumption;
  912.  
  913. if ($this->PLANET['deuterium'] < $consumption)
  914. return false;
  915.  
  916. if ($StorageNeeded > $FleetStorage)
  917. return false;
  918.  
  919. $this->PLANET['metal'] -= $TransportMetal;
  920. $this->PLANET['crystal'] -= $TransportCrystal;
  921. $this->PLANET['deuterium'] -= ($TransportDeuterium + $consumption);
  922.  
  923. if ($fleet_group_mr != 0)
  924. {
  925. $AksStartTime = $db->uniquequery("SELECT MAX(`fleet_start_time`) AS Start FROM ".FLEETS." WHERE `fleet_group` = '".$fleet_group_mr."' AND '".$CONF['max_fleets_per_acs']."' > (SELECT COUNT(*) FROM ".FLEETS." WHERE `fleet_group` = '".$fleet_group_mr."');");
  926. if (isset($AksStartTime))
  927. {
  928. if ($AksStartTime['Start'] >= $fleet['start_time'])
  929. {
  930. $fleet['end_time'] += $AksStartTime['Start'] - $fleet['start_time'];
  931. $fleet['start_time'] = $AksStartTime['Start'];
  932. }
  933. else
  934. {
  935. $SQLFleets = "UPDATE ".FLEETS." SET ";
  936. $SQLFleets .= "`fleet_start_time` = '".$fleet['start_time']."', ";
  937. $SQLFleets .= "`fleet_end_time` = fleet_end_time + '".($fleet['start_time'] - $AksStartTime['Start'])."' ";
  938. $SQLFleets .= "WHERE ";
  939. $SQLFleets .= "`fleet_group` = '".$fleet_group_mr."';";
  940. $db->query($SQLFleets);
  941. $fleet['end_time'] += $fleet['start_time'] - $AksStartTime['Start'];
  942. }
  943. } else {
  944. $mission = 1;
  945. }
  946. }
  947.  
  948. $QryInsertFleet = "LOCK TABLE ".FLEETS." WRITE, ".PLANETS." WRITE;
  949. INSERT INTO ".FLEETS." SET
  950. `fleet_owner` = '".$USER['id']."',
  951. `fleet_mission` = '".$mission."',
  952. `fleet_amount` = '".$FleetShipCount."',
  953. `fleet_array` = '".$fleet_array."',
  954. `fleet_universe` = '".$UNI."',
  955. `fleet_start_time` = '".$fleet['start_time']."',
  956. `fleet_start_id` = '".$this->PLANET['id']."',
  957. `fleet_start_galaxy` = '".$this->PLANET['galaxy']."',
  958. `fleet_start_system` = '".$this->PLANET['system']."',
  959. `fleet_start_planet` = '".$this->PLANET['planet']."',
  960. `fleet_start_type` = '".$this->PLANET['planet_type']."',
  961. `fleet_end_time` = '".$fleet['end_time']."',
  962. `fleet_end_stay` = '".$StayTime."',
  963. `fleet_end_id` = '".(int)$TargetPlanet['id']."',
  964. `fleet_end_galaxy` = '".$galaxy."',
  965. `fleet_end_system` = '".$system."',
  966. `fleet_end_planet` = '".$planet."',
  967. `fleet_end_type` = '".$planettype."',
  968. `fleet_resource_metal` = '".floattostring($TransportMetal)."',
  969. `fleet_resource_crystal` = '".floattostring($TransportCrystal)."',
  970. `fleet_resource_deuterium` = '".floattostring($TransportDeuterium)."',
  971. `fleet_target_owner` = '".(($planettype == 2) ? 0 : (int)$TargetPlanet['id_owner'])."',
  972. `fleet_group` = '".$fleet_group_mr."',
  973. `start_time` = '".TIMESTAMP."';
  974. UPDATE `".PLANETS."` SET
  975. ".substr($FleetSubQRY,0,-2)."
  976. WHERE
  977. `id` = ". $this->PLANET['id'] ." LIMIT 1;
  978. UNLOCK TABLES;";
  979.  
  980.  
  981. $db->multi_query($QryInsertFleet);
  982. return true;
  983. }
  984.  
  985. function PlanetCount()
  986. {
  987. global $FLEET;
  988.  
  989. if(!isset($this->USER['planets']))
  990. $this->USER['planets'] = self::getDB()->countquery("SELECT COUNT(*) FROM ".PLANETS." WHERE `id_owner` = ".$this->USER['id']." AND `planet_type` = '1' AND `destruyed` = 0;");
  991.  
  992. return $this->USER['planets'];
  993. }
  994.  
  995. function isFleetSlotFree()
  996. {
  997. global $FLEET;
  998.  
  999. if(!isset($this->USER['slots'])) {
  1000. $this->USER['slots'] = FleetFunctions::GetMaxFleetSlots($this->USER) - FleetFunctions::GetCurrentFleets($this->USER['id']);
  1001. }
  1002.  
  1003. return $this->USER['slots'];
  1004. }
  1005.  
  1006. function Colonizeable()
  1007. {
  1008. if($this->getPlanetResource(208) == 0)
  1009. return false;
  1010.  
  1011. $iPlanetCount = $this->PlanetCount();
  1012. $MaxPlanets = MaxPlanets($this->getUserResource(124), $this->PLANET['universe']);
  1013.  
  1014. return $iPlanetCount < $MaxPlanets && $this->isFleetSlotFree();
  1015. }
  1016.  
  1017. function getCoords()
  1018. {
  1019. global $resource, $pricelist, $CONF;
  1020. $PlanetCount = $this->PlanetCount();
  1021.  
  1022. $GalaxyAmplitude = ceil($PlanetCount / 2);
  1023. $GalaxyMin = max($this->PLANET['galaxy'] - $Amplitude, 1);
  1024. $GalaxyMax = min($this->PLANET['galaxy'] + $Amplitude, $CONF['max_galaxy']);
  1025.  
  1026. $SystemAmplitude = ceil($PlanetCount / 1);
  1027. $SystemMin = max($this->PLANET['system'] - $Amplitude, 1);
  1028. $SystemMax = min($this->PLANET['system'] + $Amplitude, $CONF['max_galaxy']);
  1029.  
  1030. $PlanetMin = 1;
  1031. $PlanetMax = $CONF['max_planets'];
  1032.  
  1033. $i = 0;
  1034.  
  1035. do {
  1036. $Galaxy = $this->randomNum($GalaxyMin, $GalaxyMax);
  1037. $System = $this->randomNum($SystemMin, $SystemMax);
  1038. $Planet = $this->randomNum($PlanetMin, $PlanetMax);
  1039.  
  1040. if($i >= self::BOTS_MAX_TRY_COLONIZE)
  1041. return false;
  1042.  
  1043. $i++;
  1044. } while(CheckPlanetIfExist($Galaxy, $System, $Planet, $this->USER['universe']));
  1045.  
  1046. return array('galaxy' => $Galaxy, 'system' => $System, 'planet' => $Planet);
  1047. }
  1048.  
  1049. function Colonize()
  1050. {
  1051. $Coords = $this->getCoords();
  1052. if(!$Coords)
  1053. return;
  1054.  
  1055. $this->SendFleet(array(
  1056. 'mission' => 3,
  1057. 'galaxy' => $Coords['galaxy'],
  1058. 'system' => $Coords['sysstem'],
  1059. 'planet' => $Coords['planet'],
  1060. 'planettype' => 1,
  1061. 'fleetgroup' => 0,
  1062. 'speed' => 10,
  1063. 'metal' => 0,
  1064. 'crystal' => 0,
  1065. 'deuterium' => 0,
  1066. 'holdingtime' => 0,
  1067. 'fleet' => array(208 => 1),
  1068. ));
  1069. }
  1070.  
  1071. function SaveFleets()
  1072. {
  1073. $Coords = $this->getSaveCoords();
  1074. $this->SendFleet(array(
  1075. 'mission' => 7,
  1076. 'galaxy' => $Coords['galaxy'],
  1077. 'system' => $Coords['sysstem'],
  1078. 'planet' => $Coords['planet'],
  1079. 'planettype' => 1,
  1080. 'fleet' => array(208 => 1),
  1081. ));
  1082. }
  1083.  
  1084. function getSaveCoords()
  1085. {
  1086. $Bots = self::getDB()->query("");
  1087. }
  1088. }
Add Comment
Please, Sign In to add comment