mirror of
https://github.com/ProjectSWGCore/NGECore2.git
synced 2026-10-13 03:13:26 -04:00
Refactored odb system (read extended), added implementation of Dynamic World Spawns
ODB no longer uses DPL layer of Berkeley DB, persistent classes have to implement the Serializable interface, annotations are no longer used, use transient modifier for variables that should not be persistent. Entity reference errors will no longer occur, however it is better to use ids if the entity that is being referenced is stored in the db too! (or you will have two different objects). DB is cached for 5 minutes then it will write its log/cache to disk, transactions are no longer used.
This commit is contained in:
@@ -21,28 +21,141 @@
|
||||
******************************************************************************/
|
||||
package services.spawn;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Random;
|
||||
import java.util.Vector;
|
||||
|
||||
import resources.datatables.Posture;
|
||||
import main.NGECore;
|
||||
import net.engio.mbassy.listener.Handler;
|
||||
import resources.common.collidables.AbstractCollidable;
|
||||
import resources.common.collidables.AbstractCollidable.EnterEvent;
|
||||
import resources.common.collidables.AbstractCollidable.ExitEvent;
|
||||
import resources.objects.creature.CreatureObject;
|
||||
import services.TerrainService;
|
||||
import services.SimulationService.MoveEvent;
|
||||
import services.ai.LairActor;
|
||||
import engine.resources.objects.SWGObject;
|
||||
import engine.resources.scene.Planet;
|
||||
import engine.resources.scene.Point3D;
|
||||
|
||||
public class DynamicSpawnArea extends SpawnArea {
|
||||
|
||||
public DynamicSpawnArea(Planet planet, AbstractCollidable area) {
|
||||
private DynamicSpawnGroup spawnGroup;
|
||||
private Vector<CreatureObject> mobiles = new Vector<CreatureObject>();
|
||||
|
||||
public DynamicSpawnArea(Planet planet, AbstractCollidable area, DynamicSpawnGroup spawnGroup) {
|
||||
super(planet, area);
|
||||
this.spawnGroup = spawnGroup;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Handler
|
||||
public void onEnter(EnterEvent event) {
|
||||
|
||||
SWGObject object = event.object;
|
||||
|
||||
if(object == null || !(object instanceof CreatureObject))
|
||||
return;
|
||||
|
||||
CreatureObject creature = (CreatureObject) object;
|
||||
|
||||
if(creature.getSlottedObject("ghost") == null)
|
||||
return;
|
||||
|
||||
creature.getEventBus().subscribe(this);
|
||||
// spawn some creatures
|
||||
for(int i = 0; i < 5; i++)
|
||||
spawnCreature(creature);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Handler
|
||||
public void onExit(ExitEvent event) {
|
||||
|
||||
SWGObject object = event.object;
|
||||
|
||||
if(object == null || !(object instanceof CreatureObject))
|
||||
return;
|
||||
|
||||
CreatureObject creature = (CreatureObject) object;
|
||||
|
||||
if(creature.getSlottedObject("ghost") == null)
|
||||
return;
|
||||
|
||||
creature.getEventBus().unsubscribe(this);
|
||||
|
||||
}
|
||||
|
||||
@Handler
|
||||
public void onMove(MoveEvent event) {
|
||||
|
||||
SWGObject object = event.object;
|
||||
|
||||
if(object == null || !(object instanceof CreatureObject) || object.getContainer() != null)
|
||||
return;
|
||||
|
||||
CreatureObject creature = (CreatureObject) object;
|
||||
|
||||
if(creature.getSlottedObject("ghost") == null)
|
||||
return;
|
||||
|
||||
if(new Random().nextFloat() <= 0.25)
|
||||
spawnCreature(creature);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void spawnCreature(CreatureObject creature) {
|
||||
|
||||
NGECore core = NGECore.getInstance();
|
||||
|
||||
Iterator<CreatureObject> it = mobiles.iterator();
|
||||
it.forEachRemaining(mobile -> {
|
||||
if(mobile.getPosture() == Posture.Dead)
|
||||
it.remove();
|
||||
});
|
||||
|
||||
if(mobiles.size() >= spawnGroup.getMaxSpawns())
|
||||
return;
|
||||
|
||||
boolean foundPos = false;
|
||||
int tries = 0;
|
||||
Point3D randomPosition = null;
|
||||
|
||||
while(!foundPos && ++tries < 10) {
|
||||
|
||||
randomPosition = getRandomPosition(creature.getWorldPosition(), 32.f, 200.f);
|
||||
|
||||
if(randomPosition == null)
|
||||
return;
|
||||
|
||||
TerrainService terrainSvc = core.terrainService;
|
||||
|
||||
float height = terrainSvc.getHeight(getPlanet().getID(), randomPosition.x, randomPosition.z);
|
||||
randomPosition.y = height;
|
||||
|
||||
for(CreatureObject mobile : mobiles) {
|
||||
if(mobile.getWorldPosition().getDistance(randomPosition) > spawnGroup.getMinSpawnDistance())
|
||||
foundPos = true;
|
||||
}
|
||||
|
||||
if(!terrainSvc.canBuildAtPosition(creature, randomPosition.x, randomPosition.z))
|
||||
foundPos = false;
|
||||
|
||||
}
|
||||
|
||||
if(!foundPos)
|
||||
return;
|
||||
|
||||
Random random = new Random();
|
||||
|
||||
String mobileTemplate = spawnGroup.getMobiles().get(random.nextInt(spawnGroup.getMobiles().size()));
|
||||
CreatureObject spawnedCreature = core.spawnService.spawnCreature(mobileTemplate, getPlanet().getName(), 0, randomPosition.x, randomPosition.y, randomPosition.z);
|
||||
if(spawnedCreature != null)
|
||||
mobiles.add(spawnedCreature);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user