Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var base = this;
  2. var enemyBase = base.getEnemies()[ 0 ];
  3. if( typeof base.init === "undefined" )
  4. {
  5.     this.startTime = this.now();
  6.    
  7.     base.enemyUnits =
  8.     {
  9.         "soldier" : 0,
  10.         "knight" : 1,
  11.         "librarian" : 2,
  12.         "griffin-rider" : 3,
  13.         "captain" : 4,
  14.         "peasant" : 5
  15.     };
  16.    
  17.     base.friendlyUnits =
  18.     [
  19.         "munchkin",
  20.         "ogre",
  21.         "shaman",
  22.         "fangrider",
  23.         "brawler",
  24.         "peon"
  25.     ];
  26.    
  27.     base.init = new function()
  28.     {
  29.         base.say( "Base initted!" );
  30.     };
  31.    
  32.     base.frame = function ()
  33.     {
  34.         base.slavesUpdate();
  35.        
  36.         if( ( base.getByType( "peon" ).length ) < 2 )
  37.         {
  38.             base.build( "peon" );
  39.             return;
  40.         }
  41.        
  42.         base.mirrorEnemy();
  43.         base.buildSlaveIfCan();
  44.        
  45.         if( ( base.now() - base.startTime ) > 100 )
  46.             base.charge();
  47.         else
  48.         {
  49.             base.say( base.now() - base.startTime );
  50.             base.mirrorEnemy();
  51.             base.buildSlaveIfCan();
  52.         }
  53.     };
  54.    
  55.     base.chargeToggle = false;
  56.     base.charge = function()
  57.     {
  58.         base.say( "CHAAAARGE!" );
  59.        
  60.         if( !base.chargeToggle )
  61.             base.build( "ogre" );
  62.         else
  63.             base.build( "shaman" );
  64.            
  65.         base.chargeToggle = !base.chargeToggle;
  66.     };
  67.    
  68.     base.slavesUpdate = function()
  69.     {
  70.         var peons = base.getByType( "peon" );
  71.         var peasants = base.getByType( "peasant" );
  72.         var allSlaves = peons.concat( peasants );
  73.        
  74.         for( var slaveIndex in allSlaves )
  75.         {
  76.             var slave = allSlaves[ slaveIndex ];
  77.             if( typeof slave.init === "undefined" )
  78.                 base.initSlave( slave );
  79.             else
  80.                 slave.frame();
  81.         }
  82.     };
  83.    
  84.     base.getNearestSlave = function( coin )
  85.     {
  86.         var peons = base.getByType( "peon" );
  87.         var peasants = base.getByType( "peasant" );
  88.         var slaves = peons.concat( peasants );
  89.        
  90.         var nearestSlave = slaves[ 0 ];
  91.         for( var slaveIndex in slaves )
  92.         {
  93.             var slave = slaves[ slaveIndex ];
  94.            
  95.             if( coin.pos.distance( slave.pos ) < coin.pos.distance( nearestSlave.pos ) )
  96.             {
  97.                 nearestSlave = slave;
  98.             }
  99.         }
  100.        
  101.         return nearestSlave;
  102.     };
  103.    
  104.     base.initSlave = function (pawn)
  105.     {
  106.         pawn.init = new function()
  107.         {
  108.             base.say(
  109.                 "Initting pawn: " +
  110.                 pawn.id +
  111.                 " (" + pawn.team + ")"
  112.             );
  113.            
  114.             pawn.targetCoin = null;
  115.            
  116.             pawn.frame = function()
  117.             {
  118.                 if( pawn.team == "humans" )
  119.                     pawn.enemyFrame();
  120.                 else
  121.                     pawn.myFrame();
  122.             };
  123.            
  124.             pawn.myFrame = function()
  125.             {
  126.                 pawn.pickCoin();
  127.                
  128.                 if( pawn.targetCoin === null )
  129.                 {
  130.                       pawn.pickCoin();  
  131.                 }
  132.                 else
  133.                 {
  134.                    
  135.                     pawn.wasCoinCollected();
  136.                 }
  137.             };
  138.            
  139.             pawn.wasCoinCollected = function()
  140.             {
  141.                 if( pawn.pos.distance( pawn.targetCoin.pos ) < 2.0 )
  142.                 {
  143.                     pawn.targetCoin = null;
  144.                 }
  145.             };
  146.            
  147.             pawn.enemyFrame = function()
  148.             {
  149.             };
  150.            
  151.             pawn.pickCoin = function()
  152.             {
  153.                 pawn.targetCoin = pawn.getClosestFreeCoin();
  154.                
  155.                 base.command( pawn, "move", pawn.targetCoin.pos );
  156.             };
  157.            
  158.             pawn.getClosestFreeCoin = function()
  159.             {
  160.                 var coins = base.getItems();
  161.                
  162.                 for( var coinIndex in coins )
  163.                 {
  164.                     var coin = coins[ coinIndex ];
  165.                    
  166.                     if( pawn == base.getNearestSlave( coin ) )
  167.                     {
  168.                         return coin;
  169.                     }
  170.                 }
  171.                
  172.                 return coins[
  173.                     Math.floor( Math.random() * coins.length  )
  174.                 ];
  175.             };
  176.         }();
  177.     };
  178.    
  179.     base.mirroredEnemies = [];
  180.     base.mirrorEnemy = function()
  181.     {
  182.         var enemies = base.getEnemies();
  183.        
  184.         for( var i = 1; i < enemies.length; i++ )
  185.         {
  186.             var enemy = enemies[ i ];
  187.             if( base.mirroredEnemies.indexOf( enemy.id ) == -1 )
  188.             {
  189.                 base.mirroredEnemies.push( enemy.id );
  190.                
  191.                 var mirrorType = base.getMyTypeById( enemy.id );
  192.                 base.say( "Trying to mirror: " + mirrorType );
  193.                
  194.                 base.build( mirrorType );
  195.             }
  196.         }
  197.     };
  198.    
  199.     base.buildSlaveIfCan = function()
  200.     {
  201.         if( ( base.gold - enemyBase.gold ) > 100 )
  202.         {
  203.             if( ( base.now() - base.startTime ) < 80 )
  204.                 base.build( "peon" );
  205.         }
  206.     };
  207.    
  208.     base.getMyTypeById = function( id )
  209.     {
  210.         var enemies = base.getEnemies();
  211.        
  212.         for( var i = 0; i < enemies.length; i++ )
  213.         {
  214.             var enemy = enemies[ i ];
  215.            
  216.             if( enemy.id == id )
  217.             {
  218.                 var index = base.enemyUnits[ enemy.type ];
  219.                
  220.                 return base.friendlyUnits[ index ];
  221.             }
  222.         }
  223.     };
  224. }
  225. else
  226. {
  227.     base.frame();
  228. }
  229. ยดvar base = this;
  230. var enemyBase = this.getEnemies()[ 0 ];
  231.  
  232. if( typeof base.init === "undefined" )
  233. {
  234.    
  235. base.init = function()
  236. {
  237.     base.coins = base.getItems();
  238.     base.peons = base.getByType( "peon" );  
  239. }();
  240.  
  241. base.getCoinRadiusValue = function( coin, coinRadius )
  242. {
  243.     var radiusValue = coin.bountyGold;
  244.    
  245.     for( var coinIndex in base.coins )
  246.     {
  247.         var otherCoin = base.coins[ coinIndex ];
  248.        
  249.         if( otherCoin.pos.distance( coin.pos ) < coinRadius )
  250.         {
  251.             radiusValue += otherCoin.bountyGold;
  252.         }
  253.     }
  254.    
  255.     return radiusValue;
  256. };
  257.  
  258. base.findCoinsInRadius = function( peon, radius )
  259. {
  260.     var coinsInRadius = [];
  261.    
  262.     for( var coinIndex in base.coins )
  263.     {
  264.         var coin = base.coins[ coinIndex ];
  265.        
  266.         if( coin.pos.distance( peon.pos ) < radius )
  267.         {
  268.             coinsInRadius.push( coin );
  269.         }
  270.     }
  271.    
  272.     return coinsInRadius;
  273. };
  274.  
  275. base.findBestCoin = function( peon, inRadius, radiusValue )
  276. {
  277.     var coins = base.findCoinsInRadius( peon, inRadius );
  278.    
  279.     var coinWithBestValue = null;
  280.     for( var coinIndex in coins )
  281.     {
  282.         var coin = coins[ coinIndex ];
  283.         if( coinWithBestValue === null )
  284.         {
  285.             coinWithBestValue = coin;
  286.         }
  287.         else
  288.         {
  289.             var coinRadiusValue = radiusValue;
  290.             if( base.getCoinRadiusValue( coin, coinRadiusValue ) > base.getCoinRadiusValue( coinWithBestValue, coinRadiusValue ) )
  291.             {
  292.                 coinWithBestValue = coin;
  293.             }
  294.         }
  295.     }
  296.    
  297.     return coinWithBestValue;
  298. };
  299.  
  300. base.peonsFindGold = function()
  301. {
  302.     for( var peonIndex in base.peons )
  303.     {
  304.         var peon = base.peons[ peonIndex ];
  305.        
  306.         var coin = base.findBestCoin( peon, 25, 10 );
  307.     }
  308. };
  309.  
  310. }
  311.  
  312. if( base.built.length === 0 )
  313. {
  314.     base.build( "peon" );
  315. }
  316.  
  317. if( ( base.built.length - base.peons.length ) + 1 < enemyBase.built.length )
  318. {
  319.     base.build( "ogre" );
  320. }
  321. else
  322. {
  323.     if( base.gold >= 75 )
  324.     {
  325.         base.build( "peon" );
  326.     }
  327. }
  328.  
  329. base.peonsFindGold();
  330. var base = this;
  331. var enemyBase = base.getEnemies()[ 0 ];
  332. if( typeof base.init === "undefined" )
  333. {
  334.     this.startTime = this.now();
  335.    
  336.     base.enemyUnits =
  337.     {
  338.         "soldier" : 0,
  339.         "knight" : 1,
  340.         "librarian" : 2,
  341.         "griffin-rider" : 3,
  342.         "captain" : 4,
  343.         "peasant" : 5
  344.     };
  345.    
  346.     base.friendlyUnits =
  347.     [
  348.         "munchkin",
  349.         "ogre",
  350.         "shaman",
  351.         "fangrider",
  352.         "brawler",
  353.         "peon"
  354.     ];
  355.    
  356.     base.init = new function()
  357.     {
  358.         base.say( "Base initted!" );
  359.     };
  360.    
  361.     base.frame = function ()
  362.     {
  363.         base.slavesUpdate();
  364.         base.mirrorEnemy();
  365.         base.buildSlaveIfCan();
  366.        
  367.         if( ( base.now() - base.startTime ) > 100 )
  368.             base.charge();
  369.         else
  370.         {
  371.             base.say( base.now() - base.startTime );
  372.             base.mirrorEnemy();
  373.             base.buildSlaveIfCan();
  374.         }
  375.     };
  376.    
  377.     base.chargeToggle = false;
  378.     base.charge = function()
  379.     {
  380.         base.say( "CHAAAARGE!" );
  381.        
  382.         if( !base.chargeToggle )
  383.             base.build( "ogre" );
  384.         else
  385.             base.build( "shaman" );
  386.            
  387.         base.chargeToggle = !base.chargeToggle;
  388.     };
  389.    
  390.     base.slavesUpdate = function()
  391.     {
  392.         var peons = base.getByType( "peon" );
  393.         var peasants = base.getByType( "peasant" );
  394.         var allSlaves = peons.concat( peasants );
  395.        
  396.         for( var slaveIndex in allSlaves )
  397.         {
  398.             var slave = allSlaves[ slaveIndex ];
  399.             if( typeof slave.init === "undefined" )
  400.                 base.initSlave( slave );
  401.             else
  402.                 slave.frame();
  403.         }
  404.     };
  405.    
  406.     base.getNearestSlave = function( coin )
  407.     {
  408.         var peons = base.getByType( "peon" );
  409.         var peasants = base.getByType( "peasant" );
  410.         var slaves = peons.concat( peasants );
  411.        
  412.         var nearestSlave = slaves[ 0 ];
  413.         for( var slaveIndex in slaves )
  414.         {
  415.             var slave = slaves[ slaveIndex ];
  416.            
  417.             if( coin.pos.distance( slave.pos ) < coin.pos.distance( nearestSlave.pos ) )
  418.             {
  419.                 nearestSlave = slave;
  420.             }
  421.         }
  422.        
  423.         return nearestSlave;
  424.     };
  425.    
  426.     base.initSlave = function (pawn)
  427.     {
  428.         pawn.init = new function()
  429.         {
  430.             base.say(
  431.                 "Initting pawn: " +
  432.                 pawn.id +
  433.                 " (" + pawn.team + ")"
  434.             );
  435.            
  436.             pawn.targetCoin = null;
  437.            
  438.             pawn.frame = function()
  439.             {
  440.                 if( pawn.team == "humans" )
  441.                     pawn.enemyFrame();
  442.                 else
  443.                     pawn.myFrame();
  444.             };
  445.            
  446.             pawn.myFrame = function()
  447.             {
  448.                 if( pawn.targetCoin === null )
  449.                 {
  450.                       pawn.pickCoin();  
  451.                 }
  452.                 else
  453.                 {
  454.                     pawn.wasCoinCollected();
  455.                 }
  456.             };
  457.            
  458.             pawn.wasCoinCollected = function()
  459.             {
  460.                 if( pawn.pos.distance( pawn.targetCoin.pos ) < 2.0 )
  461.                 {
  462.                     pawn.targetCoin = null;
  463.                 }
  464.             };
  465.            
  466.             pawn.enemyFrame = function()
  467.             {
  468.             };
  469.            
  470.             pawn.pickCoin = function()
  471.             {
  472.                 pawn.targetCoin = pawn.getClosestFreeCoin();
  473.                
  474.                 base.command( pawn, "move", pawn.targetCoin.pos );
  475.             };
  476.            
  477.             pawn.getClosestFreeCoin = function()
  478.             {
  479.                 var coins = base.getItems();
  480.                
  481.                 for( var coinIndex in coins )
  482.                 {
  483.                     var coin = coins[ coinIndex ];
  484.                    
  485.                     if( pawn == base.getNearestSlave( coin ) )
  486.                     {
  487.                         return coin;
  488.                     }
  489.                 }
  490.                
  491.                 return coins[
  492.                     Math.floor( Math.random() * coins.length  )
  493.                 ];
  494.             };
  495.         }();
  496.     };
  497.    
  498.     base.mirroredEnemies = [];
  499.     base.mirrorEnemy = function()
  500.     {
  501.         var enemies = base.getEnemies();
  502.        
  503.         for( var i = 1; i < enemies.length; i++ )
  504.         {
  505.             var enemy = enemies[ i ];
  506.             if( base.mirroredEnemies.indexOf( enemy.id ) == -1 )
  507.             {
  508.                 base.mirroredEnemies.push( enemy.id );
  509.                
  510.                 var mirrorType = base.getMyTypeById( enemy.id );
  511.                 base.say( "Trying to mirror: " + mirrorType );
  512.                
  513.                 base.build( mirrorType );
  514.             }
  515.         }
  516.     };
  517.    
  518.     base.buildSlaveIfCan = function()
  519.     {
  520.         var enemySoldiers = base.getEnemies().length;
  521.         enemySoldiers -= base.getByType( "peasant" ).length;
  522.        
  523.         var mySoldiers = base.getFriends().length;
  524.         mySoldiers -= base.getByType( "peon" ).length;
  525.        
  526.         if( enemySoldiers > mySoldiers + 1 )
  527.         {
  528.             base.build( "ogre" );
  529.         }
  530.         else if( ( base.gold - enemyBase.gold ) > 100 )
  531.         {
  532.             if( ( base.now() - base.startTime ) < 80 )
  533.                 base.build( "peon" );
  534.     };
  535.    
  536.     base.getMyTypeById = function( id )
  537.     {
  538.         var enemies = base.getEnemies();
  539.        
  540.         for( var i = 0; i < enemies.length; i++ )
  541.         {
  542.             var enemy = enemies[ i ];
  543.            
  544.             if( enemy.id == id )
  545.             {
  546.                 var index = base.enemyUnits[ enemy.type ];
  547.                
  548.                 return base.friendlyUnits[ index ];
  549.             }
  550.         }
  551.     };
  552. }
  553. else
  554. {
  555.     base.frame();
  556. }
  557. var base = this;
  558. var enemyBase = this.getEnemies()[ 0 ];
  559. var peons = base.getByType( "peon" );
  560. var coins = base.getItems();
  561.  
  562. var sectors = [];
  563. sectors[ 0 ] = new Vector( 17, 49 );
  564. sectors[ 1 ] = new Vector( 47, 68 );
  565. sectors[ 2 ] = new Vector( 46, 11 );
  566. sectors[ 3 ] = new Vector( 73, 32 );
  567.  
  568. var sectorDistance = 20;
  569.  
  570. var sectorsWithCoins = [];
  571.  
  572. function sectorCoins()
  573. {
  574.     sectorsWithCoins[ 0 ] = [];
  575.     sectorsWithCoins[ 1 ] = [];
  576.     sectorsWithCoins[ 2 ] = [];
  577.     sectorsWithCoins[ 3 ] = [];
  578.    
  579.     for( var coinIndex in coins )
  580.     {
  581.         var coin = coins[ coinIndex ];
  582.        
  583.         var closestSector = 0;
  584.         for( var i = 0; i < sectors.length; i++ )
  585.         {
  586.             var secPos = sectors[ i ];
  587.            
  588.             if(
  589.                 coin.pos.distance( secPos ) <
  590.                 coin.pos.distance( sectors[ closestSector ] )
  591.             )
  592.             {
  593.                 closestSector = i;
  594.             }
  595.         }
  596.        
  597.         sectorsWithCoins[ closestSector ].push( coin );
  598.     }
  599. }
  600.  
  601. sectorCoins();
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621. for( var peonIndex in peons )
  622. {
  623.     var peon = peons[ peonIndex ];
  624.    
  625.     if( peons.length < 4 )
  626.     {
  627.             var coin = peon.getNearest( coins );
  628.             base.command( peon, "move", coin.pos );
  629.             break;
  630.     }
  631.    
  632.     if( peonIndex > 3 )
  633.     {
  634.         var enemySlave = base.getByType( "peasant" )[ 0 ];
  635.        
  636.         if( peon.pos.distance( enemySlave.pos ) > 5 )
  637.             base.command( peon, "move", enemySlave.pos );
  638.         else
  639.         {
  640.             var coin = peon.getNearest( coins );
  641.             base.command( peon, "move", coin.pos );
  642.         }
  643.     }
  644.    
  645.     var coin = peon.getNearest( coins );
  646.    
  647.     if( coin )
  648.     {
  649.         var go = true;
  650.        
  651.         for( var peonIndex2 in peons )
  652.         {
  653.             var secondPeon = peons[ peonIndex2 ];
  654.            
  655.             if( secondPeon.target !== null )
  656.             {
  657.                 if( secondPeon.target == coin )
  658.                 {
  659.                     go = false;
  660.                 }
  661.             }
  662.         }
  663.        
  664.         if( go )
  665.         {
  666.             if( typeof peon.sector !== "undefined" )
  667.             {
  668.                 if( peon.pos.distance( sectors[ peon.sector ] ) > sectorDistance )
  669.                 {
  670.                     base.command( peon, "move", sectors[ peon.sector ] );
  671.                     return;
  672.                 }
  673.             }
  674.            
  675.             peon.target = coin;
  676.             base.command( peon, "move", coin.pos );
  677.         }
  678.     }
  679. }
  680.  
  681. if( base.built.length === 0 )
  682. {
  683.     base.build( "peon" );
  684. }
  685.  
  686. if( peons.length < 5 )
  687. {
  688.     for( var i = 0; i < peons.length; i++ )
  689.     {
  690.         peons[ i ].sector = i;
  691.     }
  692. }
  693.  
  694. if( ( base.built.length - peons.length ) + 1 < enemyBase.built.length )
  695. {
  696.     base.build( "ogre" );
  697. }
  698. else
  699. {
  700.     if( base.gold >= 75 )
  701.     {
  702.         base.build( "peon" );
  703.     }
  704. }
  705. var base = this;
  706. var enemyBase = this.getEnemies()[ 0 ];
  707.  
  708. if( typeof base.init === "undefined" )
  709. {  
  710.     /*
  711.         BASE CODE ***
  712.     */
  713.     base.init = function()
  714.     {
  715.         base.createSector( new Vector( 20, 20 ) );
  716.     };
  717.    
  718.     base.frame = function()
  719.     {
  720.         base.coins = base.getItems();
  721.         base.peons = base.getByType( "peon" );    
  722.        
  723.         base.sectorCoins();
  724.     };
  725.    
  726.     /*
  727.         SECTOR SPECIFIC CODE ***
  728.     */
  729.     base.sectorRadius = 20; //In what radius has the coin have to be to be in a sector
  730.     base.sectors = [];
  731.    
  732.     /*
  733.         Each sector contains source position
  734.         and, well, coins (derp)
  735.     */
  736.     base.Sector = function()
  737.     {
  738.         this.pos = null;
  739.         this.coins = [];
  740.     };
  741.    
  742.     /*
  743.         Create sector with source position
  744.     */
  745.     base.createSector = function( sourcePos )
  746.     {
  747.         var newSector = new base.Sector();
  748.         newSector.pos = sourcePos;
  749.        
  750.         base.sectors.push( newSector );
  751.     };
  752.    
  753.     /*
  754.         Give each coin a sector
  755.     */
  756.     base.sectorCoins = function()
  757.     {
  758.         /*
  759.             Loop through all the coins and determine their sector
  760.         */
  761.         for( var coinIndex in base.coins ) //for #1
  762.         {
  763.             var coin = base.coins[ coinIndex ];
  764.        
  765.             /*
  766.                 Loop through all the sectors and
  767.                 determine if coin is in sectors radius
  768.                 if so, push that coin into that sector
  769.             */
  770.             for( var i = 0; i < base.sectors.length; i++ ) //for #2
  771.             {
  772.                 var sector = base.sectors[ i ];
  773.                
  774.                 if( coin.pos.distance( sector.pos ) < base.sectorRadius )
  775.                 {
  776.                     sector.coins.push( coin );
  777.                     break; //Found sector for coin, lets break out of "for #2"
  778.                 }
  779.             }
  780.         }
  781.     };
  782.    
  783.     base.init();
  784. }
  785.  
  786. base.frame();
  787. var main = this;
  788.  
  789. /*
  790.     Global settings
  791. */
  792. _SECTOR_UPDATE_RATE = 5;
  793. _SECTOR_RADIUS = 20;
  794.  
  795. var ObjectType =
  796. {
  797.     SLAVE : 0,
  798.     SECTOR : 1,
  799.     SECTOR_MANAGER : 2
  800. };
  801.  
  802. var SectorManager = function()
  803. {
  804.     var core = {};
  805.     core.type = ObjectType.SECTOR_MANAGER;
  806.     core.counter = 0;
  807.    
  808.     core.sectors = [];
  809.    
  810.     core.createSector = function( centerPos )
  811.     {
  812.         core.sectors.push( new Sector( centerPos ) );
  813.     };
  814.    
  815.     core.frame = function()
  816.     {
  817.         /*
  818.             Update sectors if needed
  819.             or increment counter.
  820.         */
  821.        
  822.         core.counter++;
  823.        
  824.         if( core.counter == _SECTOR_UPDATE_RATE )
  825.         {
  826.             core.counter = 0;
  827.             core.updateSectors();
  828.         }
  829.     };
  830.    
  831.     /*
  832.         Designate coins into their sectors
  833.     */
  834.     core.updateSectors = function()
  835.     {  
  836.         /*
  837.             Clean sectors
  838.         */
  839.         for( var sectorIndex in core.sectors )
  840.         {
  841.             var sector = core.sectors[ sectorIndex ];
  842.             sector.coins = [];
  843.             sector.value = 0;
  844.         }
  845.        
  846.         /*
  847.             Push coins into proper sectors
  848.         */
  849.         for( var coinIndex in main.coins )
  850.         {
  851.             var coin = main.coins[ coinIndex ];
  852.            
  853.             core.pushCoinIntoSector( coin );
  854.         }
  855.     };
  856.    
  857.     /*
  858.         Helper function for ^
  859.     */
  860.     core.pushCoinIntoSector = function( coin )
  861.     {
  862.         var closestSector = null;
  863.         for( var i = 0; i < core.sectors.length; i++ )
  864.         {
  865.             var sector = core.sectors[ i ];
  866.                
  867.             if( closestSector === null )
  868.                 closestSector = sector;
  869.             else
  870.             {
  871.                 if(
  872.                     coin.pos.distance( sector.pos.distance ) <
  873.                     coin.pos.distance( closestSector.pos.distance )
  874.                 )
  875.                 {
  876.                     closestSector = sector;
  877.                 }
  878.             }
  879.         }
  880.        
  881.         coin.sector = closestSector;    
  882.         //closestSector.value += coin.bountyGold;
  883.         //closestSector.coins.push( coin );
  884.     };
  885.    
  886.     core.init = function()
  887.     {
  888.     }();
  889.    
  890.     main.objects.push( core );
  891.     return core;    
  892. };
  893.  
  894. var Sector = function( centerPos )
  895. {
  896.     var core = {};
  897.     core.type = ObjectType.SECTOR;
  898.     core.value = 0;
  899.     core.coins = [];
  900.    
  901.     core.init = function( centerPos )
  902.     {
  903.         core.pos = centerPos;
  904.     }();
  905.    
  906.     main.objects.push( core );
  907.     return core;
  908. };
  909.  
  910. var Slave = function( pawn, sector )
  911. {
  912.     var core = {};
  913.     core.type = ObjectType.SLAVE;
  914.     core.pawn = pawn;
  915.    
  916.     core.frame = function()
  917.     {
  918.         main.say( "Doing frame: " + main.frameCount );
  919.         var coin = main.coins[ Math.floor( Math.random() * main.coins.length ) ];
  920.        
  921.         main.command( core.pawn, "move", coin.pos );
  922.     };
  923.    
  924.     core.init = function()
  925.     {
  926.     }();
  927.    
  928.     main.objects.push( core );
  929.     return core;
  930. };
  931.  
  932. if( typeof main.init === "undefined" )
  933. {
  934.     main.objects = [];
  935.     main.init = true;
  936.     main.frameCount = 0;
  937.    
  938.     main.coins = null;
  939.     main.pawns = null;
  940.    
  941.     main.sectorManager = new SectorManager();
  942.    
  943.     main.frame = function()
  944.     {
  945.         main.frameCount++;
  946.        
  947.  
  948.     /*
  949.         Update basic shit
  950.     */
  951.     main.coins = main.getItems();
  952.     main.pawns = main.getByType( "peon" );
  953.    
  954.     /*
  955.         Loop objects
  956.             #1 Call frame function on these functions
  957.     */
  958.     for( var o = 0; o < main.objects.length; o++ )
  959.     {
  960.         var obj = main.objects[ o ];
  961.        
  962.         if( typeof obj.frame !== "undefined" )
  963.             obj.frame();
  964.     }
  965.    
  966.     /*
  967.         Loop peon pawns
  968.             #1 Init slaves if need to
  969.     */
  970.     for( var p = 0; p < main.pawns.length; p++ )
  971.     {
  972.         var pawn = main.pawns[ p ];
  973.        
  974.         if( typeof pawn.init === "undefined" )
  975.         {
  976.             pawn.init = true;
  977.             new Slave( pawn );
  978.         }
  979.     }
  980.    
  981.     /*
  982.         Increment frame count
  983.     */
  984.     };
  985.    
  986.     main.build( "peon" );
  987. }
  988. else
  989. {
  990.     main.frame();
  991. }
  992. var _ = this;
  993. var coins = _.getItems();
  994.  
  995. var d1 = "";
  996. var d2 = "";
  997. var d3 = "";
  998. var d4 = "";
  999. var d5 = "";
  1000. var d6 = "";
  1001. var d7 = "";
  1002. var d8 = "";
  1003. var d9 = "";
  1004.  
  1005. /*
  1006.     Init game
  1007. */
  1008. if( typeof _.init === "undefined" )
  1009. {
  1010.     _.init = true;
  1011.     _.slaves = [];
  1012.     _.pwns = [];
  1013.    
  1014.     _.say( "Game initted: " + Math.random() );
  1015.    
  1016.     _.getNearestSlave = function( coin )
  1017.     {
  1018.         var nearestSlave = _.slaves[ 0 ];
  1019.         for( var slaveIndex in _.slaves )
  1020.         {
  1021.             var slave = _.slaves[ slaveIndex ];
  1022.            
  1023.             if( coin.pos.distance( slave.pos ) < coin.pos.distance( nearestSlave.pawn.pos ) )
  1024.             {
  1025.                 nearestSlave = slave;
  1026.             }
  1027.         }
  1028.        
  1029.         return nearestSlave;
  1030.     };
  1031.    
  1032.     _.initSlaves = function()
  1033.     {
  1034.         var enemySlaves = _.getByType( "peasant" );
  1035.         var mySlaves = _.getByType( "peon" );
  1036.        
  1037.         var allSlaves = enemySlaves.concat( mySlaves );
  1038.        
  1039.         for( var slaveIndex in allSlaves )
  1040.         {
  1041.             var slavePawn = allSlaves[ slaveIndex ];
  1042.            
  1043.             if( typeof _.pwns[ slavePawn.id ] === "undefined" )
  1044.             {
  1045.                 var newSlave = new _.Slave( slavePawn );
  1046.                 _.pwns[ slavePawn.id ] = true;
  1047.                
  1048.                 _.say( "Got slave: " + newSlave );
  1049.             }
  1050.         }
  1051.     };
  1052.    
  1053.     _.mySlaveFrames = function()
  1054.     {
  1055.         for( var i = 0; i < _.slaves.length; i++ )
  1056.         {
  1057.             var slav = _.slaves[ i ];
  1058.             slav.frame();
  1059.         }
  1060.     };
  1061.    
  1062.     _.frame = function()
  1063.     {
  1064.         _.initSlaves();
  1065.         _.mySlaveFrames();
  1066.     };
  1067.  
  1068.     _.Slave = function( pawn )
  1069.     {
  1070.         var parent = this;
  1071.        
  1072.         parent.init = function()
  1073.         {
  1074.             _.slaves.push( this );
  1075.             parent.pawn = pawn;
  1076.             parent.targetCoin = null;
  1077.             _.say( "New slave created, id is: " + parent.pawn.id + " - " + Math.random() * 10 );
  1078.         };
  1079.        
  1080.         this.frame = function()
  1081.         {
  1082.             _.say( "Hey hoo!" );
  1083.             if( parent.targetcoin === null )
  1084.             {
  1085.                 parent.pickCoin();
  1086.             }
  1087.         };
  1088.        
  1089.         parent.pickCoin = function()
  1090.         {
  1091.             parent.targetCoin = parent.closestFreecoin();
  1092.            
  1093.             _.command( pawn, "move", parent.targetCoin.pos );
  1094.         };
  1095.        
  1096.         parent.closestFreeCoin = function()
  1097.         {
  1098.             var coins = _.getItems();
  1099.            
  1100.             for( var coinIndex in coins )
  1101.             {
  1102.                 var coin = coins[ coinIndex ];
  1103.                
  1104.                 if( parent == _.getNearestSlave( coin ) )
  1105.                 {
  1106.                     return coin;
  1107.                 }
  1108.             }
  1109.         };
  1110.        
  1111.         parent.init();
  1112.     };
  1113. }
  1114.  
  1115. _.frame();
  1116.  
  1117. if( _.built.length < 5 )
  1118. {
  1119.     _.build( "peon" );
  1120. }
  1121. else
  1122. {
  1123.     _.build( "ogre" );
  1124. }
  1125.  
  1126. if( true )
  1127. {
  1128.     for( var i = 0; i < _.getByType( "peon" ).length; i++ )
  1129.     {
  1130.         var peon = _.getByType( "peon" )[ i ];
  1131.         var item = peon.getNearest( _.getItems() );
  1132.        
  1133.         if( item )
  1134.             _.command( peon, "move", item.pos );
  1135.     }
  1136. }
  1137. var base = this;
  1138.  
  1139. if( typeof base.init === "undefined" )
  1140. {
  1141.     base.init = new function()
  1142.     {
  1143.         base.say( "sup" );
  1144.     };
  1145. }
  1146. var base = this;
  1147. var items = base.getItems();
  1148. var peons = base.getByType( "peon" );
  1149.  
  1150. if( typeof this.frame === "undefined" )
  1151. {
  1152.     this.frame = 0;
  1153.     this.enemyBase = this.getEnemies()[ 0 ];
  1154.     this.startTime = this.now();
  1155. }
  1156.  
  1157. if( base.frame === 0 )
  1158. {
  1159.     base.frames = [];
  1160.    
  1161.     base.registerFrame = function( frameCount, frameFunc )
  1162.     {
  1163.         base.frames[ frameCount ] = frameFunc;
  1164.     };
  1165.    
  1166.     base.initPeons = function()
  1167.     {
  1168.         for( var i = 0; i < base.built.length; i++ )
  1169.         {
  1170.             if( base.built[ i ].type != "peon" )
  1171.                 continue;
  1172.            
  1173.             var peon =  base.built[ i ];
  1174.            
  1175.             if( typeof peon.init === "undefined" )
  1176.             {
  1177.                 peon.init = true;
  1178.                 base.initPeon( i );
  1179.             }
  1180.         }
  1181.     };
  1182.    
  1183.     base.initPeon = function( count )
  1184.     {
  1185.         base.say( "Initted peon!" );
  1186.         var peon = base.built[ count ];
  1187.         peon.init = true;
  1188.         peon.targetArrived = false;
  1189.         peon.randomCoin = null;
  1190.         peon.lastPos = new Vector( 0, 0 );
  1191.        
  1192.         peon.goToCamp = function()
  1193.         {
  1194.             base.command( peon, "move", peon.campPos );
  1195.         };
  1196.        
  1197.         peon.frame = function()
  1198.         {    
  1199.             if( peon.lastPos == peon.pos )
  1200.                 peon.targetArrived = true;
  1201.                
  1202.             peon.lastPos = peon.pos;
  1203.            
  1204.             if( peon.targetArrived )
  1205.                 peon.getClosestCoin();  
  1206.         };
  1207.        
  1208.         peon.getClosestCoin = function()
  1209.         {
  1210.             base.say( "Getting new coin! " + base.frame );
  1211.             peon.targetArrived = false;
  1212.            
  1213.             var item = peon.getNearest( base.getItems() );
  1214.            
  1215.             var random = Math.floor( Math.random() * 5 );
  1216.            
  1217.             if( random >= 4 )
  1218.             {
  1219.                 item = items[ Math.floor( Math.random() * items.length ) ];
  1220.                 peon.randomCoin = item;
  1221.             }
  1222.            
  1223.             if( item )
  1224.             {
  1225.                 base.command( peon, "move", item.pos );
  1226.             }
  1227.         };
  1228.        
  1229.         peon.getClosestCoin();
  1230.     };
  1231.    
  1232.     base.doFrame = function()
  1233.     {  
  1234.         if( typeof base.frames[ base.frame ] !== "undefined" )
  1235.         {
  1236.             base.frames[ base.frame ]();
  1237.         }
  1238.        
  1239.         base.initPeons();
  1240.        
  1241.         for( var a = 0; a < base.built.length; a++ )
  1242.         {
  1243.             if( typeof base.built[ a ].frame !== "undefined" )
  1244.             {
  1245.                 base.built[ a ].frame();
  1246.             }
  1247.         }
  1248.        
  1249.         base.frame++;
  1250.     };
  1251. }
  1252.  
  1253. if( base.built.length < 1 )
  1254.     base.build( "peon" );
  1255. else
  1256. {
  1257.     var enemySoldiers = base.getEnemies().length;
  1258.     enemySoldiers -= base.getByType( "peasant" ).length;
  1259.    
  1260.     var mySoldiers = base.getFriends().length;
  1261.     mySoldiers -= base.getByType( "peon" ).length;
  1262.    
  1263.     if( enemySoldiers > mySoldiers )
  1264.     {
  1265.         base.build( "munchkin" );
  1266.     }
  1267.     else
  1268.     {
  1269.         if( ( this.now() - this.startTime ) < 120 )
  1270.         {
  1271.             if( peons.length < 10 )
  1272.                 base.build( "peon" );
  1273.         }
  1274.         else
  1275.         {
  1276.             var random = Math.floor( Math.random() * 5 );
  1277.             if( random >= 2 )
  1278.                 base.build( "shaman" );
  1279.             else
  1280.                 base.build( "munchkin" );
  1281.         }
  1282.     }
  1283. }
  1284.  
  1285. base.doFrame();
  1286. var base = this;
  1287. var enemyBase = this.getEnemies()[ 0 ];
  1288.  
  1289. if( typeof base.init === "undefined" )
  1290. {
  1291.    
  1292. base.init = function()
  1293. {
  1294.     base.coins = base.getItems();
  1295.     base.peons = base.getByType( "peon" );  
  1296. }();
  1297.  
  1298. base.getCoinRadiusValue = function( coin, coinRadius )
  1299. {
  1300.     var radiusValue = coin.bountyGold;
  1301.    
  1302.     for( var coinIndex in base.coins )
  1303.     {
  1304.         var otherCoin = base.coins[ coinIndex ];
  1305.        
  1306.         if( otherCoin.pos.distance( coin.pos ) < coinRadius )
  1307.         {
  1308.             radiusValue += otherCoin.bountyGold;
  1309.         }
  1310.     }
  1311.    
  1312.     return radiusValue;
  1313. };
  1314.  
  1315. base.findCoinsInRadius = function( peon, radius )
  1316. {
  1317.     var coinsInRadius = [];
  1318.    
  1319.     for( var coinIndex in base.coins )
  1320.     {
  1321.         var coin = base.coins[ coinIndex ];
  1322.        
  1323.         if( coin.pos.distance( peon.pos ) < radius )
  1324.         {
  1325.             coinsInRadius.push( coin );
  1326.         }
  1327.     }
  1328.    
  1329.     return coinsInRadius;
  1330. };
  1331.  
  1332. base.findBestCoin = function( peon, inRadius, radiusValue )
  1333. {
  1334.     var coins = base.findCoinsInRadius( peon, inRadius );
  1335.    
  1336.     var coinWithBestValue = null;
  1337.     for( var coinIndex in coins )
  1338.     {
  1339.         var coin = coins[ coinIndex ];
  1340.         if( coinWithBestValue === null )
  1341.         {
  1342.             coinWithBestValue = coin;
  1343.         }
  1344.         else
  1345.         {
  1346.             var coinRadiusValue = radiusValue;
  1347.             if( base.getCoinRadiusValue( coin, coinRadiusValue ) > base.getCoinRadiusValue( coinWithBestValue, coinRadiusValue ) )
  1348.             {
  1349.                 coinWithBestValue = coin;
  1350.             }
  1351.         }
  1352.     }
  1353.    
  1354.     return coinWithBestValue;
  1355. };
  1356.  
  1357. base.peonsFindGold = function()
  1358. {
  1359.     for( var peonIndex in base.peons )
  1360.     {
  1361.         var peon = base.peons[ peonIndex ];
  1362.        
  1363.         var coin = null;
  1364.         var iterator = 1;
  1365.         while( coin === null )
  1366.         {
  1367.             coin = base.findBestCoin( peon, 25 * iterator, 10 * iterator );
  1368.            
  1369.             for( var peonIndex2 in base.peons )
  1370.             {
  1371.                 var secondPeon = base.peons[ peonIndex2 ];
  1372.                    
  1373.                 if( secondPeon == peon )
  1374.                     continue;
  1375.                    
  1376.                 if( secondPeon.target === null )
  1377.                     continue;
  1378.                    
  1379.                 if( coin == secondPeon.target )
  1380.                 {
  1381.                     coin = null;
  1382.                 }
  1383.             }
  1384.            
  1385.             if( iterator > 100 )
  1386.             {
  1387.                 coin = base.findBestRandomCoin();
  1388.             }
  1389.            
  1390.             iterator++;
  1391.         }
  1392.        
  1393.         peon.target = coin;
  1394.         base.command( peon, "move", coin.pos );
  1395.     }
  1396. };
  1397.  
  1398. base.findBestRandomCoin = function()
  1399. {
  1400.     return base.coins[ Math.floor( Math.random() * base.coins.length ) ];
  1401. };
  1402.  
  1403. }
  1404.  
  1405. if( base.built.length === 0 )
  1406. {
  1407.     base.build( "peon" );
  1408. }
  1409.  
  1410. if( ( base.built.length - base.peons.length ) + 1 < enemyBase.built.length )
  1411. {
  1412.     base.build( "ogre" );
  1413. }
  1414. else
  1415. {
  1416.     if( base.gold >= 75 )
  1417.     {
  1418.         base.build( "peon" );
  1419.     }
  1420. }
  1421.  
  1422. base.peonsFindGold();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement