/
alife_simulator_script.cpp
537 lines (446 loc) · 17.2 KB
/
alife_simulator_script.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
////////////////////////////////////////////////////////////////////////////
// Module : alife_simulator_script.cpp
// Created : 25.12.2002
// Modified : 13.05.2004
// Author : Dmitriy Iassenev
// Description : ALife Simulator script export
////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "alife_simulator.h"
#include "ai_space.h"
#include "alife_object_registry.h"
#include "alife_story_registry.h"
#include "script_engine.h"
#include "xrServer_Objects_ALife_Monsters.h"
#include "restriction_space.h"
#include "alife_graph_registry.h"
#include "alife_spawn_registry.h"
#include "alife_registry_container.h"
#include "xrServer.h"
#include "level.h"
namespace detail
{
bool object_exists_in_alife_registry(u32 id)
{
if (ai().get_alife())
{
return ai().alife().objects().object((ALife::_OBJECT_ID)id, true) != nullptr;
}
return false;
}
} // detail
using namespace luabind;
typedef xr_map<shared_str,int> STORY_PAIRS;
typedef STORY_PAIRS SPAWN_STORY_PAIRS;
LPCSTR _INVALID_STORY_ID = "INVALID_STORY_ID";
LPCSTR _INVALID_SPAWN_STORY_ID = "INVALID_SPAWN_STORY_ID";
STORY_PAIRS story_ids;
SPAWN_STORY_PAIRS spawn_story_ids;
CALifeSimulator *alife ()
{
return (const_cast<CALifeSimulator*>(ai().get_alife()));
}
CSE_ALifeDynamicObject *alife_object (const CALifeSimulator *self, ALife::_OBJECT_ID object_id)
{
VERIFY (self);
return (self->objects().object(object_id,true));
}
bool valid_object_id (const CALifeSimulator *self, ALife::_OBJECT_ID object_id)
{
VERIFY (self);
return (object_id != 0xffff);
}
CSE_ALifeDynamicObject *alife_object (const CALifeSimulator *self, LPCSTR name)
{
VERIFY (self);
for (CALifeObjectRegistry::OBJECT_REGISTRY::const_iterator it = self->objects().objects().begin(); it != self->objects().objects().end(); it++) {
CSE_ALifeDynamicObject *obj = it->second;
if (xr_strcmp(obj->name_replace(),name) == 0)
return (it->second);
}
return (0);
}
CSE_ALifeDynamicObject *alife_object (const CALifeSimulator *self, ALife::_OBJECT_ID id, bool no_assert)
{
VERIFY (self);
return (self->objects().object(id,no_assert));
}
decltype(auto) alife_objects(const CALifeSimulator *self)
{
VERIFY(self);
return self->objects().objects();
}
CSE_ALifeDynamicObject *alife_story_object (const CALifeSimulator *self, ALife::_STORY_ID id)
{
return (self->story_objects().object(id,true));
}
template <typename _id_type>
void generate_story_ids (
STORY_PAIRS &result,
_id_type INVALID_ID,
LPCSTR section_name,
LPCSTR INVALID_ID_STRING,
LPCSTR invalid_id_description,
LPCSTR invalid_id_redefinition,
LPCSTR duplicated_id_description
)
{
result.clear ();
CInifile *Ini = pGameIni;
LPCSTR N,V;
u32 k;
shared_str temp;
LPCSTR section = section_name;
R_ASSERT (Ini->section_exist(section));
for (k = 0; Ini->r_line(section,k,&N,&V); ++k) {
temp = Ini->r_string_wb(section,N);
R_ASSERT3 (!strchr(*temp,' '),invalid_id_description,*temp);
R_ASSERT2 (xr_strcmp(*temp,INVALID_ID_STRING),invalid_id_redefinition);
auto ret = result.insert( std::make_pair( *temp, atoi( N ) ) );
ASSERT_FMT( ret.second == true, duplicated_id_description, *temp );
}
result.insert( std::make_pair( INVALID_ID_STRING, INVALID_ID ) );
}
void kill_entity0 (CALifeSimulator *alife, CSE_ALifeMonsterAbstract *monster, const GameGraph::_GRAPH_ID &game_vertex_id)
{
alife->kill_entity (monster,game_vertex_id,0);
}
void kill_entity1 (CALifeSimulator *alife, CSE_ALifeMonsterAbstract *monster)
{
alife->kill_entity (monster,monster->m_tGraphID,0);
}
void add_in_restriction (CALifeSimulator *alife, CSE_ALifeMonsterAbstract *monster, ALife::_OBJECT_ID id)
{
alife->add_restriction (monster->ID,id,RestrictionSpace::eRestrictorTypeIn);
}
void add_out_restriction (CALifeSimulator *alife, CSE_ALifeMonsterAbstract *monster, ALife::_OBJECT_ID id)
{
alife->add_restriction (monster->ID,id,RestrictionSpace::eRestrictorTypeOut);
}
void remove_in_restriction (CALifeSimulator *alife, CSE_ALifeMonsterAbstract *monster, ALife::_OBJECT_ID id)
{
alife->remove_restriction (monster->ID,id,RestrictionSpace::eRestrictorTypeIn);
}
void remove_in_restrictions (CALifeSimulator *alife, CSE_ALifeMonsterAbstract *monster)
{
alife->remove_all_restrictions (monster->ID,RestrictionSpace::eRestrictorTypeIn);
}
void remove_out_restriction (CALifeSimulator *alife, CSE_ALifeMonsterAbstract *monster, ALife::_OBJECT_ID id)
{
alife->remove_restriction (monster->ID,id,RestrictionSpace::eRestrictorTypeOut);
}
void remove_out_restrictions (CALifeSimulator *alife, CSE_ALifeMonsterAbstract *monster)
{
alife->remove_all_restrictions (monster->ID,RestrictionSpace::eRestrictorTypeOut);
}
u32 get_level_id(CALifeSimulator *self)
{
return (self->graph().level().level_id());
}
CSE_ALifeDynamicObject *CALifeSimulator__create (CALifeSimulator *self, ALife::_SPAWN_ID spawn_id)
{
const CALifeSpawnRegistry::SPAWN_GRAPH::CVertex *vertex = ai().alife().spawns().spawns().vertex(spawn_id);
THROW2 (vertex,"Invalid spawn id!");
CSE_ALifeDynamicObject *spawn = smart_cast<CSE_ALifeDynamicObject*>(&vertex->data()->object());
THROW (spawn);
CSE_ALifeDynamicObject *object;
self->create (object,spawn,spawn_id);
return (object);
}
CSE_Abstract *CALifeSimulator__spawn_item (CALifeSimulator *self, LPCSTR section, const Fvector &position, u32 level_vertex_id, GameGraph::_GRAPH_ID game_vertex_id)
{
THROW (self);
return (self->spawn_item(section,position,level_vertex_id,game_vertex_id,ALife::_OBJECT_ID(-1)));
}
CSE_Abstract *CALifeSimulator__spawn_item2 (CALifeSimulator *self, LPCSTR section, const Fvector &position, u32 level_vertex_id, GameGraph::_GRAPH_ID game_vertex_id, ALife::_OBJECT_ID id_parent)
{
if (id_parent == ALife::_OBJECT_ID(-1))
return (self->spawn_item(section,position,level_vertex_id,game_vertex_id,id_parent));
CSE_ALifeDynamicObject *object = ai().alife().objects().object(id_parent,true);
if (!object) {
Msg ("! invalid parent id [%d] specified",id_parent);
return (0);
}
if (!object->m_bOnline)
return (self->spawn_item(section,position,level_vertex_id,game_vertex_id,id_parent));
NET_Packet packet;
packet.w_begin (M_SPAWN);
packet.w_stringZ (section);
CSE_Abstract *item = self->spawn_item(section,position,level_vertex_id,game_vertex_id,id_parent,false);
item->Spawn_Write (packet,FALSE);
self->server().FreeID (item->ID,0);
F_entity_Destroy (item);
ClientID clientID;
clientID.set (0xffff);
u16 dummy;
packet.r_begin (dummy);
VERIFY (dummy == M_SPAWN);
return (self->server().Process_spawn(packet,clientID));
}
CSE_Abstract *CALifeSimulator__spawn_ammo (CALifeSimulator *self, LPCSTR section, const Fvector &position, u32 level_vertex_id, GameGraph::_GRAPH_ID game_vertex_id, ALife::_OBJECT_ID id_parent, int ammo_to_spawn)
{
// if (id_parent == ALife::_OBJECT_ID(-1))
// return (self->spawn_item(section,position,level_vertex_id,game_vertex_id,id_parent));
CSE_ALifeDynamicObject *object = 0;
if (id_parent != ALife::_OBJECT_ID(-1)) {
object = ai().alife().objects().object(id_parent,true);
if (!object) {
Msg ("! invalid parent id [%d] specified",id_parent);
return (0);
}
}
if (!object || !object->m_bOnline) {
CSE_Abstract *item = self->spawn_item(section,position,level_vertex_id,game_vertex_id,id_parent);
CSE_ALifeItemAmmo *ammo = smart_cast<CSE_ALifeItemAmmo*>(item);
THROW (ammo);
THROW (ammo->m_boxSize >= ammo_to_spawn);
ammo->a_elapsed = (u16)ammo_to_spawn;
return (item);
}
NET_Packet packet;
packet.w_begin (M_SPAWN);
packet.w_stringZ (section);
CSE_Abstract *item = self->spawn_item(section,position,level_vertex_id,game_vertex_id,id_parent,false);
CSE_ALifeItemAmmo *ammo = smart_cast<CSE_ALifeItemAmmo*>(item);
THROW (ammo);
THROW (ammo->m_boxSize >= ammo_to_spawn);
ammo->a_elapsed = (u16)ammo_to_spawn;
item->Spawn_Write (packet,FALSE);
self->server().FreeID (item->ID,0);
F_entity_Destroy (item);
ClientID clientID;
clientID.set (0xffff);
u16 dummy;
packet.r_begin (dummy);
VERIFY (dummy == M_SPAWN);
return (self->server().Process_spawn(packet,clientID));
}
void CALifeSimulator__release (CALifeSimulator *self, CSE_Abstract *object, bool)
{
VERIFY (self);
// self->release (object,true);
R_ASSERT (object);
CSE_ALifeObject *alife_object = smart_cast<CSE_ALifeObject*>(object);
THROW (alife_object);
if (!alife_object->m_bOnline) {
self->release (object,true);
return;
}
// awful hack, for stohe only
NET_Packet packet;
packet.w_begin (M_EVENT);
packet.w_u32 (Level().timeServer());
packet.w_u16 (GE_DESTROY);
packet.w_u16 (object->ID);
Level().Send (packet,net_flags(TRUE,TRUE));
}
void CALifeSimulator__assign_story_id(CALifeSimulator *self, ALife::_OBJECT_ID _id, ALife::_STORY_ID _story_id)
{
CSE_ALifeDynamicObject *obj = ai().alife().objects().object(_id);
R_ASSERT(obj);
#ifdef DEBUG
if (psAI_Flags.test(aiALife)) {
Msg("[LSS] Assigning story_id for object [%s][%s][%d][%x]", obj->name_replace(), obj->s_name.c_str(), obj->ID, obj);
}
#endif
if (!(ai().alife().story_objects().object(_story_id, true)))
{
obj->m_story_id = _story_id;
self->add_story_object(obj, _story_id);
} else
Msg("assign_story_id: specified id is already using");
}
void CALifeSimulator__use_ai_locations(CALifeSimulator *self, CSE_Abstract *object, bool _use)
{
#ifdef DEBUG
if (psAI_Flags.test(aiALife)) {
Msg("[LSS] Assigning use of ai locations for object [%s][%s][%d][%x]", object->name_replace(), *object->s_name, object->ID, smart_cast<void*>(object));
}
#endif
CSE_ALifeDynamicObject *obj = ai().alife().objects().object(object->ID);
R_ASSERT(obj);
obj->used_ai_locations(_use);
}
void CALifeSimulator__teleport_object(CALifeSimulator *self, u16 ID, Fvector _pos, u32 _lvid, GameGraph::_GRAPH_ID _gvid)
{
self->teleport_object(ID, _gvid, _lvid, _pos);
}
void FAKE_CALifeSimulator__teleport_object(CALifeSimulator*, const char*, Fvector, u32, GameGraph::_GRAPH_ID, u16)
{
FATAL("INCORRECT ARGUMENTS! Must be: alife():teleport_object(id, position, lvid, gvid)");
}
LPCSTR get_level_name (const CALifeSimulator *self, int level_id)
{
LPCSTR result = *ai().game_graph().header().level((GameGraph::_LEVEL_ID)level_id).name();
return (result);
}
CSE_ALifeCreatureActor *get_actor (const CALifeSimulator *self)
{
THROW (self);
return (self->graph().actor());
}
KNOWN_INFO_VECTOR *registry (const CALifeSimulator *self, const ALife::_OBJECT_ID &id)
{
THROW (self);
return (self->registry(info_portions).object(id, true));
}
class CFindByIDPred
{
public:
CFindByIDPred(shared_str element_to_find) {element = element_to_find;}
bool operator () (const INFO_DATA& data) const {return data.info_id == element;}
private:
shared_str element;
};
bool has_info (const CALifeSimulator *self, const ALife::_OBJECT_ID &id, LPCSTR info_id)
{
const KNOWN_INFO_VECTOR *known_info = registry(self,id);
if (!known_info)
return (false);
if (std::find_if(known_info->begin(), known_info->end(), CFindByIDPred(info_id)) == known_info->end())
return (false);
return (true);
}
bool dont_has_info (const CALifeSimulator *self, const ALife::_OBJECT_ID &id, LPCSTR info_id)
{
THROW (self);
// absurdly, but only because of scriptwriters needs
return (!has_info(self,id,info_id));
}
//void disable_info_portion (const CALifeSimulator *self, const ALife::_OBJECT_ID &id)
//{
// THROW (self);
//}
//void give_info_portion (const CALifeSimulator *self, const ALife::_OBJECT_ID &id)
//{
// THROW (self);
//}
LPCSTR get_save_name(CALifeSimulator *sim)
{
// alpet: обертка предотвращает вылет, при обращении к свойству на ранней стадии инициализации
LPCSTR result = NULL;
if (sim) result = sim->save_name(FALSE);
return result ? result : "NULL";
}
LPCSTR get_loaded_save(CALifeSimulator *sim)
{
LPCSTR result = NULL;
if (sim) result = sim->save_name(TRUE);
return result ? result : "NULL";
}
bool is_unloading(CALifeSimulator *sim)
{
return sim->is_unloading();
}
#pragma optimize("s",on)
void CALifeSimulator::script_register (lua_State *L)
{
module(L)
[
class_<CALifeSimulator>("alife_simulator")
.def("valid_object_id", &valid_object_id)
.def("level_id", &get_level_id)
.def("level_name", &get_level_name)
.def("objects", &alife_objects, return_stl_pair_iterator)
.def("object", (CSE_ALifeDynamicObject *(*) (const CALifeSimulator *,ALife::_OBJECT_ID))(alife_object))
.def("object", (CSE_ALifeDynamicObject *(*) (const CALifeSimulator *,LPCSTR))(alife_object))
.def("object", (CSE_ALifeDynamicObject *(*) (const CALifeSimulator *,ALife::_OBJECT_ID, bool))(alife_object))
.def("is_unloading", &is_unloading)
.def("story_object", (CSE_ALifeDynamicObject *(*) (const CALifeSimulator *,ALife::_STORY_ID))(alife_story_object))
.def("set_switch_online", (void (CALifeSimulator::*) (ALife::_OBJECT_ID,bool))(&CALifeSimulator::set_switch_online))
.def("set_switch_offline", (void (CALifeSimulator::*) (ALife::_OBJECT_ID,bool))(&CALifeSimulator::set_switch_offline))
.def("set_interactive", (void (CALifeSimulator::*) (ALife::_OBJECT_ID,bool))(&CALifeSimulator::set_interactive))
.def("kill_entity", &CALifeSimulator::kill_entity)
.def("kill_entity", &kill_entity0)
.def("kill_entity", &kill_entity1)
.def("add_in_restriction", &add_in_restriction)
.def("add_out_restriction", &add_out_restriction)
.def("remove_in_restriction", &remove_in_restriction)
.def("remove_in_restrictions", &remove_in_restrictions)
.def("remove_out_restriction", &remove_out_restriction)
.def("remove_out_restrictions", &remove_out_restrictions)
.def("remove_all_restrictions", &CALifeSimulator::remove_all_restrictions)
.def("create", &CALifeSimulator__create)
.def("create", &CALifeSimulator__spawn_item2)
.def("create", &CALifeSimulator__spawn_item)
.def("create_ammo", &CALifeSimulator__spawn_ammo)
.def("release", &CALifeSimulator__release)
.def("spawn_id", [](CALifeSimulator* self, ALife::_SPAWN_STORY_ID spawn_story_id) { return self->spawns().spawn_id(spawn_story_id); })
.def("spawn_id", [](CALifeSimulator* self, const char* obj_name) { return self->spawns().spawn_id(obj_name); })
.def("actor", &get_actor)
.def("has_info", &has_info)
.def("dont_has_info", &dont_has_info)
.def("switch_distance", &CALifeSimulator::switch_distance)
.def("set_switch_distance", &CALifeSimulator::set_switch_distance)
.def("teleport_object", &FAKE_CALifeSimulator__teleport_object)
.def("teleport_object", &CALifeSimulator__teleport_object)
.def("assign_story_id", &CALifeSimulator__assign_story_id)
.def("use_ai_locations", &CALifeSimulator__use_ai_locations)
.property("save_name", &get_save_name)
.property("loaded_save_name", &get_loaded_save)
,def("alife", &alife)
];
{
if (story_ids.empty())
generate_story_ids (
story_ids,
INVALID_STORY_ID,
"story_ids",
"INVALID_STORY_ID",
"Invalid story id description (contains spaces)!",
"INVALID_STORY_ID redifinition!",
"Duplicated story id description %s!"
);
class_<class_exporter<CALifeSimulator> > instance("story_ids");
for (const auto& pair : story_ids)
instance = std::move(instance).enum_("_story_ids")[luabind::value(*pair.first, pair.second)];
luabind::module (L)[std::move(instance)];
}
{
if (spawn_story_ids.empty())
generate_story_ids (
spawn_story_ids,
INVALID_SPAWN_STORY_ID,
"spawn_story_ids",
"INVALID_SPAWN_STORY_ID",
"Invalid spawn story id description (contains spaces)!",
"INVALID_SPAWN_STORY_ID redifinition!",
"Duplicated spawn story id description %s!"
);
class_<class_exporter<class_exporter<CALifeSimulator> > > instance("spawn_story_ids");
for (const auto& pair : spawn_story_ids)
instance = std::move(instance).enum_("_spawn_story_ids")[luabind::value(*pair.first, pair.second)];
luabind::module (L)[std::move(instance)];
}
}
#if 0//def DEBUG
struct dummy {
int count;
lua_State* state;
int ref;
};
void CALifeSimulator::validate ()
{
typedef CALifeSpawnRegistry::SPAWN_GRAPH::const_vertex_iterator const_vertex_iterator;
const_vertex_iterator I = spawns().spawns().vertices().begin();
const_vertex_iterator E = spawns().spawns().vertices().end();
for ( ; I != E; ++I) {
luabind::wrap_base *base = smart_cast<luabind::wrap_base*>(&(*I).second->data()->object());
if (!base)
continue;
if (!base->m_self.m_impl)
continue;
dummy *_dummy = (dummy*)((void*)base->m_self.m_impl);
lua_State **_state = &_dummy->state;
VERIFY2 (
base->m_self.state(),
make_string(
"0x%08x name[%s] name_replace[%s]",
*(int*)&_state,
(*I).second->data()->object().name(),
(*I).second->data()->object().name_replace()
)
);
}
}
#endif //DEBUG