2021-04-29 17:15:29 +00:00
|
|
|
package uulm.teamname.marvelous.gamelibrary.gamelogic;
|
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
2021-04-29 17:15:29 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.Tuple;
|
2021-05-01 22:03:03 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.Entity;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
2021-05-02 13:34:42 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.StatType;
|
2021-05-01 21:06:22 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.events.*;
|
2021-05-02 13:34:42 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.events.Event;
|
2021-05-01 21:06:22 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.requests.CharacterRequest;
|
2021-04-29 17:15:29 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.requests.Request;
|
2021-05-01 21:06:22 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
|
2021-04-29 17:15:29 +00:00
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.geom.Line2D;
|
2021-04-29 17:15:29 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/** Contains game logic handling. */
|
2021-04-29 17:15:29 +00:00
|
|
|
class GameLogic {
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
2021-05-02 12:38:03 +00:00
|
|
|
* Produces resulting {@link Event}s from a given {@link Request}.
|
|
|
|
* @param state The game state to execute on
|
|
|
|
* @param request The request to execute
|
2021-04-30 18:54:34 +00:00
|
|
|
* @return The list of resulting events
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
2021-05-02 12:38:03 +00:00
|
|
|
public static ArrayList<Event> executeRequest(GameState state, Request request) {
|
2021-05-01 21:06:22 +00:00
|
|
|
ArrayList<Event> result = new ArrayList<>();
|
2021-04-29 17:15:29 +00:00
|
|
|
|
2021-05-01 21:06:22 +00:00
|
|
|
switch(request.type) {
|
|
|
|
case MeleeAttackRequest, RangedAttackRequest -> {
|
|
|
|
CharacterRequest data = (CharacterRequest)request;
|
2021-05-03 17:36:30 +00:00
|
|
|
result.add(new EventBuilder(request.type == RequestType.MeleeAttackRequest ? EventType.MeleeAttackEvent : EventType.RangedAttackEvent)
|
|
|
|
.withOriginEntity(data.originEntity)
|
|
|
|
.withTargetEntity(data.targetEntity)
|
|
|
|
.withOriginField(data.originField)
|
|
|
|
.withTargetField(data.targetField)
|
|
|
|
.withAmount(data.value)
|
|
|
|
.buildCharacterEvent());
|
|
|
|
result.add(new EventBuilder(EventType.ConsumedAPEvent)
|
|
|
|
.withTargetEntity(data.originEntity)
|
|
|
|
.withTargetField(data.originField)
|
|
|
|
.withAmount(1)
|
|
|
|
.buildEntityEvent());
|
|
|
|
result.add(new EventBuilder(EventType.TakenDamageEvent)
|
|
|
|
.withTargetEntity(data.targetEntity)
|
|
|
|
.withTargetField(data.targetField)
|
|
|
|
.withAmount(data.value)
|
|
|
|
.buildEntityEvent());
|
2021-05-01 21:06:22 +00:00
|
|
|
}
|
|
|
|
case MoveRequest -> {
|
|
|
|
CharacterRequest data = (CharacterRequest)request;
|
2021-05-03 17:36:30 +00:00
|
|
|
result.add(new EventBuilder(EventType.MoveEvent)
|
|
|
|
.withOriginEntity(data.originEntity)
|
|
|
|
.withOriginField(data.originField)
|
|
|
|
.withTargetField(data.targetField)
|
|
|
|
.buildCharacterEvent());
|
|
|
|
result.add(new EventBuilder(EventType.ConsumedMPEvent)
|
|
|
|
.withTargetEntity(data.originEntity)
|
|
|
|
.withTargetField(data.targetField) //when this event gets handled, the character already moved to the target field
|
|
|
|
.withAmount(1)
|
|
|
|
.buildEntityEvent());
|
2021-05-02 12:38:03 +00:00
|
|
|
for(Entity entity: state.entities.findByPosition(data.targetField)) {
|
|
|
|
if(entity instanceof Character) {
|
2021-05-03 17:36:30 +00:00
|
|
|
result.add(new EventBuilder(EventType.MoveEvent)
|
|
|
|
.withOriginEntity(entity.id)
|
|
|
|
.withOriginField(data.targetField)
|
|
|
|
.withTargetField(data.originField)
|
|
|
|
.buildCharacterEvent());
|
2021-05-02 12:38:03 +00:00
|
|
|
break; //we should only have one character per field anyways
|
|
|
|
}
|
|
|
|
}
|
2021-05-01 21:06:22 +00:00
|
|
|
}
|
|
|
|
case ExchangeInfinityStoneRequest, UseInfinityStoneRequest -> {
|
|
|
|
CharacterRequest data = (CharacterRequest)request;
|
2021-05-03 17:36:30 +00:00
|
|
|
result.add(new EventBuilder(request.type == RequestType.ExchangeInfinityStoneRequest ? EventType.ExchangeInfinityStoneEvent : EventType.UseInfinityStoneEvent)
|
|
|
|
.withOriginEntity(data.originEntity)
|
|
|
|
.withOriginField(data.originField)
|
|
|
|
.withTargetEntity(data.targetEntity)
|
|
|
|
.withTargetField(data.targetField)
|
|
|
|
.withStoneType(data.stoneType)
|
|
|
|
.buildCharacterEvent());
|
|
|
|
result.add(new EventBuilder(EventType.ConsumedAPEvent)
|
|
|
|
.withTargetEntity(data.originEntity)
|
|
|
|
.withTargetField(data.originField)
|
|
|
|
.withAmount(1)
|
|
|
|
.buildEntityEvent());
|
2021-05-02 13:34:42 +00:00
|
|
|
//TODO: add infinity stone usage effect in GameLogic.executeRequest
|
2021-05-01 21:06:22 +00:00
|
|
|
}
|
|
|
|
case DisconnectRequest -> {
|
2021-05-03 17:36:30 +00:00
|
|
|
result.add(new EventBuilder(EventType.DisconnectEvent)
|
|
|
|
.buildGameEvent());
|
2021-05-01 21:06:22 +00:00
|
|
|
}
|
2021-04-29 17:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Checks a {@link Request} for validity for a {@link GameState}.
|
|
|
|
* @param state The game state to check on
|
|
|
|
* @param request The request to validate
|
2021-04-29 17:15:29 +00:00
|
|
|
* @return Whether or not the request is valid
|
|
|
|
*/
|
|
|
|
public static boolean checkRequest(GameState state, Request request) {
|
2021-05-01 22:03:03 +00:00
|
|
|
try {
|
|
|
|
switch(request.type) {
|
|
|
|
case MeleeAttackRequest, RangedAttackRequest -> {
|
|
|
|
CharacterRequest data = (CharacterRequest)request;
|
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
Character origin = getCharacter(state, data.originField, data.originEntity);
|
|
|
|
Character target = getCharacter(state, data.targetField, data.targetEntity);
|
2021-05-01 22:03:03 +00:00
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
requireAlive(origin);
|
|
|
|
requireAlive(target);
|
|
|
|
requireAP(origin, 1);
|
2021-05-01 22:03:03 +00:00
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
if(request.type == RequestType.MeleeAttackRequest) {
|
|
|
|
if(origin.meleeDamage != data.value) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
if(data.originField.distanceManhattan(data.targetField) > 1) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
}else if(request.type == RequestType.RangedAttackRequest) {
|
|
|
|
if(origin.rangedDamage != data.value) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
if(data.originField.distanceManhattan(data.targetField) > origin.attackRange) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
if(data.originField.distanceManhattan(data.targetField) <= 1) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
requireLineOfSight(state, data.originField, data.targetField);
|
2021-05-01 22:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case MoveRequest -> {
|
|
|
|
CharacterRequest data = (CharacterRequest)request;
|
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
Character origin = getCharacter(state, data.originField, data.originEntity);
|
2021-05-01 22:03:03 +00:00
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
requireAlive(origin);
|
|
|
|
requireMP(origin, 1);
|
|
|
|
verifyCoordinates(state, data.targetField);
|
2021-05-01 22:03:03 +00:00
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
if(state.entities.blocksMovement(data.targetField)) {
|
|
|
|
throw new InvalidRequestException();
|
2021-05-01 22:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case ExchangeInfinityStoneRequest -> {
|
|
|
|
CharacterRequest data = (CharacterRequest)request;
|
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
Character origin = getCharacter(state, data.originField, data.originEntity);
|
|
|
|
Character target = getCharacter(state, data.targetField, data.targetEntity);
|
2021-05-01 22:03:03 +00:00
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
requireAlive(origin);
|
|
|
|
requireAlive(target);
|
|
|
|
requireAP(origin, 1);
|
|
|
|
requireInfinityStone(origin, data.stoneType);
|
2021-05-01 22:03:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case UseInfinityStoneRequest -> {
|
|
|
|
CharacterRequest data = (CharacterRequest)request;
|
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
Character origin = getCharacter(state, data.originField, data.originEntity);
|
|
|
|
Character target = getCharacter(state, data.targetField, data.targetEntity);
|
2021-05-01 22:03:03 +00:00
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
requireAlive(origin);
|
|
|
|
requireAP(origin, 1);
|
|
|
|
requireInfinityStone(origin, data.stoneType);
|
2021-05-01 22:03:03 +00:00
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
//TODO: properly verify UseInfinityStoneRequest in GameLogic.checkRequest
|
|
|
|
if(!target.isActive()) {
|
|
|
|
throw new InvalidRequestException();
|
2021-05-01 22:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
case DisconnectRequest -> {
|
|
|
|
//TODO: add check for DisconnectRequest in GameLogic.checkRequest
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-05-02 13:34:42 +00:00
|
|
|
}catch(Exception ignored) {
|
2021-05-01 22:03:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-29 17:15:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a {@link Character} for a {@link Request}.
|
|
|
|
* @param state The game state to use
|
|
|
|
* @param position The requested position
|
|
|
|
* @param entityID The requested {@link EntityID}
|
|
|
|
* @return The found character
|
|
|
|
* @throws InvalidRequestException if the character is invalid or not found
|
|
|
|
*/
|
|
|
|
private static Character getCharacter(GameState state, IntVector2 position, EntityID entityID) throws InvalidRequestException {
|
|
|
|
Entity entity = state.entities.findEntity(entityID);
|
|
|
|
if(entity == null || entity.getPosition() != position || !(entity instanceof Character)) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return (Character)entity;
|
|
|
|
}catch(Exception ignored) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that a {@link Character} is alive.
|
|
|
|
*/
|
|
|
|
private static void requireAlive(Character entity) throws InvalidRequestException {
|
|
|
|
if(entity.hp.getValue() <= 0 || !entity.isActive()) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that a {@link Character} has enough {@link StatType#AP}.
|
|
|
|
*/
|
|
|
|
private static void requireAP(Character entity, int ap) throws InvalidRequestException {
|
|
|
|
if(entity.ap.getValue() < ap) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that a {@link Character} has enough {@link StatType#MP}.
|
|
|
|
*/
|
|
|
|
private static void requireMP(Character entity, int mp) throws InvalidRequestException {
|
|
|
|
if(entity.mp.getValue() < mp) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that a {@link Character} has the required {@link StoneType}.
|
|
|
|
*/
|
|
|
|
private static void requireInfinityStone(Character entity, StoneType stone) throws InvalidRequestException {
|
2021-05-03 17:36:30 +00:00
|
|
|
if(stone == null || !entity.inventory.hasStone(stone)) {
|
2021-05-02 13:34:42 +00:00
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that coordinates are within the playing area.
|
|
|
|
*/
|
|
|
|
private static void verifyCoordinates(GameState state, IntVector2 position) throws InvalidRequestException {
|
|
|
|
if(position.getX() < 0 || position.getX() >= state.mapSize.getX() || position.getY() < 0 || position.getY() >= state.mapSize.getY()) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that there is a line of sight between two positions.
|
|
|
|
*/
|
|
|
|
private static void requireLineOfSight(GameState state, IntVector2 start, IntVector2 end) throws InvalidRequestException {
|
|
|
|
//naive code for the win!!! \o/
|
|
|
|
//at least its early exit and probably only O(ln(n*m))
|
|
|
|
//TODO: implement proper line rasterization algorithm in GameLogic.requireLineOfSight
|
|
|
|
Line2D line = new Line2D.Float(start.getX(), start.getY(), end.getX(), end.getY());
|
|
|
|
for(int i = start.getX(); i <= end.getX(); i++) {
|
|
|
|
for(int j = start.getY(); j <= end.getY(); j++) {
|
|
|
|
var cell = new Rectangle.Float(i - 0.5f, j - 0.5f, 1, 1);
|
|
|
|
if(line.intersects(cell)) {
|
|
|
|
if(state.entities.blocksVision(new IntVector2(i, j))) {
|
|
|
|
throw new InvalidRequestException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Applies an {@link Event} to a {@link GameState}.
|
|
|
|
* @param state The game state to apply to
|
|
|
|
* @param event The event to apply
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
|
|
|
public static void applyEvent(GameState state, Event event) {
|
|
|
|
//TODO: implement GameLogic.applyEvent
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Checks a {@link GameState} for the current overtime win condition.
|
|
|
|
* @param state The game state to check
|
|
|
|
* @return The {@link ParticipantType} that is currently winning the game according to overtime ruling
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
|
|
|
public static ParticipantType checkWinConditions(GameState state) {
|
|
|
|
//TODO: GameLogic.checkWinConditions is kind of ugly
|
|
|
|
|
|
|
|
Tuple<ParticipantType, WinCondition> player1;
|
|
|
|
Tuple<ParticipantType, WinCondition> player2;
|
|
|
|
int value1;
|
|
|
|
int value2;
|
|
|
|
for(WinCondition condition: WinCondition.values()) {
|
|
|
|
player1 = new Tuple<ParticipantType, WinCondition>(ParticipantType.Player1, condition);
|
|
|
|
player2 = new Tuple<ParticipantType, WinCondition>(ParticipantType.Player2, condition);
|
|
|
|
value1 = 0;
|
|
|
|
value2 = 0;
|
|
|
|
|
|
|
|
if(state.winConditions.containsKey(player1)) {
|
|
|
|
value1 = state.winConditions.get(player1);
|
|
|
|
}
|
|
|
|
if(state.winConditions.containsKey(player2)) {
|
|
|
|
value2 = state.winConditions.get(player2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(value1 > value2) {
|
|
|
|
return ParticipantType.Player1;
|
|
|
|
}
|
|
|
|
if(value2 > value1) {
|
|
|
|
return ParticipantType.Player2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParticipantType.None;
|
|
|
|
}
|
|
|
|
}
|