Skip to content

Commit

Permalink
Merge pull request #291 from vincent4vx/feature-fight-effect-static-i…
Browse files Browse the repository at this point in the history
…nvocation

Static invocations effect
  • Loading branch information
vincent4vx authored Jun 15, 2023
2 parents 9e73978 + d9ff11c commit 231366e
Show file tree
Hide file tree
Showing 146 changed files with 1,949 additions and 560 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

package fr.quatrevieux.araknemu.data.living.constraint.player;

import fr.quatrevieux.araknemu.data.living.constraint.ConstraintBuilder;
import fr.quatrevieux.araknemu.data.living.constraint.AbstractConstraintBuilderFactory;
import fr.quatrevieux.araknemu.data.living.constraint.ConstraintBuilder;
import fr.quatrevieux.araknemu.data.living.entity.player.Player;
import fr.quatrevieux.araknemu.data.living.repository.player.PlayerRepository;
import fr.quatrevieux.araknemu.game.GameConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import fr.quatrevieux.araknemu.game.account.GameAccount;
import fr.quatrevieux.araknemu.game.admin.context.AbstractContext;
import fr.quatrevieux.araknemu.game.admin.context.Context;
import fr.quatrevieux.araknemu.game.admin.context.AbstractContextConfigurator;
import fr.quatrevieux.araknemu.game.admin.context.Context;
import fr.quatrevieux.araknemu.game.admin.context.SimpleContext;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import fr.quatrevieux.araknemu.core.di.ContainerException;
import fr.quatrevieux.araknemu.game.admin.context.AbstractContext;
import fr.quatrevieux.araknemu.game.admin.context.Context;
import fr.quatrevieux.araknemu.game.admin.context.AbstractContextConfigurator;
import fr.quatrevieux.araknemu.game.admin.context.Context;
import fr.quatrevieux.araknemu.game.admin.context.SimpleContext;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
package fr.quatrevieux.araknemu.game.admin.debug;

import fr.quatrevieux.araknemu.game.admin.AdminPerformer;
import fr.quatrevieux.araknemu.game.admin.context.AbstractContextConfigurator;
import fr.quatrevieux.araknemu.game.admin.context.ConfigurableContextResolver;
import fr.quatrevieux.araknemu.game.admin.context.Context;
import fr.quatrevieux.araknemu.game.admin.context.AbstractContextConfigurator;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package fr.quatrevieux.araknemu.game.admin.player;

import fr.quatrevieux.araknemu.game.admin.context.AbstractContext;
import fr.quatrevieux.araknemu.game.admin.context.Context;
import fr.quatrevieux.araknemu.game.admin.context.AbstractContextConfigurator;
import fr.quatrevieux.araknemu.game.admin.context.Context;
import fr.quatrevieux.araknemu.game.admin.context.SimpleContext;
import fr.quatrevieux.araknemu.game.player.GamePlayer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void execute(AdminPerformer performer, Void arguments) {
performer.info("Online : {} sessions and {} players", gameService.sessions().size(), playerService.online().size());
performer.info("Fights : {} fights with {} fighters",
fightService.fights().size(),
fightService.fights().stream().mapToLong(fight -> fight.fighters().size()).sum()
fightService.fights().stream().mapToLong(fight -> fight.fighters().all().size()).sum()
);
performer.info(
"RAM usage : {} / {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package fr.quatrevieux.araknemu.game.admin.server;

import fr.quatrevieux.araknemu.game.admin.context.AbstractContext;
import fr.quatrevieux.araknemu.game.admin.context.Context;
import fr.quatrevieux.araknemu.game.admin.context.AbstractContextConfigurator;
import fr.quatrevieux.araknemu.game.admin.context.Context;
import fr.quatrevieux.araknemu.game.admin.context.SimpleContext;

import java.util.List;
Expand Down
41 changes: 10 additions & 31 deletions src/main/java/fr/quatrevieux/araknemu/game/fight/Fight.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import fr.quatrevieux.araknemu.game.fight.event.FightStopped;
import fr.quatrevieux.araknemu.game.fight.exception.InvalidFightStateException;
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
import fr.quatrevieux.araknemu.game.fight.fighter.PlayableFighter;
import fr.quatrevieux.araknemu.game.fight.map.FightMap;
import fr.quatrevieux.araknemu.game.fight.module.FightModule;
import fr.quatrevieux.araknemu.game.fight.spectator.Spectators;
Expand Down Expand Up @@ -57,7 +58,6 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Handle fight
Expand All @@ -74,7 +74,8 @@ public final class Fight implements Dispatcher, Sender {
private final ListenerAggregate dispatcher;
private final ScheduledExecutorService executor;
private final Spectators spectators;
private final ActionsFactory<Fighter> actions;
private final ActionsFactory<PlayableFighter> actions;
private final FighterList fighters;

private final Lock executorLock = new ReentrantLock();
private final EffectsHandler effects = new EffectsHandler();
Expand All @@ -84,7 +85,7 @@ public final class Fight implements Dispatcher, Sender {
private volatile boolean alive = true;

@SuppressWarnings({"assignment", "argument"})
public Fight(int id, FightType type, FightMap map, List<FightTeam.Factory> teams, StatesFlow statesFlow, Logger logger, ScheduledExecutorService executor, ActionsFactory<Fighter> actions) {
public Fight(int id, FightType type, FightMap map, List<FightTeam.Factory> teams, StatesFlow statesFlow, Logger logger, ScheduledExecutorService executor, ActionsFactory<PlayableFighter> actions) {
this.id = id;
this.type = type;
this.map = map;
Expand All @@ -94,6 +95,7 @@ public Fight(int id, FightType type, FightMap map, List<FightTeam.Factory> teams
this.executor = executor;
this.dispatcher = new DefaultListenerAggregate(logger);
this.spectators = new Spectators(this);
this.fighters = new FighterList(this);
this.actions = actions;
}

Expand Down Expand Up @@ -132,17 +134,8 @@ public FightTeam team(int number) {
/**
* Get all fighters on the fight
*/
public List<Fighter> fighters() {
final FightTurnList turnList = this.turnList;
final Stream<Fighter> fighterStream = turnList != null
? turnList.fighters().stream()
: teams.stream().flatMap(fightTeam -> fightTeam.fighters().stream())
;

return fighterStream
.filter(Fighter::isOnFight)
.collect(Collectors.toList())
;
public FighterList fighters() {
return fighters;
}

/**
Expand Down Expand Up @@ -214,7 +207,7 @@ public EffectsHandler effects() {
/**
* Get available fight actions factories
*/
public ActionsFactory<Fighter> actions() {
public ActionsFactory<PlayableFighter> actions() {
return actions;
}

Expand Down Expand Up @@ -245,22 +238,7 @@ public void dispatch(Object event) {
* @see Fight#dispatch(Object) To dispatch on the Fight's listeners
*/
public void dispatchToAll(Object event) {
if (turnList != null) {
for (Fighter fighter : turnList.fighters()) {
if (fighter.isOnFight()) {
fighter.dispatch(event);
}
}
} else {
for (FightTeam team : teams) {
for (Fighter fighter : team.fighters()) {
if (fighter.isOnFight()) {
fighter.dispatch(event);
}
}
}
}

fighters.dispatch(event);
spectators.dispatch(event);
}

Expand Down Expand Up @@ -409,6 +387,7 @@ public void destroy() {
map.destroy();
attachments.clear();
spectators.clear();
fighters.clear();
}

/**
Expand Down
137 changes: 137 additions & 0 deletions src/main/java/fr/quatrevieux/araknemu/game/fight/FighterList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* This file is part of Araknemu.
*
* Araknemu is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Araknemu 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (c) 2017-2023 Vincent Quatrevieux
*/

package fr.quatrevieux.araknemu.game.fight;

import fr.quatrevieux.araknemu.core.event.Dispatcher;
import fr.quatrevieux.araknemu.game.fight.event.FighterRemoved;
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
import fr.quatrevieux.araknemu.game.fight.fighter.PlayableFighter;
import fr.quatrevieux.araknemu.game.fight.map.FightCell;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Stream;

/**
* Handle the fighters list of a fight
*/
public final class FighterList implements Iterable<Fighter>, Dispatcher {
private final Fight fight;
private final Set<Fighter> fighters = new LinkedHashSet<>();

public FighterList(Fight fight) {
this.fight = fight;
}

@Override
public @NonNull Iterator<Fighter> iterator() {
return fighters.iterator();
}

@Override
public void dispatch(Object event) {
for (Fighter fighter : fighters) {
if (fighter.isOnFight()) {
fighter.dispatch(event);
}
}
}

/**
* Get all fighters as collection.
*/
public Collection<Fighter> all() {
return Collections.unmodifiableCollection(fighters);
}

/**
* Add a fighter to the fight
*
* @param fighter The fighter to add
* @param cell The cell where the fighter will be placed
*
* @see Fighter#joinFight(Fight, FightCell) Will be called by this method
* @see #joinTurnList(PlayableFighter, FightCell) Call this method if you want to add the fighter to the turn list
*/
public void join(Fighter fighter, FightCell cell) {
fighter.joinFight(fight, cell);
fighters.add(fighter);
}

/**
* Add a fighter to the fight and to the turn list
*
* Note: this method can only be called if the fight is active, during placement state, use {@link #join(Fighter, FightCell)} instead
*
* @param fighter The fighter to add
* @param cell The cell where the fighter will be placed
*
* @see fr.quatrevieux.araknemu.game.fight.turn.FightTurnList#add(PlayableFighter) Will be called by this method
*/
public void joinTurnList(PlayableFighter fighter, FightCell cell) {
join(fighter, cell);
fight.turnList().add(fighter);
}

/**
* Remove a fighter from the fight
* Will trigger {@link FighterRemoved} event
*
* Note: if the fighter is on the turn list, it will be removed too
*
* @param fighter The fighter to remove
*/
public void leave(Fighter fighter) {
fighters.remove(fighter);

if (fight.active() && fighter instanceof PlayableFighter) {
fight.turnList().remove((PlayableFighter) fighter);
}

fight.dispatch(new FighterRemoved(fighter, fight));
}

/**
* Get a sequential {@code Stream} with this fighter list as its source.
*/
public Stream<Fighter> stream() {
return fighters.stream();
}

/**
* Get all alive fighters as stream
*
* @see Fighter#dead()
*/
public Stream<Fighter> alive() {
return fighters.stream().filter(fighter -> !fighter.dead());
}

/**
* Internal method for clear all fighters objects.
*/
void clear() {
fighters.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import fr.quatrevieux.araknemu.game.fight.ai.action.ActionGenerator;
import fr.quatrevieux.araknemu.game.fight.ai.action.FightAiActionFactoryAdapter;
import fr.quatrevieux.araknemu.game.fight.ai.util.AIHelper;
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
import fr.quatrevieux.araknemu.game.fight.fighter.FighterData;
import fr.quatrevieux.araknemu.game.fight.fighter.PlayableFighter;
import fr.quatrevieux.araknemu.game.fight.map.BattlefieldMap;
import fr.quatrevieux.araknemu.game.fight.turn.Turn;
import fr.quatrevieux.araknemu.game.fight.turn.action.Action;
Expand All @@ -42,10 +42,10 @@
* and the next action is scheduled after the last one.
* So the AI execution is not blocking, and executed in parallel of the turn timer.
*/
public final class FighterAI implements Runnable, AI<Fighter> {
private final Fighter fighter;
public final class FighterAI implements Runnable, AI<PlayableFighter> {
private final PlayableFighter fighter;
private final Fight fight;
private final ActionGenerator<Fighter> generator;
private final ActionGenerator<PlayableFighter> generator;
private final AIHelper helper;

private @Nullable Turn turn;
Expand All @@ -57,7 +57,7 @@ public final class FighterAI implements Runnable, AI<Fighter> {
* @param generator The action generator
*/
@SuppressWarnings({"argument", "assignment"})
public FighterAI(Fighter fighter, Fight fight, ActionGenerator<Fighter> generator) {
public FighterAI(PlayableFighter fighter, Fight fight, ActionGenerator<PlayableFighter> generator) {
this.fighter = fighter;
this.fight = fight;
this.generator = generator;
Expand Down Expand Up @@ -105,7 +105,7 @@ public void run() {
}

@Override
public Fighter fighter() {
public PlayableFighter fighter() {
return fighter;
}

Expand All @@ -127,7 +127,7 @@ public BattlefieldMap map() {

@Override
public Stream<? extends FighterData> fighters() {
return fight.fighters().stream().filter(other -> !other.dead());
return fight.fighters().alive();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import fr.arakne.utils.maps.path.PathStep;
import fr.quatrevieux.araknemu.game.fight.Fight;
import fr.quatrevieux.araknemu.game.fight.castable.validator.CastConstraintValidator;
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
import fr.quatrevieux.araknemu.game.fight.fighter.PlayableFighter;
import fr.quatrevieux.araknemu.game.fight.map.BattlefieldCell;
import fr.quatrevieux.araknemu.game.fight.map.FightCell;
import fr.quatrevieux.araknemu.game.fight.map.FightMap;
Expand All @@ -38,11 +38,11 @@
* Generated actions can be applied directly to fight
*/
public final class FightAiActionFactoryAdapter implements AiActionFactory {
private final Fighter fighter;
private final PlayableFighter fighter;
private final Fight fight;
private final ActionsFactory<Fighter> actionFactory;
private final ActionsFactory<PlayableFighter> actionFactory;

public FightAiActionFactoryAdapter(Fighter fighter, Fight fight, ActionsFactory<Fighter> actionFactory) {
public FightAiActionFactoryAdapter(PlayableFighter fighter, Fight fight, ActionsFactory<PlayableFighter> actionFactory) {
this.fighter = fighter;
this.fight = fight;
this.actionFactory = actionFactory;
Expand Down
Loading

0 comments on commit 231366e

Please sign in to comment.