Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rule for damage taken by mothership
  • Loading branch information
cswinter committed May 7, 2020
1 parent 1bfc30f commit a36d081
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
Expand Up @@ -12,6 +12,7 @@ import scala.concurrent.duration._
import scala.language.postfixOps
import akka.pattern.ask
import akka.util.Timeout
import cwinter.codecraft.core.game.SpecialRules
import play.api.mvc._

import scala.util.Try
Expand All @@ -30,8 +31,11 @@ case class ObsConfig(
lastSeen: Boolean,
isVisible: Boolean,
abstime: Boolean,
mapSize: Boolean
)
mapSize: Boolean,
ruleMsdm: Boolean
) {
def rules: Int = if (ruleMsdm) 1 else 0
}

@Singleton
class Application @Inject()(
Expand All @@ -47,11 +51,18 @@ class Application @Inject()(
Ok(Observe()).as("text/html")
}

def startGame(maxTicks: Option[Int], actionDelay: Int, scriptedOpponent: Boolean, idleOpponent: Boolean) =
def startGame(
maxTicks: Option[Int],
actionDelay: Int,
scriptedOpponent: Boolean,
idleOpponent: Boolean,
mothershipDamageMultiplier: Double
) =
Action { implicit request =>
val body = request.body.asJson.get.toString
val customMap = if (body == "\"\"") None else Some(read[MapSettings](body))
val id = multiplayerServer.startGame(maxTicks, scriptedOpponent, idleOpponent, customMap)
val rules = SpecialRules(mothershipDamageMultiplier)
val id = multiplayerServer.startGame(maxTicks, scriptedOpponent, idleOpponent, customMap, rules)
Ok(f"""{"id": $id}""").as("application/json")
}

Expand Down Expand Up @@ -93,7 +104,8 @@ class Application @Inject()(
lastSeen: Boolean,
isVisible: Boolean,
abstime: Boolean,
mapSize: Boolean) = Action { implicit request =>
mapSize: Boolean,
ruleMsdm: Boolean) = Action { implicit request =>
val obsConfig =
ObsConfig(allies,
drones,
Expand All @@ -106,7 +118,8 @@ class Application @Inject()(
lastSeen,
isVisible,
abstime,
mapSize)
mapSize,
ruleMsdm)
val games = read[Seq[(Int, Int)]](request.body.asJson.get.toString)
val payload: Seq[Observation] = for ((gameID, playerID) <- games)
yield multiplayerServer.observe(gameID, playerID, lastSeen)
Expand Down Expand Up @@ -142,7 +155,7 @@ class Application @Inject()(

class ObsSerializer(obs: Seq[Observation], obsConfig: ObsConfig) {
assert(!obsConfig.relativePositions)
private val globalFeat = 2 + (if (obsConfig.mapSize) 2 else 0) + (if (obsConfig.abstime) 2 else 0)
private val globalFeat = 2 + (if (obsConfig.mapSize) 2 else 0) + (if (obsConfig.abstime) 2 else 0) + obsConfig.rules
private val extraFeat = 5 // Unobserved features such as score, winner, ms health
private val allyDroneFeat = 15 + (if (obsConfig.obsLastAction) 8 else 0) +
(if (obsConfig.lastSeen) 2 else 0) + (if (obsConfig.isVisible) 1 else 0)
Expand Down Expand Up @@ -182,6 +195,9 @@ class ObsSerializer(obs: Seq[Observation], obsConfig: ObsConfig) {
bb.putFloat(ob.timestep)
bb.putFloat(ob.maxGameLength - ob.timestep)
}
if (obsConfig.ruleMsdm) {
bb.putFloat(ob.rules.mothershipDamageMultiplier.toFloat)
}

// Allies
ob.alliedDrones.take(obsConfig.allies).foreach(serializeDrone)
Expand Down
Expand Up @@ -32,15 +32,16 @@ class MultiplayerServer @Inject()(lifecycle: ApplicationLifecycle) {
def startGame(maxTicks: Option[Int],
scriptedOpponent: Boolean,
idleOpponent: Boolean,
customMap: Option[MapSettings]): Integer =
customMap: Option[MapSettings],
rules: SpecialRules): Integer =
synchronized {
val initialDrones = customMap.map(m => (m.player1Drones.size, m.player2Drones.size)).getOrElse((1, 1))
val maxGameLength = maxTicks.getOrElse(3 * 60 * 60)
val mapWidth = customMap.map(_.mapWidth).getOrElse(6000)
val mapHeight = customMap.map(_.mapHeight).getOrElse(4000)
val tileWidth = 400
val player1 =
new PlayerController(maxGameLength, BluePlayer, gameID + 1, mapWidth, mapHeight, tileWidth)
new PlayerController(maxGameLength, BluePlayer, gameID + 1, mapWidth, mapHeight, tileWidth, rules)
var controllers: Seq[DroneControllerBase] =
Seq.fill(initialDrones._1)(new PassiveDroneController(player1, Promise.successful(DoNothing)))
var player2: Option[PlayerController] = None
Expand All @@ -52,7 +53,13 @@ class MultiplayerServer @Inject()(lifecycle: ApplicationLifecycle) {
controllers ++= Seq.fill(initialDrones._2)(TheGameMaster.level7AI())
} else {
val p2 =
new PlayerController(maxGameLength, OrangePlayer, gameID + 1, mapWidth, mapHeight, tileWidth)
new PlayerController(maxGameLength,
OrangePlayer,
gameID + 1,
mapWidth,
mapHeight,
tileWidth,
rules)
player2 = Some(p2)
controllers ++= Seq.fill(initialDrones._2)(
new PassiveDroneController(p2, Promise.successful(DoNothing)))
Expand All @@ -70,7 +77,7 @@ class MultiplayerServer @Inject()(lifecycle: ApplicationLifecycle) {
m.symmetric
)
)
val simulator = server.startLocalGame(controllers, winCondition, map)
val simulator = server.startLocalGame(controllers, winCondition, map, rules)
gameID += 1
val playerControllers = player2 match {
case Some(p2) => Seq(player1, p2)
Expand Down Expand Up @@ -233,7 +240,8 @@ class PlayerController(
val gameID: Int,
val mapWidth: Int,
val mapHeight: Int,
val tileWidth: Int
val tileWidth: Int,
val rules: SpecialRules
) extends MetaController {
@volatile var alliedDrones = Seq.empty[PassiveDroneController]
@volatile var enemyDrones = Seq.empty[Drone]
Expand Down Expand Up @@ -320,7 +328,8 @@ class PlayerController(
minEnemyMSHealth,
sim.config.worldSize.height,
sim.config.worldSize.width,
tiles.flatMap(_.toSeq)
tiles.flatMap(_.toSeq),
rules
)
}

Expand Down Expand Up @@ -396,7 +405,8 @@ case class Observation(
minEnemyMSHealth: Double,
mapHeight: Double,
mapWidth: Double,
tiles: Seq[MapTile]
tiles: Seq[MapTile],
rules: SpecialRules
)

case class DroneObservation(
Expand Down
4 changes: 2 additions & 2 deletions server/conf/routes
Expand Up @@ -5,10 +5,10 @@
# Home page
GET / com.clemenswinter.codecraftserver.controllers.Application.index
GET /observe com.clemenswinter.codecraftserver.controllers.Application.observe
POST /start-game com.clemenswinter.codecraftserver.controllers.Application.startGame(maxTicks: Option[Int], actionDelay: Int ?= 0, scriptedOpponent: Boolean ?= true, idleOpponent: Boolean ?= true)
POST /start-game com.clemenswinter.codecraftserver.controllers.Application.startGame(maxTicks: Option[Int], actionDelay: Int ?= 0, scriptedOpponent: Boolean ?= true, idleOpponent: Boolean ?= true, mothershipDamageMultiplier: Double ?= 1.0)
GET /observation com.clemenswinter.codecraftserver.controllers.Application.playerState(gameID: Int, playerID: Int)
POST /act com.clemenswinter.codecraftserver.controllers.Application.act(gameID: Int, playerID: Int)
GET /batch-observation com.clemenswinter.codecraftserver.controllers.Application.batchPlayerState(json: Boolean ?= true, allies: Int ?= 1, drones: Int ?= 10, minerals: Int ?= 10, globalDrones: Int ?= 0, tiles: Int ?= 0, relativePositions: Boolean ?= true, v2: Boolean ?= false, actions: Seq[Int] ?= Seq.empty, obsLastAction: Boolean ?= false, lastSeen: Boolean ?= false, isVisible: Boolean ?= false, abstime: Boolean ?= false, mapSize: Boolean ?= false)
GET /batch-observation com.clemenswinter.codecraftserver.controllers.Application.batchPlayerState(json: Boolean ?= true, allies: Int ?= 1, drones: Int ?= 10, minerals: Int ?= 10, globalDrones: Int ?= 0, tiles: Int ?= 0, relativePositions: Boolean ?= true, v2: Boolean ?= false, actions: Seq[Int] ?= Seq.empty, obsLastAction: Boolean ?= false, lastSeen: Boolean ?= false, isVisible: Boolean ?= false, abstime: Boolean ?= false, mapSize: Boolean ?= false, ruleMsdm: Boolean ?= false)
POST /batch-act com.clemenswinter.codecraftserver.controllers.Application.batchAct()

GET /ajax/multiplayerServerStatus com.clemenswinter.codecraftserver.controllers.Application.mpssJson
Expand Down

0 comments on commit a36d081

Please sign in to comment.