TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
jitter.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include "cherrypi.h"
11 
12 namespace cherrypi {
13 
14 struct Unit;
15 class State;
16 
17 class BaseJitter {
18  public:
20  virtual ~BaseJitter() = default;
21 
22  virtual Position operator()(Unit* u) const = 0;
23 };
24 
25 class NoJitter : public BaseJitter {
26  public:
27  NoJitter(){};
28  Position operator()(Unit* u) const override;
29 };
30 
31 /**
32  * When featurizing units, we represent each 2D cell as having one unit.
33  * Of course, StarCraft isn't so neat and tidy. Multiple units can be stacked on
34  * one location; sometimes ground units, but frequently air units as well.
35  *
36  * In order to featurize units on a 2D grid, we apply jitter to shake those
37  * units out into a one-to-one cell-to-(unit or no unit) mapping. Units get
38  * moved into nearby cells for featurization.
39  *
40  * This jitter class treats all units indiscriminately. If allowSameType is
41  * true, then we allow units of the same type to be on the same tile (no matter
42  * if they are jittered or not).
43  *
44  * Warning: This will not behave as expected for tanks since sieged and unsieged
45  * are two different units. However, stacked tanks should be almost impossible
46  * in normal situations.
47  *
48  * Note that neutral units will always be ignored.
49  */
50 class Jitter : public BaseJitter {
51  public:
52  Jitter(State* st, Rect const& crop, bool allowSameType);
53  Position operator()(Unit* u) const override;
54 
55  protected:
56  Jitter(){};
57 
58  void fillJitter(
59  State* st,
60  Rect const& crop,
61  std::function<bool(Unit*, Unit*)> const& compatible);
62 
63  std::unordered_map<Unit*, Position> jitteredPos_;
64 };
65 
66 /**
67  * This jitter class treats all units depending on their height: flying, on
68  * the ground or under ground (burrowed). For example, we make sure that each
69  * flying unit is on a separate tile but a flying unit can be on the same tile
70  * as a ground unit.
71  *
72  * Note that neutral units will always be ignored
73  */
74 class LayeredJitter : public Jitter {
75  public:
77  State* st,
78  Rect const& crop,
79  bool allowSameTypeAir = true,
80  bool allowSameTypeGround = false);
81 };
82 
83 } // namespace cherrypi
Definition: jitter.h:17
Game state.
Definition: state.h:42
This jitter class treats all units depending on their height: flying, on the ground or under ground (...
Definition: jitter.h:74
Definition: jitter.h:25
When featurizing units, we represent each 2D cell as having one unit.
Definition: jitter.h:50
Jitter()
Definition: jitter.h:56
replayer::Unit Unit
Definition: state.h:36
virtual ~BaseJitter()=default
Represents a unit in the game.
Definition: unitsinfo.h:35
std::unordered_map< Unit *, Position > jitteredPos_
Definition: jitter.h:63
NoJitter()
Definition: jitter.h:27
BaseJitter()
Definition: jitter.h:19
Main namespace for bot-related code.
Definition: areainfo.cpp:17
virtual Position operator()(Unit *u) const =0