Bladeren bron

Refactoring L2AttackableAI

Changed _skill to protected.
Now L2AttackableAI won't implement Runnable anymore.
Externalized class FearTask to reduce the size of L2AttackableAI.
Removed method getActiveChar(), replaced it with a covariant version of
getActor() method, honoring OOP.
Renamed variables from me and npc to actor.
Using Java 10 local variable type inference where it's possible.
Other minor fixes.
Zoey76 1 jaar geleden
bovenliggende
commit
099e6fbadd

+ 1 - 1
src/main/java/com/l2jserver/gameserver/ai/AbstractAI.java

@@ -132,7 +132,7 @@ public abstract class AbstractAI implements Ctrl {
 	protected L2Character _followTarget;
 	
 	/** The skill we are currently casting by INTENTION_CAST */
-	Skill _skill;
+	protected Skill _skill;
 	
 	/** Different internal state flags */
 	private int _moveToPawnTimeout;

+ 50 - 0
src/main/java/com/l2jserver/gameserver/ai/FearTask.java

@@ -0,0 +1,50 @@
+/*
+ * Copyright © 2004-2023 L2J Server
+ * 
+ * This file is part of L2J Server.
+ * 
+ * L2J Server 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.
+ * 
+ * L2J Server 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.l2jserver.gameserver.ai;
+
+import com.l2jserver.gameserver.model.actor.L2Character;
+
+/**
+ * Fear task.
+ * @author Zoey76
+ * @version 2.6.3.0
+ */
+public class FearTask implements Runnable {
+	
+	protected static final int FEAR_TICKS = 5;
+	
+	private final L2AttackableAI ai;
+	
+	private final L2Character effector;
+	
+	private boolean start;
+	
+	public FearTask(L2AttackableAI ai, L2Character effector, boolean start) {
+		this.ai = ai;
+		this.effector = effector;
+		this.start = start;
+	}
+	
+	@Override
+	public void run() {
+		ai.setFearTime(ai.getFearTime() - FEAR_TICKS);
+		ai.onEvtAfraid(effector, start);
+		start = false;
+	}
+}

File diff suppressed because it is too large
+ 223 - 259
src/main/java/com/l2jserver/gameserver/ai/L2AttackableAI.java


+ 17 - 25
src/main/java/com/l2jserver/gameserver/ai/L2ControllableMobAI.java

@@ -22,7 +22,6 @@ import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
 import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
 
 import java.util.Collection;
-import java.util.List;
 import java.util.stream.Collectors;
 
 import com.l2jserver.commons.util.Rnd;
@@ -80,11 +79,11 @@ public final class L2ControllableMobAI extends L2AttackableAI {
 	
 	@Override
 	protected void onEvtThink() {
-		if (isThinking()) {
+		if (_isThinking) {
 			return;
 		}
 		
-		setThinking(true);
+		_isThinking = true;
 		
 		try {
 			switch (getAlternateAI()) {
@@ -114,30 +113,31 @@ public final class L2ControllableMobAI extends L2AttackableAI {
 					break;
 			}
 		} finally {
-			setThinking(false);
+			_isThinking = false;
 		}
 	}
 	
 	@Override
 	protected void thinkCast() {
-		L2Attackable npc = (L2Attackable) _actor;
-		if ((getAttackTarget() == null) || getAttackTarget().isAlikeDead()) {
+		final var attackTarget = getAttackTarget();
+		if ((attackTarget == null) || attackTarget.isAlikeDead()) {
 			setAttackTarget(findNextRndTarget());
 			clientStopMoving(null);
+			
+			if (attackTarget == null) {
+				return;
+			}
 		}
 		
-		if (getAttackTarget() == null) {
-			return;
-		}
-		
-		npc.setTarget(getAttackTarget());
+		final var actor = getActor();
+		actor.setTarget(attackTarget);
 		
 		if (!_actor.isMuted()) {
 			int max_range = 0;
 			// check distant skills
 			
 			for (Skill sk : _actor.getAllSkills()) {
-				if (Util.checkIfInRange(sk.getCastRange(), _actor, getAttackTarget(), true) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume2(sk))) {
+				if (Util.checkIfInRange(sk.getCastRange(), _actor, attackTarget, true) && !_actor.isSkillDisabled(sk) && (_actor.getCurrentMp() > _actor.getStat().getMpConsume2(sk))) {
 					_actor.doCast(sk);
 					return;
 				}
@@ -146,7 +146,7 @@ public final class L2ControllableMobAI extends L2AttackableAI {
 			}
 			
 			if (!isNotMoving()) {
-				moveToPawn(getAttackTarget(), max_range);
+				moveToPawn(attackTarget, max_range);
 			}
 		}
 	}
@@ -351,8 +351,8 @@ public final class L2ControllableMobAI extends L2AttackableAI {
 			return false;
 		}
 		
-		final L2Attackable me = getActiveChar();
-		if (!me.isInsideRadius(target, me.getAggroRange(), false, false) || (Math.abs(_actor.getZ() - target.getZ()) > 100)) {
+		final var actor = getActor();
+		if (!_actor.isInsideRadius(target, actor.getAggroRange(), false, false) || (Math.abs(_actor.getZ() - target.getZ()) > 100)) {
 			return false;
 		}
 		
@@ -363,11 +363,11 @@ public final class L2ControllableMobAI extends L2AttackableAI {
 				return false;
 			}
 		}
-		return me.isAggressive();
+		return actor.isAggressive();
 	}
 	
 	private L2Character findNextRndTarget() {
-		final List<L2Character> potentialTarget = _actor.getKnownList().getKnownCharactersInRadius(getActiveChar().getAggroRange()).stream().filter(this::checkAutoAttackCondition).collect(Collectors.toList());
+		final var potentialTarget = _actor.getKnownList().getKnownCharactersInRadius(getActor().getAggroRange()).stream().filter(this::checkAutoAttackCondition).collect(Collectors.toList());
 		if (potentialTarget.isEmpty()) {
 			return null;
 		}
@@ -411,10 +411,6 @@ public final class L2ControllableMobAI extends L2AttackableAI {
 		setForcedTarget(target);
 	}
 	
-	public boolean isThinking() {
-		return _isThinking;
-	}
-	
 	public boolean isNotMoving() {
 		return _isNotMoving;
 	}
@@ -423,10 +419,6 @@ public final class L2ControllableMobAI extends L2AttackableAI {
 		_isNotMoving = isNotMoving;
 	}
 	
-	public void setThinking(boolean isThinking) {
-		_isThinking = isThinking;
-	}
-	
 	private L2Character getForcedTarget() {
 		return _forcedTarget;
 	}

+ 1 - 1
src/main/java/com/l2jserver/gameserver/model/actor/instance/L2PcInstance.java

@@ -4108,7 +4108,7 @@ public final class L2PcInstance extends L2Playable {
 			oldTarget.removeStatusListener(this);
 		}
 		
-		if (newTarget instanceof final L2Character target) {
+		if (newTarget instanceof L2Character target) {
 			// Validate location of the new target.
 			if (newTarget.getObjectId() != getObjectId()) {
 				sendPacket(new ValidateLocation(target));

Some files were not shown because too many files changed in this diff