aboutsummaryrefslogtreecommitdiff
path: root/src/data.rs
blob: 77a547255536cc65e0ffd9b5a3447cfaaeaf4bf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Data structures for the game

use async_std::sync::Arc;
use std::{
    collections::BTreeMap,
    sync::atomic::{AtomicBool, AtomicU32, AtomicU16},
};

/// A node is a computer on the network graph
///
/// It's owned by a player, and has some upgrade state, as well as
/// base stats.
pub struct Node {
    /// Each node has a unique ID by which it's addressed
    id: u16,
    /// The current health
    health: AtomicU32,
    /// The max health
    max_health: AtomicU32,
    /// The owner of this node
    owner: Owner,
    /// Upgrade state
    type_: Upgrade,
    /// Number of links on the map
    links: u8,
    /// Active link states
    link_states: Vec<Arc<Link>>,
    /// Input buffer
    buffer: Vec<Packet>,
}

/// A one-to-one link between two nodes
pub struct Link {
    /// This link ID
    id: u16,
    /// Node 1
    a: u16,
    /// Node 2
    b: u16,
    length: u32,
    /// Packets present on this link
    pp: Vec<(Packet, AtomicU32)>,
}

/// A packet going across the network
pub struct Packet {
    /// The packet ID
    id: u16,
    /// Declare this packet to be removed
    dead: AtomicBool,
    /// Each packet is owned by a player
    owner: Arc<Player>,
    /// What type of packet this is
    data_: PacketType,
}

/// A player who's having fun
pub struct Player {
    /// A unique player ID (per match)
    id: u16,
    /// The player name
    name: String,
    /// Player color
    color: Color,
    /// The player's money
    money: AtomicU16,
}

/// Optionally, players can create teams
pub struct Team {
    /// Name of the team
    name: String,
    /// Unified color of the team
    color: Color,
    /// All team members by their ID
    roster: Vec<u16>
}

/// An RGB color without alpha
pub struct Color(u8, u8, u8);

/// Describes ownership state
pub enum Owner {
    /// Nobody owns this
    Neutral,
    /// A player owns this
    Player(Player),
}

/// Encodes upgrade level without numbers
#[derive(Copy, Clone)]
pub enum Level {
    /// 1
    One,
    /// 2
    Two,
    /// 3 (wow)
    Three,
}

/// Describes upgrade state
pub enum Upgrade {
    /// A basic node
    Base,
    /// Battle (attack/defence) nodes
    Guard(Level),
    /// These nodes make money
    Compute(Level),
    /// Good at packet switching
    Relay(Level),
}

/// Possible types of packets
pub enum PacketType {
    /// A keepalive packet
    ///
    /// These are sent by all nodes if their neighbours are either
    /// friendly or neutral, and used to keep the network alive.
    /// Sending them costs nothing, and when they are received, they
    /// yield a small amount of funds, and restoring health of a node.
    Ping,
    /// A non exploit capture
    ///
    /// This is a packet that can be sent out by any node (except a
    /// switch) to claim a neutral node on the network.  The path to
    /// the node needs to consist only of friendlies, and if an enemy
    /// node processes this type, nothing happens.
    Capture,
    /// A compute packet
    ///
    /// The first value is the target compute value, which each
    /// compute node adds on to.  The second value is the current.  If
    /// a compute packet passes through a compromised or enemy node,
    /// it might subtract from the second value, before palling it on.
    Compute {
        max: u16,
        curr: AtomicU16,
        step: u16,
    },
    /// A special wrapper packet generated by guards
    ///
    /// Sometimes, when a hostily attack packet encounters a guard, it
    /// manages to capture the attack, and forwards it to a random
    /// compute node.  If the node manages to handle the packet,
    /// without it getting dropped in the meantime), it yields a
    /// specified reward, like a computation would.
    Payload {
        inner: Box<PacketType>,
        reward: u16
    },
    /// A reset attack packet
    ///
    /// When encountering a hostile node, it will make that node drop
    /// all packets in it's buffers.
    Reset,
    /// Cross-node-scripting attack
    ///
    /// Decreases the strength of a node, also having a small chance
    /// of spawning a new packet into a random output buffer.  When
    /// applied to a neutral node, it makes capturing nodes go faster.
    CNS,
    /// Node-in-the-middle attack
    ///
    /// Infect the routing behaviour of a node to route all traffic to
    /// a specified enemy node instead
    Nitm,
    /// Virus infection attack
    ///
    /// Infects a node to capture it's earnings, both active and
    /// passive, for a short time, without taking on it's costs.
    Virus,
    /// A total control exploit
    ///
    /// This is very hard to do, and a node will basically always
    /// resist it, but if successful, transforms the node into a guard
    /// node and yields control to the attackinng player.
    TakeOver,
}