/*** HELPER FUNCTIONS ****/ /// <summary> /// This function will search the cells that the unit can see for the closest enemy Unit. /// </summary> /// <param name="unit">The unit doing the searching</param> /// <param name="gw">The GameWorld to search</param> /// <returns>The closest enemy Unit or null if none exists</returns> private static Unit searchCellsForEnemy(Unit unit, GameWorld gw) { byte offset = (byte)unit.stats.visibilityRange; int xStart = (short)unit.x - offset; int xEnd = (short)unit.x + offset; int yStart = (short)unit.y - offset; int yEnd = (short)unit.y + offset; // Make sure that our bounds are valid. (Assumes that no Unit has a visibility range longer than the map.) if (xStart < 0) { xStart = 0; } else if (xEnd >= gw.map.width) { xEnd = gw.map.width; } if (yStart < 0) { yStart = 0; } else if (yEnd >= gw.map.height) { yEnd = gw.map.height; } Unit target = null; float distance = 10000f; // Set all cell explored flags to true. for (int i = xStart; i < xEnd; i++) { for (int j = yStart; j < yEnd; j++) { Unit temp = gw.map.getCell(i, j).getUnit(); if (temp != null && unit.getOwner().isEnemy(temp.getOwner())) { float tDis = EntityLocController.findDistance(unit.x, unit.y, temp.x, temp.y); if (tDis < distance) { target = temp; distance = tDis; } } } } return(target); }
/// <summary> /// This method will find the cell closest to 'unit' that 'entity' is currently occupying. /// </summary> /// <param name="unit"></param> /// <param name="entity"></param> /// <returns></returns> public static Cell findClosestCell(Unit unit, StaticEntity se, GameWorld gw) { Cell cell = null; float dis = 10000; short xC = se.orginCell.Xcoord; short yC = se.orginCell.Ycoord; short width = se.width; short height = se.height; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (EntityLocController.findDistance(unit.x, unit.y, xC + i, yC + j) <= dis) { cell = gw.map.getCell(xC + i, yC + j); dis = EntityLocController.findDistance(unit.x, unit.y, xC + i, yC + j); } } } return(cell); }