-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spiders that appeared below 50 blocks are afraid of light (Test logic)
- Loading branch information
1 parent
d3207b7
commit 224952c
Showing
6 changed files
with
274 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
.../src/main/java/org/imesense/dynamicspawncontrol/ai/spider/action/DelayedEntityAITask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.imesense.dynamicspawncontrol.ai.spider.action; | ||
|
||
import net.minecraft.entity.EntityCreature; | ||
import net.minecraft.entity.ai.EntityAIBase; | ||
|
||
/** | ||
* Почти оптимально работает логика боязни свет для паука под землей | ||
*/ | ||
public class DelayedEntityAITask extends EntityAIBase | ||
{ | ||
private final EntityAIBase originalTask; | ||
private int delay; | ||
|
||
public DelayedEntityAITask(EntityCreature entity, EntityAIBase originalTask, int delay) | ||
{ | ||
this.originalTask = originalTask; | ||
this.delay = delay; | ||
} | ||
|
||
@Override | ||
public boolean shouldExecute() | ||
{ | ||
if (delay > 0) { | ||
delay--; | ||
return false; | ||
} | ||
return originalTask.shouldExecute(); | ||
} | ||
|
||
@Override | ||
public boolean shouldContinueExecuting() | ||
{ | ||
return originalTask.shouldContinueExecuting(); | ||
} | ||
|
||
@Override | ||
public void startExecuting() | ||
{ | ||
originalTask.startExecuting(); | ||
} | ||
|
||
@Override | ||
public void updateTask() | ||
{ | ||
originalTask.updateTask(); | ||
} | ||
} |
153 changes: 153 additions & 0 deletions
153
...main/java/org/imesense/dynamicspawncontrol/ai/spider/action/EntityAISpiderAvoidLight.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package org.imesense.dynamicspawncontrol.ai.spider.action; | ||
|
||
import net.minecraft.entity.EntityCreature; | ||
import net.minecraft.entity.ai.EntityAIBase; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
import org.imesense.dynamicspawncontrol.core.logfile.Log; | ||
|
||
/** | ||
* | ||
*/ | ||
public class EntityAISpiderAvoidLight extends EntityAIBase | ||
{ | ||
private final EntityCreature spider; | ||
private final double speed; | ||
private final int lightThreshold; | ||
private BlockPos targetPosition; | ||
|
||
public EntityAISpiderAvoidLight(EntityCreature spider, double speed, int lightThreshold) | ||
{ | ||
this.spider = spider; | ||
this.speed = speed; | ||
this.lightThreshold = lightThreshold; | ||
this.setMutexBits(1); //-' Только движение | ||
} | ||
|
||
@Override | ||
public boolean shouldExecute() | ||
{ | ||
if (spider.getAttackTarget() != null) | ||
{ | ||
return false; //-' Не пугаемся, если есть цель | ||
} | ||
|
||
World world = spider.world; | ||
BlockPos pos = spider.getPosition(); | ||
|
||
// Проверяем освещение | ||
if (pos.getY() >= 50 || world.getLight(pos) <= lightThreshold) | ||
{ | ||
Log.writeDataToLogFile(0, "shouldExecute == false"); | ||
return false; //-' Условия не выполнены | ||
} | ||
|
||
//-' Ищем менее освещённое место | ||
this.targetPosition = findDarkerSpot(pos, world); | ||
return true; //-' Всегда пытаемся что-то сделать | ||
} | ||
|
||
/* | ||
@Override | ||
public boolean shouldExecute() | ||
{ | ||
if (spider.getAttackTarget() != null) | ||
{ | ||
return false; | ||
} | ||
World world = spider.world; | ||
BlockPos pos = spider.getPosition(); | ||
// Проверяем, готов ли уровень света | ||
if (!world.isAreaLoaded(pos, 1)) | ||
{ | ||
return false; // Область ещё не загружена | ||
} | ||
if (pos.getY() >= 50 || world.getLight(pos) <= lightThreshold) | ||
{ | ||
return false; | ||
} | ||
this.targetPosition = findDarkerSpot(pos, world); | ||
return true; | ||
} | ||
*/ | ||
|
||
@Override | ||
public boolean shouldContinueExecuting() | ||
{ | ||
if (spider.getAttackTarget() != null) | ||
{ | ||
return false; //-' Прекращаем, если появилась цель | ||
} | ||
|
||
World world = spider.world; | ||
BlockPos pos = spider.getPosition(); | ||
|
||
//-' Проверяем текущий свет | ||
return pos.getY() < 50 && world.getLight(pos) > lightThreshold; | ||
} | ||
|
||
@Override | ||
public void startExecuting() | ||
{ | ||
if (this.targetPosition != null) | ||
{ | ||
Log.writeDataToLogFile(0, "0"); | ||
moveAwayFromLight(); | ||
} | ||
else | ||
{ | ||
Log.writeDataToLogFile(0, "1"); | ||
moveRandomly(); //-' Если нет цели, двигаемся случайно | ||
} | ||
} | ||
|
||
private void moveAwayFromLight() | ||
{ | ||
if (this.targetPosition != null) | ||
{ | ||
spider.getNavigator().tryMoveToXYZ( | ||
this.targetPosition.getX(), | ||
this.targetPosition.getY(), | ||
this.targetPosition.getZ(), | ||
speed | ||
); | ||
} | ||
} | ||
|
||
private void moveRandomly() | ||
{ | ||
double randomX = spider.posX + (spider.getRNG().nextDouble() - 0.5) * 10; | ||
double randomZ = spider.posZ + (spider.getRNG().nextDouble() - 0.5) * 10; | ||
spider.getNavigator().tryMoveToXYZ(randomX, spider.posY, randomZ, speed); | ||
} | ||
|
||
private BlockPos findDarkerSpot(BlockPos pos, World world) | ||
{ | ||
BlockPos darkerSpot = null; | ||
int lowestLight = Integer.MAX_VALUE; | ||
|
||
// Проверяем более широкий радиус | ||
for (int dx = -15; dx <= 15; dx++) | ||
{ | ||
for (int dz = -15; dz <= 15; dz++) | ||
{ | ||
BlockPos newPos = pos.add(dx, 0, dz); | ||
int lightLevel = world.getLight(newPos); | ||
|
||
// Ищем место с минимальным светом | ||
if (lightLevel < lowestLight && world.isAirBlock(newPos)) | ||
{ | ||
lowestLight = lightLevel; | ||
darkerSpot = newPos; | ||
} | ||
} | ||
} | ||
|
||
return darkerSpot; | ||
} | ||
} | ||
|
69 changes: 69 additions & 0 deletions
69
...2/src/main/java/org/imesense/dynamicspawncontrol/ai/spider/event/OnSearchEnemyAttack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.imesense.dynamicspawncontrol.ai.spider.event; | ||
|
||
import net.minecraft.entity.monster.EntitySpider; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
import net.minecraftforge.event.entity.EntityJoinWorldEvent; | ||
import net.minecraftforge.event.entity.living.LivingDeathEvent; | ||
import net.minecraftforge.event.entity.living.LivingEvent; | ||
import net.minecraftforge.fml.common.Mod; | ||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | ||
import org.imesense.dynamicspawncontrol.DynamicSpawnControlStructure; | ||
import org.imesense.dynamicspawncontrol.ai.spider.action.DelayedEntityAITask; | ||
import org.imesense.dynamicspawncontrol.ai.spider.action.EntityAISpiderAvoidLight; | ||
//import org.imesense.dynamicspawncontrol.ai.spider.action.EntityAISpiderFearLight; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* | ||
*/ | ||
@Mod.EventBusSubscriber(modid = DynamicSpawnControlStructure.STRUCT_INFO_MOD.MOD_ID) | ||
public final class OnSearchEnemyAttack | ||
{ | ||
//-' Вариант 0 (Лагающий) | ||
/*@SubscribeEvent | ||
public static void onSpiderSpawn(EntityJoinWorldEvent event) | ||
{ | ||
if (event.getEntity() instanceof EntitySpider) | ||
{ | ||
EntitySpider spider = (EntitySpider) event.getEntity(); | ||
if (spider.getPosition().getY() < 50) | ||
{ | ||
//-' Принудительно запускаем проверку света при спавне | ||
spider.tasks.addTask(5, new EntityAISpiderAvoidLight(spider, 1.2, 7)); | ||
} | ||
} | ||
}*/ | ||
|
||
//-' Вариант 1 (Принудительный тест) | ||
/** | ||
@SubscribeEvent | ||
public static void onSpiderSpawn(EntityJoinWorldEvent event) | ||
{ | ||
if (event.getEntity() instanceof EntitySpider) | ||
{ | ||
EntitySpider spider = (EntitySpider) event.getEntity(); | ||
World world = spider.world; | ||
BlockPos pos = spider.getPosition(); | ||
//-' Принудительно обновляем освещение | ||
world.checkLight(pos); | ||
spider.tasks.addTask(5, new EntityAISpiderAvoidLight(spider, 1.2, 7)); | ||
} | ||
} | ||
*/ | ||
|
||
//-' Вариант 2 (Почти оптимально) | ||
@SubscribeEvent | ||
public static void onSpiderSpawn(EntityJoinWorldEvent event) | ||
{ | ||
if (event.getEntity() instanceof EntitySpider) | ||
{ | ||
EntitySpider spider = (EntitySpider) event.getEntity(); | ||
spider.tasks.addTask(5, new DelayedEntityAITask(spider, new EntityAISpiderAvoidLight(spider, 1.2, 7), 20)); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...i/zombie/entityaibase/BreakTorchTask.java → ...trol/ai/zombie/action/BreakTorchTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters