Skip to content

Commit ee33469

Browse files
committed
Merge pull request scalatron#52 from arbfranklin/Issue-52
BotWar: Make "max slaves" and "slave count" variables available to bot plugins
2 parents 3db284b + f068fce commit ee33469

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

BotWar/src/scalatron/botwar/AugmentedDynamics.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ case object AugmentedDynamics extends ((State,Random,Iterable[(Entity.Id,Iterabl
138138
val maxSlaveCount = state.config.boardParams.maxSlaveCount
139139
if(maxSlaveCount < Int.MaxValue) {
140140
val siblings = board.siblingsOfBot(thisBot)
141-
val siblingCount = siblings.size
142-
if(siblingCount > maxSlaveCount + 1) {
141+
val slaveCount = siblings.size - 1
142+
if (slaveCount >= maxSlaveCount) {
143143
throw new IllegalStateException("Spawn(): maximum permissible number of mini-bots has been reached: " + maxSlaveCount )
144144
}
145145
}

BotWar/src/scalatron/botwar/Board.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ object Board
163163
)
164164
= {
165165
val boardSize = boardParams.size
166+
val maxSlaves = boardParams.maxSlaveCount
166167

167168
var updatedBoard = Empty
168169
val rnd = new Random(randomSeed)
@@ -180,6 +181,7 @@ object Board
180181
"name=" + entityController.name + "," +
181182
"apocalypse=" + stepsPerRound + "," +
182183
"round=" + roundIndex +
184+
(if (maxSlaves < Int.MaxValue) ",maxslaves=" + maxSlaves else "") +
183185
")")
184186
} catch {
185187
case t: Throwable =>

BotWar/src/scalatron/botwar/Entity.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,16 @@ object Bot {
173173
Protocol.PropertyName.Time + "=" + state.time + "," +
174174
Protocol.PropertyName.View + "=" + renderedView + "," +
175175
maybeMasterParameter +
176-
Protocol.PropertyName.Energy + "=" + bot.energy +
176+
Protocol.PropertyName.Energy + "=" + bot.energy + "," +
177+
Protocol.PropertyName.Slaves + "=" + slaveCount(bot, state) +
177178
(if(stateMapString.isEmpty) "" else "," + stateMapString) +
178179
")"
179180

180181
controlFunctionInput
181182
}
182183

184+
def slaveCount(bot: Bot, state: State): Int = state.board.siblingsOfBot(bot).size - 1
185+
183186
val controlFunctionInput =
184187
if(isMaster) computeBotInputForMaster(bot, state)
185188
else computeBotInputForSlave(bot, state)

BotWar/src/scalatron/botwar/Protocol.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ object Protocol {
4747
val Collision = "collision"
4848
val Generation = "generation"
4949
val Name = "name"
50+
val Slaves = "slaves"
5051

5152
val ListOfReserved =
5253
List(
53-
Energy, Time, View, Generation, Name,
54+
Energy, Time, View, Generation, Name, Slaves,
5455
PluginOpcode.ParameterName.Direction,
5556
ServerOpcode.ParameterName.Master
5657
)

Scalatron/doc/markdown/Scalatron Protocol.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ following property names are reserved and must not be used for custom properties
232232
* "direction"
233233
* "master"
234234
* "collision"
235+
* "slaves"
235236

236237
Custom state properties cannot have empty strings as their values. Setting a custom state property
237238
to an empty string deletes it from the state.
@@ -246,7 +247,7 @@ These are the opcodes valid for commands sent by the server to a plug-in's contr
246247
Only one opcode will be present per control function invocation.
247248

248249

249-
### Welcome(name=String,apocalypse=int,round=int)
250+
### Welcome(name=String,apocalypse=int,round=int,maxslaves=int)
250251

251252
"Welcome" is the first command sent by the server to a plug-in before any other invocations of
252253
the control function.
@@ -262,9 +263,11 @@ Parameters:
262263
* `round`: the index of the round for which the control function was instantiated.
263264
A game server continually runs rounds of the game, and the round index is
264265
incremented each time.
266+
* `maxslaves`: the number of slave bots that a user can have alive at any one time. If a call
267+
to `Spawn` is made when this number of user bots exist, then the request will be
268+
denied.
265269

266-
267-
### React(generation=int,name=string,time=int,view=string,energy=string,master=int:int,collision=int:int,...)
270+
### React(generation=int,name=string,time=int,view=string,energy=string,master=int:int,collision=int:int,slaves=int,...)
268271

269272
"React" is invoked by the server once for each entity for each step in which the entity is
270273
allowed to move (mini-bots every cycle, bots every second cycle - see the *Game Rules* for
@@ -296,6 +299,7 @@ Parameters:
296299
collision with another entity occurred, this parameter is set to the direction of the
297300
failed move, e.g. "1:-1" if a move right and up could not be executed. If no collision
298301
occurred, this property is not defined.
302+
* `slaves` the number of slave bots that the user currently has alive as at the current `time`.
299303

300304
In addition to these system-generated parameters, the server passes in all state parameters of
301305
the entity that were set by the player via `Spawn()` or `Set()` (see below). If, for example,

0 commit comments

Comments
 (0)