Welcome to mirror list, hosted at ThFree Co, Russian Federation.

paths.cpp « Game « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1fdf8bcf604a1596cf9fccc3700cbd502038130a (plain)
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
//-----------------------------------------------------------------------------
//           Name: paths.cpp
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#include "paths.h"

#include <Math/enginemath.h>
#include <Math/vec3math.h>

#include <Graphics/pxdebugdraw.h>
#include <Scripting/angelscript/ascontext.h>

#include <sstream>

void AIPaths::Draw() {
    for(PointMap::const_iterator iter = points.begin(); iter != points.end(); ++iter){
        const vec3& point = iter->second;
        DebugDraw::Instance()->AddWireSphere(point, 
                                             0.5f, 
                                             vec4(0.0f,0.5f,0.5f,0.5f),
                                             _delete_on_draw);
    }
    for(auto & connection : connections){
        DebugDraw::Instance()->AddLine(GetPointPosition(connection.point_ids[0]),
                                       GetPointPosition(connection.point_ids[1]),
                                       vec4(0.0f,0.5f,0.5f,0.5f), 
                                       _delete_on_draw);
    }
}

int AIPaths::GetNearestPoint( const vec3 &pos )
{
    float closest_distance;
    float distance;
    int closest_point = -1;
    for(PointMap::const_iterator iter = points.begin(); iter != points.end(); ++iter){
        const vec3 &point = iter->second;
        int id = iter->first;
        distance = distance_squared(pos, point);
        if(closest_point == -1 || distance < closest_distance){
            closest_point = id;
            closest_distance = distance;
        }
    }
    return closest_point;
}

vec3 AIPaths::GetPointPosition( int point_id ) {
    PointMap::const_iterator iter = points.find(point_id);
    if(iter != points.end()){
        return iter->second;
    } else {
        std::ostringstream oss;
        oss << "Could not find waypoint " << point_id;
        DisplayError("Error",oss.str().c_str());
        return vec3(0.0f);
    }
}

int AIPaths::GetConnectedPoint( int point_id )
{
    for(auto & connection : connections){
        if(connection.point_ids[0] == point_id){
            return connection.point_ids[1];
        }
        if(connection.point_ids[1] == point_id){
            return connection.point_ids[0];
        }
    }
    return -1;
}

int AIPaths::GetOtherConnectedPoint( int point_id, int other_id )
{
    for(auto & connection : connections){
        if(connection.point_ids[0] == point_id &&
           connection.point_ids[1] != other_id){
            return connection.point_ids[1];
        }
        if(connection.point_ids[1] == point_id &&
           connection.point_ids[0] != other_id){
            return connection.point_ids[0];
        }
    }
    return -1;
}

void AIPaths::AddPoint( int id, const vec3 &pos ) {
    points[id] = pos;
}

void AIPaths::AddConnection( int a, int b ) {
    connections.push_back( PathConnection(a, b) );
}

void AIPaths::RemovePoint( int id ) {
    PointMap::iterator iter = points.find(id);
    if(iter != points.end()){
        points.erase(iter);
    }
    for(std::vector<PathConnection>::iterator iter = connections.begin(); iter != connections.end();){
        PathConnection &connection = (*iter);
        if(connection.point_ids[0] == id || connection.point_ids[1] == id){
            iter = connections.erase(iter);
        } else {
            ++iter;
        }
    }
}

void AIPaths::RemoveConnection( int a, int b ) {
    for(std::vector<PathConnection>::iterator iter = connections.begin(); iter != connections.end();){
        PathConnection &connection = (*iter);
        if(connection.point_ids[0] == a && connection.point_ids[1] == b){
            iter = connections.erase(iter);
        } else {
            ++iter;
        }
    }
}

void AIPaths::SetPoint( int id, const vec3 &pos ) {
    PointMap::iterator iter = points.find(id);
    if(iter != points.end()){
        iter->second = pos;
    }/* else {
        std::ostringstream oss;
        oss << "Could not find waypoint " << id;
        DisplayError("Error", oss.str());
    }*/
}

void PathScriptReader::AttachToScript( ASContext *as_context, const std::string& as_name )
{
    as_context->RegisterObjectType("PathScriptReader", 0, asOBJ_REF | asOBJ_NOHANDLE);
    as_context->RegisterObjectMethod("PathScriptReader",
        "int GetNearestPoint(vec3)",
        asMETHOD(PathScriptReader, GetNearestPoint), asCALL_THISCALL);
    as_context->RegisterObjectMethod("PathScriptReader",
        "vec3 GetPointPosition(int point_id)",
        asMETHOD(PathScriptReader, GetPointPosition), asCALL_THISCALL);
    as_context->RegisterObjectMethod("PathScriptReader",
        "int GetConnectedPoint(int point_id)",
        asMETHOD(PathScriptReader, GetConnectedPoint), asCALL_THISCALL);
    as_context->RegisterObjectMethod("PathScriptReader",
        "int GetOtherConnectedPoint(int point_id, int other_id)",
        asMETHOD(PathScriptReader, GetOtherConnectedPoint), asCALL_THISCALL);
    as_context->DocsCloseBrace();

    as_context->RegisterGlobalProperty(("PathScriptReader "+as_name).c_str(), this);
}

int PathScriptReader::GetNearestPoint( vec3 pos ) {
    return AIPaths::Instance()->GetNearestPoint(pos);
}

vec3 PathScriptReader::GetPointPosition( int point_id ) {
    return AIPaths::Instance()->GetPointPosition(point_id);
}

int PathScriptReader::GetConnectedPoint( int point_id ) {
    return AIPaths::Instance()->GetConnectedPoint(point_id);
}

int PathScriptReader::GetOtherConnectedPoint( int point_id, int other_id ) {
    return AIPaths::Instance()->GetOtherConnectedPoint(point_id, other_id);
}