src/robocode_tankroyale_botapi/event_queue

Source   Edit  

Priority-based event queue for Robocode Tank Royale bot API.

Every tick, the server may deliver several events at once (for example you scanned a bot, got hit by a bullet, and a bullet of yours hit an enemy). This module collects them, sorts them by importance, and dispatches them to your bot's event handlers in the correct order.

Why priority matters

Some events are more urgent than others. If you die this tick, it makes no sense to also run your onScannedBot logic. Events are sorted so the most important ones run first. The default priority values come from constants (e.g. PRIORITY_DEATH = 10 is lowest, PRIORITY_WON_ROUND = 150 is highest). You can change a kind's priority at runtime with bot.setEventPriority.

Critical events

Three event kinds are marked critical: ekDeath, ekWonRound, and ekSkippedTurn. Critical events are never deleted when old events are cleaned up, and they are allowed to interrupt a lower-priority handler that is currently running.

See also

  • bot.setEventPriority -- change dispatch order at runtime
  • bot.setInterruptible -- let the current handler be interrupted
  • bot.addCustomEvent -- register your own event condition

Types

BotEvent = object
  turnNumber*: int
  case kind*: EventKind
  of ekTick:
    tick*: TickEventForBot
  of ekSkippedTurn:
    skippedTurn*: SkippedTurnEvent
  of ekBotDeath:
    botDeath*: BotDeathEvent
  of ekDeath:
    death*: BotDeathEvent
  of ekBulletFired:
    bulletFired*: BulletFiredEvent
  of ekBulletHitBot:
    bulletHitBot*: BulletHitBotEvent
  of ekBulletHitBullet:
    bulletHitBullet*: BulletHitBulletEvent
  of ekBulletHitWall:
    bulletHitWall*: BulletHitWallEvent
  of ekHitByBullet:
    hitByBullet*: HitByBulletEvent
  of ekHitBot:
    hitBot*: BotHitBotEvent
  of ekHitWall:
    hitWall*: BotHitWallEvent
  of ekScannedBot:
    scannedBot*: ScannedBotEvent
  of ekWonRound:
    wonRound*: WonRoundEvent
  of ekTeamMessage:
    teamMessage*: TeamMessageEvent
  of ekCustom:
    condition*: Condition
A single event with a turnNumber and a kind-specific payload. Source   Edit  
Condition = object
  name*: string
  test*: proc (): bool {.closure.}
A named boolean test evaluated every tick for custom events. Source   Edit  
EventKind = enum
  ekTick,                   ## Regular tick (normal game state update)
  ekSkippedTurn,            ## You ran out of time on a turn (critical)
  ekBotDeath,               ## Another bot was destroyed
  ekDeath,                  ## Your bot was destroyed (critical)
  ekBulletFired,            ## You fired a bullet
  ekBulletHitBot,           ## Your bullet struck an enemy
  ekBulletHitBullet,        ## Your bullet struck another bullet
  ekBulletHitWall,          ## Your bullet struck a wall
  ekHitByBullet,            ## You were struck by a bullet
  ekHitBot,                 ## You collided with another bot
  ekHitWall,                ## You collided with a wall
  ekScannedBot,             ## Your radar detected a bot
  ekWonRound,               ## You won the round (critical)
  ekTeamMessage,            ## A teammate sent you a message
  ekCustom                   ## A custom condition you registered became true
The kind of an event. Used as the sort key and dispatch selector. Source   Edit  
EventQueue = object
  events*: seq[BotEvent]
  interruptible*: set[EventKind]
  currentTopEventKind*: EventKind
  currentTopPriority*: int
  conditions*: seq[Condition]
Internal queue holding the events for the current tick. Source   Edit  

Procs

proc addCondition(eq: var EventQueue; c: Condition) {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc addCustomEvents(eq: var EventQueue; turnNumber: int) {....raises: [],
    tags: [RootEffect], forbids: [].}
Source   Edit  
proc addEvent(eq: var EventQueue; e: BotEvent) {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc clear(eq: var EventQueue) {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc clearEvents(eq: var EventQueue) {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc getEvents(eq: EventQueue): seq[BotEvent] {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc getPriority(eq: EventQueue; kind: EventKind): int {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc initEventQueue(): EventQueue {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc isCritical(e: BotEvent): bool {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc isInterruptible(eq: EventQueue; kind: EventKind): bool {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc popFirst(eq: var EventQueue): BotEvent {....raises: [], tags: [], forbids: [].}
Remove and return the head element. Source   Edit  
proc priorityOf(kind: EventKind): int {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc removeConditionByName(eq: var EventQueue; name: string) {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc removeOldEvents(eq: var EventQueue; turnNumber: int) {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc setInterruptible(eq: var EventQueue; kind: EventKind; v: bool) {.
    ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc setPriority(eq: var EventQueue; kind: EventKind; p: int) {....raises: [],
    tags: [], forbids: [].}
Source   Edit  
proc sortEvents(eq: var EventQueue) {....raises: [], tags: [], forbids: [].}
Source   Edit