-
Notifications
You must be signed in to change notification settings - Fork 134
/
ocgapi.cpp
352 lines (350 loc) · 11.7 KB
/
ocgapi.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
/*
* interface.cpp
*
* Created on: 2010-5-2
* Author: Argon
*/
#include <cstdio>
#include <cstring>
#include "ocgapi.h"
#include "duel.h"
#include "card.h"
#include "group.h"
#include "effect.h"
#include "field.h"
#include "interpreter.h"
#include "buffer.h"
#include <set>
static script_reader sreader = default_script_reader;
static card_reader creader = default_card_reader;
static message_handler mhandler = default_message_handler;
static byte buffer[0x20000];
static std::set<duel*> duel_set;
extern "C" DECL_DLLEXPORT void set_script_reader(script_reader f) {
sreader = f;
}
extern "C" DECL_DLLEXPORT void set_card_reader(card_reader f) {
creader = f;
}
extern "C" DECL_DLLEXPORT void set_message_handler(message_handler f) {
mhandler = f;
}
byte* read_script(const char* script_name, int* len) {
return sreader(script_name, len);
}
uint32 read_card(uint32 code, card_data* data) {
return creader(code, data);
}
uint32 handle_message(void* pduel, uint32 msg_type) {
return mhandler((intptr_t)pduel, msg_type);
}
byte* default_script_reader(const char* script_name, int* slen) {
FILE *fp;
fp = std::fopen(script_name, "rb");
if (!fp)
return nullptr;
size_t len = std::fread(buffer, 1, sizeof buffer, fp);
std::fclose(fp);
if (len >= sizeof buffer)
return nullptr;
*slen = (int)len;
return buffer;
}
uint32 default_card_reader(uint32 code, card_data* data) {
return 0;
}
uint32 default_message_handler(intptr_t pduel, uint32 message_type) {
return 0;
}
extern "C" DECL_DLLEXPORT intptr_t create_duel(uint_fast32_t seed) {
duel* pduel = new duel();
duel_set.insert(pduel);
pduel->random.reset(seed);
return (intptr_t)pduel;
}
extern "C" DECL_DLLEXPORT void start_duel(intptr_t pduel, uint32 options) {
duel* pd = (duel*)pduel;
pd->game_field->core.duel_options |= options & 0xffff;
int32 duel_rule = options >> 16;
if(duel_rule)
pd->game_field->core.duel_rule = duel_rule;
else if(options & DUEL_OBSOLETE_RULING) //provide backward compatibility with replay
pd->game_field->core.duel_rule = 1;
if (pd->game_field->core.duel_rule < 1 || pd->game_field->core.duel_rule > CURRENT_RULE)
pd->game_field->core.duel_rule = CURRENT_RULE;
if (pd->game_field->core.duel_rule == MASTER_RULE3) {
pd->game_field->player[0].szone_size = 8;
pd->game_field->player[1].szone_size = 8;
}
pd->game_field->core.shuffle_hand_check[0] = FALSE;
pd->game_field->core.shuffle_hand_check[1] = FALSE;
pd->game_field->core.shuffle_deck_check[0] = FALSE;
pd->game_field->core.shuffle_deck_check[1] = FALSE;
if(pd->game_field->player[0].start_count > 0)
pd->game_field->draw(0, REASON_RULE, PLAYER_NONE, 0, pd->game_field->player[0].start_count);
if(pd->game_field->player[1].start_count > 0)
pd->game_field->draw(0, REASON_RULE, PLAYER_NONE, 1, pd->game_field->player[1].start_count);
if(options & DUEL_TAG_MODE) {
for(int i = 0; i < pd->game_field->player[0].start_count && pd->game_field->player[0].tag_list_main.size(); ++i) {
card* pcard = pd->game_field->player[0].tag_list_main.back();
pd->game_field->player[0].tag_list_main.pop_back();
pd->game_field->player[0].tag_list_hand.push_back(pcard);
pcard->current.controler = 0;
pcard->current.location = LOCATION_HAND;
pcard->current.sequence = (uint8)pd->game_field->player[0].tag_list_hand.size() - 1;
pcard->current.position = POS_FACEDOWN;
}
for(int i = 0; i < pd->game_field->player[1].start_count && pd->game_field->player[1].tag_list_main.size(); ++i) {
card* pcard = pd->game_field->player[1].tag_list_main.back();
pd->game_field->player[1].tag_list_main.pop_back();
pd->game_field->player[1].tag_list_hand.push_back(pcard);
pcard->current.controler = 1;
pcard->current.location = LOCATION_HAND;
pcard->current.sequence = (uint8)pd->game_field->player[1].tag_list_hand.size() - 1;
pcard->current.position = POS_FACEDOWN;
}
}
pd->game_field->add_process(PROCESSOR_TURN, 0, 0, 0, 0, 0);
}
extern "C" DECL_DLLEXPORT void end_duel(intptr_t pduel) {
duel* pd = (duel*)pduel;
if(duel_set.count(pd)) {
duel_set.erase(pd);
delete pd;
}
}
extern "C" DECL_DLLEXPORT void set_player_info(intptr_t pduel, int32 playerid, int32 lp, int32 startcount, int32 drawcount) {
if (!check_playerid(playerid))
return;
duel* pd = (duel*)pduel;
if(lp > 0)
pd->game_field->player[playerid].lp = lp;
if(startcount >= 0)
pd->game_field->player[playerid].start_count = startcount;
if(drawcount >= 0)
pd->game_field->player[playerid].draw_count = drawcount;
}
extern "C" DECL_DLLEXPORT void get_log_message(intptr_t pduel, char* buf) {
std::strcpy(buf, ((duel*)pduel)->strbuffer);
}
extern "C" DECL_DLLEXPORT int32 get_message(intptr_t pduel, byte* buf) {
int32 len = ((duel*)pduel)->read_buffer(buf);
((duel*)pduel)->clear_buffer();
return len;
}
extern "C" DECL_DLLEXPORT uint32 process(intptr_t pduel) {
duel* pd = (duel*)pduel;
uint32 result = 0;
do {
result = pd->game_field->process();
} while ((result & PROCESSOR_BUFFER_LEN) == 0 && (result & PROCESSOR_FLAG) == 0);
return result;
}
extern "C" DECL_DLLEXPORT void new_card(intptr_t pduel, uint32 code, uint8 owner, uint8 playerid, uint8 location, uint8 sequence, uint8 position) {
if (!check_playerid(owner) || !check_playerid(playerid))
return;
duel* ptduel = (duel*)pduel;
if(ptduel->game_field->is_location_useable(playerid, location, sequence)) {
card* pcard = ptduel->new_card(code);
pcard->owner = owner;
ptduel->game_field->add_card(playerid, pcard, location, sequence);
pcard->current.position = position;
if(!(location & LOCATION_ONFIELD) || (position & POS_FACEUP)) {
pcard->enable_field_effect(true);
ptduel->game_field->adjust_instant();
} if(location & LOCATION_ONFIELD) {
if(location == LOCATION_MZONE)
pcard->set_status(STATUS_PROC_COMPLETE, TRUE);
}
}
}
extern "C" DECL_DLLEXPORT void new_tag_card(intptr_t pduel, uint32 code, uint8 owner, uint8 location) {
duel* ptduel = (duel*)pduel;
if(owner > 1 || !(location & (LOCATION_DECK | LOCATION_EXTRA)))
return;
card* pcard = ptduel->new_card(code);
switch(location) {
case LOCATION_DECK:
ptduel->game_field->player[owner].tag_list_main.push_back(pcard);
pcard->owner = owner;
pcard->current.controler = owner;
pcard->current.location = LOCATION_DECK;
pcard->current.sequence = (uint8)ptduel->game_field->player[owner].tag_list_main.size() - 1;
pcard->current.position = POS_FACEDOWN_DEFENSE;
break;
case LOCATION_EXTRA:
ptduel->game_field->player[owner].tag_list_extra.push_back(pcard);
pcard->owner = owner;
pcard->current.controler = owner;
pcard->current.location = LOCATION_EXTRA;
pcard->current.sequence = (uint8)ptduel->game_field->player[owner].tag_list_extra.size() - 1;
pcard->current.position = POS_FACEDOWN_DEFENSE;
break;
}
}
extern "C" DECL_DLLEXPORT int32 query_card(intptr_t pduel, uint8 playerid, uint8 location, uint8 sequence, int32 query_flag, byte* buf, int32 use_cache) {
if (!check_playerid(playerid))
return LEN_FAIL;
duel* ptduel = (duel*)pduel;
card* pcard = nullptr;
location &= 0x7f;
if(location & LOCATION_ONFIELD)
pcard = ptduel->game_field->get_field_card(playerid, location, sequence);
else {
card_vector* lst = nullptr;
if (location == LOCATION_HAND)
lst = &ptduel->game_field->player[playerid].list_hand;
else if (location == LOCATION_GRAVE)
lst = &ptduel->game_field->player[playerid].list_grave;
else if (location == LOCATION_REMOVED)
lst = &ptduel->game_field->player[playerid].list_remove;
else if (location == LOCATION_EXTRA)
lst = &ptduel->game_field->player[playerid].list_extra;
else if (location == LOCATION_DECK)
lst = &ptduel->game_field->player[playerid].list_main;
else
return LEN_FAIL;
if(sequence >= (int32)lst->size())
pcard = nullptr;
else
pcard = (*lst)[sequence];
}
if (pcard) {
return pcard->get_infos(buf, query_flag, use_cache);
}
else {
buffer_write<int32_t>(buf, LEN_EMPTY);
return LEN_EMPTY;
}
}
extern "C" DECL_DLLEXPORT int32 query_field_count(intptr_t pduel, uint8 playerid, uint8 location) {
duel* ptduel = (duel*)pduel;
if (!check_playerid(playerid))
return 0;
auto& player = ptduel->game_field->player[playerid];
if(location == LOCATION_HAND)
return (int32)player.list_hand.size();
if(location == LOCATION_GRAVE)
return (int32)player.list_grave.size();
if(location == LOCATION_REMOVED)
return (int32)player.list_remove.size();
if(location == LOCATION_EXTRA)
return (int32)player.list_extra.size();
if(location == LOCATION_DECK)
return (int32)player.list_main.size();
if(location == LOCATION_MZONE) {
int32 count = 0;
for(auto& pcard : player.list_mzone)
if(pcard)
++count;
return count;
}
if(location == LOCATION_SZONE) {
int32 count = 0;
for(auto& pcard : player.list_szone)
if(pcard)
++count;
return count;
}
return 0;
}
extern "C" DECL_DLLEXPORT int32 query_field_card(intptr_t pduel, uint8 playerid, uint8 location, uint32 query_flag, byte* buf, int32 use_cache) {
if (!check_playerid(playerid))
return LEN_FAIL;
duel* ptduel = (duel*)pduel;
auto& player = ptduel->game_field->player[playerid];
byte* p = buf;
if(location == LOCATION_MZONE) {
for(auto& pcard : player.list_mzone) {
if(pcard) {
int32 clen = pcard->get_infos(p, query_flag, use_cache);
p += clen;
} else {
buffer_write<int32_t>(p, LEN_EMPTY);
}
}
}
else if(location == LOCATION_SZONE) {
for(auto& pcard : player.list_szone) {
if(pcard) {
int32 clen = pcard->get_infos(p, query_flag, use_cache);
p += clen;
} else {
buffer_write<int32_t>(p, LEN_EMPTY);
}
}
}
else {
card_vector* lst = nullptr;
if(location == LOCATION_HAND)
lst = &player.list_hand;
else if(location == LOCATION_GRAVE)
lst = &player.list_grave;
else if(location == LOCATION_REMOVED)
lst = &player.list_remove;
else if(location == LOCATION_EXTRA)
lst = &player.list_extra;
else if(location == LOCATION_DECK)
lst = &player.list_main;
else
return LEN_FAIL;
for(auto& pcard : *lst) {
int32 clen = pcard->get_infos(p, query_flag, use_cache);
p += clen;
}
}
return (int32)(p - buf);
}
extern "C" DECL_DLLEXPORT int32 query_field_info(intptr_t pduel, byte* buf) {
duel* ptduel = (duel*)pduel;
byte* p = buf;
*p++ = MSG_RELOAD_FIELD;
*p++ = (uint8)ptduel->game_field->core.duel_rule;
for(int playerid = 0; playerid < 2; ++playerid) {
auto& player = ptduel->game_field->player[playerid];
buffer_write<int32_t>(p, player.lp);
for(auto& pcard : player.list_mzone) {
if(pcard) {
*p++ = 1;
*p++ = pcard->current.position;
*p++ = (uint8)pcard->xyz_materials.size();
} else {
*p++ = 0;
}
}
for(auto& pcard : player.list_szone) {
if(pcard) {
*p++ = 1;
*p++ = pcard->current.position;
} else {
*p++ = 0;
}
}
*p++ = (uint8)player.list_main.size();
*p++ = (uint8)player.list_hand.size();
*p++ = (uint8)player.list_grave.size();
*p++ = (uint8)player.list_remove.size();
*p++ = (uint8)player.list_extra.size();
*p++ = (uint8)player.extra_p_count;
}
*p++ = (uint8)ptduel->game_field->core.current_chain.size();
for(const auto& ch : ptduel->game_field->core.current_chain) {
effect* peffect = ch.triggering_effect;
buffer_write<uint32_t>(p, peffect->get_handler()->data.code);
buffer_write<uint32_t>(p, peffect->get_handler()->get_info_location());
*p++ = ch.triggering_controler;
*p++ = (uint8)ch.triggering_location;
*p++ = ch.triggering_sequence;
buffer_write<uint32_t>(p, peffect->description);
}
return (int32)(p - buf);
}
extern "C" DECL_DLLEXPORT void set_responsei(intptr_t pduel, int32 value) {
((duel*)pduel)->set_responsei(value);
}
extern "C" DECL_DLLEXPORT void set_responseb(intptr_t pduel, byte* buf) {
((duel*)pduel)->set_responseb(buf);
}
extern "C" DECL_DLLEXPORT int32 preload_script(intptr_t pduel, const char* script_name) {
return ((duel*)pduel)->lua->load_script(script_name);
}