// Run public int Run() { int result = 0; // Whether a meaningful action has been taken // If asleep if (actor.isAsleep) { actor.actionPoints = 0; } else { // If is hostile if(actor.isHostile) { // If has target if(targetEnemy != null) { // If the target isn't dead and can be seen if(!targetEnemy.dead && targetEnemy.visible && worldMap.TileDistance(actor.tileX, actor.tileY, targetEnemy.tileX, targetEnemy.tileY) <= actor.sightRange) { // If in melee range to target if (worldMap.TileDistance(actor.tileX,actor.tileY, targetEnemy.tileX, targetEnemy.tileY) <= 1) { // If can attack melee if (typeAttack == AI_TYPE_ATTACK_MELEE) { // Try to attack if (actor.TryAttackingAnotherActor(targetEnemy, actor.attackMain) > 0) { actor.actionPoints -= 6; } } } else { // Get path to target Path path = new Path(); path = worldMap.FindPathToTile(actor.tileX, actor.tileY, targetEnemy.tileX, targetEnemy.tileY); int tx = 0; int ty = 0; if (path.length > 1) { if (path.nodes[1].x > actor.tileX) { tx = 1; } if (path.nodes[1].x < actor.tileX) { tx = -1; } if (path.nodes[1].y > actor.tileY) { ty = 1; } if (path.nodes[1].y < actor.tileY) { ty = -1; } } // Try moving towards target if (tx != 0 || ty != 0) { // If successful if (actor.TryMoving(tx, ty) > 0) { actor.actionPoints -= 3; result = 1; Console.WriteLine(actor.nameTitle+" moves to ("+actor.tileX+","+actor.tileY+")"); } else { actor.actionPoints = 0; } } else { actor.actionPoints = 0; } } } else { targetEnemy = null; actor.actionPoints = 0; } } else { // Find an enemy worldMap.FindTargetInSight(actor, TARGET_TYPE_ENEMY); actor.actionPoints = 0; } } else { actor.actionPoints = 0; } } return result; }
// Find a path to a tile public Path FindPathToTile(int sx, int sy, int tx, int ty) { Path path = new Path(); // Make the first node the start int nodeX = sx; int nodeY = sy; path.AddNode(nodeX, nodeY); // Before having reached the target position /*while (nodeX != tx && nodeY != ty && path.length < 100) { }*/ // Make the last node the target tile nodeX = tx; nodeY = ty; path.AddNode(nodeX, nodeY); return path; }