Skip to content

Commit

Permalink
Memory optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-dev committed Aug 25, 2024
1 parent e230aff commit a8446ea
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 253 deletions.
2 changes: 1 addition & 1 deletion game-server/data/handlers/ai/ServantNpcAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void healOrAttack() {
if ((template.getType() != SkillType.MAGICAL || !getOwner().getEffectController().isAbnormalSet(AbnormalState.SILENCE))
&& (template.getType() != SkillType.PHYSICAL || !getOwner().getEffectController().isAbnormalSet(AbnormalState.BIND))
&& (!getOwner().getEffectController().isInAnyAbnormalState(AbnormalState.CANT_ATTACK_STATE))
&& (!getOwner().getTransformModel().isActive() || getOwner().getTransformModel().getBanUseSkills() != 1)) {
&& (!getOwner().isTransformed() || getOwner().getTransformModel().getBanUseSkills() != 1)) {
SkillEngine.getInstance().getSkill(getOwner(), skill.getSkillId(), skill.getSkillLevel(), getOwner().getTarget()).useSkill();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected static void skillAction(NpcAI npcAI) {
if ((template.getType() == SkillType.MAGICAL && owner.getEffectController().isAbnormalSet(AbnormalState.SILENCE))
|| (template.getType() == SkillType.PHYSICAL && owner.getEffectController().isAbnormalSet(AbnormalState.BIND))
|| (owner.getEffectController().isInAnyAbnormalState(AbnormalState.CANT_ATTACK_STATE))
|| (owner.getTransformModel().isActive() && owner.getTransformModel().getBanUseSkills() == 1)) {
|| (owner.isTransformed() && owner.getTransformModel().getBanUseSkills() == 1)) {
afterUseSkill(npcAI);
} else {
if (template.getProperties().getFirstTarget() == FirstTargetAttribute.ME) {
Expand Down Expand Up @@ -233,7 +233,7 @@ private static boolean isReady(Npc owner, NpcSkillEntry entry) {
if ((template.getType() == SkillType.MAGICAL && owner.getEffectController().isAbnormalSet(AbnormalState.SILENCE))
|| (template.getType() == SkillType.PHYSICAL && owner.getEffectController().isAbnormalSet(AbnormalState.BIND))
|| (owner.getEffectController().isInAnyAbnormalState(AbnormalState.CANT_ATTACK_STATE))
|| (owner.getTransformModel().isActive() && owner.getTransformModel().getBanUseSkills() == 1)) {
|| (owner.isTransformed() && owner.getTransformModel().getBanUseSkills() == 1)) {
return false;
} else {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantLock;

import com.aionemu.gameserver.controllers.attack.AttackResult;
import com.aionemu.gameserver.controllers.attack.AttackStatus;
Expand All @@ -20,21 +19,17 @@
*/
public class ObserveController {

private ReentrantLock lock = new ReentrantLock();
protected Collection<ActionObserver> observers = new CopyOnWriteArrayList<>();
protected List<ActionObserver> onceUsedObservers = new ArrayList<>();
protected Collection<AttackCalcObserver> attackCalcObservers = new CopyOnWriteArrayList<>();
protected final Collection<ActionObserver> observers = new CopyOnWriteArrayList<>();
protected final List<ActionObserver> onceUsedObservers = new ArrayList<>();
protected final Collection<AttackCalcObserver> attackCalcObservers = new CopyOnWriteArrayList<>();

/**
* Once used observer add to observerController. If observer notify will be removed.
*/
public void attach(ActionObserver observer) {
observer.makeOneTimeUse();
lock.lock();
try {
synchronized (onceUsedObservers) {
onceUsedObservers.add(observer);
} finally {
lock.unlock();
}
}

Expand All @@ -48,11 +43,8 @@ public void addAttackCalcObserver(AttackCalcObserver observer) {

public void removeObserver(ActionObserver observer) {
observers.remove(observer);
lock.lock();
try {
synchronized (onceUsedObservers) {
onceUsedObservers.remove(observer);
} finally {
lock.unlock();
}
}

Expand All @@ -62,8 +54,7 @@ public void removeAttackCalcObserver(AttackCalcObserver observer) {

public void notifyObservers(ObserverType type, Object... object) {
List<ActionObserver> tempOnceused = Collections.emptyList();
lock.lock();
try {
synchronized (onceUsedObservers) {
if (onceUsedObservers.size() > 0) {
tempOnceused = new ArrayList<>();
Iterator<ActionObserver> iterator = onceUsedObservers.iterator();
Expand All @@ -77,8 +68,6 @@ public void notifyObservers(ObserverType type, Object... object) {
}
}
}
} finally {
lock.unlock();
}

// notify outside of lock
Expand Down Expand Up @@ -276,11 +265,8 @@ public float getBaseMagicalDamageMultiplier() {
* Clear all observers
*/
public void clear() {
lock.lock();
try {
synchronized (onceUsedObservers) {
onceUsedObservers.clear();
} finally {
lock.unlock();
}
observers.clear();
attackCalcObservers.clear();
Expand Down
54 changes: 38 additions & 16 deletions game-server/src/com/aionemu/gameserver/model/PlayerClass.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.aionemu.gameserver.model;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.xml.bind.annotation.XmlEnum;

import com.aionemu.gameserver.configs.main.GSConfig;
Expand Down Expand Up @@ -48,7 +44,7 @@ public enum PlayerClass implements L10n {
/** Tells whether player can create new character with this class */
private PlayerClass startingClass;

private Map<Integer, StatsTemplate> templatesByLevel = new HashMap<>();
private final StatsTemplate[] templatesByLevel = new StatsTemplate[GSConfig.PLAYER_MAX_LEVEL];
private final int power, health, agility, accuracy, knowledge, will, healthMultiplier, willMultiplier;

PlayerClass(int classId, int nameId, PlayerClass startingClass, int power, int health, int agility, int accuracy, int knowledge, int will, int healthMultiplier, int willMultiplier, int physicalAttack, int physicalCritical, int magicalCritical, int magicalCriticalResist) {
Expand All @@ -74,14 +70,8 @@ public enum PlayerClass implements L10n {
}

private void initializeTemplatesForEachLevel(int physicalAttack, int physicalCritical, int magicalCritical, int magicalCriticalResist) {
for (int level = 1; level <= 65; level++) {
StatsTemplate statsTemplate = new StatsTemplate();
statsTemplate.setPower(power);
statsTemplate.setHealth(health);
statsTemplate.setAgility(agility);
statsTemplate.setBaseAccuracy(accuracy);
statsTemplate.setKnowledge(knowledge);
statsTemplate.setWill(will);
for (int level = 1; level <= templatesByLevel.length; level++) {
PlayerStatsTemplate statsTemplate = new PlayerStatsTemplate();
statsTemplate.setMaxHp(PlayerStatCalculator.calculateMaxHp(this, level));
statsTemplate.setMaxMp(PlayerStatCalculator.calculateMaxMp(this, level));
statsTemplate.setBlock(PlayerStatCalculator.calculateBlockEvasionOrParry(level));
Expand All @@ -99,7 +89,7 @@ private void initializeTemplatesForEachLevel(int physicalAttack, int physicalCri
speeds.setRunSpeed(6f);
speeds.setFlySpeed(9f);
statsTemplate.setSpeeds(speeds);
templatesByLevel.put(level, statsTemplate);
templatesByLevel[level - 1] = statsTemplate;
}
}

Expand Down Expand Up @@ -190,8 +180,7 @@ public String getIconImage() {
}

public StatsTemplate getStatsTemplateFor(int level) {
StatsTemplate template = templatesByLevel.get(Math.min(level, GSConfig.PLAYER_MAX_LEVEL));
return Objects.requireNonNull(template, () -> "Missing template for PlayerClass." + this + " on level " + level);
return templatesByLevel[Math.min(level, templatesByLevel.length) - 1];
}

public int getPower() {
Expand Down Expand Up @@ -237,4 +226,37 @@ public int getAccuracyMultiplier() {
public int getNoWeaponPowerMultiplier() {
return 70;
}

private class PlayerStatsTemplate extends StatsTemplate {

@Override
public int getPower() {
return power;
}

@Override
public int getHealth() {
return health;
}

@Override
public int getAgility() {
return agility;
}

@Override
public int getBaseAccuracy() {
return accuracy;
}

@Override
public int getKnowledge() {
return knowledge;
}

@Override
public int getWill() {
return will;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ public abstract class Creature extends VisibleObject {
private Skill castingSkill;
private Map<Integer, Long> skillCoolDowns;
private ObserveController observeController;
private final TransformModel transformModel;
private TransformModel transformModel;
private final AggroList aggroList;
private Item usingItem;
private final byte[] zoneTypes = new byte[ZoneType.values().length];
private int skillNumber;
private int attackedCount;
private long spawnTime = System.currentTimeMillis();
private TribeClass tribe = TribeClass.GENERAL;

public Creature(int objId, CreatureController<? extends Creature> controller, SpawnTemplate spawnTemplate, CreatureTemplate objectTemplate,
WorldPosition position, boolean autoReleaseObjectId) {
Expand All @@ -68,7 +66,6 @@ public Creature(int objId, CreatureController<? extends Creature> controller, Sp
aiName = SpawnTemplate.NO_AI.equals(spawnTemplate.getAiName()) ? null : spawnTemplate.getAiName();
this.ai = AIEngine.getInstance().newAI(aiName, this);
this.observeController = new ObserveController();
this.transformModel = new TransformModel(this);
this.aggroList = createAggroList();
}

Expand Down Expand Up @@ -210,42 +207,6 @@ public void clearAttackedCount() {
attackedCount = 0;
}

/**
* Is using item
*
* @return
*/
public boolean isUsingItem() {
return usingItem != null;
}

/**
* Set using item
*
* @param usingItem
*/
public void setUsingItem(Item usingItem) {
this.usingItem = usingItem;
}

/**
* get Using ItemId
*
* @return
*/
public int getUsingItemId() {
return usingItem != null ? usingItem.getItemTemplate().getTemplateId() : 0;
}

/**
* Using Item
*
* @return
*/
public Item getUsingItem() {
return usingItem;
}

/**
* All abnormal effects are checked that disable movements
*
Expand Down Expand Up @@ -374,10 +335,9 @@ public boolean isInSeeState(CreatureSeeState seeState) {
return false;
}

/**
* @return the transformModel
*/
public TransformModel getTransformModel() {
if (transformModel == null)
transformModel = new TransformModel(this);
return transformModel;
}

Expand All @@ -386,7 +346,7 @@ public void endTransformation() {
}

public boolean isTransformed() {
return getTransformModel().isActive();
return transformModel != null && getTransformModel().isActive();
}

/**
Expand Down Expand Up @@ -437,11 +397,7 @@ public boolean isEnemyFrom(Npc npc) {
}

public TribeClass getTribe() {
return tribe;
}

public void setTribe(TribeClass tribe) {
this.tribe = tribe;
return TribeClass.GENERAL;
}

public TribeClass getBaseTribe() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public boolean isPathWalker() {

@Override
public TribeClass getTribe() {
TribeClass transformTribe = getTransformModel().getTribe();
TribeClass transformTribe = isTransformed() ? getTransformModel().getTribe() : null;
if (transformTribe != null) {
return transformTribe;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public class Player extends Creature {
private Storage[] cabinets = new Storage[StorageType.HOUSE_WH_MAX - StorageType.HOUSE_WH_MIN + 1];
private Storage regularWarehouse;
private Equipment equipment;
private Item usingItem;

private final AbsoluteStatOwner absStatsHolder;
private PlayerSettings playerSettings;
Expand Down Expand Up @@ -465,6 +466,14 @@ public void setEquipment(Equipment equipment) {
this.equipment = equipment;
}

public Item getUsingItem() {
return usingItem;
}

public void setUsingItem(Item usingItem) {
this.usingItem = usingItem;
}

/**
* @return the player private store
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.apache.commons.lang3.ArrayUtils;

import com.aionemu.gameserver.model.EmotionType;
import com.aionemu.gameserver.model.SkillElement;
import com.aionemu.gameserver.model.enchants.EnchantEffect;
import com.aionemu.gameserver.model.gameobjects.Creature;
Expand All @@ -20,7 +21,9 @@
import com.aionemu.gameserver.model.stats.calc.functions.StatFunctionProxy;
import com.aionemu.gameserver.model.templates.itemset.ItemSetTemplate;
import com.aionemu.gameserver.model.templates.stats.StatsTemplate;
import com.aionemu.gameserver.network.aion.serverpackets.SM_EMOTION;
import com.aionemu.gameserver.skillengine.model.Effect;
import com.aionemu.gameserver.utils.PacketSendUtility;
import com.aionemu.gameserver.utils.stats.CalculationType;

/**
Expand All @@ -35,7 +38,7 @@ public abstract class CreatureGameStats<T extends Creature> {

private long lastGeoUpdate = 0;
private int attackCounter = 0;
private int cachedMaxHp, cachedMaxMp;
private int cachedMaxHp, cachedMaxMp, cachedSpeed;

protected CreatureGameStats(T owner) {
this.owner = owner;
Expand Down Expand Up @@ -314,6 +317,17 @@ public void updateStatInfo() {
* Send packet about speed info
*/
public void updateSpeedInfo() {
PacketSendUtility.broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.CHANGE_SPEED));
}

protected boolean checkSpeedStats() {
int currentSpeed = getMovementSpeed().getCurrent();
if (currentSpeed != cachedSpeed) {
updateSpeedInfo();
cachedSpeed = currentSpeed;
return true;
}
return false;
}

/**
Expand Down
Loading

0 comments on commit a8446ea

Please sign in to comment.