1
0
Fork 0

Added actions by Bully A.I.

This commit is contained in:
Michaël Lemaire 2015-02-17 01:00:00 +01:00
parent 6d9093061d
commit cc5331ee5a
5 changed files with 54 additions and 23 deletions

View file

@ -43,8 +43,24 @@ module SpaceTac.Game.AI {
} }
// Add a work item to the work queue // Add a work item to the work queue
addWorkItem(item: Function): void { addWorkItem(item: Function, delay: number = null): void {
this.workqueue.push(item); var wrapped = () => {
if (item) {
item();
}
this.processNextWorkItem();
};
if (!delay) {
delay = 100;
}
if (this.async && delay) {
this.workqueue.push(() => {
setTimeout(wrapped, delay);
});
} else {
this.workqueue.push(wrapped);
}
} }
// Initially fill the work queue. // Initially fill the work queue.
@ -58,27 +74,12 @@ module SpaceTac.Game.AI {
if (this.workqueue.length > 0) { if (this.workqueue.length > 0) {
// Take the first item // Take the first item
var item = this.workqueue.shift(); var item = this.workqueue.shift();
this.processWorkItem(item); item();
} else { } else {
this.endTurn(); this.endTurn();
} }
} }
// Process one work item, then call processNextWorkItem
private processWorkItem(item: Function): void {
// Process current item
item();
// On to the next item
if (this.async) {
setTimeout(() => {
this.processNextWorkItem();
}, 100);
} else {
this.processNextWorkItem();
}
}
// Called when we want to end the ship turn // Called when we want to end the ship turn
private endTurn(): void { private endTurn(): void {
if (this.async) { if (this.async) {

View file

@ -19,12 +19,26 @@ module SpaceTac.Game.AI {
super(fleet); super(fleet);
} }
protected initWork(): void {
this.addWorkItem(() => {
var moves = this.listAllMoves();
if (moves.length > 0) {
var move = moves[0];
this.applyMove(move);
// Try to make another move
this.initWork();
}
});
}
// List all enemy ships that can be a target // List all enemy ships that can be a target
listAllEnemies(): Ship[] { listAllEnemies(): Ship[] {
var result: Ship[] = []; var result: Ship[] = [];
this.fleet.battle.play_order.forEach((ship: Ship) => { this.fleet.battle.play_order.forEach((ship: Ship) => {
if (ship.getPlayer() !== this.ship.getPlayer()) { if (ship.alive && ship.getPlayer() !== this.ship.getPlayer()) {
result.push(ship); result.push(ship);
} }
}); });
@ -76,7 +90,7 @@ module SpaceTac.Game.AI {
} else { } else {
var engine = engines[0]; var engine = engines[0];
var move_distance = distance - weapon.distance; var move_distance = distance - weapon.distance;
var move_ap = engine.ap_usage * move_distance; var move_ap = engine.ap_usage * move_distance / engine.distance;
if (move_ap > remaining_ap) { if (move_ap > remaining_ap) {
// Not enough AP to move in range // Not enough AP to move in range
return null; return null;
@ -99,5 +113,20 @@ module SpaceTac.Game.AI {
return result; return result;
} }
} }
// Effectively apply the move
private applyMove(move: BullyMove): void {
if (move.move_to) {
this.addWorkItem(() => {
this.ship.moveTo(move.move_to.x, move.move_to.y);
}, 1000);
}
this.addWorkItem(() => {
move.weapon.action.apply(this.fleet.battle, this.ship, Target.newFromShip(move.target));
}, 1500);
this.addWorkItem(null, 500);
}
} }
} }

View file

@ -45,6 +45,7 @@ module SpaceTac.Game.AI.Specs {
var ship = new Ship(); var ship = new Ship();
var engine = new Equipment(SlotType.Engine); var engine = new Equipment(SlotType.Engine);
engine.ap_usage = 3; engine.ap_usage = 3;
engine.distance = 1;
ship.addSlot(SlotType.Engine).attach(engine); ship.addSlot(SlotType.Engine).attach(engine);
ship.ap_current.setMaximal(10); ship.ap_current.setMaximal(10);
ship.ap_current.set(8); ship.ap_current.set(8);

View file

@ -9,7 +9,7 @@ module SpaceTac.Game.Equipments {
super(SlotType.Engine, "Conventional Engine"); super(SlotType.Engine, "Conventional Engine");
this.min_level = new IntegerRange(1, 1); this.min_level = new IntegerRange(1, 1);
this.distance = new Range(300, 300); this.distance = new Range(500, 500);
this.ap_usage = new Range(3); this.ap_usage = new Range(3);
this.addPermanentAttributeMaxEffect(AttributeCode.Initiative, 1); this.addPermanentAttributeMaxEffect(AttributeCode.Initiative, 1);

View file

@ -8,9 +8,9 @@ module SpaceTac.Game.Equipments {
constructor() { constructor() {
super("Gatling Gun", 50, 100); super("Gatling Gun", 50, 100);
this.setRange(1000, 1000, false); this.setRange(500, 500, false);
this.ap_usage = new Range(3, 4); this.ap_usage = new Range(2, 3);
this.min_level = new IntegerRange(1, 3); this.min_level = new IntegerRange(1, 3);
} }
} }