rust/hedgewars-engine-messages/src/messages.rs
author unC0Rr
Thu, 22 Nov 2018 13:40:58 +0100
changeset 14278 645e4591361f
parent 14277 3152d9fdb499
child 15269 7515ae6010bb
permissions -rw-r--r--
Send seed to engine to receive random map previews
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
     1
use byteorder::{BigEndian, WriteBytesExt};
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
     2
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14260
diff changeset
     3
#[derive(Debug, PartialEq, Clone)]
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     4
pub enum KeystrokeAction {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     5
    Press,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     6
    Release,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     7
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
     8
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14260
diff changeset
     9
#[derive(Debug, PartialEq, Clone)]
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    10
pub enum SyncedEngineMessage {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    11
    Left(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    12
    Right(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    13
    Up(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    14
    Down(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    15
    Precise(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    16
    Attack(KeystrokeAction),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    17
    NextTurn,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    18
    Switch,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    19
    Timer(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    20
    Slot(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    21
    SetWeapon(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    22
    Put(i32, i32),
14216
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    23
    CursorMove(i32, i32),
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    24
    HighJump,
14216
0f7b92397b9e Parse some synced messages, tests
unC0Rr
parents: 14215
diff changeset
    25
    LongJump,
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    26
    Skip,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    27
    TeamControlGained(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    28
    TeamControlLost(String),
14215
1c55c54013c4 Recognize timestamps
unC0Rr
parents: 14214
diff changeset
    29
    TimeWrap,
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    30
    Taunt(u8),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    31
    HogSay(String),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    32
    Heartbeat,
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    33
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    34
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14260
diff changeset
    35
#[derive(Debug, PartialEq, Clone)]
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    36
pub enum UnsyncedEngineMessage {
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    37
    TeamControlGained(String),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    38
    TeamControlLost(String),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    39
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    40
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14260
diff changeset
    41
#[derive(Debug, PartialEq, Clone)]
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    42
pub enum UnorderedEngineMessage {
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    43
    Ping,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    44
    Pong,
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    45
    ChatMessage(String),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    46
    TeamMessage(String),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    47
    Error(String),
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    48
    Warning(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    49
    StopSyncing,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    50
    GameOver,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    51
    GameInterrupted,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    52
    GameSetupChecksum(String),
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    53
    PauseToggled,
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    54
}
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
    55
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14260
diff changeset
    56
#[derive(Debug, PartialEq, Clone)]
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    57
pub enum ConfigEngineMessage {
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
    58
    GameType(u8),
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    59
    ConfigRequest,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    60
    SetAmmo(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    61
    SetScript(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    62
    SetScriptParam(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    63
    Spectate,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    64
    TeamLocality(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    65
    SetMap(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    66
    SetTheme(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    67
    SetSeed(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    68
    SetTemplateFilter(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    69
    SetMapGenerator(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    70
    SetFeatureSize(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    71
    SetDelay(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    72
    SetReadyDelay(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    73
    SetCratesFrequency(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    74
    SetHealthCrateProbability(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    75
    SetHealthCratesNumber(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    76
    SetRoundsTilSuddenDeath(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    77
    SetSuddenDeathWaterRiseSpeed(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    78
    SetSuddenDeathHealthDecreaseRate(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    79
    SetDamageMultiplier(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    80
    SetRopeLength(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    81
    SetGetawayTime(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    82
    SetDudMinesPercent(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    83
    SetMinesNumber(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    84
    SetAirMinesNumber(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    85
    SetBarrelsNumber(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    86
    SetTurnTime(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    87
    SetMinesTime(u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    88
    SetWorldEdge(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    89
    Draw,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    90
    // TODO
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    91
    SetVoicePack(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    92
    AddHedgehog(String, u8, u32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    93
    AddTeam(String, u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    94
    SetHedgehogCoordinates(i32, i32),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    95
    SetFort(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    96
    SetGrave(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    97
    SetHat(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    98
    SetFlag(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
    99
    SetOwner(String),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   100
    SetOneClanMode(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   101
    SetMultishootMode(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   102
    SetSolidLand(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   103
    SetBorders(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   104
    SetDivideTeams(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   105
    SetLowGravity(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   106
    SetLaserSight(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   107
    SetInvulnerability(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   108
    SetHealthReset(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   109
    SetVampiric(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   110
    SetKarma(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   111
    SetArtilleryMode(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   112
    SetHedgehogSwitch(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   113
    SetRandomOrder(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   114
    SetKingMode(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   115
    SetPlaceHedgehog(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   116
    SetSharedAmmo(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   117
    SetGirdersEnabled(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   118
    SetLandObjectsEnabled(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   119
    SetAISurvivalMode(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   120
    SetInfiniteAttack(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   121
    SetResetWeapons(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   122
    SetAmmoPerHedgehog(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   123
    SetWindMode(u8),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   124
    SetTagTeam(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   125
    SetBottomBorder(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   126
    SetShoppaBorder(bool),
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   127
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   128
14277
3152d9fdb499 - Move EngineInstance into a separate module
unC0Rr
parents: 14260
diff changeset
   129
#[derive(Debug, PartialEq, Clone)]
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   130
pub enum EngineMessage {
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   131
    Unknown,
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   132
    Empty,
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   133
    Synced(SyncedEngineMessage, u32),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   134
    Unsynced(UnsyncedEngineMessage),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   135
    Unordered(UnorderedEngineMessage),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   136
    Config(ConfigEngineMessage),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   137
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   138
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   139
macro_rules! em {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   140
    [$msg: expr] => {
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   141
        vec![($msg)]
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   142
    };
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   143
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   144
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   145
macro_rules! ems {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   146
    [$msg: expr, $param: expr] => {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   147
        {
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   148
            let mut v = vec![($msg)];
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   149
            v.extend(String::into_bytes($param.to_string()).iter());
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   150
            v
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   151
        }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   152
    };
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   153
}
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   154
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   155
impl SyncedEngineMessage {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   156
    fn to_bytes(&self) -> Vec<u8> {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   157
        use self::KeystrokeAction::*;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   158
        use self::SyncedEngineMessage::*;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   159
        match self {
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   160
            Left(Press) => em![b'L'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   161
            Left(Release) => em![b'l'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   162
            Right(Press) => em![b'R'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   163
            Right(Release) => em![b'r'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   164
            Up(Press) => em![b'U'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   165
            Up(Release) => em![b'u'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   166
            Down(Press) => em![b'D'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   167
            Down(Release) => em![b'd'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   168
            Precise(Press) => em![b'Z'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   169
            Precise(Release) => em![b'z'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   170
            Attack(Press) => em![b'A'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   171
            Attack(Release) => em![b'a'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   172
            NextTurn => em![b'N'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   173
            Switch => em![b'S'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   174
            Timer(t) => vec![b'0' + t],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   175
            Slot(s) => vec![b'~' , *s],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   176
            SetWeapon(s) => vec![b'~', *s],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   177
            Put(x, y) => {
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   178
                let mut v = vec![b'p'];
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   179
                v.write_i24::<BigEndian>(*x).unwrap();
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   180
                v.write_i24::<BigEndian>(*y).unwrap();
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   181
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   182
                v
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   183
            },
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   184
            CursorMove(x, y) => {
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   185
                let mut v = vec![b'P'];
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   186
                v.write_i24::<BigEndian>(*x).unwrap();
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   187
                v.write_i24::<BigEndian>(*y).unwrap();
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   188
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   189
                v
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   190
            },
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   191
            HighJump => em![b'J'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   192
            LongJump => em![b'j'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   193
            Skip => em![b','],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   194
            TeamControlGained(str) => ems![b'g', str],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   195
            TeamControlLost(str) => ems![b'f', str],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   196
            Taunt(s) => vec![b't', *s],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   197
            HogSay(str) => ems![b'h', str],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   198
            Heartbeat => em![b'+'],
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   199
            TimeWrap => unreachable!(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   200
        }
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   201
    }
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   202
}
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   203
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   204
impl UnsyncedEngineMessage {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   205
    fn to_bytes(&self) -> Vec<u8> {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   206
        use self::UnsyncedEngineMessage::*;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   207
        match self {
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   208
            TeamControlGained(str) => ems![b'G', str],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   209
            TeamControlLost(str) => ems![b'F', str],
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   210
        }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   211
    }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   212
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   213
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   214
impl UnorderedEngineMessage {
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   215
    fn to_bytes(&self) -> Vec<u8> {
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   216
        use self::UnorderedEngineMessage::*;
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   217
        match self {
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   218
            Ping => em![b'?'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   219
            Pong => em![b'!'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   220
            ChatMessage(str) => ems![b's', str],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   221
            TeamMessage(str) => ems![b'b', str],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   222
            Error(str) => ems![b'E', str],
14260
a239e4243cf9 Start outlining ipc subsystem
unC0Rr
parents: 14259
diff changeset
   223
            Warning(_) => unreachable!(),
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   224
            StopSyncing => unreachable!(),
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   225
            GameOver => em![b'q'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   226
            GameInterrupted => em![b'Q'],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   227
            GameSetupChecksum(str) => ems![b'M', str],
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   228
            PauseToggled => unreachable!(),
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   229
        }
14214
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   230
    }
ab1ef7074e07 Rearrange code
unC0Rr
parents:
diff changeset
   231
}
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   232
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   233
impl ConfigEngineMessage {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   234
    fn to_bytes(&self) -> Vec<u8> {
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   235
        unreachable!()
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   236
    }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   237
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   238
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   239
impl EngineMessage {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   240
    pub const MAX_LEN: u16 = 49215;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   241
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   242
    fn to_unwrapped(&self) -> Vec<u8> {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   243
        use self::EngineMessage::*;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   244
        match self {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   245
            Unknown => unreachable!("you're not supposed to construct such messages"),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   246
            Empty => unreachable!("you're not supposed to construct such messages"),
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   247
            Synced(SyncedEngineMessage::TimeWrap, _) => vec![b'#', 0xff, 0xff],
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   248
            Synced(msg, timestamp) => {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   249
                let mut v = msg.to_bytes();
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   250
                v.push((*timestamp / 256) as u8);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   251
                v.push(*timestamp as u8);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   252
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   253
                v
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   254
            }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   255
            Unsynced(msg) => msg.to_bytes(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   256
            Unordered(msg) => msg.to_bytes(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   257
            Config(msg) => msg.to_bytes(),
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   258
        }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   259
    }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   260
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   261
    pub fn to_bytes(&self) -> Vec<u8> {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   262
        let mut unwrapped = self.to_unwrapped();
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   263
        let mut size = unwrapped.len();
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   264
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   265
        if size > EngineMessage::MAX_LEN as usize - 2 {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   266
            size = EngineMessage::MAX_LEN as usize - 2;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   267
            unwrapped.truncate(size);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   268
        }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   269
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   270
        if size < 64 {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   271
            unwrapped.insert(0, size as u8);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   272
        } else {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   273
            size -= 64;
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   274
            unwrapped.insert(0, (size / 256 + 64) as u8);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   275
            unwrapped.insert(1, size as u8);
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   276
        }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   277
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   278
        unwrapped
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   279
    }
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   280
}
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   281
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   282
#[test]
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   283
fn message_contruction() {
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   284
    assert_eq!(
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   285
        EngineMessage::Synced(SyncedEngineMessage::TimeWrap, 0).to_bytes(),
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   286
        vec![3, b'#', 255, 255]
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   287
    );
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   288
    assert_eq!(
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   289
        EngineMessage::Synced(SyncedEngineMessage::NextTurn, 258).to_bytes(),
14259
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   290
        vec![3, b'N', 1, 2]
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   291
    );
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   292
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   293
    assert_eq!(
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   294
        EngineMessage::Synced(SyncedEngineMessage::Put(-31337, 65538), 0).to_bytes(),
9f4d75fa4a2c Parse and render more messages
unC0Rr
parents: 14219
diff changeset
   295
        vec![9, b'p', 255, 133, 151, 1, 0, 2, 0, 0]
14219
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   296
    );
77b95406631e Implement more of parsing, start implementation of engine messages rendering
unC0Rr
parents: 14216
diff changeset
   297
}