forked from google-deepmind/lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_pickups.cc
361 lines (311 loc) · 11.4 KB
/
context_pickups.cc
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
// Copyright (C) 2017 Google Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
////////////////////////////////////////////////////////////////////////////////
#include "deepmind/engine/context_pickups.h"
#include <algorithm>
#include <string>
#include <unordered_map>
#include "deepmind/support/logging.h"
#include "deepmind/lua/call.h"
#include "deepmind/lua/class.h"
#include "deepmind/lua/push.h"
#include "deepmind/lua/read.h"
namespace deepmind {
namespace lab {
namespace {
constexpr int kMaxSpawnVars = 64;
constexpr int kMaxSpawnVarChars = 4096;
constexpr int kMaxPickupChars = 256;
// If the string "arg" fits into the array pointed to by "dest" (including the
// null terminator), copies the string into the array and returns true;
// otherwise returns false.
bool StringCopy(const std::string& arg, char* dest, std::size_t max_size) {
auto len = arg.length() + 1;
if (len <= max_size) {
std::copy_n(arg.c_str(), len, dest);
return true;
} else {
return false;
}
}
void ReadSpawnVars(const ContextPickups::EntityInstance& spawn_vars,
char* spawn_var_chars, int* num_spawn_var_chars,
int spawn_var_offsets[][2], int* num_spawn_vars) {
*num_spawn_var_chars = 0;
*num_spawn_vars = spawn_vars.size();
CHECK_NE(0, *num_spawn_vars) << "Must have spawn vars or return nil. (Make "
"sure all values are strings.)";
CHECK_LT(*num_spawn_vars, kMaxSpawnVars) << "Too many spawn vars!";
char* mem = spawn_var_chars;
auto it = spawn_vars.begin();
for (int i = 0; i < *num_spawn_vars; ++i) {
const auto& key = it->first;
const auto& value = it->second;
++it;
std::size_t kl = key.length() + 1;
std::size_t vl = value.length() + 1;
*num_spawn_var_chars += kl + vl;
CHECK_LT(*num_spawn_var_chars, kMaxSpawnVarChars) << "Too large spawn vars";
std::copy(key.c_str(), key.c_str() + kl, mem);
spawn_var_offsets[i][0] = std::distance(spawn_var_chars, mem);
mem += kl;
std::copy(value.c_str(), value.c_str() + vl, mem);
spawn_var_offsets[i][1] = std::distance(spawn_var_chars, mem);
mem += vl;
}
}
class LuaPickupsModule : public lua::Class<LuaPickupsModule> {
friend class Class;
static const char* ClassName() { return "deepmind.lab.Pickups"; }
public:
// '*ctx' owned by the caller and should out-live this object.
explicit LuaPickupsModule(ContextPickups* ctx) : ctx_(ctx) {}
static void Register(lua_State* L) {
const Class::Reg methods[] = {
{"spawn", Member<&LuaPickupsModule::Spawn>},
};
Class::Register(L, methods);
}
private:
// Spawn entity at position
// [0, 1, -]
lua::NResultsOr Spawn(lua_State* L) {
ContextPickups::EntityInstance spawn_entity;
if (lua::Read(L, 2, &spawn_entity)) {
ctx_->SpawnDynamicEntity(std::move(spawn_entity));
return 0;
} else {
return "[pickups.spawn] - Must be called with a string-string Lua table.";
}
}
ContextPickups* ctx_;
};
} // namespace
lua::NResultsOr ContextPickups::Module(lua_State* L) {
if (auto* ctx = static_cast<ContextPickups*>(
lua_touserdata(L, lua_upvalueindex(1)))) {
LuaPickupsModule::Register(L);
LuaPickupsModule::CreateObject(L, ctx);
return 1;
} else {
return "Missing context!";
}
}
void ContextPickups::SpawnDynamicEntity(EntityInstance spawn_entity) {
dynamic_spawn_entities_.push_back(std::move(spawn_entity));
}
void ContextPickups::ReadDynamicSpawnEntity(int entity_index,
char* spawn_var_chars,
int* num_spawn_var_chars,
int spawn_var_offsets[][2],
int* num_spawn_vars) const {
ReadSpawnVars(dynamic_spawn_entities_[entity_index], spawn_var_chars,
num_spawn_var_chars, spawn_var_offsets, num_spawn_vars);
}
bool ContextPickups::UpdateSpawnVars(char* spawn_var_chars,
int* num_spawn_var_chars,
int spawn_var_offsets[][2],
int* num_spawn_vars) {
lua_State* L = script_table_ref_.LuaState();
script_table_ref_.PushMemberFunction("updateSpawnVars");
if (lua_isnil(L, -2)) {
lua_pop(L, 2);
return true;
}
auto table = lua::TableRef::Create(L);
for (int i = 0; i < *num_spawn_vars; ++i) {
table.Insert(std::string(spawn_var_chars + spawn_var_offsets[i][0]),
std::string(spawn_var_chars + spawn_var_offsets[i][1]));
}
lua::Push(L, table);
auto result = lua::Call(L, 2);
CHECK(result.ok()) << result.error();
// Nil return so spawn is ignored.
if (lua_isnil(L, -1)) {
lua_pop(L, result.n_results());
return false;
}
EntityInstance entity;
lua::Read(L, -1, &entity);
lua_pop(L, result.n_results());
ReadSpawnVars(entity, spawn_var_chars, num_spawn_var_chars,
spawn_var_offsets, num_spawn_vars);
return true;
}
// Clears extra_spawn_vars_ and reads new entities from Lua.
// Returns number of entities created.
int ContextPickups::MakeExtraEntities() {
lua_State* L = script_table_ref_.LuaState();
script_table_ref_.PushMemberFunction("extraEntities");
if (lua_isnil(L, -2)) {
lua_pop(L, 2);
return 0;
}
auto result = lua::Call(L, 1);
CHECK(result.ok()) << result.error();
// Nil return so no spawns returned.
if (lua_isnil(L, -1)) {
lua_pop(L, result.n_results());
return 0;
}
extra_entities_.clear();
CHECK(lua::Read(L, -1, &extra_entities_))
<< "[extraEntities] - Invalid return value";
lua_pop(L, result.n_results());
return extra_entities_.size();
}
// Clears extra_spawn_vars_ and reads new entities from Lua.
// Returns number of entities created.
int ContextPickups::RegisterDynamicItems() {
lua_State* L = script_table_ref_.LuaState();
script_table_ref_.PushMemberFunction("registerDynamicItems");
if (lua_isnil(L, -2)) {
lua_pop(L, 2);
return 0;
}
auto result = lua::Call(L, 1);
CHECK(result.ok()) << result.error();
// Nil return so no spawns returned.
if (lua_isnil(L, -1)) {
lua_pop(L, result.n_results());
return 0;
}
dynamic_items_.clear();
CHECK(lua::Read(L, -1, &dynamic_items_))
<< "[extraEntities] - Invalid return value";
lua_pop(L, result.n_results());
return dynamic_items_.size();
}
void ContextPickups::ReadDynamicItemName(int item_index,
char* item_name) const {
const auto& item = dynamic_items_[item_index];
std::size_t length = item.length() + 1;
CHECK_LE(length, kMaxPickupChars) << "Too long pickup name! - " << item;
std::copy(item.c_str(), item.c_str() + length, item_name);
}
// Read specific spawn var from extra_spawn_vars_. Shall be called after
// with spawn_var_index in range [0, MakeExtraSpawnVars()).
void ContextPickups::ReadExtraEntity( //
int entity_index, //
char* spawn_var_chars, //
int* num_spawn_var_chars, //
int spawn_var_offsets[][2], //
int* num_spawn_vars) {
CHECK(0 <= entity_index && entity_index < extra_entities_.size());
ReadSpawnVars(extra_entities_[entity_index], spawn_var_chars,
num_spawn_var_chars, spawn_var_offsets, num_spawn_vars);
}
bool ContextPickups::FindItem(const char* class_name, int* index) {
lua_State* L = script_table_ref_.LuaState();
script_table_ref_.PushMemberFunction("createPickup");
// Check function exists.
if (lua_isnil(L, -2)) {
lua_pop(L, 2);
return false;
}
lua::Push(L, class_name);
auto result = lua::Call(L, 2);
CHECK(result.ok()) << result.error();
// If no description is returned or the description is nil, don't create item.
if (result.n_results() == 0 || lua_isnil(L, -1)) {
lua_pop(L, result.n_results());
return false;
}
lua::TableRef table;
CHECK(Read(L, -1, &table)) << "Failed to read pickup table!";
PickupItem item = {};
CHECK(table.LookUp("name", &item.name));
CHECK(table.LookUp("classname", &item.class_name));
CHECK(table.LookUp("model", &item.model_name));
CHECK(table.LookUp("quantity", &item.quantity));
CHECK(table.LookUp("type", &item.type));
table.LookUp("typeTag", &item.tag);
// Optional fields.
if (!table.LookUp("moveType", &item.move_type)) {
// Legacy name.
table.LookUp("tag", &item.move_type);
}
items_.push_back(item);
*index = ItemCount() - 1;
lua_pop(L, result.n_results());
return true;
}
bool ContextPickups::GetItem(int index, char* item_name, int max_item_name, //
char* class_name, int max_class_name, //
char* model_name, int max_model_name, //
int* quantity, int* type, int* tag,
int* move_type) const {
CHECK_GE(index, 0) << "Index out of range!";
CHECK_LT(index, ItemCount()) << "Index out of range!";
const auto& item = items_[index];
CHECK(StringCopy(item.name, item_name, max_item_name));
CHECK(StringCopy(item.class_name, class_name, max_class_name));
CHECK(StringCopy(item.model_name, model_name, max_model_name));
*quantity = item.quantity;
*type = static_cast<int>(item.type);
*tag = item.tag;
*move_type = item.move_type;
return true;
}
bool ContextPickups::CanPickup(int entity_id, int player_id) {
lua_State* L = script_table_ref_.LuaState();
script_table_ref_.PushMemberFunction("canPickup");
// Check function exists.
if (lua_isnil(L, -2)) {
lua_pop(L, 2);
return true;
}
lua::Push(L, entity_id);
lua::Push(L, player_id + 1);
auto result = lua::Call(L, 3);
CHECK(result.ok()) << result.error();
// If nothing returned or the return is nil, the default.
if (result.n_results() == 0 || lua_isnil(L, -1)) {
lua_pop(L, result.n_results());
return true;
}
bool can_pickup = true;
CHECK(lua::Read(L, -1, &can_pickup))
<< "Failed to read canPickup return value";
lua_pop(L, result.n_results());
return can_pickup;
}
bool ContextPickups::OverridePickup(int entity_id, int* respawn,
int player_id) {
lua_State* L = script_table_ref_.LuaState();
script_table_ref_.PushMemberFunction("pickup");
// Check function exists.
if (lua_isnil(L, -2)) {
lua_pop(L, 2);
return false;
}
lua::Push(L, entity_id);
lua::Push(L, player_id + 1);
auto result = lua::Call(L, 3);
CHECK(result.ok()) << result.error();
// If nothing returned or the return is nil, we're not overriding the
// pickup behaviour.
if (result.n_results() == 0 || lua_isnil(L, -1)) {
lua_pop(L, result.n_results());
return false;
}
CHECK(lua::Read(L, -1, respawn)) << "Failed to read the respawn time";
lua_pop(L, result.n_results());
return true;
}
} // namespace lab
} // namespace deepmind