notice: Flash editor updated! Join the development! Thanks to MiniBuilder


embed

FORKED
  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.text.TextField;
  4.     import flash.system.Security;
  5.     dynamic public class MazeWalker extends Sprite {
  6.         /**
  7.          * Simple maze walker for http://tinyurl.com/aibattle
  8.          * @see http://makc3d.wordpress.com/2009/10/31/ai-battle-again/
  9.          */
  10.         public function MazeWalker () {
  11.             Security.allowDomain ("http://makc.googlecode.com/");
  12.             if (stage != null) {
  13.                 var tf:TextField = new TextField;
  14.                 tf.autoSize = "left";
  15.                 tf.text = stage.loaderInfo.url;
  16.                 addChild (tf);
  17.             }
  18.         }
  19.         private var lastAngle:int = -1;
  20.         public function behavior ():void {
  21.             // find closest wall
  22.             var i:int, d:Number = 1e6, a:Number = 0;
  23.             for (i=0; i< this.inColors.length; i++) {
  24.                 if (this.inColors [i] == 0) {
  25.                     if (this.inDistances [i] < d) {
  26.                         a = i; d = this.inDistances [i];
  27.                     }
  28.                 }
  29.             }
  30.             if (d > 20) {
  31.                 if (lastAngle < 0) {
  32.                     // get closer
  33.                     this.outMoveAngle = a;
  34.                 } else {
  35.                     // maintain direction
  36.                     this.outMoveAngle = lastAngle;
  37.                 }
  38.             } else {
  39.                 // move along the wall
  40.                 lastAngle = (a + 9) % 36;
  41.                 this.outMoveAngle = lastAngle;
  42.             }
  43.             this.outMoveSpeed = this.inMaxSpeed;
  44.         }
  45.     }
  46. }
noswf
  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.text.TextField;
  4.     import flash.system.Security;
  5.     dynamic public class scooby56 extends Sprite {
  6.         /**
  7.          * scooby56 bot for http://tinyurl.com/aibattle
  8.          * @scooby56
  9.          * @see http://makc3d.wordpress.com/2009/10/31/ai-battle-again/
  10.          */
  11.         public function scooby56 () {
  12.             Security.allowDomain ("http://makc.googlecode.com/");
  13.             if (stage != null) {
  14.                 var tf:TextField = new TextField;
  15.                 tf.autoSize = "left";
  16.                 tf.text = stage.loaderInfo.url;
  17.                 addChild (tf);
  18.             }
  19. this.objective = 2;
  20. this.danger = false;
  21.         }
  22. private function search ():int {
  23.     var d:scooby56 = this;
  24.     //loop through all angles return direction to items in the following priority
  25.     for (var i:int=0; i< d.inColors.length; i++){
  26.         //1. check theres no danger
  27.         if (d.inColors[i] == 3 && d.inDistances[i] < 100){
  28.             
  29.             //make a desision on direction to evade
  30.             var evadeDirection:Array = new Array();
  31.             evadeDirection[1] = i - 9;
  32.             evadeDirection[2] = i - 18;
  33.             evadeDirection[3] = i + 9;
  34.             
  35.             var bestDirection:int = 0
  36.             for (var j:int=0;j<3;j++){
  37.                 if (evadeDirection[j] < 0)
  38.                     evadeDirection[j] += 36;
  39.                 if (evadeDirection[j] > 36)
  40.                     evadeDirection[j] -= 36;
  41.                 
  42.                 if (d.inDistances[evadeDirection[j]] > bestDirection)
  43.                     bestDirection = evadeDirection[j];
  44.             }
  45.             
  46.             //d.trace('retreat bestDirection:' + bestDirection)
  47.             d.danger = true;
  48.             return bestDirection;
  49.         }
  50.         
  51.         d.danger = false;
  52.         
  53.                 
  54.         //2. current objective location
  55.         if (d.inColors[i] == d.objective)
  56.             return i;    
  57.             
  58.     }
  59.     // next line was not part of scooby56 script
  60.     return -1;
  61. }
  62. private function changeDirection ():void {
  63.     var d:scooby56 = this;
  64.     var i:int, arrOptions:Array = new Array();
  65.     //look in all 4 directions
  66.     for (i=0;i<37;i+=9){
  67.         //over 100 px = possible option
  68.         if(d.inDistances[i] > 100)
  69.             arrOptions.push(i)        
  70.     }
  71.     
  72.     //if cornered, (ie all options < 100) loop all angles for exit, take most open option
  73.     if(arrOptions.length < 1){
  74.         var intLargest:Number = 0;
  75.         for (i=0;i<37;i++){
  76.             if(d.inDistances[i] > intLargest)
  77.                 intLargest = d.inDistances[i]
  78.         }
  79.         arrOptions.push(intLargest)
  80.     };
  81.         
  82.     //pick one direction from available at random
  83.     var rand:int = Math.floor(Math.random() * 2)
  84.     d.outMoveAngle = arrOptions[rand]
  85.     d.outMoveSpeed = d.inMaxSpeed;
  86. }
  87.         public function behavior ():void {
  88. this.outThrowBullet = false;
  89. //set away if not moving
  90. if(!this.outMoveSpeed || this.outMoveAngle == undefined || this.inDistances[this.outMoveAngle] == undefined){
  91.     this.changeDirection();    
  92. };
  93. //set objective
  94. this.objective = (this.inHasBullet) ? 12;
  95. //this.trace("objective: " + this.objective)
  96. //look for objects
  97. var dir:int = this.search();
  98. if (dir > -1){
  99.     //this.trace("dir @ " + dir)
  100.     this.outMoveSpeed = this.inMaxSpeed;
  101.     this.outMoveAngle = dir;
  102.     if(this.objective == 1){
  103.         if(!this.danger){
  104.             if(this.inHasBullet){
  105.                 if(this.inDistances[dir] < 120){
  106.                     //this.trace("firing at " + dir + " danger: " + this.danger)
  107.                     this.outThrowAngle = dir;
  108.                     this.outThrowBullet = true;
  109.                     this.objective = 2;
  110.                 }
  111.             };
  112.         };
  113.     };
  114.     
  115. else {
  116.     //if heading towards a wall
  117.     if(this.inColors[this.outMoveAngle] == 0 ){        
  118.         if(this.inDistances[this.outMoveAngle] < 50){            
  119.             this.changeDirection();    
  120.             //this.trace("angle: " + this.outMoveAngle + " distance: " + this.inDistances[this.outMoveAngle])
  121.             
  122.         }             
  123.     }
  124. }
  125.         }
  126.     }
  127. }
noswf
  1. // forked from makc3d's Basic bot
  2. package {
  3.     import flash.display.Sprite;
  4.     import flash.text.TextField;
  5.     import flash.system.Security;
  6.     dynamic public class hohoho extends Sprite {
  7.         /**
  8.          * hohoho bot for http://tinyurl.com/aibattle
  9.          * @author hohoho
  10.          * @see http://makc3d.wordpress.com/2009/10/31/ai-battle-again/
  11.          */
  12.         public function hohoho () {
  13.             Security.allowDomain ("http://makc.googlecode.com/");
  14.             if (stage != null) {
  15.                 var tf:TextField = new TextField;
  16.                 tf.autoSize = "left";
  17.                 tf.text = stage.loaderInfo.url;
  18.                 addChild (tf);
  19.             }
  20. this.dodging=false;
  21.         }
  22.         public function behavior ():void {
  23. var lookang:int, theta:int, bestangle:int;
  24. this.outMoveSpeed = this.inMaxSpeed;
  25. this.outThrowBullet = false;
  26. // Find a bullet
  27. var closestbullet:Number = 1000;
  28. if(!this.inHasBullet){
  29. for(lookang=0;lookang<36;lookang++){
  30. if (this.inColors[lookang] == 2){
  31. if(this.inDistances[lookang] < closestbullet){
  32. bestangle = lookang;
  33. closestbullet = this.inDistances[lookang];
  34. }
  35. }
  36. }
  37. }
  38. // Find open spaces
  39. var maxdist:Number = 0;
  40. if (closestbullet == 1000){
  41. for(theta=-8;theta<=8;theta++){
  42. lookang = this.outMoveAngle + theta;
  43. if (lookang > 35) lookang = lookang - 36;
  44. if (lookang < 0) lookang = 36 + lookang;
  45. if ((this.inDistances[lookang] > maxdist) && (this.inColors[lookang] == 0)){
  46. maxdist = this.inDistances[lookang];
  47. if(theta>1)
  48. bestangle = this.outMoveAngle + 2;
  49. else if(theta<-1)
  50. bestangle = this.outMoveAngle - 2;
  51. else bestangle = lookang; 
  52. }
  53. if(maxdist < 15){
  54. bestangle = bestangle - 18;
  55. }
  56. }
  57. //Randomize when it's aimless.
  58. if(Math.random() < 0.4){ 
  59. bestangle = bestangle + 1
  60. }
  61. if(bestangle < 0) bestangle = bestangle + 36;
  62. if(bestangle > 35) bestangle = bestangle - 36;
  63. }
  64. // Find enemy
  65. var foundenemy:Boolean = false;
  66. if (this.inHasBullet){
  67. for(lookang=0;lookang<36;lookang++){
  68. if (this.inColors[lookang] == 1){
  69. bestangle = lookang;
  70. this.outThrowAngle = lookang;
  71. foundenemy = true;
  72. }
  73. }
  74. }
  75. // Dodge bullets
  76. if((this.dodging==false) && this.inMaxSpeed >= 3){
  77. for(lookang=0; lookang<36; lookang++){
  78. if(this.inColors[lookang] == 3){
  79. this.dodging = true;
  80. theta = lookang + 11;
  81. if (theta > 35) theta = theta - 36;
  82. maxdist = this.inDistances[theta];
  83. bestangle = lookang - 11;
  84. if (bestangle < 0) bestangle = bestangle + 36;
  85. if(this.inDistances[bestangle] < maxdist) bestangle = theta;
  86. this.outMoveAngle = bestangle;
  87. }
  88. }
  89. }
  90. // Finish dodging bullets
  91. if(this.inMaxSpeed >= 3){
  92. this.dodging = false;
  93. for(lookang=0; lookang<36; lookang++){
  94. if(this.inColors[lookang] == 3)
  95. this.dodging = true;
  96. }
  97. }
  98. else this.dodging = false;
  99. if (!this.dodging) this.outMoveAngle = bestangle;
  100. if (foundenemy && (this.inDistances[this.outMoveAngle] < 80)) this.outThrowBullet = true;
  101.         }
  102.     }
  103. }
noswf
  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.text.TextField;
  4.     import flash.system.Security;
  5.     dynamic public class parad0xl0g extends Sprite {
  6.         /**
  7.          * parad0xl0g bot for http://tinyurl.com/aibattle
  8.          * @author parad0xl0g
  9.          * @see http://makc3d.wordpress.com/2009/10/31/ai-battle-again/
  10.          */
  11.         public function parad0xl0g () {
  12.             Security.allowDomain ("http://makc.googlecode.com/");
  13.             if (stage != null) {
  14.                 var tf:TextField = new TextField;
  15.                 tf.autoSize = "left";
  16.                 tf.text = stage.loaderInfo.url;
  17.                 addChild (tf);
  18.             }
  19.             this.curAngle = 27;
  20.         }
  21.         public function behavior ():void {
  22. var i:int, wallAt:int = -1, enemyAt:int = -1;
  23. for (i=0; i< this.inColors.length; i++) {
  24.   if (this.inColors[i] == 0 && this.inDistances[i]<20) {
  25.     wallAt = i; break;
  26.   }
  27. }
  28. if ( wallAt > -1 ){
  29.   var minAngle:int = wallAt  + 18;
  30.   var maxAngle:int  = minAngle + 9;
  31.   var temp:int = Math.floor( Math.random()*(maxAngle-minAngle)+minAngle );
  32.   this.curAngle = (temp >35)?(temp%35):temp;
  33. }
  34. this.outMoveAngle = this.curAngle;
  35. this.outMoveSpeed = this.inMaxSpeed;
  36. if (this.inHasBullet){
  37.   var dangerAt:int = -1;
  38.   enemyAt = -1;
  39.   for (i=0; i< this.inColors.length; i++) {
  40.     if (this.inColors[i] == 3) {
  41.       dangerAt = i; break;
  42.     }
  43.     if (this.inColors[i] == 1) {
  44.       enemyAt = i; break;
  45.     }
  46.   }
  47.   if ( dangerAt > -1) {
  48.     this.outMoveAngle = ( dangerAt >26 ) ? ( 35-(dangerAt+9)  ) : ( dangerAt-9 );
  49.     this.outMoveSpeed = this.inMaxSpeed;
  50.     ifthis.inDistances[dangerAt]<50 ){
  51.       enemyAt = -1;
  52.       for (i=0; i< this.inColors.length; i++) {
  53.         if (this.inColors[i] == 1) {
  54.           enemyAt = i; break;
  55.         }
  56.       }
  57.       if ( enemyAt > -1){
  58.         this.outMoveSpeed = 0;
  59.         this.outThrowBullet = true;
  60.         this.outThrowAngle = enemyAt;
  61.       }
  62.     }
  63.   }
  64.   else if (enemyAt > -1 && this.inDistances[enemyAt]<250) {
  65.     this.outMoveSpeed = 0;
  66.     this.outThrowBullet = true;
  67.     this.outThrowAngle = enemyAt;
  68.   }
  69.   else{
  70.     this.outThrowBullet = false;
  71.   }
  72. }
  73. else
  74. {
  75.   var bulletAt:int = -1;
  76.   for (i=0; i< this.inColors.length; i++) {
  77.     if (this.inColors[i] == 2) {
  78.       bulletAt = i; break;
  79.     }
  80.   }
  81.   if( bulletAt>-1 ){
  82.     this.outMoveAngle = bulletAt ;
  83.     this.outMoveSpeed = this.inMaxSpeed;
  84.   } 
  85. }
  86.         }
  87.     }
  88. }
noswf
Get Adobe Flash Player