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

attackscript.cpp « Game « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5d7d6d032a56973868da4a6ccf609699e2117c5 (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
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
//-----------------------------------------------------------------------------
//           Name: attackscript.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 "attackscript.h"

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

#include <Scripting/angelscript/ascontext.h>
#include <Main/engine.h>

#include <stack>
#include <sstream>

extern std::stack<ASContext*> active_context_stack;

static const std::string attack_getter_empty_path = "";
static const std::string attack_getter_empty_material_event = "";

bool AttackScriptGetter::Load( std::string _path ) {
    if(_path.empty()){
        return false;
    }
    path_ = _path;
    if(_path[_path.size()-2] == ' ' &&
       _path[_path.size()-1] == 'm') 
    {
       mirror_ = true;
       _path.resize(_path.size()-2);
    } else {
        mirror_ = false;
    }
    //Attacks::Instance()->ReturnRef(_path);
    attack_ref_ = Engine::Instance()->GetAssetManager()->LoadSync<Attack>(_path);
    return true;
}

std::string AttackScriptGetter::GetUnblockedAnimPath() {
    return attack_ref_.valid() ? attack_ref_->unblocked_anim_path : attack_getter_empty_path;
}

std::string AttackScriptGetter::GetBlockedAnimPath() {
    return attack_ref_.valid() ? attack_ref_->blocked_anim_path : attack_getter_empty_path;
}

std::string AttackScriptGetter::GetThrowAnimPath() {
    return attack_ref_.valid() ? attack_ref_->throw_anim_path : attack_getter_empty_path;
}

std::string AttackScriptGetter::GetThrownAnimPath() {
    return attack_ref_.valid() ? attack_ref_->thrown_anim_path : attack_getter_empty_path;
}

std::string AttackScriptGetter::GetSpecial() {
    return attack_ref_.valid() ? attack_ref_->special : attack_getter_empty_path;
}

int AttackScriptGetter::GetHeight() {
    return attack_ref_.valid() ? attack_ref_->height : AttackHeight::_medium;
}

int AttackScriptGetter::GetDirection() {
    return attack_ref_.valid() ? attack_ref_->direction : AttackDirection::_front;
}

int AttackScriptGetter::GetSwapStance() {
    return (int)(attack_ref_.valid() ? attack_ref_->swap_stance : false);
}

int AttackScriptGetter::GetSwapStanceBlocked() {
    return (int)(attack_ref_.valid() ? attack_ref_->swap_stance_blocked : false);
}

int AttackScriptGetter::GetUnblockable() {
    return (int)(attack_ref_.valid() ? attack_ref_->unblockable : false);
}

std::string AttackScriptGetter::GetMaterialEvent() {
    return attack_ref_.valid() ? attack_ref_->materialevent : attack_getter_empty_material_event;
}

std::string AttackScriptGetter::GetReactionPath() {
    if(attack_ref_.valid()) {
        if(attack_ref_->direction == _front || !mirror_) {
            return attack_ref_->reaction;
        } else {
            return attack_ref_->reaction + " m";
        }
    } else {
        return attack_getter_empty_path;
    }
}

vec3 AttackScriptGetter::GetImpactDir() {
    if(attack_ref_.valid()) {
        if(!mirror_){
            return attack_ref_->impact_dir;
        } else {
            return attack_ref_->impact_dir*vec3(-1.0f,1.0f,1.0f);
        }
    } else {
        return vec3();
    }
}

float AttackScriptGetter::GetBlockDamage() {
    return attack_ref_.valid() ? RangedRandomFloat(attack_ref_->block_damage[0], attack_ref_->block_damage[1]) : 0.0f;
}

float AttackScriptGetter::GetSharpDamage() {
    return attack_ref_.valid() ? RangedRandomFloat(attack_ref_->sharp_damage[0], attack_ref_->sharp_damage[1]) : 0.0f;
}

float AttackScriptGetter::GetDamage() {
    return attack_ref_.valid() ? RangedRandomFloat(attack_ref_->damage[0], attack_ref_->damage[1]) : 0.0f;
}

float AttackScriptGetter::GetForce() {
    return attack_ref_.valid() ? RangedRandomFloat(attack_ref_->force[0], attack_ref_->force[1]) : 0.0f;
}

std::string AttackScriptGetter::GetPath() {
    return path_;
}

AttackScriptGetter *AttackScriptGetter_Factory() {
    return new AttackScriptGetter();
}

static void ASLoad(AttackScriptGetter *asg, const std::string &path){
    if(!asg->Load(path)){
        std::string callstack = active_context_stack.top()->GetCallstack();
        FatalError("Error", "Attempting to load attack with empty path\n Called from:\n%s", callstack.c_str());
    }
}

void AttachAttackScriptGetter( ASContext *as_context ) {
    as_context->RegisterObjectType("AttackScriptGetter", 0, asOBJ_REF, "Used to query information from an attack xml file");
    as_context->RegisterObjectBehaviour("AttackScriptGetter", asBEHAVE_FACTORY, "AttackScriptGetter@ f()", asFUNCTION(AttackScriptGetter_Factory), asCALL_CDECL);
    as_context->RegisterObjectBehaviour("AttackScriptGetter", asBEHAVE_ADDREF, "void AttackScriptGetter()", asMETHOD(AttackScriptGetter,AddRef), asCALL_THISCALL);
    as_context->RegisterObjectBehaviour("AttackScriptGetter", asBEHAVE_RELEASE, "void AttackScriptGetter()", asMETHOD(AttackScriptGetter,ReleaseRef), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "void Load(const string &in path)", asFUNCTION(ASLoad), asCALL_CDECL_OBJFIRST, "Load an attack xml file into the attack script getter (e.g. \"Data/Attacks/knifeslash.xml\")");
    as_context->RegisterObjectMethod("AttackScriptGetter", "string GetPath()", asMETHOD(AttackScriptGetter, GetPath), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "string GetSpecial()", asMETHOD(AttackScriptGetter, GetSpecial), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "string GetUnblockedAnimPath()", asMETHOD(AttackScriptGetter, GetUnblockedAnimPath), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "string GetBlockedAnimPath()", asMETHOD(AttackScriptGetter, GetBlockedAnimPath), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "string GetThrowAnimPath()", asMETHOD(AttackScriptGetter, GetThrowAnimPath), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "string GetThrownAnimPath()", asMETHOD(AttackScriptGetter, GetThrownAnimPath), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "string GetThrownCounterAnimPath()", asMETHOD(AttackScriptGetter, GetThrownCounterAnimPath), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int IsThrow()", asMETHOD(AttackScriptGetter, IsThrow), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int GetHeight()", asMETHOD(AttackScriptGetter, GetHeight), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "vec3 GetCutPlane()", asMETHOD(AttackScriptGetter, GetCutPlane), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int GetCutPlaneType()", asMETHOD(AttackScriptGetter, GetCutPlaneType), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "bool HasCutPlane()", asMETHOD(AttackScriptGetter, HasCutPlane), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "vec3 GetStabDir()", asMETHOD(AttackScriptGetter, GetStabDir), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int GetStabDirType()", asMETHOD(AttackScriptGetter, GetStabDirType), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "bool HasStabDir()", asMETHOD(AttackScriptGetter, HasStabDir), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int GetDirection()", asMETHOD(AttackScriptGetter, GetDirection), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int GetSwapStance()", asMETHOD(AttackScriptGetter, GetSwapStance), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int GetSwapStanceBlocked()", asMETHOD(AttackScriptGetter, GetSwapStanceBlocked), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int GetUnblockable()", asMETHOD(AttackScriptGetter, GetUnblockable), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int GetFleshUnblockable()", asMETHOD(AttackScriptGetter, GetFleshUnblockable), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int GetAsLayer()", asMETHOD(AttackScriptGetter, GetAsLayer), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "string GetAlternate()", asMETHOD(AttackScriptGetter, GetAlternate), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "string GetReactionPath()", asMETHOD(AttackScriptGetter, GetReactionPath), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "string GetMaterialEvent()", asMETHOD(AttackScriptGetter, GetMaterialEvent), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "vec3 GetImpactDir()", asMETHOD(AttackScriptGetter, GetImpactDir), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "float GetBlockDamage()", asMETHOD(AttackScriptGetter, GetBlockDamage), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "float GetSharpDamage()", asMETHOD(AttackScriptGetter, GetSharpDamage), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "float GetDamage()", asMETHOD(AttackScriptGetter, GetDamage), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "float GetForce()", asMETHOD(AttackScriptGetter, GetForce), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int GetMirrored()", asMETHOD(AttackScriptGetter, GetMirrored), asCALL_THISCALL);
    as_context->RegisterObjectMethod("AttackScriptGetter", "int GetMobile()", asMETHOD(AttackScriptGetter, GetMobile), asCALL_THISCALL);
    as_context->DocsCloseBrace();

    static const int const_high = _high;
    static const int const_medium = _medium;
    static const int const_low = _low;
    static const int const_right = _right;
    static const int const_front = _front;
    static const int const_left = _left;

    as_context->RegisterGlobalProperty("const int _high",   (void*)&const_high);
    as_context->RegisterGlobalProperty("const int _medium", (void*)&const_medium);
    as_context->RegisterGlobalProperty("const int _low",    (void*)&const_low);
    as_context->RegisterGlobalProperty("const int _right",  (void*)&const_right);
    as_context->RegisterGlobalProperty("const int _front",  (void*)&const_front);
    as_context->RegisterGlobalProperty("const int _left",   (void*)&const_left);
}

int AttackScriptGetter::GetMirrored() {
    return (int)mirror_;
}

int AttackScriptGetter::GetMobile() {
    return (int)(attack_ref_.valid() ? attack_ref_->mobile : false);
}

int AttackScriptGetter::IsThrow() {
    if(!attack_ref_.valid()){
        return 0;
    }
    return (int)(!(attack_ref_->throw_anim_path.empty() && 
                   attack_ref_->thrown_anim_path.empty()));
}

vec3 AttackScriptGetter::GetCutPlane() {
    return attack_ref_.valid() ? attack_ref_->cut_plane : vec3();
}

int AttackScriptGetter::GetCutPlaneType() {
    return attack_ref_.valid() ? attack_ref_->cut_plane_type : CutPlaneType::_heavy;
}

bool AttackScriptGetter::HasCutPlane() {
    return attack_ref_.valid() ? attack_ref_->has_cut_plane : false;
}

vec3 AttackScriptGetter::GetStabDir() {
    return attack_ref_.valid() ? attack_ref_->stab_dir : vec3();
}

int AttackScriptGetter::GetStabDirType() {
    return attack_ref_.valid() ? attack_ref_->stab_type : CutPlaneType::_heavy;
}

bool AttackScriptGetter::HasStabDir() {
    return attack_ref_.valid() ? attack_ref_->has_stab_dir : false;
}

int AttackScriptGetter::GetFleshUnblockable() {
    return (int)(attack_ref_.valid() ? attack_ref_->flesh_unblockable : false);
}

int AttackScriptGetter::GetAsLayer() {
    return (int)(attack_ref_.valid() ? attack_ref_->as_layer : false);
}

std::string AttackScriptGetter::GetAlternate() {
    return attack_ref_.valid() ? attack_ref_->alternate : attack_getter_empty_path;
}

std::string AttackScriptGetter::GetThrownCounterAnimPath() {
    return attack_ref_.valid() ? attack_ref_->thrown_counter_anim_path : attack_getter_empty_path;
}

AttackScriptGetter::AttackScriptGetter():
    ref_count(1)
{}

void AttackScriptGetter::AddRef() {
    ++ref_count;
}

void AttackScriptGetter::ReleaseRef() {
    --ref_count;
    if(ref_count == 0){
        delete this;
    }
}