1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-10 09:22:49 +00:00

Monster can now be killed. Their corpses disappear after 5 seconds and

respawn in the specified time.

Signed-off-by: Rogiel <rogiel@rogiel.com>
This commit is contained in:
2011-06-01 00:59:15 -03:00
parent 551dc6917e
commit b188f054df
23 changed files with 42308 additions and 41813 deletions

View File

@@ -54,7 +54,8 @@ public class Log4JLoggingService extends AbstractService implements
l2jLogger.setLevel(Level.INFO);
nettyLogger.setLevel(Level.DEBUG);
Logger.getLogger("com.l2jserver.model.id.object.allocator").setLevel(Level.WARN);
Logger.getLogger("com.l2jserver.model.id.object.allocator").setLevel(
Level.WARN);
}
@Override

View File

@@ -16,6 +16,7 @@
*/
package com.l2jserver.service.core.threading;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -30,10 +31,10 @@ public interface AsyncFuture<T> extends Future<T> {
/**
* Waits until the task is executed
*
* @throws InterruptedException
* @throws ExecutionException
* if the thread has been interrupted while waiting
*/
void await() throws InterruptedException;
void await() throws ExecutionException;
/**
* Waits until the task is executed

View File

@@ -149,10 +149,10 @@ public class ThreadServiceImpl extends AbstractService implements ThreadService
}
@Override
public void await() throws InterruptedException {
public void await() throws ExecutionException {
try {
this.get();
} catch (ExecutionException e) {
} catch (InterruptedException e) {
}
}

View File

@@ -26,11 +26,13 @@ import com.l2jserver.model.server.attack.AttackCalculator.AttackCalculatorType;
import com.l2jserver.model.server.attack.AttackCalculatorContext;
import com.l2jserver.model.server.attack.PhysicalAttackCalculator;
import com.l2jserver.model.world.Actor;
import com.l2jserver.model.world.NPC;
import com.l2jserver.model.world.actor.event.ActorAttackHitEvent;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.AbstractService.Depends;
import com.l2jserver.service.core.threading.AsyncFuture;
import com.l2jserver.service.core.threading.ThreadService;
import com.l2jserver.service.game.npc.NPCService;
import com.l2jserver.service.game.world.event.WorldEventDispatcher;
/**
@@ -47,6 +49,11 @@ public class AttackServiceImpl extends AbstractService implements AttackService
* The {@link ThreadService} is used to schedule asynchronous attacks
*/
private final ThreadService threadService;
/**
* The {@link NPCService}
*/
private final NPCService npcService;
/**
* The {@link WorldEventDispatcher} is used to dispatch attack events to the
* world
@@ -55,8 +62,9 @@ public class AttackServiceImpl extends AbstractService implements AttackService
@Inject
public AttackServiceImpl(ThreadService threadService,
WorldEventDispatcher eventDispatcher) {
NPCService npcService, WorldEventDispatcher eventDispatcher) {
this.threadService = threadService;
this.npcService = npcService;
this.eventDispatcher = eventDispatcher;
}
@@ -91,15 +99,25 @@ public class AttackServiceImpl extends AbstractService implements AttackService
@Override
public AttackHit call() throws Exception {
final double hp = target.getHP();
final double damage = PHYSICAL_ATTACK_CALCULATOR.calculate(
AttackCalculatorType.DAMAGE, new AttackCalculatorContext(
attacker, target));
final double dealDamage = (hp < damage ? hp : damage);
// TODO calculate miss
// TODO calculate critical
// TODO calculate soulshot
// reduce target life
target.setHP(target.getHP() - dealDamage);
final AttackHit hit = new AttackHit(attacker, target, damage);
eventDispatcher.dispatch(new ActorAttackHitEvent(hit));
if (target.getHP() <= 0) {
if (target instanceof NPC)
npcService.die((NPC) target, attacker);
}
return hit;
}
}

View File

@@ -31,9 +31,9 @@ import com.l2jserver.game.net.packet.server.SM_MOVE_TYPE;
import com.l2jserver.game.net.packet.server.SM_TARGET;
import com.l2jserver.model.id.object.CharacterID;
import com.l2jserver.model.world.Actor;
import com.l2jserver.model.world.Actor.ActorState;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.L2Character.CharacterMoveType;
import com.l2jserver.model.world.L2Character.CharacterState;
import com.l2jserver.model.world.NPC;
import com.l2jserver.model.world.character.event.CharacterEnterWorldEvent;
import com.l2jserver.model.world.character.event.CharacterEvent;
@@ -325,7 +325,7 @@ public class CharacterServiceImpl extends AbstractService implements
// we don't set the character coordinate here, this will be done by
// validation packets, sent by client
character.setState(CharacterState.MOVING);
character.setState(ActorState.MOVING);
character.setTargetLocation(coordinate.toPoint());
// for now, let's just write the packet, we don't have much validation

View File

@@ -21,6 +21,7 @@ import java.util.Collection;
import com.l2jserver.game.net.Lineage2Connection;
import com.l2jserver.game.net.packet.client.CM_CHAR_ACTION.CharacterAction;
import com.l2jserver.model.template.NPCTemplate;
import com.l2jserver.model.world.Actor;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.NPC;
import com.l2jserver.service.Service;
@@ -68,6 +69,17 @@ public interface NPCService extends Service {
void action(NPC npc, L2Character character, String... args)
throws ActionServiceException, CannotSetTargetServiceException;
/**
* Kills the given <tt>npc</tt>. If "nobody" killed the NPC (i.e. died by
* admin command), <tt>killer</tt> can be null.
*
* @param npc
* the npc being killed
* @param killer
* the killer actor
*/
void die(NPC npc, Actor killer);
/**
* Moves an given <tt>npc</tt> to an <tt>point</tt>
*

View File

@@ -28,10 +28,12 @@ import com.l2jserver.db.dao.NPCDAO;
import com.l2jserver.game.net.Lineage2Connection;
import com.l2jserver.game.net.packet.client.CM_CHAR_ACTION.CharacterAction;
import com.l2jserver.model.template.NPCTemplate;
import com.l2jserver.model.world.Actor;
import com.l2jserver.model.world.Actor.ActorState;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.NPC;
import com.l2jserver.model.world.NPC.NPCState;
import com.l2jserver.model.world.npc.controller.NPCController;
import com.l2jserver.model.world.npc.event.NPCDieEvent;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.AbstractService.Depends;
import com.l2jserver.service.core.threading.AsyncFuture;
@@ -43,6 +45,8 @@ import com.l2jserver.service.game.character.CharacterService;
import com.l2jserver.service.game.spawn.AlreadySpawnedServiceException;
import com.l2jserver.service.game.spawn.SpawnPointNotFoundServiceException;
import com.l2jserver.service.game.spawn.SpawnService;
import com.l2jserver.service.game.world.WorldService;
import com.l2jserver.service.game.world.event.WorldEventDispatcher;
import com.l2jserver.service.network.NetworkService;
import com.l2jserver.util.exception.L2Exception;
import com.l2jserver.util.factory.CollectionFactory;
@@ -77,6 +81,10 @@ public class NPCServiceImpl extends AbstractService implements NPCService {
*/
private final AttackService attackService;
/**
* The {@link WorldService} event dispatcher
*/
private final WorldEventDispatcher eventDispatcher;
/**
* The {@link NPCDAO}
*/
@@ -98,12 +106,14 @@ public class NPCServiceImpl extends AbstractService implements NPCService {
public NPCServiceImpl(SpawnService spawnService,
NetworkService networkService, CharacterService characterService,
ThreadService threadService, AttackService attackService,
NPCDAO npcDao, Injector injector) {
WorldEventDispatcher eventDispatcher, NPCDAO npcDao,
Injector injector) {
this.spawnService = spawnService;
this.networkService = networkService;
this.characterService = characterService;
this.threadService = threadService;
this.attackService = attackService;
this.eventDispatcher = eventDispatcher;
this.npcDao = npcDao;
this.injector = injector;
}
@@ -143,12 +153,34 @@ public class NPCServiceImpl extends AbstractService implements NPCService {
}
}
@Override
public void die(NPC npc, Actor killer) {
Preconditions.checkNotNull(npc, "npc");
Preconditions.checkNotNull(killer, "killer");
// set npc as dead
npc.setState(ActorState.DEAD);
// dispatch die event
eventDispatcher.dispatch(new NPCDieEvent(npc, killer));
// schedule corpse removal -- npc will be kept in the world until then
spawnService.unspawn(npc, 5, TimeUnit.SECONDS);
// schedule an respawn
spawnService.spawn(npc, null, npc.getRespawnInterval(),
TimeUnit.MILLISECONDS);
// reset hp and cp
npc.setHP(npc.getStats().getMaxHP());
npc.setMP(npc.getStats().getMaxMP());
}
@Override
public AsyncFuture<Boolean> move(final NPC npc, final Point3D point) {
if (!npc.isIdle())
// TODO throw an exception
return null;
npc.setState(NPCState.MOVING);
npc.setState(ActorState.MOVING);
// calculate walking time
final Point3D start = npc.getPoint();
final double distance = start.getDistance(point);

View File

@@ -16,14 +16,17 @@
*/
package com.l2jserver.service.game.spawn;
import java.util.concurrent.TimeUnit;
import com.l2jserver.model.world.Actor.ActorState;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.L2Character.CharacterState;
import com.l2jserver.model.world.Player;
import com.l2jserver.model.world.PositionableObject;
import com.l2jserver.model.world.event.SpawnEvent;
import com.l2jserver.model.world.player.event.PlayerTeleportedEvent;
import com.l2jserver.model.world.player.event.PlayerTeleportingEvent;
import com.l2jserver.service.Service;
import com.l2jserver.service.core.threading.AsyncFuture;
import com.l2jserver.util.geometry.Coordinate;
import com.l2jserver.util.geometry.Point3D;
@@ -40,13 +43,13 @@ public interface SpawnService extends Service {
* registered in the world (if it isn't already)
*
* @param object
* the spawnable object
* the PositionableObject object
* @param point
* the spawning point. If null, will try to use
* {@link Spawnable#getPoint()}.
* {@link PositionableObject#getPoint()}.
* @throws SpawnPointNotFoundServiceException
* if could not find an spawn point (i.e <tt>point</tt> and
* {@link Spawnable#getPoint()} are null)
* {@link PositionableObject#getPoint()} are null)
* @throws AlreadySpawnedServiceException
* if the object is already spawned in the world
*/
@@ -54,6 +57,48 @@ public interface SpawnService extends Service {
throws SpawnPointNotFoundServiceException,
AlreadySpawnedServiceException;
/**
* Schedules an {@link PositionableObject} object to be spawned in a certain
* time.
*
* @param object
* the PositionableObject object
* @param point
* the spawning point. If null, will try to use
* {@link PositionableObject#getPoint()}.
* @param time
* the amount of time to wait before spawn
* @param unit
* the unit of <tt>time</tt>
* @return an future that can be used to obtain spawn exceptions
*/
AsyncFuture<?> spawn(PositionableObject object, Point3D point, long time,
TimeUnit unit);
/**
* Unspawns an {@link PositionableObject} object from the world
*
* @param object
* the PositionableObject object
* @throws NotSpawnedServiceException
* if the object is not spawned
*/
void unspawn(PositionableObject object) throws NotSpawnedServiceException;
/**
* Schedules an {@link PositionableObject} object to be spawned in a certain
* time.
*
* @param object
* the PositionableObject object
* @param time
* the amount of time to wait before respawn
* @param unit
* the unit of <tt>time</tt>
* @return an future that can be used to obtain spawn exceptions
*/
AsyncFuture<?> unspawn(PositionableObject object, long time, TimeUnit unit);
/**
* Teleports the object to the given <tt>point</tt>.
* <p>
@@ -85,29 +130,8 @@ public interface SpawnService extends Service {
* the character object
* @throws CharacterNotTeleportingServiceException
* if the character state is not
* {@link CharacterState#TELEPORTING}
* {@link ActorState#TELEPORTING}
*/
void finishTeleport(L2Character character)
throws CharacterNotTeleportingServiceException;
/**
* Schedules an {@link Spawnable} object to be respawn in a certain time.
* <p>
* TODO this is not complete
*
* @param spawnable
* the spawnable object
*/
void scheduleRespawn(PositionableObject spawnable);
/**
* Unspawns an object from the world
*
* @param spawnable
* the spawnable object
* @throws NotSpawnedServiceException
* if the object is not spawned
*/
void unspawn(PositionableObject spawnable)
throws NotSpawnedServiceException;
}

View File

@@ -16,24 +16,32 @@
*/
package com.l2jserver.service.game.spawn;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.l2jserver.game.net.Lineage2Connection;
import com.l2jserver.game.net.packet.server.SM_CHAR_INFO_EXTRA;
import com.l2jserver.game.net.packet.server.SM_CHAR_INFO;
import com.l2jserver.game.net.packet.server.SM_CHAR_INFO_EXTRA;
import com.l2jserver.game.net.packet.server.SM_TELEPORT;
import com.l2jserver.model.id.object.CharacterID;
import com.l2jserver.model.world.Actor;
import com.l2jserver.model.world.Actor.ActorState;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.L2Character.CharacterState;
import com.l2jserver.model.world.NPC;
import com.l2jserver.model.world.Player;
import com.l2jserver.model.world.PositionableObject;
import com.l2jserver.model.world.event.SpawnEvent;
import com.l2jserver.model.world.event.UnspawnEvent;
import com.l2jserver.model.world.npc.event.NPCSpawnEvent;
import com.l2jserver.model.world.npc.event.NPCUnspawnEvent;
import com.l2jserver.model.world.player.event.PlayerTeleportedEvent;
import com.l2jserver.model.world.player.event.PlayerTeleportingEvent;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.AbstractService.Depends;
import com.l2jserver.service.core.threading.AsyncFuture;
import com.l2jserver.service.core.threading.ThreadService;
import com.l2jserver.service.game.world.WorldService;
import com.l2jserver.service.game.world.event.WorldEventDispatcher;
import com.l2jserver.service.network.NetworkService;
@@ -45,7 +53,7 @@ import com.l2jserver.util.geometry.Point3D;
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
@Depends({ WorldService.class })
@Depends({ WorldService.class, NetworkService.class, ThreadService.class })
public class SpawnServiceImpl extends AbstractService implements SpawnService {
/**
* The {@link WorldService}
@@ -59,18 +67,25 @@ public class SpawnServiceImpl extends AbstractService implements SpawnService {
* The {@link NetworkService}
*/
private final NetworkService networkService;
/**
* The {@link ThreadService}
*/
private final ThreadService threadService;
@Inject
public SpawnServiceImpl(WorldService worldService,
WorldEventDispatcher eventDispatcher, NetworkService networkService) {
WorldEventDispatcher eventDispatcher,
NetworkService networkService, ThreadService threadService) {
this.worldService = worldService;
this.eventDispatcher = eventDispatcher;
this.networkService = networkService;
this.threadService = threadService;
}
@Override
public void spawn(PositionableObject object, Point3D point)
throws SpawnPointNotFoundServiceException {
throws SpawnPointNotFoundServiceException,
AlreadySpawnedServiceException {
Preconditions.checkNotNull(object, "object");
// sanitize
if (point == null)
@@ -83,11 +98,13 @@ public class SpawnServiceImpl extends AbstractService implements SpawnService {
// set the spawning point
object.setPoint(point);
// reset actor state
if (object instanceof Actor) {
((Actor) object).setState(null);
}
// register object in the world
if (!worldService.add(object))
// TODO this should throw an exception
// object was already in world
return;
throw new AlreadySpawnedServiceException();
// create the SpawnEvent
SpawnEvent event = null;
@@ -95,6 +112,7 @@ public class SpawnServiceImpl extends AbstractService implements SpawnService {
final NPC npc = (NPC) object;
event = new NPCSpawnEvent(npc, point);
} else if (object instanceof L2Character) {
// TODO character spawn event
event = null;
}
@@ -105,6 +123,68 @@ public class SpawnServiceImpl extends AbstractService implements SpawnService {
// remember: broadcasting is done through events!
}
@Override
public AsyncFuture<?> spawn(final PositionableObject object,
final Point3D point, long time, TimeUnit unit) {
Preconditions.checkNotNull(object, "object");
Preconditions.checkArgument(time > 0, "time < 0");
Preconditions.checkNotNull(unit, "unit");
return threadService.async(time, unit,
new Callable<PositionableObject>() {
@Override
public PositionableObject call() throws Exception {
spawn(object, point);
return object;
}
});
}
@Override
public void unspawn(PositionableObject object)
throws NotSpawnedServiceException {
Preconditions.checkNotNull(object, "object");
if (object.getPoint() == null)
throw new NotSpawnedServiceException();
// unregister object in the world
if (!worldService.remove(object))
throw new NotSpawnedServiceException();
final Point3D point = object.getPoint();
// create the SpawnEvent
UnspawnEvent event = null;
if (object instanceof NPC) {
final NPC npc = (NPC) object;
event = new NPCUnspawnEvent(npc, point);
} else if (object instanceof L2Character) {
// TODO character unspawn event
event = null;
}
// TODO throw an exception if event is null
if (event != null)
// dispatch unspawn event
eventDispatcher.dispatch(event);
}
@Override
public AsyncFuture<?> unspawn(final PositionableObject object, long time,
TimeUnit unit) {
Preconditions.checkNotNull(object, "object");
Preconditions.checkArgument(time > 0, "time <= 0");
Preconditions.checkNotNull(unit, "unit");
return threadService.async(time, unit,
new Callable<PositionableObject>() {
@Override
public PositionableObject call() throws Exception {
unspawn(object);
return object;
}
});
}
@Override
public void teleport(Player player, Coordinate coordinate)
throws CharacterAlreadyTeleportingServiceException {
@@ -119,9 +199,9 @@ public class SpawnServiceImpl extends AbstractService implements SpawnService {
if (conn == null)
// TODO throw an exception here
return;
conn.write(new SM_TELEPORT(conn.getCharacter(),
coordinate.toPoint()));
((L2Character) player).setState(CharacterState.TELEPORTING);
conn.write(new SM_TELEPORT(conn.getCharacter(), coordinate
.toPoint()));
((L2Character) player).setState(ActorState.TELEPORTING);
((L2Character) player).setTargetLocation(coordinate.toPoint());
} else {
player.setPosition(coordinate);
@@ -153,18 +233,4 @@ public class SpawnServiceImpl extends AbstractService implements SpawnService {
conn.write(new SM_CHAR_INFO(character));
conn.write(new SM_CHAR_INFO_EXTRA(character));
}
@Override
public void scheduleRespawn(PositionableObject object) {
Preconditions.checkNotNull(object, "object");
// TODO Auto-generated method stub
}
@Override
public void unspawn(PositionableObject object) {
Preconditions.checkNotNull(object, "object");
// TODO Auto-generated method stub
}
}

View File

@@ -37,6 +37,7 @@ public interface WorldService extends Service, Iterable<WorldObject> {
*
* @param object
* the object
* @return true if object was not present in the world
*/
boolean add(WorldObject object);
@@ -45,6 +46,7 @@ public interface WorldService extends Service, Iterable<WorldObject> {
*
* @param object
* the object
* @return true if object was present in the world
*/
boolean remove(WorldObject object);

View File

@@ -22,6 +22,7 @@ import com.l2jserver.game.net.Lineage2Connection;
import com.l2jserver.game.net.SystemMessage;
import com.l2jserver.game.net.packet.server.SM_ATTACK;
import com.l2jserver.game.net.packet.server.SM_CHAR_INFO_BROADCAST;
import com.l2jserver.game.net.packet.server.SM_DIE;
import com.l2jserver.game.net.packet.server.SM_MOVE;
import com.l2jserver.game.net.packet.server.SM_MOVE_TYPE;
import com.l2jserver.game.net.packet.server.SM_NPC_INFO;
@@ -32,6 +33,8 @@ import com.l2jserver.model.world.NPC;
import com.l2jserver.model.world.PositionableObject;
import com.l2jserver.model.world.WorldObject;
import com.l2jserver.model.world.actor.event.ActorAttackHitEvent;
import com.l2jserver.model.world.actor.event.ActorDieEvent;
import com.l2jserver.model.world.actor.event.ActorUnspawnEvent;
import com.l2jserver.model.world.character.event.CharacterEnterWorldEvent;
import com.l2jserver.model.world.character.event.CharacterLeaveWorldEvent;
import com.l2jserver.model.world.character.event.CharacterMoveEvent;
@@ -76,7 +79,7 @@ public class BroadcastServiceImpl extends AbstractService implements
final CharacterID id = character.getID();
// broadcast everything nearby
//broadcast(conn);
// broadcast(conn);
// event broadcast listener
// this listener will be filtered so that only interesting events are
@@ -96,7 +99,8 @@ public class BroadcastServiceImpl extends AbstractService implements
|| e instanceof CharacterEnterWorldEvent) {
broadcast(conn, e.getObject());
} else if (e instanceof PlayerTeleportingEvent
|| e instanceof CharacterLeaveWorldEvent) {
|| e instanceof CharacterLeaveWorldEvent
|| e instanceof ActorUnspawnEvent) {
// object is now out of sight
conn.write(new SM_OBJECT_REMOVE(object));
} else if (e instanceof CharacterWalkingEvent) {
@@ -105,6 +109,8 @@ public class BroadcastServiceImpl extends AbstractService implements
} else if (e instanceof CharacterRunningEvent) {
conn.write(new SM_MOVE_TYPE(((CharacterRunningEvent) e)
.getCharacter()));
} else if (e instanceof ActorDieEvent) {
conn.write(new SM_DIE(((ActorDieEvent) e).getActor()));
}
// keep listener alive
return true;