Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
/*
   Copyright (c) 2017 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software
   and associated documentation files (the "Software"), to deal in the Software
   without restriction,
   including without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
   IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

#ifndef _WAR_SCENE_AI_ACTION_LIST_H_
#define _WAR_SCENE_AI_ACTION_LIST_H_

#include "AI/ActionInterface/Headers/AITeam.h"
#include "AI/ActionInterface/Headers/AIProcessor.h"
#include "Scenes/WarScene/AESOPActions/Headers/WarSceneActions.h"
#include <fstream>

#ifndef PRINT_AI_TO_FILE
#define PRINT_AI_TO_FILE
#include "Core/Headers/Console.h"
#endif

namespace Divide {
class Unit;

namespace AI {

enum class UnitAttributes : U32 {
    HEALTH_POINTS = 0,
    DAMAGE = 1,
    ALIVE_FLAG = 2,
    COUNT
};

enum class AIMsg : U32 { 
    HAVE_FLAG = 0,
    RETURNED_FLAG = 1,
    ENEMY_HAS_FLAG = 2,
    HAVE_SCORED = 3,
    ATTACK = 4,
    HAVE_DIED = 5,
    COUNT
};

enum class FactType : U32 {
    POSITION = 0,
    COUNTER_SMALL = 1,
    COUNTER_MEDIUM = 2,
    COUNTER_LARGE = 3,
    TOGGLE_STATE = 4,
    AI_NODE = 5,
    SGN_NODE = 6,
    COUNT
};

template <typename T, FactType F>
class WorkingMemoryFact {
   public:
    WorkingMemoryFact()
    {
        _value = T();
        _type = F;
        _belief = 0.0f;
    }

    inline void value(const T& val) {
        _value = val;
        belief(1.0f);
    }

    inline void belief(F32 belief) { _belief = belief; }

    inline const T& value() const { return _value; }
    inline FactType type() const { return _type; }
    inline F32 belief() const { return _belief; }

   protected:
    T _value;
    F32 _belief;
    FactType _type;
};

typedef WorkingMemoryFact<AIEntity*, FactType::AI_NODE> AINodeFact;
typedef WorkingMemoryFact<SceneGraphNode_wptr, FactType::SGN_NODE> SGNNodeFact;
typedef WorkingMemoryFact<vec3<F32>, FactType::POSITION> PositionFact;
typedef WorkingMemoryFact<U8, FactType::COUNTER_SMALL> SmallCounterFact;
typedef WorkingMemoryFact<U16, FactType::COUNTER_MEDIUM> MediumCounterFact;
typedef WorkingMemoryFact<U32, FactType::COUNTER_LARGE> LargeCounterFact;
typedef WorkingMemoryFact<bool, FactType::TOGGLE_STATE> ToggleStateFact;

class GlobalWorkingMemory {
public:
    GlobalWorkingMemory()
    {
        _flags[0].value(SceneGraphNode_wptr());
        _flags[1].value(SceneGraphNode_wptr());
        _flagCarriers[0].value(nullptr);
        _flagCarriers[1].value(nullptr);
        _flagsAtBase[0].value(true);
        _flagsAtBase[1].value(true);
    }

    SGNNodeFact _flags[2];
    AINodeFact  _flagCarriers[2];
    SmallCounterFact _score[2];
    SmallCounterFact _teamAliveCount[2];
    SmallCounterFact _flagRetrieverCount[2];
    PositionFact _teamFlagPosition[2];
    ToggleStateFact _flagsAtBase[2];
};

class LocalWorkingMemory {
   public:
    LocalWorkingMemory()
    {
        _hasEnemyFlag.value(false);
        _enemyHasFlag.value(false);
       _isFlagRetriever.value(false);
       _currentTarget.value(SceneGraphNode_wptr());
    }

    SGNNodeFact _currentTarget;
    ToggleStateFact _hasEnemyFlag;
    ToggleStateFact _enemyHasFlag;
    ToggleStateFact _isFlagRetriever;
};

class WarSceneOrder : public Order {
   public:
    enum class WarOrder : U32 {
        IDLE = 0,
        CAPTURE_ENEMY_FLAG = 1,
        SCORE_ENEMY_FLAG = 2,
        KILL_ENEMY = 3,
        PROTECT_FLAG_CARRIER = 4,
        RECOVER_FLAG = 5,
        COUNT
    };

    WarSceneOrder(WarOrder order) : Order(to_uint(order))
    {
    }

    void evaluatePriority();
    void lock() { Order::lock(); }
    void unlock() { Order::unlock(); }
};

struct GOAPPackage {
    GOAPWorldState _worldState;
    GOAPGoalList _goalList;
    vectorImpl<WarSceneAction> _actionSet;
};

namespace Attorney {
    class WarAISceneWarAction;
};

class WarSceneAIProcessor : public AIProcessor {
    friend class Attorney::WarAISceneWarAction;
   public:
       typedef hashMapImpl<I64, AIEntity*> NodeToUnitMap;
   public:
       enum class AIType {
           ANIMAL = 0,
           LIGHT = 1,
           HEAVY = 2,
           COUNT
       };
    WarSceneAIProcessor(AIType type, AIManager& parentManager);
    ~WarSceneAIProcessor();

    void registerGOAPPackage(const GOAPPackage& package);

    bool processData(const U64 deltaTime);
    bool processInput(const U64 deltaTime);
    bool update(const U64 deltaTime, NPC* unitRef = nullptr);
    void processMessage(AIEntity& sender, AIMsg msg, const cdiggins::any& msg_content);

    static void registerFlags(SceneGraphNode_wptr flag1, SceneGraphNode_wptr flag2) {
        _globalWorkingMemory._flags[0].value(flag1);
        _globalWorkingMemory._flags[1].value(flag2);
    }

    static void registerScoreCallback(const DELEGATE_CBK<void, U8, const stringImpl&>& cbk) {
        _scoreCallback = cbk;
    }

    static void registerMessageCallback(const DELEGATE_CBK<void, U8, const stringImpl&>& cbk) {
        _messageCallback = cbk;
    }

    static U8 getScore(U16 teamID) {
        return _globalWorkingMemory._score[teamID].value();
    }

    static void reset();
    static void incrementScore(U16 teamID) {
        _globalWorkingMemory._score[teamID].value(getScore(teamID) + 1);
    }

    static void resetScore(U8 teamID) {
        _globalWorkingMemory._score[teamID].value(0);
    }
   protected:
    bool preAction(ActionType type, const WarSceneAction* warAction);
    bool postAction(ActionType type, const WarSceneAction* warAction);
    bool checkCurrentActionComplete(const GOAPAction& planStep);
    void invalidateCurrentPlan();
    stringImpl toString(bool state = false) const;

   private:
    bool DIE();
    void requestOrders();
    void updatePositions();
    bool performAction(const GOAPAction& planStep);
    bool performActionStep(GOAPAction::operationsIterator step);
    const std::string& printActionStats(const GOAPAction& planStep) const;
    void printWorkingMemory();
    void initInternal();
    void beginPlan(const GOAPGoal& currentGoal);
    AIEntity* getUnitForNode(U32 teamID, SceneGraphNode_wptr node) const;

    template <typename... T>
    void PRINT(const char* format, T&&... args) const;

    bool atHomeBase() const;
    bool nearOwnFlag() const;
    bool nearEnemyFlag() const;

  private:
    AIType _type;
    U16 _tickCount;
    U64 _deltaTime;
    U8 _visualSensorUpdateCounter;
    U64 _attackTimer;
    stringImpl _planStatus;
    stringImpl _tempString;
    VisualSensor* _visualSensor;
    AudioSensor* _audioSensor;
    LocalWorkingMemory _localWorkingMemory;
    /// Keep this in memory at this level
    vectorImpl<WarSceneAction> _actionList;
    NodeToUnitMap _nodeToUnitMap[2];
    std::array<bool, to_const_uint(ActionType::COUNT)> _actionState;
    static DELEGATE_CBK<void, U8, const stringImpl&> _scoreCallback;
    static DELEGATE_CBK<void, U8, const stringImpl&> _messageCallback;
    static GlobalWorkingMemory _globalWorkingMemory;
    static vec3<F32> _initialFlagPositions[2];

#if defined(PRINT_AI_TO_FILE)
    mutable std::ofstream _WarAIOutputStream;
#endif
};

template <typename... Args>
void WarSceneAIProcessor::PRINT(const char* format, Args&&... args) const {
    #if defined(PRINT_AI_TO_FILE)
    Console::d_printfn(_WarAIOutputStream, format, std::forward<Args>(args)...);
    #endif
}

namespace Attorney {
class WarAISceneWarAction {
   private:
    static bool preAction(WarSceneAIProcessor& aiProcessor, ActionType type,
                          const Divide::AI::WarSceneAction* warAction) {
        return aiProcessor.preAction(type, warAction);
    }
    static bool postAction(WarSceneAIProcessor& aiProcessor, ActionType type,
                           const Divide::AI::WarSceneAction* warAction) {
        return aiProcessor.postAction(type, warAction);
    }

    friend class Divide::AI::WarSceneAction;
};

};  // namespace Attorney
};  // namespace AI
};  // namespace Divide

#endif

Commits for Divide-Framework/trunk/Source Code/Scenes/WarScene/Headers/WarSceneAIProcessor.h

Diff revisions: vs.
Revision Author Commited Message
842 Diff Diff IonutCava picture IonutCava Wed 01 Feb, 2017 17:25:15 +0000

[IonutCava]
- Start to implement scripting support via ChaiScript: http://chaiscript.com/
- Cleanup DELEGATE_CBK alias

836 Diff Diff IonutCava picture IonutCava Fri 27 Jan, 2017 14:59:56 +0000

[IonutCava]
- Update copyright notice

815 Diff Diff IonutCava picture IonutCava Thu 12 Jan, 2017 23:33:01 +0000

[IonutCava]
- Fixed viewport alignment issue
- Added per-player RenderTargets (added index support to GFXRTPool)
- Initial implementation of per-player cameras
— Removed defaultCamera
— Scenes now use a baseCamera as a template for new player cameras

711 Diff Diff IonutCava picture IonutCava Mon 23 May, 2016 14:31:55 +0000

[IonutCava]
- More scene change fixes: pending tasks, GUI elements, etc
- Singleton removal: AIManager is a per-scene member instead of a global singleton now

671 Diff Diff IonutCava picture IonutCava Fri 08 Apr, 2016 16:26:49 +0000

[Ionut]
- Custom memory allocation system part 1:
— Add http://www.codeproject.com/Articles/1089905/A-Custom-STL-std-allocator-Replacement-Improves-Pe
— Add custom allocators to strings and vectors
— Add custom allocators to a few test classes
— No support for aligned memory yet, so vec4<F32> and mat4<F32> vectors use the old allocation system
- Replace more std::async call with our custom task system

658 Diff Diff IonutCava picture IonutCava Tue 15 Mar, 2016 21:51:58 +0000

[IonutCava]
- Unify variable argument string formatting (GUI now uses the Util::StringFormat method)
— Console does not use the general purpose formatting for performance reasons (uses internally statically allocated buffer instead)
- Re-enabled allocation tracking in debug builds
— Added (and enabled) option to log all memory allocations, not just leaks.
- Added a macro that returns the current function signature (on all platforms)
- Fixed minor 3 memory leaks

648 Diff Diff IonutCava picture IonutCava Sun 21 Feb, 2016 16:32:52 +0000

[IonutCava]
- Update copyright notice
- Move BoundingBox and BoundingSphere to a new BoundsComponent
— Add a temp hack in SceneGraphNode to update these

524 Diff Diff IonutCava picture IonutCava Thu 08 Oct, 2015 16:14:58 +0000

[IonutCava]
- Profiler guided optimizations:
— Replace most hashMaps with vectors where the common access pattern is linear (e.g. SceneGraphNode’s child list)
— Specify ShaderBuffer access patters at creation time to properly inform the rendering API
— Fix and optimise the LockManager & BufferLockManager classes
— Add a AtomicWrapper class that allows assignment and copy-constructions for atomic variables
-— This allows us to use vector of atomic bools for BB creation for SkinnedSubMesh objects

495 Diff Diff IonutCava picture IonutCava Sun 16 Aug, 2015 16:37:59 +0000

[IonutCava]
- GUI multi-line text display fixes
- AI updates

492 IonutCava picture IonutCava Fri 14 Aug, 2015 14:50:53 +0000

[Ionut]
- WarScene multi-game support (untested)
- Fixed particle rendering by disabling Z-writes and changing Z-compare to LEQUAL