sc2 0.3.3

Rust implementation of the StarCraft II Client API
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use std::rc::Rc;

use sc2_proto::{debug, raw, sc2api};
// use sc2_proto::spatial::{
//     ActionSpatialCameraMove,
//     ActionSpatialUnitCommand,
//     ActionSpatialUnitSelectionPoint,
//     ActionSpatialUnitSelectionPoint_Type as ProtoPointSelectionType,
//     ActionSpatialUnitSelectionRect,
// };

use super::super::{FromProto, IntoProto, Result};
use data::{Ability, Color, Point2, Point3, Tag, Unit};

/// action target
#[derive(Debug, Copy, Clone)]
pub enum ActionTarget {
    /// target a unit with this action
    Unit(Tag),
    /// target a location with this action
    Location(Point2),
}

/// an action (command or ability) applied to a unit or set of units
#[derive(Debug, Clone)]
pub struct Action {
    /// the ability to invoke
    ability: Ability,
    units: Vec<Tag>,
    target: Option<ActionTarget>,
}

impl Action {
    /// perform the given ability
    pub fn new(ability: Ability) -> Self {
        Self {
            ability: ability,
            units: vec![],
            target: None,
        }
    }

    /// units that this action applies to
    ///
    /// take the tags from an arbitrary iterator of units
    pub fn units<'a, T>(self, units: T) -> Self
    where
        T: Iterator<Item = &'a Rc<Unit>>,
    {
        Self {
            units: units.map(|u| u.get_tag()).collect(),
            ..self
        }
    }

    /// units that this action applies to
    ///
    /// directly assign the unit tags
    pub fn unit_tags(self, units: Vec<Tag>) -> Self {
        Self {
            units: units,
            ..self
        }
    }

    /// set the target of the action
    pub fn target(self, target: ActionTarget) -> Self {
        Self {
            target: Some(target),
            ..self
        }
    }
}

impl FromProto<raw::ActionRawUnitCommand> for Action {
    /// convert from protobuf data
    fn from_proto(action: raw::ActionRawUnitCommand) -> Result<Self> {
        Ok(Self {
            ability: Ability::from_proto(action.get_ability_id() as u32)?,
            units: {
                let mut unit_tags = vec![];

                for tag in action.get_unit_tags() {
                    unit_tags.push(*tag);
                }

                unit_tags
            },
            target: {
                if action.has_target_unit_tag() {
                    Some(ActionTarget::Unit(action.get_target_unit_tag()))
                } else if action.has_target_world_space_pos() {
                    let pos = action.get_target_world_space_pos();
                    Some(ActionTarget::Location(Point2::new(
                        pos.get_x(),
                        pos.get_y(),
                    )))
                } else {
                    None
                }
            },
        })
    }
}

impl IntoProto<sc2api::Action> for Action {
    fn into_proto(self) -> Result<sc2api::Action> {
        let mut action = sc2api::Action::new();
        {
            let cmd = action.mut_action_raw().mut_unit_command();

            cmd.set_ability_id(self.ability.into_proto()? as i32);

            match self.target {
                Some(ActionTarget::Unit(tag)) => {
                    cmd.set_target_unit_tag(tag);
                },
                Some(ActionTarget::Location(pos)) => {
                    let target = cmd.mut_target_world_space_pos();
                    target.set_x(pos.x);
                    target.set_y(pos.y);
                },
                None => (),
            }

            for tag in self.units {
                cmd.mut_unit_tags().push(tag);
            }
        }

        Ok(action)
    }
}

/// target for debugging text
#[derive(Debug, Copy, Clone)]
pub enum DebugTextTarget {
    /// screen coordinates for debug text
    Screen(Point2),
    /// world coordinates for debug text
    World(Point3),
}

/// debug text
#[derive(Debug, Clone)]
pub struct DebugText {
    text: String,
    target: Option<DebugTextTarget>,
    color: Color,
}

impl DebugText {
    /// text to display
    pub fn new(text: String) -> Self {
        Self {
            text: text,
            target: None,
            color: (0xFF, 0xFF, 0xFF),
        }
    }

    /// target in screen or world space (default is None)
    ///
    /// if the target is None, then text appears at top-left of screen.
    pub fn target(self, target: DebugTextTarget) -> Self {
        Self {
            target: Some(target),
            ..self
        }
    }

    /// set the color of the text (default is white)
    pub fn color(self, color: Color) -> Self {
        Self {
            color: color,
            ..self
        }
    }
}

/// a debug line defined by a start and end point
#[derive(Debug, Copy, Clone)]
pub struct DebugLine {
    /// point 1 of the line
    p1: Point3,
    /// point 2 of the line
    p2: Point3,
    /// color of the line
    color: Color,
}

impl DebugLine {
    /// create a line from p1 to p2
    pub fn new(p1: Point3, p2: Point3) -> Self {
        Self {
            p1: p1,
            p2: p2,
            color: (0xFF, 0xFF, 0xFF),
        }
    }

    /// set the color of the line (default is white)
    pub fn color(self, color: Color) -> Self {
        Self {
            color: color,
            ..self
        }
    }
}

/// a debug axis-aligned bounding box defined by two corners
#[derive(Debug, Copy, Clone)]
pub struct DebugAabb {
    /// minimum corner of the box
    min: Point3,
    /// maximum corner of the box
    max: Point3,
    /// color of the box
    color: Color,
}

impl DebugAabb {
    /// create an AABB
    pub fn new(min: Point3, max: Point3) -> Self {
        Self {
            min: min,
            max: max,
            color: (0xFF, 0xFF, 0xFF),
        }
    }

    /// set the color of the box (default is white)
    pub fn color(self, color: Color) -> Self {
        Self {
            color: color,
            ..self
        }
    }
}

/// a debug sphere defined by a point in world space and a radius
#[derive(Debug, Copy, Clone)]
pub struct DebugSphere {
    /// center of the sphere
    center: Point3,
    /// radius of the sphere
    radius: f32,
    /// color of the sphere
    color: Color,
}

impl DebugSphere {
    /// create a debug sphere
    pub fn new(center: Point3, radius: f32) -> Self {
        Self {
            center: center,
            radius: radius,
            color: (0xFF, 0xFF, 0xFF),
        }
    }

    /// set the color of the sphere (default is white)
    pub fn color(self, color: Color) -> Self {
        Self {
            color: color,
            ..self
        }
    }
}

/// a debug command for the game
#[derive(Debug, Clone)]
pub enum DebugCommand {
    /// shows debug text in the game instance
    Text(DebugText),
    /// shows a debug line in the game from p1 to p2
    Line(DebugLine),
    /// shows a debug axis-aligned bounding box in the game
    Aabb(DebugAabb),
    /// shows a debug sphere in the game
    Sphere(DebugSphere),
}

impl From<DebugText> for DebugCommand {
    fn from(text: DebugText) -> Self {
        DebugCommand::Text(text)
    }
}

impl From<DebugLine> for DebugCommand {
    fn from(line: DebugLine) -> Self {
        DebugCommand::Line(line)
    }
}

impl From<DebugAabb> for DebugCommand {
    fn from(aabb: DebugAabb) -> Self {
        DebugCommand::Aabb(aabb)
    }
}

impl From<DebugSphere> for DebugCommand {
    fn from(sphere: DebugSphere) -> Self {
        DebugCommand::Sphere(sphere)
    }
}

impl IntoProto<debug::DebugCommand> for DebugCommand {
    fn into_proto(self) -> Result<debug::DebugCommand> {
        match self {
            DebugCommand::Text(DebugText {
                text,
                target,
                color,
            }) => {
                let mut cmd = debug::DebugCommand::new();
                let mut debug_text = debug::DebugText::new();

                debug_text.set_text(text);

                match target {
                    Some(DebugTextTarget::Screen(p)) => {
                        debug_text.mut_virtual_pos().set_x(p.x);
                        debug_text.mut_virtual_pos().set_y(p.y);
                    },
                    Some(DebugTextTarget::World(p)) => {
                        debug_text.mut_world_pos().set_x(p.x);
                        debug_text.mut_world_pos().set_y(p.y);
                        debug_text.mut_world_pos().set_z(p.z);
                    },
                    None => (),
                }

                debug_text.mut_color().set_r(color.0 as u32);
                debug_text.mut_color().set_g(color.1 as u32);
                debug_text.mut_color().set_b(color.2 as u32);

                cmd.mut_draw().mut_text().push(debug_text);

                Ok(cmd)
            },
            DebugCommand::Line(DebugLine { p1, p2, color }) => {
                let mut cmd = debug::DebugCommand::new();
                let mut debug_line = debug::DebugLine::new();

                debug_line.mut_line().mut_p0().set_x(p1.x);
                debug_line.mut_line().mut_p0().set_y(p1.y);
                debug_line.mut_line().mut_p0().set_z(p1.z);

                debug_line.mut_line().mut_p1().set_x(p2.x);
                debug_line.mut_line().mut_p1().set_y(p2.y);
                debug_line.mut_line().mut_p1().set_z(p2.z);

                debug_line.mut_color().set_r(color.0 as u32);
                debug_line.mut_color().set_g(color.1 as u32);
                debug_line.mut_color().set_b(color.2 as u32);

                cmd.mut_draw().mut_lines().push(debug_line);

                Ok(cmd)
            },
            DebugCommand::Aabb(DebugAabb { min, max, color }) => {
                let mut cmd = debug::DebugCommand::new();
                let mut debug_box = debug::DebugBox::new();

                debug_box.mut_min().set_x(min.x);
                debug_box.mut_min().set_y(min.y);
                debug_box.mut_min().set_z(min.z);

                debug_box.mut_max().set_x(max.x);
                debug_box.mut_max().set_y(max.y);
                debug_box.mut_max().set_z(max.z);

                debug_box.mut_color().set_r(color.0 as u32);
                debug_box.mut_color().set_g(color.1 as u32);
                debug_box.mut_color().set_b(color.2 as u32);

                cmd.mut_draw().mut_boxes().push(debug_box);

                Ok(cmd)
            },
            DebugCommand::Sphere(DebugSphere {
                center,
                radius,
                color,
            }) => {
                let mut cmd = debug::DebugCommand::new();
                let mut debug_sphere = debug::DebugSphere::new();

                debug_sphere.mut_p().set_x(center.x);
                debug_sphere.mut_p().set_y(center.y);
                debug_sphere.mut_p().set_z(center.z);

                debug_sphere.set_r(radius);

                debug_sphere.mut_color().set_r(color.0 as u32);
                debug_sphere.mut_color().set_g(color.1 as u32);
                debug_sphere.mut_color().set_b(color.2 as u32);

                cmd.mut_draw().mut_spheres().push(debug_sphere);

                Ok(cmd)
            },
        }
    }
}

// /// target of a feature layer command
// #[derive(Debug, Copy, Clone)]
// pub enum SpatialUnitCommandTarget {
//     /// screen coordinate target
//     Screen(Point2I),
//     /// minimap coordinate target
//     Minimap(Point2I),
// }

// /// type of point selection
// #[derive(Debug, Copy, Clone, Eq, PartialEq)]
// pub enum PointSelectType {
//     /// changes selection to unit (equal to normal click)
//     Select,
//     /// toggle selection of unit (equal to shift+click)
//     Toggle,
//     /// select all units of a given type (equal to ctrl+click)
//     All,
//     /// select all units of a given type additively (equal to
//     /// shift+ctrl+click)
//     AddAll,
// }

// impl FromProto<ProtoPointSelectionType> for PointSelectType {
//     fn from_proto(select_type: ProtoPointSelectionType) -> Result<Self> {
//         Ok(match select_type {
//             ProtoPointSelectionType::Select => PointSelectType::Select,
//             ProtoPointSelectionType::Toggle => PointSelectType::Toggle,
//             ProtoPointSelectionType::AllType => PointSelectType::All,
//             ProtoPointSelectionType::AddAllType => PointSelectType::AddAll,
//         })
//     }
// }

// /// feature layer action
// #[derive(Debug, Clone)]
// pub enum SpatialAction {
//     /// issue a feature layer unit command
//     UnitCommand {
//         /// ability to invoke
//         ability: Ability,
//         /// target of command
//         target: Option<SpatialUnitCommandTarget>,
//         /// whether this action should replace or queue behind other
//         /// actions
//         queued: bool,
//     },
//     /// move the camera to a new location
//     CameraMove {
//         /// minimap location
//         center_minimap: Point2I,
//     },
//     /// select a point on the screen
//     SelectPoint {
//         /// point in screen coordinates
//         select_screen: Point2I,
//         /// point selection type
//         select_type: PointSelectType,
//     },
//     /// select a rectangle on the screen
//     SelectRect {
//         /// rectangle in screen coordinates
//         select_screen: Vec<Rect2I>,
//         /// whether selection is additive
//         select_add: bool,
//     },
// }

// impl FromProto<ActionSpatialUnitCommand> for SpatialAction {
//     fn from_proto(cmd: ActionSpatialUnitCommand) -> Result<Self> {
//         Ok(SpatialAction::UnitCommand {
//             ability: Ability::from_proto(cmd.get_ability_id() as u32)?,
//             queued: cmd.get_queue_command(),
//             target: {
//                 if cmd.has_target_screen_coord() {
//                     let pos = cmd.get_target_screen_coord();
//                     Some(SpatialUnitCommandTarget::Screen(Point2I::new(
//                         pos.get_x(),
//                         pos.get_y(),
//                     )))
//                 } else if cmd.has_target_minimap_coord() {
//                     let pos = cmd.get_target_minimap_coord();
//                     Some(SpatialUnitCommandTarget::Minimap(Point2I::new(
//                         pos.get_x(),
//                         pos.get_y(),
//                     )))
//                 } else {
//                     None
//                 }
//             },
//         })
//     }
// }

// impl FromProto<ActionSpatialCameraMove> for SpatialAction {
//     fn from_proto(cmd: ActionSpatialCameraMove) -> Result<Self> {
//         Ok(SpatialAction::CameraMove {
//             center_minimap: {
//                 let pos = cmd.get_center_minimap();
//                 Point2I::new(pos.get_x(), pos.get_y())
//             },
//         })
//     }
// }

// impl FromProto<ActionSpatialUnitSelectionPoint> for SpatialAction {
//     fn from_proto(cmd: ActionSpatialUnitSelectionPoint) -> Result<Self> {
//         Ok(SpatialAction::SelectPoint {
//             select_screen: {
//                 let pos = cmd.get_selection_screen_coord();
//                 Point2I::new(pos.get_x(), pos.get_y())
//             },
//             select_type: cmd.get_field_type().into_sc2()?,
//         })
//     }
// }

// impl FromProto<ActionSpatialUnitSelectionRect> for SpatialAction {
//     fn from_proto(cmd: ActionSpatialUnitSelectionRect) -> Result<Self> {
//         Ok(SpatialAction::SelectRect {
//             select_screen: {
//                 let mut rects = vec![];

//                 for r in cmd.get_selection_screen_coord() {
//                     rects.push(Rect2I {
//                         from: {
//                             let p = r.get_p0();
//                             Point2I::new(p.get_x(), p.get_y())
//                         },
//                         to: {
//                             let p = r.get_p1();
//                             Point2I::new(p.get_x(), p.get_y())
//                         },
//                     })
//                 }

//                 rects
//             },
//             select_add: cmd.get_selection_add(),
//         })
//     }
// }