我为Agar.io副本制作了一个AI,并且该AI跟随比它小的最近单元格尝试尝试吃它,但是我想根据优先级使其跟随附近的一个单元格,因此不一定是最接近AI的单元格,例如:最接近AI的单元格要比距离稍远的AI单元格小得多,后者要大得多,因此请选择较大的单元格,因为其质量较高,因此优先级较高。>
这是我的代码。 (如果您需要完整的课程,here is the entire class on pastebin.)
public void Update() {
if (this.mass > 3500) {
this.mass = 3500;
}
for (Cell cell : cells) {
if (this.checkCollide(cell.x, cell.y, cell.mass) && this != cell && this.mass > cell.mass + 10) {
if (1 / (this.mass / cell.mass) >= .4 && this.mass < 4000) {
addMass(cell.mass);
}
respawn(cell);
}
}
if (!isPlayer) {
if (goalReached) {
if (returnNearestCell() > -1) { // No Cell Found
if (!isTarget) {
target = cells.get(returnNearestCell());
isTarget = true;
targetType = "c";
} else if (isTarget && targetType.equals("c")) {
targetType = "n";
isTarget = false;
}
} else if (returnNearestCell() == -1) { // Cell Found
if (!isTarget) {
pTarget = Particle.particles.get(returnNearestP());
isTarget = true;
targetType = "p";
} else if (isTarget) {
targetType = "n";
isTarget = false;
}
}
goalReached = false;
} else {
double dx = 0;
double dy = 0;
if (targetType.equals("c")) {
if (returnNearestCell() > -1) {
target = cells.get(returnNearestCell());
dx = (target.x - this.x);
dy = (target.y - this.y);
} else {
goalReached = true;
}
} else if (targetType.equals("p")) {
pTarget = Particle.particles.get(returnNearestP());
dx = (pTarget.x - this.x);
dy = (pTarget.y - this.y);
} else {
goalReached = true;
}
double distance = Math.sqrt((dx) * (dx) + (dy) * (dy));
if (distance > 1) {
x += (dx) / distance * speed;
y += (dy) / distance * speed;
} else if (distance <= 1) {
goalReached = true;
}
}
} else {
double dx = (goalX - this.x);
double dy = (goalY - this.y);
this.x += (dx) * 1 / 50;
this.y += (dy) * 1 / 50;
// addMass(10);
}
}
UPDATE:到目前为止,这是我为优先级系统编写的内容,然后可以在Update()函数中调用它,但是目前我对下一步的工作一无所知。
。 public int targetPriority() {
int priority = 0;
int distance = 0;
int mass = 0;
List<Cell> possibleTarget = null;
int aPriority = 0;
int bPriority = 0;
int cPriority = 0;
for (Cell cell : cells) {
mass = cell.mass / 10;
distance = (int) Math.sqrt((this.x - cell.x) * (this.x - cell.x) + (cell.y - this.y) * (cell.y - this.y));
priority = distance - mass;
for (Cell A : possibleTarget) {
for (Cell B : possibleTarget) {
for (Cell C : possibleTarget) {
}
}
}
}
return priority;
}
答案 0 :(得分:1)
我将使用评分系统解决上述问题,因为每个附近的座席都会根据其距离和大小为其分配一个分数,然后将基于该座席的分数插入基于分数。
例如:假设您附近有3个代理商A,B和C A:1m距离&4尺寸 B:2m距离&1大小 C:5m距离和8尺寸
假设它们都比您小,那么它们将根据距离和大小之间的差异进行评分,如下所示:
A-->3
B-->1
C-->3
A和C相等->选择最接近的一个
这是解决问题的简单方法,但真正的解决方案是使用启发式函数并确定要选择的最佳路径是什么