Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MCTS pt. 1 #15

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/dk/sdu/mmmi/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public static void main(String[] args) throws IOException, URISyntaxException {
var battleMonsterProcessor = new BattleMonsterProcessor();

var battleAI = new dk.sdu.mmmi.modulemon.BattleAI.BattleAIFactory();
battleAI.setSettingsService(settings);
var mctsBattleAI = new MCTSBattleAIFactory();
mctsBattleAI.setSettingsService(settings);
var simpleBattleAI = new dk.sdu.mmmi.modulemon.SimpleAI.BattleAIFactory(); // Uncomment for Simple AI

var battleSimulation = new BattleSimulation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ public boolean equals(Object obj) {
}
return false;
}

@Override
public String toString() {
return String.format("Participant with %s (UUID: %s...)", this.activeMonster.toString(), this.uuid.toString().substring(0,5));
}
}
38 changes: 27 additions & 11 deletions src/main/java/dk/sdu/mmmi/modulemon/BattleScene/BattleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class BattleView implements IGameViewService, IBattleView {
private Music _battleMusic;
private Sound _winSound;
private Sound _loseSound;
private Sound _crashSound;
private MenuState menuState = MenuState.DEFAULT;
private BattleSpeedController forcedAIDelay;
private Queue<BaseAnimation> blockingAnimations;
Expand Down Expand Up @@ -126,6 +127,7 @@ public void startBattle(List<IMonster> playerMonsters, List<IMonster> enemyMonst
_battleMusic = loader.getMusicAsset("/music/battle_music_" + battleMusic_type.toLowerCase() + ".ogg", this.getClass());
_winSound = loader.getSoundAsset("/sounds/you_won.ogg", this.getClass());
_loseSound = loader.getSoundAsset("/sounds/you_lost.ogg", this.getClass());
_crashSound = loader.getSoundAsset("/sounds/metal-pipe.ogg", this.getClass());
setBattleAIFactory();
_battleSimulation.StartBattle(player, enemy);
_currentBattleState = _battleSimulation.getState().clone(); // Set an initial battle-state
Expand All @@ -149,6 +151,7 @@ private void setBattleAIFactory() {
IBattleAIFactory desiredAI;
if (settings.getSetting("AI").equals("MCTS")) {
desiredAI = new MCTSBattleAIFactory();
((MCTSBattleAIFactory) desiredAI).setSettingsService(settings);
} else if (settings.getSetting("AI").equals("Simple")) {
desiredAI = new dk.sdu.mmmi.modulemon.SimpleAI.BattleAIFactory();
} else {
Expand Down Expand Up @@ -206,7 +209,6 @@ public void setBattleScene(BattleScene battleScene) {
this._battleScene = battleScene;
}


@Override
public void update(GameData gameData, IGameViewManager gameViewManager) {
if (!_isInitialized || _battleSimulation == null || !_battleStarted) {
Expand Down Expand Up @@ -253,18 +255,18 @@ public void update(GameData gameData, IGameViewManager gameViewManager) {
updateHasRunOnce = true;

// Check the current AI is done thinking
if(_battleSimulation.hasNextBattleEvent()){
if (_battleSimulation.hasNextBattleEvent()) {
_battleScene.setShowEnemySpinner(false);
_battleScene.setShowPlayerSpinner(false);
}else{
if(_battleSimulation.isPlayerControlledByAI() && _battleSimulation.getState().isPlayersTurn()){
} else {
if (_battleSimulation.isPlayerControlledByAI() && _battleSimulation.getState().isPlayersTurn()) {
// there is no current event, and the player is controlled by an AI
_battleScene.setShowPlayerSpinner(true);
_battleScene.setShowEnemySpinner(false);
}else if(!_battleSimulation.getState().isPlayersTurn()){
} else if (!_battleSimulation.getState().isPlayersTurn()) {
_battleScene.setShowPlayerSpinner(false);
_battleScene.setShowEnemySpinner(true);
}else {
} else {
_battleScene.setShowEnemySpinner(false);
_battleScene.setShowPlayerSpinner(false);
}
Expand All @@ -283,10 +285,10 @@ public void update(GameData gameData, IGameViewManager gameViewManager) {
currentAnimation.start();
}

if(forcedAIDelay.getSpeed() <= 0){
if (forcedAIDelay.getSpeed() <= 0) {
currentAnimation.forceEndAnimation();
}else if(currentAnimation.getAnimationLength() > forcedAIDelay.getSpeed()
&& menuState == MenuState.SPECTATOR){ // We only rescale animations while spectating
} else if (currentAnimation.getAnimationLength() > forcedAIDelay.getSpeed()
&& menuState == MenuState.SPECTATOR) { // We only rescale animations while spectating
// While we can re-scale all animations (even to be longer), we only
// scale animations that are longer than our forcedAIDelay.
currentAnimation.rescaleAnimation(forcedAIDelay.getSpeed());
Expand Down Expand Up @@ -358,7 +360,7 @@ public void update(GameData gameData, IGameViewManager gameViewManager) {
PlayerChangeInAnimation changeInAnimation = new PlayerChangeInAnimation(_battleScene);
blockingAnimations.add(changeInAnimation);

if(forcedAIDelay.getSpeed() > 0) {
if (forcedAIDelay.getSpeed() > 0) {
addEmptyAnimation(forcedAIDelay.getSpeed(), false);
}
this._battleScene.setTextToDisplay(battleEvent.getText());
Expand Down Expand Up @@ -409,10 +411,24 @@ public void update(GameData gameData, IGameViewManager gameViewManager) {
e.start();
blockingAnimations.add(e);
}
} else if (battleEvent instanceof AICrashedEvent crashEvent) {
BaseAnimation anim;
if (crashEvent.getParticipant().equals(_battleSimulation.getState().getPlayer())) {
anim = new PlayerCrashAnimation(_battleScene);
} else {
anim = new EnemyCrashAnimation(_battleScene);
}

this._crashSound.play((int) settings.getSetting(SettingsRegistry.getInstance().getSoundVolumeSetting()) / 100f);
anim.start();
anim.setOnEventDone(() -> _battleScene.resetPositions());
blockingAnimations.add(anim);
_currentBattleState = battleEvent.getState();
this._battleScene.setTextToDisplay(battleEvent.getText());
} else {
//Unknown event (Or TextEvent)
_currentBattleState = eventState;
if(forcedAIDelay.getSpeed() > 0) {
if (forcedAIDelay.getSpeed() > 0) {
addEmptyAnimation(forcedAIDelay.getSpeed(), true);
}
this._battleScene.setTextToDisplay(battleEvent.getText());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dk.sdu.mmmi.modulemon.BattleScene.animations;

import dk.sdu.mmmi.modulemon.BattleScene.scenes.BattleScene;
import dk.sdu.mmmi.modulemon.BattleScene.scenes.BattleSceneDefaults;
import dk.sdu.mmmi.modulemon.common.animations.BaseAnimation;
import dk.sdu.mmmi.modulemon.common.data.GameData;
import dk.sdu.mmmi.modulemon.common.drawing.Position;

import java.util.ArrayList;

public class EnemyCrashAnimation extends BaseAnimation {

private BattleScene _battleScene;

public EnemyCrashAnimation(BattleScene battleScene) {
super();
Timeline = new int[]{0, 2000};
States = new ArrayList<>(Timeline.length);

//Initial state
States.add(new float[]{
BattleSceneDefaults.enemyMonsterRotation() //rotation
});

States.add(new float[]{
3600f //rotation
});

this._battleScene = battleScene;
}

@Override
public void update(GameData gameData) {
super.tick();

float[] states = super.getCurrentStates();
this._battleScene.setEnemyMonsterRotation(states[0]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dk.sdu.mmmi.modulemon.BattleScene.animations;

import dk.sdu.mmmi.modulemon.BattleScene.scenes.BattleScene;
import dk.sdu.mmmi.modulemon.BattleScene.scenes.BattleSceneDefaults;
import dk.sdu.mmmi.modulemon.common.animations.BaseAnimation;
import dk.sdu.mmmi.modulemon.common.data.GameData;

import java.util.ArrayList;

public class PlayerCrashAnimation extends BaseAnimation {

private BattleScene _battleScene;

public PlayerCrashAnimation(BattleScene battleScene) {
super();
Timeline = new int[]{0, 2000};
States = new ArrayList<>(Timeline.length);

//Initial state
States.add(new float[]{
BattleSceneDefaults.playerMonsterRotation() //rotation
});

States.add(new float[]{
3600f //rotation
});

this._battleScene = battleScene;
}

@Override
public void update(GameData gameData) {
super.tick();

float[] states = super.getCurrentStates();
this._battleScene.setPlayerMonsterRotation(states[0]);
}
}
Loading