-
Notifications
You must be signed in to change notification settings - Fork 23
/
duel_build.py
118 lines (115 loc) · 3.76 KB
/
duel_build.py
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
from cffi import FFI
ffibuilder = FFI()
ffibuilder.set_source("_duel",
r"""
#include "ocgapi.h"
#include "card.h"
#include "duel.h"
#include "field.h"
#include <vector>
int32 is_declarable(card_data const& cd, const std::vector<uint32>& opcode);
int32 declarable(card_data *cd, int32 size, uint32 *array) {
std::vector<uint32> v;
for (int i=0; i < size; i++) {
v.push_back(array[i]);
}
return is_declarable(*cd, v);
}
// modified from query_card()
uint32 query_linked_zone(intptr_t pduel, uint8 playerid, uint8 location, uint8 sequence) {
if(playerid != 0 && playerid != 1)
return 0;
duel* ptduel = (duel*)pduel;
card* pcard = 0;
location &= 0x7f;
if(location & LOCATION_ONFIELD)
pcard = ptduel->game_field->get_field_card(playerid, location, sequence);
else {
field::card_vector* lst = 0;
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;
if(!lst || sequence > lst->size())
pcard = 0;
else {
auto cit = lst->begin();
for(uint32 i = 0; i < sequence; ++i, ++cit);
pcard = *cit;
}
}
if(pcard)
return pcard->get_linked_zone();
else {
return 0;
}
}
void set_setcode(struct card_data *cd, uint64 value) {
cd->set_setcode(value);
}
""",
libraries = ['ygo'],
library_dirs=['.'],
source_extension='.cpp',
include_dirs=['../ygopro-core', './core'],
extra_compile_args=['-std=c++0x'],
extra_link_args=['-Wl,-rpath,.'],
)
ffibuilder.cdef("""
typedef long ptr;
typedef uint32_t uint32;
typedef int32_t int32;
typedef uint8_t uint8;
typedef int32_t int32;
typedef uint8_t byte;
typedef uint64_t uint64;
struct card_data {
uint32 code;
uint32 alias;
uint16_t setcode[16];
uint32 type;
uint32 level;
uint32 attribute;
uint32 race;
int32 attack;
int32 defense;
uint32 lscale;
uint32 rscale;
uint32 link_marker;
...;
};
extern "Python" uint32 card_reader_callback(uint32, struct card_data *);
typedef uint32 (*card_reader)(uint32, struct card_data*);
void set_card_reader(card_reader f);
void set_setcode(struct card_data *cd, uint64 value);
typedef byte* (*script_reader)(const char*, int*);
typedef uint32 (*message_handler)(intptr_t, uint32);
extern "Python" uint32 message_handler_callback (intptr_t, uint32);
void set_message_handler(message_handler f);
extern "Python" byte *script_reader_callback(const char *, int *);
void set_script_reader(script_reader f);
ptr create_duel(uint32_t seed);
void start_duel(ptr pduel, int32 options);
void end_duel(ptr pduel);
void get_log_message(ptr pduel, char* buf);
int32 get_message(ptr pduel, byte* buf);
uint32 process(ptr pduel);
void new_card(ptr pduel, uint32 code, uint8 owner, uint8 playerid, uint8 location, uint8 sequence, uint8 position);
void new_tag_card(ptr pduel, uint32 code, uint8 owner, uint8 location);
void set_player_info(ptr pduel, int32 playerid, int32 lp, int32 startcount, int32 drawcount);
void set_responsei(ptr pduel, int32 value);
void set_responseb(ptr pduel, byte *value);
int32 query_card(ptr pduel, uint8 playerid, uint8 location, uint8 sequence, int32 query_flag, byte* buf, int32 use_cache);
int32 query_field_count(ptr pduel, uint8 playerid, uint8 location);
int32 query_field_card(ptr pduel, uint8 playerid, uint8 location, int32 query_flag, byte* buf, int32 use_cache);
uint32 query_linked_zone(ptr pduel, uint8 playerid, uint8 location, uint8 sequence);
int32 declarable(struct card_data *cd, int32 size, uint32 *array);
""")
if __name__ == "__main__":
ffibuilder.compile(verbose=True)