TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
harass.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 "module.h"
11 #include "task.h"
12 
13 namespace cherrypi {
14 
15 class MasterHarassTask;
16 class HarassTask;
17 class BuildingHarassTask;
18 
19 /**
20  * Scouts the enemy's base with a worker.
21  * Harasses the enemy while scouting.
22  */
23 class HarassModule : public Module {
24  public:
25  // for gas steal, only build and not cancel
26  // is implemented, deactivated by default
27  struct BuildPolicy {
28  BuildPolicy() : BuildPolicy(false, false) {}
29  BuildPolicy(bool br, bool cr) : buildRefinery(br), cancelRefinery(cr) {}
32  };
33 
34  // harassment in itself
35  // attack worker is deactivated by default because
36  // not robust against bots' worker defence
37  struct TargetPolicy {
38  TargetPolicy() : TargetPolicy(true, false) {}
39  TargetPolicy(bool asd, bool aw)
40  : attackResourceDepot(asd), attackWorkers(aw) {}
43  };
44 
45  // what to do if oponent responds
46  // TODO: come back to base -- needs smartMove for far away target
47  struct FleePolicy {
48  FleePolicy() : FleePolicy(true) {}
49  FleePolicy(bool tag) : turnAroundGeyser(tag) {}
51  };
52 
53  virtual ~HarassModule() = default;
54 
55  virtual void step(State* s) override;
56 
57  // visitor-/double dispath- style to keep behavior in module and data in tasks
58  void postCommand(State* state, MasterHarassTask* htask) {
59  throw std::runtime_error("HarassModule: calling postCommand on base class");
60  }
61  void postCommand(State* state, HarassTask* htask);
62  void postCommand(State* state, BuildingHarassTask* htask);
63 
65  targetPolicy_ = pol;
66  }
68  buildPolicy_ = pol;
69  }
71  fleePolicy_ = pol;
72  }
73 
74  protected:
75  bool attack(State* state, HarassTask* htask);
76  bool attackResourceDepot(State* state, HarassTask* htask);
77  bool attackWorkers(State* state, HarassTask* htask);
78  bool dangerousAttack(State* state, HarassTask* task);
79  bool buildRefinery(State* state, HarassTask* task);
80  bool flee(State* state, HarassTask* task);
81  bool exploreEnemyBase(State* state, HarassTask* task);
82  std::vector<Position> getFleePositions(State* state, HarassTask* task);
83 
84  // basic task/upc management
85  void consumeUPC(State* state, UpcId upcId, std::shared_ptr<UPCTuple> upc);
86  std::shared_ptr<MasterHarassTask>
87  createTask(State*, UpcId, std::shared_ptr<UPCTuple>);
88 
89  // policies
93 
94  Unit* enemyGeyser(Position const& pos); // assumes a single enemy geyser...
95  Unit* enemyRefinery(Position const& pos);
96  void findClosestGeyser(State* state, Position const& nmyLoc);
97  void checkEnemyRefineryBuilt(State* state, Position const& nmyLoc);
98 
99  std::unordered_map<Position, Unit*> enemyGeyser_;
100  std::unordered_map<Position, Unit*> enemyRefinery_;
101 };
102 
103 } // namespace cherrypi
Game state.
Definition: state.h:42
Scouts the enemy&#39;s base with a worker.
Definition: harass.h:23
Unit * enemyGeyser(Position const &pos)
Definition: harass.cpp:567
std::vector< Position > getFleePositions(State *state, HarassTask *task)
Definition: harass.cpp:583
BuildPolicy()
Definition: harass.h:28
Definition: harass.h:47
void setBuildPolicy(BuildPolicy pol)
Definition: harass.h:67
Definition: harass.cpp:59
Definition: harass.cpp:86
bool attackWorkers(State *state, HarassTask *htask)
Definition: harass.cpp:373
std::unordered_map< Position, Unit * > enemyGeyser_
Definition: harass.h:99
bool turnAroundGeyser
Definition: harass.h:50
bool attackWorkers
Definition: harass.h:42
bool attack(State *state, HarassTask *htask)
Definition: harass.cpp:346
std::shared_ptr< MasterHarassTask > createTask(State *, UpcId, std::shared_ptr< UPCTuple >)
Definition: harass.cpp:533
bool cancelRefinery
Definition: harass.h:31
bool buildRefinery
Definition: harass.h:30
virtual void step(State *s) override
Definition: harass.cpp:270
FleePolicy(bool tag)
Definition: harass.h:49
Represents a unit in the game.
Definition: unitsinfo.h:35
Unit * enemyRefinery(Position const &pos)
Definition: harass.cpp:575
FleePolicy fleePolicy_
Definition: harass.h:92
void checkEnemyRefineryBuilt(State *state, Position const &nmyLoc)
Definition: harass.cpp:643
bool dangerousAttack(State *state, HarassTask *task)
Definition: harass.cpp:503
bool flee(State *state, HarassTask *task)
Definition: harass.cpp:410
bool attackResourceDepot
Definition: harass.h:41
TargetPolicy(bool asd, bool aw)
Definition: harass.h:39
TargetPolicy targetPolicy_
Definition: harass.h:90
std::unordered_map< Position, Unit * > enemyRefinery_
Definition: harass.h:100
TargetPolicy()
Definition: harass.h:38
bool attackResourceDepot(State *state, HarassTask *htask)
Definition: harass.cpp:356
Definition: harass.cpp:23
virtual ~HarassModule()=default
bool exploreEnemyBase(State *state, HarassTask *task)
Definition: harass.cpp:456
Main namespace for bot-related code.
Definition: areainfo.cpp:17
BuildPolicy(bool br, bool cr)
Definition: harass.h:29
void findClosestGeyser(State *state, Position const &nmyLoc)
Definition: harass.cpp:608
BuildPolicy buildPolicy_
Definition: harass.h:91
void consumeUPC(State *state, UpcId upcId, std::shared_ptr< UPCTuple > upc)
Definition: harass.cpp:509
Definition: basetypes.h:41
void setTargetPolicy(TargetPolicy pol)
Definition: harass.h:64
int UpcId
Definition: basetypes.h:23
void setFleePolicy(FleePolicy pol)
Definition: harass.h:70
Interface for bot modules.
Definition: module.h:30
void postCommand(State *state, MasterHarassTask *htask)
Definition: harass.h:58
FleePolicy()
Definition: harass.h:48