Skip to content
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
1 change: 1 addition & 0 deletions src/main/java/dk/sdu/mmmi/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static void main(String[] args) throws IOException, URISyntaxException {
var settings = new Settings();
var monsterRegistry = new MonsterRegistry();
var battleMonsterProcessor = new BattleMonsterProcessor();
battleMonsterProcessor.setSettings(settings);

var battleAI = new dk.sdu.mmmi.modulemon.BattleAI.BattleAIFactory();
battleAI.setSettingsService(settings);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/dk/sdu/mmmi/modulemon/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ public void setSettingsService(IGameSettings settings) {
if (settings.getSetting(SettingsRegistry.getInstance().getBattleAISetting()) == null){
settings.setSetting(SettingsRegistry.getInstance().getBattleAISetting(), "MCTS");
}
if(settings.getSetting(SettingsRegistry.getInstance().getNonDeterminism()) == null){
settings.setSetting(SettingsRegistry.getInstance().getNonDeterminism(), true);
}
if(settings.getSetting(SettingsRegistry.getInstance().getConcurrentBattleAmount()) == null){
settings.setSetting(SettingsRegistry.getInstance().getConcurrentBattleAmount(), 5);
}
gvm.setSettings(settings);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void init(IGameViewManager gameViewManager) {

scene = new HeadlessBattleScene(settings);
battlingScene = new HeadlessBattlingScene();
concurrentBattles = (int) settings.getSetting(settingsRegistry.getConcurrentBattleAmount());
}

@Override
Expand Down Expand Up @@ -315,6 +316,7 @@ private Future<IBattleResult> runBattle() {

var battleSim = new BattleSimulation();
var processor = new BattleMonsterProcessor();
processor.setSettings(settings);
battleSim.setMonsterProcessor(processor);
battleSim.setPlayerAIFactory(teamAAI);
battleSim.setOpponentAIFactory(teamBAI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import dk.sdu.mmmi.modulemon.CommonBattleSimulation.IBattleMonsterProcessor;
import dk.sdu.mmmi.modulemon.CommonMonster.IMonster;
import dk.sdu.mmmi.modulemon.CommonMonster.IMonsterMove;
import dk.sdu.mmmi.modulemon.Settings.Settings;
import dk.sdu.mmmi.modulemon.common.SettingsRegistry;
import dk.sdu.mmmi.modulemon.common.services.IGameSettings;

import java.util.Random;

public class BattleMonsterProcessor implements IBattleMonsterProcessor {

private IGameSettings settings;

@Override
public IMonster whichMonsterStarts(IMonster iMonster1, IMonster iMonster2) {
Monster monster1 = (Monster) iMonster1;
Expand All @@ -24,7 +29,9 @@ public int calculateDamage(IMonster iSource, IMonsterMove iMove, IMonster iTarge
float sourceAttack = (float) source.getAttack();
float targetDefence = (float) target.getDefence();

if (!doAccuracyHit(move.getAccuracy())) { return 0; }
boolean shouldUseNonDeterminism = (Boolean) settings.getSetting(SettingsRegistry.getInstance().getNonDeterminism());

if (!doAccuracyHit(move.getAccuracy()) && shouldUseNonDeterminism) { return 0; }
// Same type attack bonus. Effectively the same as STAB in that other game
boolean same_attack_type = source.getMonsterType() == move.getType();
float attack_bonus = 1;
Expand All @@ -35,7 +42,10 @@ public int calculateDamage(IMonster iSource, IMonsterMove iMove, IMonster iTarge

float monsterAttackDefence = (0.2f * sourceAttack + 3 + 20 ) / (targetDefence + 50);

int damage = Math.round( monsterAttackDefence * moveDamage * attack_bonus * calculateTypeAdvantage(move.getType(), target.getMonsterType()) * calculateCriticalHit(source) );
int damage = Math.round( monsterAttackDefence * moveDamage * attack_bonus * calculateTypeAdvantage(move.getType(), target.getMonsterType()));
if(shouldUseNonDeterminism){
damage *= calculateCriticalHit(source);
}
return damage;
}

Expand Down Expand Up @@ -109,7 +119,7 @@ public float calculateCriticalHit (Monster monster) {
//Generate random number between 0-255
float randomVal = random.nextInt(256);
//true if the generated number is less than the threshold
boolean criticalHit = randomVal >= threshold;
boolean criticalHit = randomVal < threshold;

if (criticalHit) {
System.out.println("critical hit");
Expand All @@ -126,4 +136,8 @@ public boolean doAccuracyHit(float factor) {
return random.nextFloat() <= factor;

}

public void setSettings(IGameSettings settings) {
this.settings = settings;
}
}
10 changes: 10 additions & 0 deletions src/main/java/dk/sdu/mmmi/modulemon/common/SettingsRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class SettingsRegistry {
private UUID rectangle_style = UUID.randomUUID();
private UUID battle_theme = UUID.randomUUID();
private UUID battle_AI = UUID.randomUUID();
private UUID non_determinism = UUID.randomUUID();
private UUID concurrent_battles = UUID.randomUUID();

private SettingsRegistry(){
settingsMap = new HashMap<>();
Expand All @@ -39,6 +41,8 @@ private void populateSettings(){
settingsMap.put(rectangle_style, "personaRectangles");
settingsMap.put(battle_theme, "battleMusicTheme");
settingsMap.put(battle_AI, "AI");
settingsMap.put(non_determinism, "Nondeterminism");
settingsMap.put(concurrent_battles, "Concurrent Battles");
}

/*
Expand Down Expand Up @@ -74,6 +78,12 @@ public String getBattleMusicThemeSetting(){

public String getBattleAISetting() { return settingsMap.get(battle_AI); }

public String getNonDeterminism() {
return settingsMap.get(non_determinism);
}
public String getConcurrentBattleAmount() {
return settingsMap.get(concurrent_battles);
}

public static SettingsRegistry getInstance(){
if(instance == null){
Expand Down
66 changes: 59 additions & 7 deletions src/main/java/dk/sdu/mmmi/modulemon/gameviews/MenuView.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public class MenuView implements IGameViewService {
"Battle Music Theme",
"AI",
"Use AI knowledge states",
"Nondeterminism",
"Concurrent battle amount"
};

private int AIIndex = 0;
Expand Down Expand Up @@ -213,7 +215,7 @@ public void draw(GameData data) {
(Game.HEIGHT - glyphLayout.height) / 1.25f
);

if(!subtitle.isBlank()) {
if (!subtitle.isBlank()) {
subtitleFont.setColor(Color.valueOf("ffcb05"));
glyphLayout.setText(subtitleFont, subtitle);
subtitleFont.draw(
Expand Down Expand Up @@ -278,6 +280,9 @@ public void draw(GameData data) {

@Override
public void handleInput(GameData gameData, IGameViewManager gameViewManager) {
if (gameData.getKeys().isPressed(GameKeys.BACK)) {
goBackToMainMenu();
}
// Moves up in the menu
if (gameData.getKeys().isPressed(GameKeys.UP)) {
if (currentOption > 0) {
Expand Down Expand Up @@ -316,10 +321,7 @@ public void handleInput(GameData gameData, IGameViewManager gameViewManager) {
*/
private void selectOption(IGameViewManager gvm) {
if (menuOptions[currentOption].equalsIgnoreCase("GO BACK")) {
currentMenuState = MenuStates.DEFAULT;
currentOption = 0;
showSettings = false;
chooseSound.play(getSoundVolumeAsFloat());
goBackToMainMenu();
return;
}

Expand Down Expand Up @@ -355,6 +357,14 @@ private void selectOption(IGameViewManager gvm) {
}
}

private void goBackToMainMenu() {
currentMenuState = MenuStates.DEFAULT;
currentOption = 0;
showSettings = false;
chooseSound.play(getSoundVolumeAsFloat());
return;
}

/**
* Method to handle what logic should happen in the Settings menu. It checks the currently selected/hovered setting
* and executes the code relevant to that. It takes the parameter keyInput.
Expand Down Expand Up @@ -408,7 +418,7 @@ private void handleSettings(String keyInput) {
break;
}

if(menuOptions[currentOption].equalsIgnoreCase("AI")){
if (menuOptions[currentOption].equalsIgnoreCase("AI")) {
AIIndex++;

AIIndex = AIIndex % AIOptions.length;
Expand Down Expand Up @@ -436,6 +446,20 @@ private void handleSettings(String keyInput) {
chooseSound.play(getSoundVolumeAsFloat());
break;
}

if (menuOptions[currentOption].equalsIgnoreCase("Concurrent battle amount")) {
// Sets the maximum concurrent battles to 10
if (((int) settings.getSetting(settingsRegistry.getConcurrentBattleAmount()) < 16)) {
// increases the concurrent battles by 1
int concurrentBattles = (int) settings.getSetting(settingsRegistry.getConcurrentBattleAmount()) + 1;
settings.setSetting(settingsRegistry.getConcurrentBattleAmount(), concurrentBattles);

settingsValueList.set(9, String.valueOf(concurrentBattles));
chooseSound.play(getSoundVolumeAsFloat());
}
break;
}

boolean_settings_switch_on_off();
break;
}
Expand Down Expand Up @@ -483,7 +507,7 @@ private void handleSettings(String keyInput) {
}


if(menuOptions[currentOption].equalsIgnoreCase("AI")){
if (menuOptions[currentOption].equalsIgnoreCase("AI")) {
if (AIIndex == 0) {
AIIndex = AIOptions.length - 1;
} else {
Expand Down Expand Up @@ -517,6 +541,19 @@ private void handleSettings(String keyInput) {
break;
}

if (menuOptions[currentOption].equalsIgnoreCase("Concurrent battle amount")) {
// Sets the minimum concurrent battles to 1
if (((int) settings.getSetting(settingsRegistry.getConcurrentBattleAmount()) > 1)) {
// Decreases the concurrent battles by 1
int concurrentBattles = (int) settings.getSetting(settingsRegistry.getConcurrentBattleAmount()) - 1;
settings.setSetting(settingsRegistry.getConcurrentBattleAmount(), concurrentBattles);

settingsValueList.set(9, String.valueOf(concurrentBattles));
chooseSound.play(getSoundVolumeAsFloat());
}
break;
}


boolean_settings_switch_on_off();
break;
Expand Down Expand Up @@ -574,6 +611,18 @@ private void boolean_settings_switch_on_off() {
settings.setSetting(settingsRegistry.getAIKnowlegdeStateEnabled(), true);
}
chooseSound.play(getSoundVolumeAsFloat());
} else if (menuOptions[currentOption].equalsIgnoreCase("Nondeterminism")) {
/*
If setting for using nondeterminism for battles,
*/
if (((Boolean) settings.getSetting(settingsRegistry.getNonDeterminism()))) {
settingsValueList.set(8, "Off");
settings.setSetting(settingsRegistry.getNonDeterminism(), false);
} else {
settingsValueList.set(8, "On");
settings.setSetting(settingsRegistry.getNonDeterminism(), true);
}
chooseSound.play(getSoundVolumeAsFloat());
}
}
}
Expand Down Expand Up @@ -604,6 +653,9 @@ private void settingsInitializer() {

settingsValueList.add((Boolean) settings.getSetting(settingsRegistry.getAIKnowlegdeStateEnabled()) ? "On" : "Off");
AIIndex = Arrays.asList(AIOptions).indexOf(AI);
settingsValueList.add((Boolean) settings.getSetting(settingsRegistry.getNonDeterminism()) ? "On" : "Off");
var battleAmount = String.valueOf(settings.getSetting(settingsRegistry.getConcurrentBattleAmount()));
settingsValueList.add(battleAmount);
}
}

Expand Down