In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

flash on 2013-1-17

Experiment with ai
goal is to make an entertaining one rather than a truly intelligent one
what it attempts to do is figure out where it can see you ( the mouse pointer in this case)
and move towards that point
if it cannot see you it gets bored and
walks around in a seemingly random pattern (but actually it tries to guess the path to take to find you, so it's cheating because it knows your position regardless, just pretends it doesn't)
Get Adobe Flash player
by mutantleg 17 Jan 2013

    Talk

    hemingway at 18 Jan 2013 19:08
    write a small script engine, where it can generate and parse all on it's own. provide some basic commands and constraints for this script engine as to what it can do with the canvas, or what objects it has domain over within the canvas, then add some basic rule set like for trial & error -or a problem-solving process like the 'scientific method' that is used for generating new code or editing what it already 'knows'
    mutantleg at 18 Jan 2013 23:28
    sounds good, only problem is i'm way too lazy to make something like that
    hemingway at 19 Jan 2013 00:08
    i got pretty close to true AI a year or two ago, writing the same concept in a different language. each AI has two threads, first thread is the execution of it's code, second thread is reflection of code. through reflection it constantly produces new situations depending on your rule set. the way i think about it: apply basic parsing techniques that humans demonstrate intuitively, as a rule set for the AI. although this skips the idea of genetic algorithms (at least in a fundamental form) it doesn't restrict it from growing into an algorithm from that point, but merely constraints it and provides guidelines to facilitate that process (like giving a human baby problem-solving wisdom of an old man)
    hemingway at 19 Jan 2013 00:10
    i suppose what would make it close to "true AI' even though it intuitively, is not, is the fact that it reflects upon it's code using a rule set. the only problem i can see is not providing enough constraints and the execution thread mimicing or overriding the reflection thread, that would be a mess

    Tags

    Embed
/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/bR1D
 */

 package {
    import flash.text.TextField;
    import flash.display.Graphics;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
      
        public var mheight:int = 0;
        public var mwidth:int = 0;
        public var vecGrid:Vector.<int>;
        public var cw:Number = 16;
      
        public function FlashTest() {
            
            mwidth = 22;
            mheight = 22;
            
            graphics.clear();
            
            var num:int;
            var i:int;
            var k:int;
            var t:int;
            var yt:int;
      
            num = mwidth*mheight;
            
            vecGrid = new Vector.<int>(num, false);
            
            for (i = 0; i < mheight; i++)
            {
                yt = i * mwidth;
                for (k = 0; k < mwidth; k++)
                {
                    t = Math.random() < 0.2 ? 1 : 0 ;
                    vecGrid[yt+k]=t;
                    if (t > 0)
                    {
                        graphics.beginFill(0,1);
                        graphics.drawRect(k*cw,i*cw,cw,cw);
                        graphics.endFill();
                    }
                    
                }//nextit
            }//nexti      
            
            deb = new Sprite();
            addChild(deb);
            
            dt = new TextField();
            addChild(dt);
            dt.y = 350;
            
            cx = 200;
            cy = 200;
            
            var k:int;
             k =0;
           
            while (k < 256)
            {
                if (isWall(cx, cy))
                {
                  cx+=cw;
                   
                }
                else
                {
                  break;  
                }
                
                k+=1;
            } 
            
            stage.addEventListener(Event.ENTER_FRAME, onEnter);
        }//ctor
        
        public var deb:Sprite;
        public var dt:TextField;
        
        public var cx:Number = 0;
        public var cy:Number = 0;
        public var vx:Number = 0;
        public var vy:Number = 0;
        
        public var px:Number = 0;
        public var py:Number = 0;
        public var pt:int = 0;      
      public function onEnter(e:Event):void
      {
          var g:Graphics;
          g = deb.graphics;
          
          g.clear();
          g.lineStyle(2, 0xFF0000);
          g.drawCircle(mouseX, mouseY, 4);
          
          if (isWall(mouseX, mouseY) )
          {
            g.drawRect( Math.floor(mouseX/cw)*cw, Math.floor(mouseY/cw)*cw,cw,cw);  
              
          }
          
          var t:Number;
         // t = lineTest(300,300, mouseX, mouseY);
          
          //dt.text = " " + t;
          
          
          t = lineTest(cx,cy, mouseX,mouseY);

                      
          if (t >= 1)
          {
              var mag:Number;
              vx = mouseX-cx;
              vy = mouseY-cy;
              mag = Math.sqrt(vx*vx+vy*vy);
              if (mag == 0) { mag = 0.0000001;}
              vx/=mag;
              vy/=mag;
              
              pt = 1;
              px = mouseX;
              py = mouseY;
              
             if (mag < 20) { pt = 0; vx = 0; vy = 0;}
          }
          else
          {
              if (pt > 0)
              {
                vx = px-cx;
                vy = py-cy;
                mag = Math.sqrt(vx*vx+vy*vy);
                if (mag == 0) { mag = 0.0000001;}
                //if (mag < 10) { pt = 1;}
                
                
                vx/=mag;
                vy/=mag;
                if (mag < 16) { pt = 0;}
              }
              
          }
          
          if (pt <= 0) 
          {
               pt -= 1;  
            if (pt <= -20)
            {
               
               var k:int;
               
               for (k = 0; k < 8; k+=1)
               {
                  px = cx + Math.random() * 40;
                  py = cy + Math.random() * 40;
              
                  if (lineTest(cx,cy, px,py) >= 1) { break;}
               }
              
                if (Math.random() < 0.4)
                {
                  px = mouseX + Math.random()*80;
                  py = mouseY + Math.random()*80;  
                }
              
              pt = 1;  
            }
           
           }
          
          
          vx *= 0.96;
          vy *= 0.96;
          
          var wc:int;
          wc = 0;
          
          if (vx>0 && isWall(cx+4,cy)) { vx = 0; wc = 1;}
          if (vx<0 && isWall(cx-4,cy)) { vx = 0; wc = 1;}
          if (vy>0 && isWall(cx,cy+4)) { vy = 0; wc = 1;}
          if (vy<0 && isWall(cx,cy-4)) { vy = 0; wc = 1;}
          
          if (wc > 0) { pt +=1;  if (pt >=8) {pt =0; } }
         
          
          
          cx+=vx;
          cy+=vy;
          
          g.lineStyle(2,0xFF0000);
          g.drawCircle(cx,cy, 8);
          
          g.lineStyle(2,0x0000FF);
          g.drawCircle(px,py,4);
          
          
      }//onenter
      
      public function lineTest(ax:Number, ay:Number, bx:Number, by:Number):Number
      {
          var nx:Number;
          var ny:Number;
          var mag:Number;
          var u:Number;
          var t:Number;
          
          nx = bx-ax;
          ny = by-ay;
          mag = Math.sqrt(nx*nx+ny*ny);
          if (mag == 0) { return 1; }
          
          nx /= mag;
          ny /= mag;
          
          u = (cw/2) / mag;
          
          var kx:Number;
          var ky:Number;
          
          for (t = 0; t < 1; t+=u)
          {
              kx = ax+(bx-ax)*t;
              ky = ay+(by-ay)*t;
              
              deb.graphics.drawCircle(kx,ky, 2);
              
              if (isWall(kx,ky))
              {
                return t;  
               }
          }//nextt 
          
          
          
          return 1;
      }//linetest
      
      
      
       public function isWall(wx:Number, wy:Number):Boolean
       {
          var tx:int;
          var ty:int;
          
          tx = Math.floor(wx / cw);
          ty = Math.floor(wy / cw);     
           
           if (tx < 0) { return false; }
           if (ty < 0) { return false; }
           if (tx >= mwidth) { return false; }
           if (ty >= mheight) { return false; }
           
           
          return vecGrid[tx + (ty*mwidth) ]; 
       }//iswall
      
        
    }//classends
}