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

Initial work in the attack service

Signed-off-by: Rogiel <rogiel@rogiel.com>
This commit is contained in:
2011-05-25 20:22:02 -03:00
parent f955208c2e
commit 96bf7df86f
10108 changed files with 41199 additions and 21558 deletions

View File

@@ -0,0 +1,43 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game;
import com.l2jserver.model.server.AttackHit;
import com.l2jserver.model.world.Actor;
import com.l2jserver.service.Service;
import com.l2jserver.service.core.threading.AsyncFuture;
/**
* This service handles attacking. It can schedule auto-attack events and also
* deals the damage.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface AttackService extends Service {
/**
* Schedules an attack command. The attack order will be put in queue and
* the will be executed as soon as possible.
*
* @param attacker
* the actor attacking <tt>target</tt>
* @param target
* the actor receiving the attack from <tt>attacker</tt>
* @return the {@link AsyncFuture} that can be used to retrieve
* {@link AttackHit} object, once the attack has been executed.
*/
AsyncFuture<AttackHit> attack(Actor attacker, Actor target);
}

View File

@@ -0,0 +1,86 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game;
import java.util.concurrent.Callable;
import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.l2jserver.model.server.AttackHit;
import com.l2jserver.model.world.Actor;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.core.threading.AsyncFuture;
import com.l2jserver.service.core.threading.ThreadService;
import com.l2jserver.service.game.world.event.WorldEventDispatcher;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class AttackServiceImpl extends AbstractService implements AttackService {
/**
* The {@link ThreadService} is used to schedule asynchronous attacks
*/
private final ThreadService threadService;
/**
* The {@link WorldEventDispatcher} is used to dispatch attack events to the
* world
*/
private final WorldEventDispatcher eventDispatcher;
@Inject
public AttackServiceImpl(ThreadService threadService,
WorldEventDispatcher eventDispatcher) {
this.threadService = threadService;
this.eventDispatcher = eventDispatcher;
}
@Override
public AsyncFuture<AttackHit> attack(Actor attacker, Actor target) {
Preconditions.checkNotNull(attacker, "attacker");
Preconditions.checkNotNull(target, "target");
Preconditions.checkArgument(!attacker.equals(target),
"attacker must not be equal to target");
return threadService.async(new AttackCallable(attacker, target));
}
/**
* {@link Callable} implementation used to execute attacks.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
private class AttackCallable implements Callable<AttackHit> {
/**
* The attacker
*/
private final Actor attacker;
/**
* The target
*/
private final Actor target;
public AttackCallable(Actor attacker, Actor target) {
this.attacker = attacker;
this.target = target;
}
@Override
public AttackHit call() throws Exception {
return null;
}
}
}

View File

@@ -19,6 +19,7 @@ package com.l2jserver.service.game.character;
import com.l2jserver.model.world.Actor;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.service.Service;
import com.l2jserver.service.game.npc.NotAttackableNPCServiceException;
import com.l2jserver.service.game.spawn.AlreadySpawnedServiceException;
import com.l2jserver.service.game.spawn.NotSpawnedServiceException;
import com.l2jserver.service.game.spawn.SpawnPointNotFoundServiceException;
@@ -79,10 +80,13 @@ public interface CharacterService extends Service {
* if target cannot be set
* @throws ActorIsNotAttackableServiceException
* if the target is not attackable
* @throws NotAttackableNPCServiceException
* if the actor is not attackable
*/
void attack(L2Character character, Actor target)
throws CannotSetTargetServiceException,
ActorIsNotAttackableServiceException;
ActorIsNotAttackableServiceException,
NotAttackableNPCServiceException;
/**
* Jails the given <tt>character</tt> for <tt>time</tt> seconds.

View File

@@ -59,6 +59,8 @@ import com.l2jserver.service.game.chat.ChatChannel;
import com.l2jserver.service.game.chat.ChatChannelListener;
import com.l2jserver.service.game.chat.ChatMessageDestination;
import com.l2jserver.service.game.chat.ChatService;
import com.l2jserver.service.game.npc.NPCService;
import com.l2jserver.service.game.npc.NotAttackableNPCServiceException;
import com.l2jserver.service.game.spawn.AlreadySpawnedServiceException;
import com.l2jserver.service.game.spawn.NotSpawnedServiceException;
import com.l2jserver.service.game.spawn.SpawnPointNotFoundServiceException;
@@ -104,6 +106,10 @@ public class CharacterServiceImpl extends AbstractService implements
* The {@link SpawnService}
*/
private final SpawnService spawnService;
/**
* The {@link NPCService}
*/
private final NPCService npcService;
/**
* The {@link ItemDAO}
*/
@@ -118,12 +124,13 @@ public class CharacterServiceImpl extends AbstractService implements
public CharacterServiceImpl(WorldService worldService,
WorldEventDispatcher eventDispatcher, ChatService chatService,
NetworkService networkService, SpawnService spawnService,
ItemDAO itemDao) {
NPCService npcService, ItemDAO itemDao) {
this.worldService = worldService;
this.eventDispatcher = eventDispatcher;
this.chatService = chatService;
this.networkService = networkService;
this.spawnService = spawnService;
this.npcService = npcService;
this.itemDao = itemDao;
}
@@ -334,7 +341,8 @@ public class CharacterServiceImpl extends AbstractService implements
character.setTargetID(target.getID());
eventDispatcher.dispatch(new CharacterTargetSelectedEvent(
character, target));
conn.write(new CharacterTargetSelectedPacket(target));
conn.write(new CharacterTargetSelectedPacket(target, character
.getLevel() - target.getLevel()));
} else {
// this indicates an inconsistency: reset target and throws an
// exception
@@ -347,7 +355,8 @@ public class CharacterServiceImpl extends AbstractService implements
@Override
public void attack(L2Character character, Actor target)
throws CannotSetTargetServiceException,
ActorIsNotAttackableServiceException {
ActorIsNotAttackableServiceException,
NotAttackableNPCServiceException {
Preconditions.checkNotNull(character, "character");
Preconditions.checkNotNull(target, "target");
final CharacterID id = character.getID();
@@ -362,7 +371,10 @@ public class CharacterServiceImpl extends AbstractService implements
// first try to target this, if it is not already
target(character, target);
// TODO issue attack
npcService.attack(npc, conn, character);
} else {
// TODO throw an exception
conn.sendActionFailed();
}
}

View File

@@ -18,6 +18,7 @@ package com.l2jserver.service.game.npc;
import java.util.List;
import com.l2jserver.game.net.Lineage2Connection;
import com.l2jserver.game.net.packet.client.CharacterActionPacket.CharacterAction;
import com.l2jserver.model.template.NPCTemplate;
import com.l2jserver.model.world.L2Character;
@@ -76,11 +77,13 @@ public interface NPCService extends Service {
*
* @param npc
* the npc
* @param conn
* the {@link Lineage2Connection} object
* @param attacker
* the character
* @throws NotAttackableNPCServiceException
* if {@link NPC} is not attackable
*/
void attack(NPC npc, L2Character attacker)
void attack(NPC npc, Lineage2Connection conn, L2Character attacker)
throws NotAttackableNPCServiceException;
}

View File

@@ -17,21 +17,27 @@
package com.l2jserver.service.game.npc;
import java.util.List;
import java.util.Map;
import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.l2jserver.db.dao.NPCDAO;
import com.l2jserver.game.net.Lineage2Connection;
import com.l2jserver.game.net.packet.client.CharacterActionPacket.CharacterAction;
import com.l2jserver.game.net.packet.server.ActorAttackPacket;
import com.l2jserver.model.server.AttackHit;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.NPC;
import com.l2jserver.model.world.npc.controller.TeleporterController;
import com.l2jserver.model.world.npc.controller.NPCController;
import com.l2jserver.service.AbstractService;
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.network.NetworkService;
import com.l2jserver.util.exception.L2Exception;
import com.l2jserver.util.factory.CollectionFactory;
/**
* Default {@link NPCService} implementation
@@ -47,6 +53,10 @@ public class NPCServiceImpl extends AbstractService implements NPCService {
* The {@link NetworkService} used to discover {@link Lineage2Connection}
*/
private final NetworkService networkService;
/**
* The {@link CharacterService}
*/
private final CharacterService characterService;
/**
* The {@link NPCDAO}
@@ -54,17 +64,26 @@ public class NPCServiceImpl extends AbstractService implements NPCService {
private final NPCDAO npcDao;
/**
* Temporary only
* The {@link Injector} used to create {@link NPCController} instances
*/
@Inject
private TeleporterController controller;
private Injector injector;
/**
* The map containing all active controllers
*/
private Map<Class<? extends NPCController>, NPCController> controllers = CollectionFactory
.newMap();
@Inject
public NPCServiceImpl(SpawnService spawnService,
NetworkService networkService, NPCDAO npcDao) {
NetworkService networkService, CharacterService characterService,
NPCDAO npcDao, Injector injector) {
this.spawnService = spawnService;
this.networkService = networkService;
this.characterService = characterService;
this.npcDao = npcDao;
this.injector = injector;
}
@Override
@@ -77,6 +96,7 @@ public class NPCServiceImpl extends AbstractService implements NPCService {
final Lineage2Connection conn = networkService.discover(character
.getID());
try {
final NPCController controller = getController(npc);
controller.action(npc, conn, character, new String[0]);
} catch (L2Exception e) {
throw new ActionServiceException(e);
@@ -94,6 +114,7 @@ public class NPCServiceImpl extends AbstractService implements NPCService {
final Lineage2Connection conn = networkService.discover(character
.getID());
try {
final NPCController controller = getController(npc);
controller.action(npc, conn, character, args);
} catch (L2Exception e) {
throw new ActionServiceException(e);
@@ -111,9 +132,27 @@ public class NPCServiceImpl extends AbstractService implements NPCService {
}
@Override
public void attack(NPC npc, L2Character attacker)
public void attack(NPC npc, Lineage2Connection conn, L2Character attacker)
throws NotAttackableNPCServiceException {
Preconditions.checkNotNull(npc, "npc");
Preconditions.checkNotNull(conn, "conn");
Preconditions.checkNotNull(attacker, "attacker");
conn.write(new ActorAttackPacket(conn.getCharacter(), new AttackHit(
conn.getCharacter(), npc)));
}
private NPCController getController(NPC npc) {
// make sure everything's synchronized-no duplicated instances
synchronized (controllers) {
final Class<? extends NPCController> controllerClass = npc
.getTemplate().getControllerClass();
NPCController controller = controllers.get(controllerClass);
if (controller == null) {
controller = injector.getInstance(controllerClass);
controllers.put(controllerClass, controller);
}
return controller;
}
}
}