TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
resourceworkers.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 "unitsinfo.h"
11 
12 #include <map>
13 #include <set>
14 
15 namespace cherrypi {
16 
17 /**
18  * Data structure for storing the state of our gathering assignments.
19  * Enforces the bidirectional mapping of (Worker -> Resource)
20  * and (Resource -> Set<Workers>)
21  *
22  * Iterable as a C++ Range over (Resource -> Set<Workers>)
23  */
25  public:
26  void assignWorker(Unit* worker, Unit* resource);
27  void unassignWorker(Unit* worker);
28  void includeResource(Unit* resource);
29  bool excludeResource(Unit* resource);
30  bool containsResource(Unit* resource);
31  Unit* getResource(Unit* worker) const;
32  size_t countWorkers(Unit* resource) const;
33 
34  /// Start iterator for worker assignments; allows treatment as a C++ Range
35  auto begin() {
36  return workersByResource.begin();
37  }
38  /// End iterator for worker assignments; allows treatment as a C++ Range
39  auto end() {
40  return workersByResource.end();
41  }
42  /// For how many resources is gathering enabled?
43  size_t size() {
44  return workersByResource.size();
45  }
46 
47  protected:
48  std::unordered_map<Unit*, Unit*> resourceByWorker;
49  std::unordered_map<Unit*, std::unordered_set<Unit*>> workersByResource;
50 };
51 } // namespace cherrypi
std::unordered_map< Unit *, std::unordered_set< Unit * > > workersByResource
Definition: resourceworkers.h:49
bool containsResource(Unit *resource)
Is this resource currently included in gathering?
Definition: resourceworkers.cpp:48
Data structure for storing the state of our gathering assignments.
Definition: resourceworkers.h:24
bool excludeResource(Unit *resource)
Disallow gathering from a resource.
Definition: resourceworkers.cpp:53
size_t size()
For how many resources is gathering enabled?
Definition: resourceworkers.h:43
Represents a unit in the game.
Definition: unitsinfo.h:35
std::unordered_map< Unit *, Unit * > resourceByWorker
Definition: resourceworkers.h:48
auto end()
End iterator for worker assignments; allows treatment as a C++ Range.
Definition: resourceworkers.h:39
Unit * getResource(Unit *worker) const
To which resource (if any) is this worker assigned?
Definition: resourceworkers.cpp:66
void unassignWorker(Unit *worker)
Remove a worker from gathering and any resources it might be assigned to.
Definition: resourceworkers.cpp:30
size_t countWorkers(Unit *resource) const
How many workers are assigned to this resource?
Definition: resourceworkers.cpp:72
Main namespace for bot-related code.
Definition: areainfo.cpp:17
void assignWorker(Unit *worker, Unit *resource)
Assign a worker to gathering a specific resource.
Definition: resourceworkers.cpp:16
void includeResource(Unit *resource)
Allow gathering from a resource.
Definition: resourceworkers.cpp:41
auto begin()
Start iterator for worker assignments; allows treatment as a C++ Range.
Definition: resourceworkers.h:35