summaryrefslogtreecommitdiffstats
path: root/game/code/roads/roadmanager.h
blob: a5b6a837b244b6fcd63316f705e22c08727f6f8a (plain) (blame)
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
//=============================================================================
// Copyright (C) 2002 Radical Entertainment Ltd.  All rights reserved.
//
// File:        roadmanager.h
//
// Description: Blahblahblah
//
// History:     26/06/2002 + Created -- Cary Brisebois (based on TBJ's work)
//
//=============================================================================

#ifndef ROADMANAGER_H
#define ROADMANAGER_H

//========================================
// Nested Includes
//========================================
#include <render/culling/swaparray.h>

//========================================
// Forward References
//========================================
class Road;
class Intersection;
class RoadSegmentData;
class RoadSegment;
class Lane;

class RoadRenderTest;

static const float NEAR_INFINITY = 100000.0f;

//=============================================================================
//
// Synopsis:    Blahblahblah
//
//=============================================================================

class RoadManager
{
public:
    enum { STARTUP = false, SHUTDOWN = true };

    static const float AGAINST_TRAFFIC_COST_MULTIPLIER;

    struct ShortestRoad
    {
        Road* road;
        bool isOutRoad;
        float cost;
    };
    struct NodeData
    {
        Intersection* destIn;
        ShortestRoad* roadToIn;
        ShortestRoad* roadJustBeforeIn;
        float dist; // total distance to the destination intersection
    };
    struct BigIntersection
    {
        Intersection* in;
        SwapArray<NodeData> routesWithMultiplier;
        SwapArray<NodeData> routesNoMultiplier;
    };
    enum ElementType
    {
        ET_INTERSECTION, // Intersection*
        ET_NORMALROAD    // Road*
    };
    struct PathElement
    {
        ElementType type; 
        void* elem;
        bool operator==( const PathElement& right ) const 
        { 
            return(elem==right.elem);
        }
        bool operator!=( const PathElement& right ) const
        {
            return(elem!=right.elem);
        }
    };


    static RoadManager* GetInstance();
    static void Destroy();

    //---------------Initialization
    void Init( bool shutdown );

    void InitializeRoadMemory( unsigned int numRoads );
    void InitializeIntersectionMemory( unsigned int numIntersections );
    void InitializeRoadSegmentDataMemory( unsigned int numSegments );
    void InitializeRoadSegmentMemory( unsigned int numRoadSegments );

    void DumpRoadSegmentDataMemory();

    Road* GetFreeRoadMemory( );
    Intersection* GetFreeIntersectionMemory( );
    RoadSegmentData* GetFreeRoadSegmentDataMemory( );
    RoadSegment* GetFreeRoadSegmentMemory( );

    void AddRoad( Road* pRoad );
    void AddIntersection( Intersection* pIntersection );
    void AddRoadSegmentData( RoadSegmentData* pRoadSegmentData );
    void AddRoadSegment( RoadSegment* pRoadSegment );

    int GetMaxPathElements();

    void CreateRoadNetwork( void );


    //---------------Data Aquisition
    Intersection* FindIntersection( const char* name );
    Intersection* FindIntersection( tUID name );
    Intersection* FindIntersection( rmt::Vector& point );

    Intersection* FindIntersection( int iIndex );

    int GetNumIntersectionsUsed( );
    unsigned int GetNumRoads( );

    bool FindRoad( const rmt::Vector& point, 
        const Road** ppRoad, 
        RoadSegment** ppOutRoadSegment, 
        int& segmentIndex,
        float& in, 
        float& lateral,
        bool considerShortCuts=true ) const;

    static bool FindClosestPointOnRoad( const Road* pRoad, 
                                        const rmt::Vector& pos, 
                                        rmt::Vector& closestPos, 
                                        float& closestDist, 
                                        int& segmentIndex );

    static float DetermineRoadT( RoadSegment* seg, float segT );
    static float DetermineSegmentT( const rmt::Vector& pos, RoadSegment* seg );



    RoadSegmentData* FindRoadSegmentData( const char* name );
    RoadSegmentData* FindRoadSegmentData( tUID name );

    RoadRenderTest* GetRoadRenderTest();

    // fully pathfind from source to target, returning float distance
    float FindPathElementsBetween( 
        bool useMultiplier,                 // IN: direction-biased?
        PathElement& sourceElem,            // IN: starting element
        float sourceT,                      // IN: used only if sourceElem is a road
        const rmt::Vector& sourcePos,       // IN: used only if sourceElem is an intersection
        PathElement& targetElem,            // IN: terminating element
        float targetT,                      // IN: used only if targetElem is a road
        const rmt::Vector& targetPos,       // IN: used only if targetElem is an intersection
        SwapArray<PathElement>& elems );    // OUT: accumulate roads or intersections

    enum PathfindingOption
    {
        PO_SEARCH_SHORTCUTS     = 1 << 0, // whether or not to take into account shortcut roads
        PO_SEARCH_ROADS         = 1 << 1, // whether or not to search for roads
        PO_SEARCH_INTERSECTIONS = 1 << 2, // whether or not to search for intersections

        NUM_POS = ( PO_SEARCH_SHORTCUTS | 
                    PO_SEARCH_ROADS | 
                    PO_SEARCH_INTERSECTIONS ) 
    };

    typedef char PathfindingOptions;

    // search roads and intersections for whichever's closest, given a position
    void FindClosestPathElement(
        const rmt::Vector& pos,         // IN: search center
        float searchRadius,             // IN: search radius
        PathfindingOptions options,        // IN: search options
        PathElement& closestElem,       // OUT: closest element (road or intersection)
        RoadSegment*& closestRoadSeg,   // OUT: if closest element is road, this is closest seg
        float& closestRoadSegT,         // OUT: if closest element is road, this is segment t value
        float& closestRoadT );          // OUT: if closest element is road, this is road's t value

    enum ErrorValue
    {
        DEAD_END,
        STILL_LOOKING,
        FOUND_TARGET,
        FOUND_BIGINTERSECTION,
        UNEXPECTED
    };
    struct DistErrMap
    {
        int ID;
        ErrorValue errVal;
        float dist;
    };

    float FindDistToTargetInOneDirection( 
        bool useMultiplier,
        Intersection* targetInt, 
        Intersection* currInt,
        Intersection* lastInt,
        ShortestRoad* shortestRoadFromLastInt,
        SwapArray<PathElement>& elems,
        ShortestRoad*& firstShortRoad, // needed for traversal dist from src road to srcInt
        ShortestRoad*& lastShortRoad,  // needed for traversal dist from targetInt to target road
        ErrorValue& errVal );
    
    void TraverseRoads( 
        bool useMultiplier,
        Intersection* targetInt, 
        Intersection* currInt,
        Intersection* lastInt,
        SwapArray<PathElement>& elems,
        ErrorValue& errVal );



private:

    static RoadManager* mInstance;

    Road* mRoads;
    unsigned int mNumRoads;
    unsigned int mNumRoadsUsed;

    Intersection* mIntersections;
    unsigned int mNumIntersections;
    unsigned int mNumIntersectionsUsed;

    struct DijkstraNode
    {
        struct AdjacencyData
        {
            DijkstraNode* adjacentNode;
            ShortestRoad* shortestRoadThere;
        };

        Intersection* in;
        float distToSrc;
        DijkstraNode* predecessor;
        ShortestRoad* shortestRoadFromPred;
        bool addedToS;
        SwapArray<AdjacencyData> adjacents;

        DijkstraNode()
        {
            in = NULL;

            distToSrc = NEAR_INFINITY;
            predecessor = NULL;
            shortestRoadFromPred = NULL;
            addedToS = false;
        }

        ~DijkstraNode()
        {
            in = NULL;
            adjacents.Clear();

            distToSrc = NEAR_INFINITY;
            predecessor = NULL;
            shortestRoadFromPred = NULL;
            addedToS = false;
        }

        void Init( float dist, DijkstraNode* pred )
        {
            distToSrc = dist;
            predecessor = pred;

            shortestRoadFromPred = NULL;
            addedToS = false;
        }
    };
    SwapArray<BigIntersection*> mBigIntersections;
    void PopulateConnectivityData( bool useMultiplier, Intersection* intersections, int numInts );
    void VisitAll( SwapArray<DijkstraNode>& nodes );

    RoadSegmentData* mRoadSegmentData;
    unsigned int mNumRoadSegmentData;
    unsigned int mNumRoadSegmentDataUsed;

    RoadSegment** mRoadSegments; // dynamically allocated array of pointers to RoadSegments
    unsigned int mNumRoadSegments;
    unsigned int mNumRoadSegmentsUsed;
    /*
    TransformRoadSegment** mRoadSegments;
    unsigned int mNumRoadSegments;
    unsigned int mNumRoadSegmentsUsed;
    */

    RoadRenderTest* mRender;

    float GetTraversalDistance( ShortestRoad* fromRoad, ShortestRoad* toRoad );


    //Singleton
    RoadManager();
    virtual ~RoadManager();

    //Prevent wasteful constructor creation.
    RoadManager( const RoadManager& roadmanager );
    RoadManager& operator=( const RoadManager& roadmanager );
};

inline unsigned int RoadManager::GetNumRoads()
{
    return mNumRoadsUsed;
}
inline int RoadManager::GetNumIntersectionsUsed()
{
    return mNumIntersectionsUsed;
}
inline RoadRenderTest* RoadManager::GetRoadRenderTest()
{
    return mRender;
}

#endif //ROADMANAGER_H