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

forked from: My Bots 0.25 (あそべるぞ)

My Bots 0.25
Original work by itatsu
Feb.2.2009
Hold down left mousebutton and drag to scroll.
Left click to select a bot.
Your bot are indicated as a red one,
and as a red dot in the minimap.
While selecting your own bot,
you can move around your mouse to guide them,
and also you'll be able to upgrade its parameter,
by clicking on the upgrade button near your status panel.
If you deselected your bot, it will be assigned to an AI,
the same as other bots.
When your bot die, the game is over.
Press R to restart the game.
Press Space to pause/resume.
Press F to free the viewpoint.
Press S to show/hide the HP bar.
Press Q to direct select your bot.
Bots撃破のエフェクトを追加しました。
とりあえずあそべるようになりました。
どうみてもクソゲーです。自分でもうまくプレーできないww
追記:最初の所持金増加によって難易度が下がる。はず。
Get Adobe Flash player
by hacker_blyns37k 02 Feb 2009
// forked from itatsu's My Bots 0.25 (あそべるぞ)
// My Bots 0.25
// Original work by itatsu
// Feb.2.2009
//
// Hold down left mousebutton and drag to scroll.
// Left click to select a bot.
// Your bot are indicated as a red one,
//   and as a red dot in the minimap.
// While selecting your own bot,
//   you can move around your mouse to guide them,
//   and also you'll be able to upgrade its parameter,
//   by clicking on the upgrade button near your status panel.
// If you deselected your bot, it will be assigned to an AI,
//   the same as other bots.
// When your bot die, the game is over.
// Press R to restart the game.
// Press Space to pause/resume.
// Press F to free the viewpoint.
// Press S to show/hide the HP bar.
// Press Q to direct select your bot.
// 
// Bots撃破のエフェクトを追加しました。
// とりあえずあそべるようになりました。
// どうみてもクソゲーです。自分でもうまくプレーできないww
// 追記:最初の所持金増加によって難易度が下がる。はず。

package {
	import flash.display.*;
	import flash.events.*;

	[SWF(width="465", height="465", backgroundColor="30", frameRate="60")]
	public class MyBots extends Sprite {
		public function MyBots() {
			global = this;

			stage.quality = StageQuality.HIGH;

			Initialize();

			global.addEventListener(Event.ENTER_FRAME, Update);
			stage.addEventListener(MouseEvent.CLICK, MouseClick);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
		}
	}
}

import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.filters.*;

// Global object
var global:Sprite;
// Main text field
var tField:TextField = new TextField;
// Sub text field
var stField:TextField = new TextField;
// Game over text field
var gtField:TextField = new TextField;
// Pause text field
var ptField:TextField = new TextField;
// Frame ticks
var ticks:uint;

// Upgrade buttons
var ub:Vector.<TextField> = new Vector.<TextField>();

// Bots vector
var bots:Vector.<Bot> = new Vector.<Bot>();
// Bullets vector
var bullets:Vector.<Bullet> = new Vector.<Bullet>();
// Splashdots vector
var splashdots:Vector.<Splashdot> = new Vector.<Splashdot>();

// Viewpoint
var viewpoint:Point = new Point(0, 0); 		// Top left of FOV
var tempviewpoint:Point = new Point(0, 0); 	// Top left of FOV
var curviewpoint:Point = new Point(0, 0); 	// Top left of FOV
// Mousedownpoint
var mousedownpoint:Point = new Point(0, 0);

// Selected bot
var selectedbot:Bot;

// Your bot
var mybot:Bot;

// Paused?
var paused:Boolean = false;
// Draw HP bar?
var drawhpbar:Boolean = true;
// Game over?
var gameover:Boolean = false;

// Bot count from start
var botcount:uint = 0;

// Map size
const mapsizex:uint = 1000;
const mapsizey:uint = 1000;

// Performance related
const maxbots:uint = 10;

// Parameters
const StandardHP:Array = [100.0, 140.0, 180.0, 220.0, 260.0, 300.0, 340.0, 380.0, 420.0, 460.0, 500.0];
const StandardSpeed:Array = [1.0, 1.12, 1.24, 1.36, 1.48, 1.60, 1.72, 1.84, 1.96, 2.08, 2.20];
const StandardTurn:Array = [1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2,8, 3.0];
const StandardShotspeed:Array = [8, 7, 6, 5, 4];
const StandardBDamage:Array = [15.0, 18.0, 21.0, 24.0, 27.0, 30.0, 33.0, 36.0, 39.0, 42.0];
const StandardBSpeed:Array = [4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0];
const StandardBLifespan:Array = [60, 65, 70, 75, 80, 85, 90, 95, 100];
const StandardBDeviation:Array = [20.0, 16.0, 12.0, 8.0, 4.0, 0.0];
const StandardShield:Array = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
const StandardRegeneration:Array = [0.1, 0.17, 0.24, 0.31, 0.38, 0.45, 0.52, 0.59, 0.66, 0.73];

// Upgrade Cost
const UCHP:Array = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000];
const UCSpeed:Array = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000];
const UCTurn:Array = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000];
const UCShotspeed:Array = [300, 600, 1200, 2100];
const UCBDamage:Array = [300, 400, 500, 600, 700, 800, 900, 1000, 1100];
const UCBSpeed:Array = [50, 100, 140, 180, 210, 240, 260, 280];
const UCBLifespan:Array = [50, 100, 150, 200, 250, 300, 350, 400];
const UCBDeviation:Array = [100, 200, 300, 400, 500];
const UCShield:Array = [100, 200, 300, 400, 500, 600, 700, 800, 900];
const UCRegeneration:Array = [200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800];

// Splash
const SplashdotSpeed:Number = 2.5;
const SplashdotSpeedDecay:Number = 0.97;
const SplashdotAlphaDecay:Number = 0.97;
const SplashdotChance:Number = 0.07;
const SplashdotAmount:uint = 40;
const SplashdotBDir:Number = 360 / SplashdotAmount;
const SplashdotMaxLength:Number = 24;
const SplashdotMinLength:Number = 4;

// 
const CollisionDistance:Number = 85.0;
const sCollisionDistance:Number = Math.sqrt(CollisionDistance);
const SafeDistance:Number = 12.0;


// Initialize
function Initialize():void {
	var tFormat:TextFormat = new TextFormat;
	tFormat.font = "Arial";
	tFormat.bold = false;
	tFormat.color = 0xffffff;
	tFormat.size = 11;
	tFormat.align = TextFormatAlign.LEFT;

	tField.defaultTextFormat = tFormat;
	tField.selectable = false;
	tField.x = 5;
	tField.y = 428;
	tField.width = 460;
	tField.antiAliasType = AntiAliasType.ADVANCED;
	
	stField.defaultTextFormat = tFormat;
	stField.selectable = false;
	stField.x = 370;
	stField.y = 105;
	stField.width = 95;
	stField.height = 360;
	stField.antiAliasType = AntiAliasType.ADVANCED;
	
	var tFormat2:TextFormat = new TextFormat;
	tFormat2.font = "Arial";
	tFormat2.bold = false;
	tFormat2.color = 0xffffff;
	tFormat2.size = 50;
	tFormat2.align = TextFormatAlign.CENTER;

	gtField.defaultTextFormat = tFormat2;
	gtField.selectable = false;
	gtField.x = 465 / 2 - 200;
	gtField.y = 465 / 2 - 35;
	gtField.width = 400;
	gtField.height = 80;
	gtField.text = "GAME OVER";
	gtField.filters = [new GlowFilter(0xffffff, 1, 2, 2)];
	
	var tFormat3:TextFormat = new TextFormat;
	tFormat3.font = "Arial";
	tFormat3.bold = false;
	tFormat3.color = 0xffffff;
	tFormat3.size = 20;
	tFormat3.align = TextFormatAlign.CENTER;

	ptField.defaultTextFormat = tFormat3;
	ptField.selectable = false;
	ptField.x = 365;
	ptField.y = 435;
	ptField.width = 100;
	ptField.height = 30;
	ptField.text = "PAUSED";
	ptField.filters = [new GlowFilter(0xffffff, 1, 2, 2)];
	
	ub.push(ubTextField(265, 105 + 6 * 14));	// HP
	ub.push(ubTextField(265, 105 + 7 * 14));	// MaxSpeed
	ub.push(ubTextField(265, 105 + 8 * 14));	// MaxTurn
	ub.push(ubTextField(265, 105 + 9 * 14));	// ShotSpeed
	ub.push(ubTextField(265, 105 + 10 * 14));	// BulletDamage
	ub.push(ubTextField(265, 105 + 11 * 14));	// BulletSpeed
	ub.push(ubTextField(265, 105 + 12 * 14));	// BulletLife
	ub.push(ubTextField(265, 105 + 13 * 14));	// BulletDeviation
	ub.push(ubTextField(265, 105 + 14 * 14));	// Shield
	ub.push(ubTextField(265, 105 + 15 * 14));	// Regeneration
}

// Main Update
function Update(evt:Event):void {
	while (global.numChildren > 0) {
		global.removeChildAt(0);
	}
	if (!paused) {
		ticks++;
		
		// Control my bot
		if (selectedbot == mybot && selectedbot != null) {
			mybot.FaceToPosition(new Point(global.mouseX + curviewpoint.x, global.mouseY + curviewpoint.y));
		}
		
		UpdateAction();
		UpdateWorld();
	}
	
	Draw();
}

// Update bots action
function UpdateAction():void {
	UpdateBotsAI();
}

// Update world status
function UpdateWorld():void {
	// Generate bots
	if (ticks == 1) {
		GenBot("Your Bot", 0xff7777, 0);
		mybot = bots[0];
		selectedbot = bots[0];
	}
	if (ticks % 10 == 0) {
        if (bots.length < maxbots) {
            GenBot("Bot-" + String(botcount), 0xffffff, 0);
            botcount++;
        }
    }

	// 
	UpdateBots();
	UpdateBullets();
	UpdateSplashdots();
	CollisionCheckes();
	
	// Rubbish collecting
	CollectBots();
	CollectBullets();
	CollectSplashdots();
}

// Draw
function Draw():void {
	UpdateViewpoint();
	DrawMap();
	DrawBots();
	DrawBullets();
	DrawSplashdots();
	DrawMinimap();
	DrawSelectedBot();
	DrawBotStatus();
	DrawTextField();
	DrawUButton();
	DrawPaused();
	DrawGameover();
}

// Update bots decision
function UpdateBotsAI():void {
	var len:uint = bots.length;
	for (var i:uint = 0; i < len; i++) {
		if (bots[i].exist) {
			if (selectedbot == mybot && mybot == bots[i]) {
				//do nothing, I am taking control of it
			}
			else {
				//AI 1
				var nearestno:int = NearestBot(i);
				if (nearestno != -1) {
					bots[i].FaceToPosition(bots[nearestno].pos);
					bots[i].dir = (bots[i].dir + 360) % 360;
				}
				else {
					//do nothing
				}
			}
		}
	}
}


// Update bots
function UpdateBots():void {
	var len:uint = bots.length;
	for (var i:uint = 0; i < len; i++) {
		// regen
		bots[i].hp += bots[i].regen;
		if (bots[i].hp > bots[i].maxhp) bots[i].hp = bots[i].maxhp;
		// move
		MoveBot(i);
		// shoot
		var genpos:Point = new Point;
		genpos.x = bots[i].pos.x;
		genpos.y = bots[i].pos.y;
		genpos.x += Math.sin(bots[i].dir / 180 * Math.PI) * SafeDistance;
		genpos.y -= Math.cos(bots[i].dir / 180 * Math.PI) * SafeDistance;
		if (ticks % bots[i].shotspeed == 0) {
			GenBullet(bots[i], genpos,
			bots[i].dir + Math.random() * bots[i].bdev * 2 - bots[i].bdev, 	// 散布界
			bots[i].bdamage, bots[i].bspeed, bots[i].blifespan
			); 
		}
	}
}
// Move specific bot
function MoveBot(no:uint):void {	
	// Do move
	bots[no].pos.x += Math.sin(bots[no].dir / 180 * Math.PI) * bots[no].speed;
	bots[no].pos.y -= Math.cos(bots[no].dir / 180 * Math.PI) * bots[no].speed;
	
	// Loop
	if (bots[no].pos.x < 0) bots[no].pos.x += mapsizex
	if (bots[no].pos.x > mapsizex) bots[no].pos.x -= mapsizex;
	if (bots[no].pos.y < 0) bots[no].pos.y += mapsizey;
	if (bots[no].pos.y > mapsizey) bots[no].pos.y -= mapsizey;
}

// Update bullets
function UpdateBullets():void {
	var len:uint = bullets.length;
	for (var i:uint = 0; i < len; i++) {
		// move
		MoveBullet(i);
		// life
		bullets[i].lifetime++;
		if (bullets[i].lifetime >= bullets[i].lifespan) {
			bullets[i].exist = false;
		}
	}
}
// Move specific bullet
function MoveBullet(no:uint):void {
	// Do move
	bullets[no].pos.x += Math.sin(bullets[no].dir / 180 * Math.PI) * bullets[no].speed;
	bullets[no].pos.y -= Math.cos(bullets[no].dir / 180 * Math.PI) * bullets[no].speed;
	
	// Loop
	if (bullets[no].pos.x < 0) bullets[no].pos.x += mapsizex;
	if (bullets[no].pos.x > mapsizex) bullets[no].pos.x -= mapsizex;
	if (bullets[no].pos.y < 0) bullets[no].pos.y += mapsizey;
	if (bullets[no].pos.y > mapsizey) bullets[no].pos.y -= mapsizey;
}

// Update splashdots
function UpdateSplashdots():void {
	var len:uint = splashdots.length;
	for (var i:uint = 0; i < len; i++) {
		// move
		MoveSplashdot(i);
		// geometric die out
		if (Math.random() < SplashdotChance) {
			splashdots[i].exist = false;
		}
	}
}
// Move specific splashdot
function MoveSplashdot(no:uint):void {
	// Do move
	splashdots[no].pos.x += Math.sin(splashdots[no].dir / 180 * Math.PI) * splashdots[no].speed;
	splashdots[no].pos.y -= Math.cos(splashdots[no].dir / 180 * Math.PI) * splashdots[no].speed;
	
	// Speed decay
	splashdots[no].speed *= SplashdotSpeedDecay;
	
	// Alpha decay
	splashdots[no].alpha *= SplashdotAlphaDecay;
	
	// Loop
	if (splashdots[no].pos.x < 0) splashdots[no].pos.x += mapsizex;
	if (splashdots[no].pos.x > mapsizex) splashdots[no].pos.x -= mapsizex;
	if (splashdots[no].pos.y < 0) splashdots[no].pos.y += mapsizey;
	if (splashdots[no].pos.y > mapsizey) splashdots[no].pos.y -= mapsizey;
}


// Collision detect
function CollisionCheckes():void {
	var len:uint = bullets.length;
	for (var i:uint = 0; i < len; i++) {
		var target:int = CollisionCheck(i);
		if (target > -1) {
			var dam:Number = bullets[i].damage - bots[target].shield;
			if (dam < 0) dam = 0;
			if (dam > bots[target].hp) dam = bots[target].hp;
			bots[target].hp -= dam;
			bullets[i].exist = false;
			bullets[i].bot.cash += dam / 2;
			if (bots[target].hp <= 0) {
				bots[target].exist = false;	
				bullets[i].bot.kills++;
				bullets[i].bot.cash += 50;
				
				// Game over?
				if (bots[target] == mybot) {
					gameover = true;
				}
				
				// Splash
				for (var j:uint = 0; j < SplashdotAmount; j++) {
					GenSplashdot(new Point(bots[target].pos.x, bots[target].pos.y),
								SplashdotBDir * j + Math.random() * SplashdotBDir, SplashdotSpeed,
								SplashdotMinLength + Math.random() * (SplashdotMaxLength - SplashdotMinLength),
								1);
				}
			}
		}
	}
}
function CollisionCheck(no:uint):int {
	var len:uint = bots.length;
	for (var i:uint = 0; i < len; i++) {
		var dist:Number = Distance(bullets[no].pos, bots[i].pos, sCollisionDistance);
		if (dist <= CollisionDistance && dist > 0) {
			return i;
		}
	}
	return -1;
}

// Rubbish collecting
function CollectBots():void {
	var _bots:Vector.<Bot> = new Vector.<Bot>();
	var len:uint = bots.length;
	for (var i:uint = 0; i < len; i++) {
		if (bots[i].exist) {
			_bots.push(bots[i]);
		}
		else bots[i] = null;
	}
	bots = _bots;
}
function CollectBullets():void {
	var _bullets:Vector.<Bullet> = new Vector.<Bullet>();
	var len:uint = bullets.length;
	for (var i:uint = 0; i < len; i++) {
		if (bullets[i].exist) {
			_bullets.push(bullets[i]);
		}
		else bullets[i] = null;
	}
	bullets = _bullets;
}
function CollectSplashdots():void {
	var _splashdots:Vector.<Splashdot> = new Vector.<Splashdot>();
	var len:uint = splashdots.length;
	for (var i:uint = 0; i < len; i++) {
		if (splashdots[i].exist) {
			_splashdots.push(splashdots[i]);
		}
		else splashdots[i] = null;
	}
	splashdots = _splashdots;
}

function GenBot(name:String, color:uint, level:uint):void {
	bots.push(new Bot(name, new Point(Math.random() * mapsizex, Math.random() * mapsizey),
			Math.random() * 360, StandardHP[level], StandardHP[level],
			StandardSpeed[level], StandardTurn[level], StandardShotspeed[level], StandardBDamage[level],
			StandardBSpeed[level], StandardBLifespan[level], StandardBDeviation[level],
			StandardShield[level], StandardRegeneration[level],
			0, 2000, color
			));
}
function GenBullet(bot:Bot, pos:Point, dir:Number, damage:Number, speed:Number, lifespan:uint):void {				
	bullets.push(new Bullet(bot, pos, dir, damage, speed, lifespan));
}
function GenSplashdot(pos:Point, dir:Number, speed:Number, length:Number, alpha:Number):void {				
	splashdots.push(new Splashdot(pos, dir, speed, length, alpha));
}


function NearestBot(no:uint):uint {
	var nearestno:int = -1;
	var nearestdis:Number;
	var len:uint = bots.length;
	for (var i:uint = 0; i < len; i++) {
		if (i != no) {
			var dist:Number = Distance(bots[no].pos, bots[i].pos, 10000);
			if ((nearestno == -1 || dist < nearestdis) && dist > 0 && bots[i].exist) {
				nearestno = i;
				nearestdis = dist;
			}
		}
	}
	return nearestno;
}
function Distance(p1:Point, p2:Point, maxdis:Number):Number {
	var dx:Number = Math.abs(p1.x - p2.x);
	var dy:Number = Math.abs(p1.y - p2.y);
	if (dx > maxdis) return -1;
	if (dy > maxdis) return -1;
	if (dx + dy > maxdis * 2) return -1;
	return (dx * dx + dy * dy);
}


// View point following and updating
function UpdateViewpoint():void {
	if (selectedbot != null && selectedbot.exist) {
		tempviewpoint.x = selectedbot.pos.x - (465 / 2 - 50);
		tempviewpoint.y = selectedbot.pos.y - 465 / 2;
		viewpoint.x = curviewpoint.x;
		viewpoint.y = curviewpoint.y;
	}
	
	// Update view points
	curviewpoint.x = 0.92 * curviewpoint.x + 0.08 * tempviewpoint.x;
	curviewpoint.y = 0.92 * curviewpoint.y + 0.08 * tempviewpoint.y;
}


function DrawBots():void {
	var lbound:Number = curviewpoint.x - 20;
	var rbound:Number = curviewpoint.x + 465 + 20;
	var tbound:Number = curviewpoint.y - 20;
	var bbound:Number = curviewpoint.y + 465 + 20;
	
	var len:uint = bots.length;
	for (var i:uint = 0; i < len; i++) {
		if (bots[i].exist) {
			var px:Number = bots[i].pos.x;
			var py:Number = bots[i].pos.y;
			if (px > lbound && px < rbound && py > tbound && py < bbound) DrawBot(bots[i]);
		}
	}
}
function DrawBot(bot:Bot):void {
	var shape:Shape = BotShape(bot.color);

	shape.x = bot.pos.x - curviewpoint.x;
	shape.y = bot.pos.y - curviewpoint.y;
	shape.rotation = bot.dir;

	global.addChild(shape);
	
	if (drawhpbar) {
		var shape2:Shape = HPBarShape(bot.hp / bot.maxhp);
		shape2.x = bot.pos.x - curviewpoint.x;
		shape2.y = bot.pos.y - curviewpoint.y;
		
		global.addChild(shape2);
	}
}
function BotShape(color:uint):Shape {
	var _shape:Shape = new Shape();
	_shape.graphics.lineStyle(2, color);

	var sp:Point = new Point(0, -12);
	var sp2:Point = new Point(-6, 10);
	var sp3:Point = new Point(6, 10);

	_shape.graphics.moveTo(sp.x, sp.y);
	_shape.graphics.lineTo(sp2.x, sp2.y);
	_shape.graphics.lineTo(sp3.x, sp3.y);
	_shape.graphics.lineTo(sp.x, sp.y);
	
	_shape.filters = [new GlowFilter(color, 1, 8, 8, 1.3, 2)];

	return _shape;
}
function HPBarShape(hp:Number):Shape {
	var _shape:Shape = new Shape();
	_shape.graphics.lineStyle(1, 0xffffff, 0.5);

	_shape.graphics.drawRect(-12, 17, 24, 5);
	
	_shape.graphics.beginFill(0xffffff, 0.5);
	_shape.graphics.drawRect(-12, 17, hp * 24, 5);
	_shape.graphics.endFill();
	
	return _shape;
}

function DrawBullets():void {
	var lbound:Number = curviewpoint.x - 20;
	var rbound:Number = curviewpoint.x + 465 + 20;
	var tbound:Number = curviewpoint.y - 20;
	var bbound:Number = curviewpoint.y + 465 + 20;
	
	var len:uint = bullets.length;
	for (var i:uint = 0; i < len; i++) {
		if (bullets[i].exist) {
			var px:Number = bullets[i].pos.x;
			var py:Number = bullets[i].pos.y;
			if (px > lbound && px < rbound && py > tbound && py < bbound) DrawBullet(bullets[i]);
		}
	}
}
function DrawBullet(bullet:Bullet):void {
	var shape:Shape = BulletShape();

	shape.x = bullet.pos.x - curviewpoint.x;
	shape.y = bullet.pos.y - curviewpoint.y;
	shape.rotation = bullet.dir;

	global.addChild(shape);
}
function BulletShape():Shape {
	var _shape:Shape = new Shape();
	_shape.graphics.lineStyle(1, 0xffffff, 1);

	_shape.graphics.moveTo(0, -3);
	_shape.graphics.lineTo(0, 3);
	
	_shape.filters = [new GlowFilter(0xffffff, 1, 2, 2)];

	return _shape;
}

function DrawSplashdots():void {
	var lbound:Number = curviewpoint.x - 20;
	var rbound:Number = curviewpoint.x + 465 + 20;
	var tbound:Number = curviewpoint.y - 20;
	var bbound:Number = curviewpoint.y + 465 + 20;
	
	var len:uint = splashdots.length;
	for (var i:uint = 0; i < len; i++) {
		if (splashdots[i].exist) {
			var px:Number = splashdots[i].pos.x;
			var py:Number = splashdots[i].pos.y;
			if (px > lbound && px < rbound && py > tbound && py < bbound) DrawSplashdot(splashdots[i]);
		}
	}
}
function DrawSplashdot(splashdot:Splashdot):void {
	var shape:Shape = SplashdotShape(splashdot.length, splashdot.alpha);

	shape.x = splashdot.pos.x - curviewpoint.x;
	shape.y = splashdot.pos.y - curviewpoint.y;
	shape.rotation = splashdot.dir;

	global.addChild(shape);
}
function SplashdotShape(length:Number, alpha:Number):Shape {
	var _shape:Shape = new Shape();
	_shape.graphics.lineStyle(2, 0xffffff, alpha);

	_shape.graphics.moveTo(0, -length / 2);
	_shape.graphics.lineTo(0, length / 2);
	
	_shape.filters = [new GlowFilter(0xffffff, alpha)];

	return _shape;
}

function DrawMap():void {
	var shape:Shape = MapShape();
	
	shape.x = -curviewpoint.x;
	shape.y = -curviewpoint.y;
	shape.rotation = 0;
	
	global.addChild(shape);
}
function MapShape():Shape {
	var _shape:Shape = new Shape();
	_shape.graphics.lineStyle(2, 0x0088BB, 0.7);
	
	_shape.graphics.moveTo(0, 0);
	_shape.graphics.lineTo(mapsizex, 0);
	_shape.graphics.lineTo(mapsizex, mapsizey);
	_shape.graphics.lineTo(0, mapsizey);
	_shape.graphics.lineTo(0, 0);
	
	_shape.graphics.lineStyle(1, 0x0088BB, 0.7);
	
	for( var i:uint = 50; i < mapsizex; i+=50) {
		_shape.graphics.moveTo(i, 0);
		_shape.graphics.lineTo(i, mapsizey);
	}
	
	for( var j:uint = 50; j < mapsizey; j+=50) {
		_shape.graphics.moveTo(0, j);
		_shape.graphics.lineTo(mapsizex, j);
	}
	
	return _shape;
}

function DrawMinimap():void {
	var shape:Array = MinimapShape();
	var shape2:Shape = MiniFOVShape();
	
	shape2.x = 365 + curviewpoint.x / 10;
	shape2.y = curviewpoint.y / 10;
	
	global.addChild(shape[0]);
	global.addChild(shape[1]);
	global.addChild(shape2);
	
	var len:uint = bots.length;
	for (var i:uint = 0; i < len; i++) {
		if (bots[i].exist) DrawMiniBot(bots[i]);
	}
}
function MinimapShape():Array {
	var minimapx:Number = mapsizex / 10;
	var minimapy:Number = mapsizey / 10;

	var shape:Array = [];
	
	var _shape:Shape = new Shape();
	_shape.graphics.beginFill(0x0088BB, 0.4);
	
	_shape.graphics.moveTo(365, 0);
	_shape.graphics.lineTo(365 + minimapx, 0);
	_shape.graphics.lineTo(365 + minimapx, minimapy);
	_shape.graphics.lineTo(365, minimapy);
	_shape.graphics.lineTo(365, 0);
	
	_shape.graphics.endFill();
	
	var _shape2:Shape = new Shape();
	_shape2.graphics.beginFill(0x00001E, 0.4);
	
	_shape2.graphics.moveTo(366 , 1);
	_shape2.graphics.lineTo(365 + minimapx - 1, 1);
	_shape2.graphics.lineTo(365 + minimapx - 1, minimapy - 1);
	_shape2.graphics.lineTo(366, minimapy - 1);
	_shape2.graphics.lineTo(366, 1);
	
	_shape2.graphics.endFill();
	
	shape.push(_shape);
	shape.push(_shape2);
	
	return shape;
}
function MiniFOVShape():Shape {
	var _shape:Shape = new Shape();
	_shape.graphics.lineStyle(1, 0x0088BB, 0.5);
	
	_shape.graphics.moveTo(0, 0);
	_shape.graphics.lineTo(46.5, 0);
	_shape.graphics.lineTo(46.5, 46.5);
	_shape.graphics.lineTo(0, 46.5);
	_shape.graphics.lineTo(0, 0);
	
	return _shape;
}

function DrawMiniBot(bot:Bot):void {
	var shape:Shape = MiniBotShape(bot.color);

	shape.x = 365 + bot.pos.x / 10;
	shape.y = bot.pos.y / 10;

	global.addChild(shape);
}
function MiniBotShape(color:uint):Shape {
	var _shape:Shape = new Shape();
	_shape.graphics.beginFill(color, 1);

	_shape.graphics.drawCircle(0, 0, 1);

	_shape.graphics.endFill();

	return _shape;
}

function DrawSelectedBot():void {
	// Unselect non-exist bot
	if (selectedbot != null) {
		if (selectedbot.exist) {
			var shape:Shape = new Shape();
			shape.graphics.lineStyle(2, 0xffffff, 0.5);
			
			shape.graphics.drawCircle(0, 0, 16);
			shape.graphics.moveTo(6, -19);
			shape.graphics.lineTo(0, -23);
			shape.graphics.lineTo(-6, -19);
			
			shape.filters = [new GlowFilter(0xffffff, 1, 8, 8, 3, 2)];
			
			shape.x = selectedbot.pos.x - curviewpoint.x;
			shape.y = selectedbot.pos.y - curviewpoint.y;
			shape.rotation = selectedbot.dir;
			
			global.addChild(shape);
		}
		else {
			selectedbot = null;
		}
	}
}

function DrawBotStatus():void {
	// Unselect non-exist bot
	if (selectedbot != null) {
		if (selectedbot.exist) {
			var shape:Array = BotStatusShape();
			global.addChild(shape[0]);
			global.addChild(shape[1]);
			
			stField.text = "Name: " + String(selectedbot.name) + "\n"
						+ "\n"
						+ "Posx: " + String(getPoint(selectedbot.pos.x, 2)) + "\n"
						+ "Posy: " + String(getPoint(selectedbot.pos.y, 2)) + "\n"
						+ "Direction: " + String(getPoint(selectedbot.dir, 2)) + "\n"
						+ "\n"
						+ "HP: " + String(getPoint(selectedbot.hp, 0)) + "/" + String(getPoint(selectedbot.maxhp, 0)) + "\n"
						+ "MaxSpeed: " + String(getPoint(selectedbot.speed, 2)) + "\n"
						+ "MaxTurn: " + String(getPoint(selectedbot.turn, 2)) + "\n"
						+ "ShotSpeed: " + String(getPoint(selectedbot.shotspeed, 2)) + "\n"
						+ "BulletDamage: " + String(getPoint(selectedbot.bdamage, 2)) + "\n"
						+ "BulletSpeed: " + String(getPoint(selectedbot.bspeed, 2)) + "\n"
						+ "BulletLife: " + String(getPoint(selectedbot.blifespan, 2)) + "\n"
						+ "BulletDeviation: " + String(getPoint(selectedbot.bdev, 2)) + "\n"
						+ "Shield: " + String(getPoint(selectedbot.shield, 2)) + "\n"
						+ "Regeneration: " + String(getPoint(selectedbot.regen, 2)) + "\n"
						+ "\n"
						+ "Kills: " + String(selectedbot.kills) + "\n"
						+ "Cash: " + String(selectedbot.cash);
			global.addChild(stField);
		}
		else {
			selectedbot = null;
		}
	}	
}
function BotStatusShape():Array {
	var shape:Array = [];
	
	var _shape:Shape = new Shape();
	_shape.graphics.beginFill(0x0088BB, 0.4);
	
	_shape.graphics.moveTo(365, 100);
	_shape.graphics.lineTo(365, 465);
	_shape.graphics.lineTo(465, 465);
	_shape.graphics.lineTo(465, 100);
	_shape.graphics.lineTo(365, 100);
	
	_shape.graphics.endFill();
	
	var _shape2:Shape = new Shape();
	_shape2.graphics.beginFill(0x00001E, 0.4);
	
	_shape2.graphics.moveTo(366, 101);
	_shape2.graphics.lineTo(366, 464);
	_shape2.graphics.lineTo(464, 464);
	_shape2.graphics.lineTo(464, 101);
	_shape2.graphics.lineTo(366, 101);
	
	_shape2.graphics.endFill();
	
	shape.push(_shape);
	shape.push(_shape2);
	
	return shape;
}

function DrawTextField():void {
	tField.text = "Frames: " + String(ticks)
				+ "    Bots:" + String(bots.length) + "    Bullets:" + String(bullets.length)
				+ "    Splashdots:" + String(splashdots.length) + "\n";
	tField.text = tField.text + "mouseX:" + String(global.mouseX) + "    mouseY:" + String(global.mouseY)
				+ "    cFOVx: " + String(int(curviewpoint.x)) + "    cFOVy: " + String(int(curviewpoint.y));
				
	global.addChild(tField);
}

function DrawUButton():void {
	if (selectedbot == mybot && selectedbot != null) {
		var len:uint = ub.length;
		for (var i:uint = 0; i < len; i++) {
			global.addChild(ub[i]);
		}
	}
}

function DrawPaused():void {
	if (paused) {		
		global.addChild(ptField);
	}
}

function DrawGameover():void {
	if (gameover) {
		global.addChild(gtField);
	}
}


function MouseClick(evt:MouseEvent):void {
	SelectBots(new Point(evt.stageX + curviewpoint.x, evt.stageY + curviewpoint.y));
	
	var mouseX:Number = evt.stageX;
	var mouseY:Number = evt.stageY;
	if (mouseX > 320 && mouseX < 370) {	
		var un:uint = Math.round((mouseY - 114 - 6 * 14) / 14);
		switch (un) {
		case 0:
			if (mybot.ULHP < StandardHP.length && mybot.cash >= UCHP[mybot.ULHP]) {
				mybot.cash -= UCHP[mybot.ULHP];
				mybot.ULHP++;
				mybot.maxhp = StandardHP[mybot.ULHP];
			}
			break;
		case 1:
			if (mybot.ULSpeed < StandardSpeed.length && mybot.cash >= UCSpeed[mybot.ULSpeed]) {
				mybot.cash -= UCSpeed[mybot.ULSpeed];
				mybot.ULSpeed++;
				mybot.speed = StandardSpeed[mybot.ULSpeed];
			}
			break;
		case 2:
			if (mybot.ULTurn < StandardTurn.length && mybot.cash >= UCTurn[mybot.ULTurn]) {
				mybot.cash -= UCTurn[mybot.ULTurn];
				mybot.ULTurn++;
				mybot.turn = StandardTurn[mybot.ULTurn];
			}
			break;
		case 3:
			if (mybot.ULShotspeed < StandardShotspeed.length && mybot.cash >= UCShotspeed[mybot.ULShotspeed]) {
				mybot.cash -= UCShotspeed[mybot.ULShotspeed];
				mybot.ULShotspeed++;
				mybot.shotspeed = StandardShotspeed[mybot.ULShotspeed];
			}
			break;
		case 4:
			if (mybot.ULBDamage < StandardBDamage.length && mybot.cash >= UCBDamage[mybot.ULBDamage]) {
				mybot.cash -= UCBDamage[mybot.ULBDamage];
				mybot.ULBDamage++;
				mybot.bdamage = StandardBDamage[mybot.ULBDamage];
			}
			break;
		case 5:
			if (mybot.ULBSpeed < StandardBSpeed.length && mybot.cash >= UCBSpeed[mybot.ULBSpeed]) {
				mybot.cash -= UCBSpeed[mybot.ULBSpeed];
				mybot.ULBSpeed++;
				mybot.bspeed = StandardBSpeed[mybot.ULBSpeed];
			}
			break;
		case 6:
			if (mybot.ULBLifespan < StandardBLifespan.length && mybot.cash >= UCBLifespan[mybot.ULBLifespan]) {
				mybot.cash -=UCBLifespan[mybot.ULBLifespan];
				mybot.ULBLifespan++;
				mybot.blifespan = StandardBLifespan[mybot.ULBLifespan];
			}
			break;
		case 7:
			if (mybot.ULBDeviation < StandardBDeviation.length && mybot.cash >= UCBDeviation[mybot.ULBDeviation]) {
				mybot.cash -= UCBDeviation[mybot.ULBDeviation];
				mybot.ULBDeviation++;
				mybot.bdev = StandardBDeviation[mybot.ULBDeviation];
			}
			break;
		case 8:
			if (mybot.ULShield < StandardShield.length && mybot.cash >= UCShield[mybot.ULShield]) {
				mybot.cash -= UCShield[mybot.ULShield];
				mybot.ULShield++;
				mybot.shield = StandardShield[mybot.ULShield];
			}
			break;
		case 9:
			if (mybot.ULRegeneration < StandardRegeneration.length && mybot.cash >= UCRegeneration[mybot.ULRegeneration]) {
				mybot.cash -= UCRegeneration[mybot.ULRegeneration];
				mybot.ULRegeneration++;
				mybot.regen = StandardRegeneration[mybot.ULRegeneration];
			}
		}
	}
}
function SelectBots(clickpos:Point):void {
	var mindist:Number;
	var minno:int = -1;

	var len:uint = bots.length;
    for (var i:uint = 0; i < len; i++) {
        if (bots[i].exist) {
			var dist:Number = Distance(clickpos, bots[i].pos, 20);
			if ((minno == -1 || dist < mindist) && dist > 0 && dist <= 256) {
				mindist = dist;
				minno = i;
			}
		}
	}
	
	if (minno > -1) {
		selectedbot = bots[minno];
	}
}

function MouseDown(evt:MouseEvent):void {
	if (evt.buttonDown) {
		mousedownpoint.x = evt.stageX;
		mousedownpoint.y = evt.stageY;
	}
}

function MouseMove(evt:MouseEvent):void {
	if (evt.buttonDown) {
		var mouseX:Number = evt.stageX;
		var mouseY:Number = evt.stageY;
		tempviewpoint.x = viewpoint.x - mouseX + mousedownpoint.x;
		tempviewpoint.y = viewpoint.y - mouseY + mousedownpoint.y;
		if (tempviewpoint.x < 0 - 200) tempviewpoint.x = 0 - 200;
		if (tempviewpoint.y < 0 - 200) tempviewpoint.y = 0 - 200;
		if (tempviewpoint.x > mapsizex - 465 + 200) tempviewpoint.x = mapsizex - 465 + 200;
		if (tempviewpoint.y > mapsizey - 465 + 200) tempviewpoint.y = mapsizey - 465 + 200;
	}
	
	var _mouseX:Number = evt.stageX;
	var _mouseY:Number = evt.stageY;
	if (_mouseX > 320 && _mouseX < 375) {	
		var len:uint = ub.length;
		for (var i:uint = 0; i < len; i++) {
			ub[i].text = "Upgrade";
		}
		var un:uint = Math.round((_mouseY - 114 - 6 * 14) / 14);
				switch (un) {
		case 0:
			if (mybot.ULHP < StandardHP.length - 1) ub[un].text = "$" + String(UCHP[mybot.ULHP]) + "->" + StandardHP[mybot.ULHP + 1];
			else ub[un].text = "MAX";
			break;
		case 1:
			if (mybot.ULSpeed < StandardSpeed.length - 1) ub[un].text = "$" + String(UCSpeed[mybot.ULSpeed]) + "->" + StandardSpeed[mybot.ULSpeed + 1];
			else ub[un].text = "MAX";
			break;
		case 2:
			if (mybot.ULTurn < StandardTurn.length - 1) ub[un].text = "$" + String(UCTurn[mybot.ULTurn]) + "->" + StandardTurn[mybot.ULTurn + 1];
			else ub[un].text = "MAX";
			break;
		case 3:
			if (mybot.ULShotspeed < StandardShotspeed.length - 1) ub[un].text = "$" + String(UCShotspeed[mybot.ULShotspeed]) + "->" + StandardShotspeed[mybot.ULShotspeed + 1];
			else ub[un].text = "MAX";
			break;
		case 4:
			if (mybot.ULBDamage < StandardBDamage.length - 1) ub[un].text = "$" + String(UCBDamage[mybot.ULBDamage]) + "->" + StandardBDamage[mybot.ULBDamage + 1];
			else ub[un].text = "MAX";
			break;
		case 5:
			if (mybot.ULBSpeed < StandardBSpeed.length - 1) ub[un].text = "$" + String(UCBSpeed[mybot.ULBSpeed]) + "->" + StandardBSpeed[mybot.ULBSpeed + 1];
			else ub[un].text = "MAX";
			break;
		case 6:
			if (mybot.ULBLifespan < StandardBLifespan.length - 1) ub[un].text = "$" + String(UCBLifespan[mybot.ULBLifespan]) + "->" + StandardBLifespan[mybot.ULBLifespan + 1];
			else ub[un].text = "MAX";
			break;
		case 7:
			if (mybot.ULBDeviation < StandardBDeviation.length - 1) ub[un].text = "$" + String(UCBDeviation[mybot.ULBDeviation]) + "->" + StandardBDeviation[mybot.ULBDeviation + 1];
			else ub[un].text = "MAX";
			break;
		case 8:
			if (mybot.ULShield < StandardShield.length - 1) ub[un].text = "$" + String(UCShield[mybot.ULShield]) + "->" + StandardShield[mybot.ULShield + 1];
			else ub[un].text = "MAX";
			break;
		case 9:
			if (mybot.ULRegeneration < StandardRegeneration.length - 1) ub[un].text = "$" + String(UCRegeneration[mybot.ULRegeneration]) + "->" + StandardRegeneration[mybot.ULRegeneration + 1];
			else ub[un].text = "MAX";
		}
	}
}

function MouseUp(evt:MouseEvent):void {
	viewpoint.x = tempviewpoint.x;
	viewpoint.y = tempviewpoint.y;
}

function KeyDown(evt:KeyboardEvent):void {
	// Space to pause
	if (evt.charCode == 32) paused = !paused;
	// F to free view point
	if (evt.charCode == 70 || evt.charCode == 102) selectedbot = null;
	// S to show/hide HP bar
	if (evt.charCode == 83 || evt.charCode == 115) drawhpbar = !drawhpbar;
	// R to restart
	if (evt.charCode == 82 || evt.charCode == 114) {
		tField.text = "";
		stField.text = "";
		selectedbot = null;
		mybot = null;
		ticks = 0;
		botcount = 0;
		bots = new Vector.<Bot>();
		bullets = new Vector.<Bullet>();
		splashdots = new Vector.<Splashdot>();
		gameover = false;
	}
	// Q to direct select my bot
	if (evt.charCode == 81 || evt.charCode == 113) {
		selectedbot = mybot;
	}
}


class Bot {
	function Bot(_name:String, _pos:Point, _dir:Number,
			_hp:Number, _maxhp:Number,
			_speed:Number, _turn:Number,
			_shotspeed:uint,
			_bdamage:Number, _bspeed:Number,
			_blifespan:uint, _bdev:Number,
			_shield:Number, _regen:Number,
			_kills:uint, _cash:uint,
			_color:uint,
			_exist:Boolean = true) {
		this.hp = _hp;
		this.maxhp = _maxhp;
		this.exist = _exist;
		this.name = _name;
		this.pos = new Point(_pos.x, _pos.y);
		this.dir = _dir;
		this.speed = _speed;
		this.turn = _turn;
		this.shotspeed = _shotspeed;
		this.bdamage = _bdamage;
		this.bspeed = _bspeed;
		this.blifespan = _blifespan;
		this.bdev = _bdev;
		this.shield = _shield;
		this.regen = _regen;
		this.kills = _kills;
		this.cash = _cash;
		this.color = _color;
	}
	
	public var hp:Number;
	public var maxhp:Number;

	public var exist:Boolean;
	public var name:String;				// Unique name 
	
	public var pos:Point;				// Position in Point
	public var dir:Number;				// Direction in degrees

	public var speed:Number;			// Move speed in pixels/s
	public var turn:Number;				// Rotate speed in degrees/s
	
	public var shotspeed:uint;
	
	public var bdamage:Number; 			// Bullet damage
	public var bspeed:Number; 			// Bullet speed in pixels/s
	public var blifespan:uint; 			// Bullet lifespan in frames
	
	public var bdev:Number; 			// Bullet direction deviation in degrees
	
	public var shield:Number;			// Shield
	public var regen:Number;			// Regeneration in 1/s
	
	public var kills:uint;
	public var cash:uint;
	
	public var color:uint;
	
	public var ULHP:uint = 0;
	public var ULSpeed:uint = 0;
	public var ULTurn:uint = 0;
	public var ULShotspeed:uint = 0;
	public var ULBDamage:uint = 0;
	public var ULBSpeed:uint = 0;
	public var ULBLifespan:uint = 0;
	public var ULBDeviation:uint = 0;
	public var ULShield:uint = 0;
	public var ULRegeneration:uint = 0;
	
	public function FaceToPosition(pos:Point):void {
		var temp:Number = this.turn;

		var theta:Number = Math.atan2(pos.y - this.pos.y, pos.x - this.pos.x);
		theta = (theta * 180 / Math.PI - this.dir + 810) % 360;
		if (1 <= theta && theta <= 179) {
			if (temp > theta) temp = theta;
			this.dir = (this.dir + temp) % 360;
		}
		else if (181 <= theta && theta <= 359) {
			if (temp > 360 - theta) temp = 360 - theta;
			this.dir = (this.dir - temp) % 360;
		}
	}
}

class Bullet {
	function Bullet(_bot:Bot, _pos:Point, _dir:Number,
					_damage:Number, _speed:Number, _lifespan:uint,
					_lifetime:uint = 0,
					_exist:Boolean = true) {
		this.damage = _damage;
		this.exist = _exist;
		this.bot = _bot;
		this.pos = new Point(_pos.x, _pos.y);
		this.dir = _dir;
		this.lifetime = _lifetime;
		this.speed = _speed;
		this.lifespan = _lifespan;
	}
	
	public var damage:Number;
	
	public var exist:Boolean;
	public var bot:Bot;					// Belongs to which bot

	public var pos:Point;				// Position in Point
	public var dir:Number;				// Direction in degrees
	public var lifetime:uint;			// Lifetime in frames

	public var speed:Number;			// Move speed in pixels/s
	public var lifespan:uint;			// Lifespan in frames	
}

class Splashdot {
	function Splashdot(_pos:Point, _dir:Number, _speed:Number, _length:Number, _alpha:Number, _exist:Boolean = true) {
		this.exist = _exist;
		this.pos = _pos;
		this.dir = _dir;
		this.speed = _speed;
		this.length = _length;
		this.alpha = _alpha;
	}
	
	public var exist:Boolean;
	
	public var pos:Point;				// Position in Point
	public var dir:Number;				// Direction in degrees
	
	public var speed:Number;			// Move speed in pixels/s
	public var length:Number;			// Length in pixels
	public var alpha:Number;
}

function getPoint(num:Number, l:int):Number {
    return (Math.round(num * Math.pow(10, l)) / Math.pow(10, l));
}

function ubTextField(px:uint, py:uint):TextField {
	var tFormat:TextFormat = new TextFormat;
	tFormat.font = "Arial";
	tFormat.bold = false;
	tFormat.color = 0xffffff;
	tFormat.size = 11;
	tFormat.align = TextFormatAlign.RIGHT;

	var textField:TextField = new TextField;
	textField.defaultTextFormat = tFormat;
	textField.selectable = false;
	textField.x = px;
	textField.y = py;
	textField.width = 100;
	textField.height = 30;
	textField.text = "Upgrade";
	tField.antiAliasType = AntiAliasType.ADVANCED;
	
	return textField;
}