-
Notifications
You must be signed in to change notification settings - Fork 135
/
duel.cpp
149 lines (147 loc) · 3.36 KB
/
duel.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
/*
* duel.cpp
*
* Created on: 2010-5-2
* Author: Argon
*/
#include <cstring>
#include "duel.h"
#include "interpreter.h"
#include "field.h"
#include "card.h"
#include "effect.h"
#include "group.h"
#include "ocgapi.h"
#include "buffer.h"
duel::duel() {
lua = new interpreter(this);
game_field = new field(this);
game_field->temp_card = new_card(0);
message_buffer.reserve(SIZE_MESSAGE_BUFFER);
#ifdef _WIN32
_set_error_mode(_OUT_TO_MSGBOX);
#endif // _WIN32
}
duel::~duel() {
for(auto& pcard : cards)
delete pcard;
for(auto& pgroup : groups)
delete pgroup;
for(auto& peffect : effects)
delete peffect;
delete lua;
delete game_field;
}
void duel::clear() {
for(auto& pcard : cards)
delete pcard;
for(auto& pgroup : groups)
delete pgroup;
for(auto& peffect : effects)
delete peffect;
delete game_field;
cards.clear();
groups.clear();
effects.clear();
game_field = new field(this);
game_field->temp_card = new_card(0);
}
card* duel::new_card(uint32 code) {
card* pcard = new card(this);
cards.insert(pcard);
if(code)
::read_card(code, &(pcard->data));
pcard->data.code = code;
lua->register_card(pcard);
return pcard;
}
group* duel::register_group(group* pgroup) {
groups.insert(pgroup);
if(lua->call_depth)
sgroups.insert(pgroup);
lua->register_group(pgroup);
return pgroup;
}
group* duel::new_group() {
group* pgroup = new group(this);
return register_group(pgroup);
}
group* duel::new_group(card* pcard) {
group* pgroup = new group(this, pcard);
return register_group(pgroup);
}
group* duel::new_group(const card_set& cset) {
group* pgroup = new group(this, cset);
return register_group(pgroup);
}
effect* duel::new_effect() {
effect* peffect = new effect(this);
effects.insert(peffect);
lua->register_effect(peffect);
return peffect;
}
void duel::delete_card(card* pcard) {
cards.erase(pcard);
delete pcard;
}
void duel::delete_group(group* pgroup) {
lua->unregister_group(pgroup);
groups.erase(pgroup);
sgroups.erase(pgroup);
delete pgroup;
}
void duel::delete_effect(effect* peffect) {
lua->unregister_effect(peffect);
effects.erase(peffect);
delete peffect;
}
int32 duel::read_buffer(byte* buf) {
auto size = buffer_size();
if (size)
std::memcpy(buf, message_buffer.data(), size);
return (int32)size;
}
void duel::release_script_group() {
for(auto& pgroup : sgroups) {
if(pgroup->is_readonly == GTYPE_DEFAULT) {
lua->unregister_group(pgroup);
groups.erase(pgroup);
delete pgroup;
}
}
sgroups.clear();
}
void duel::restore_assumes() {
for(auto& pcard : assumes)
pcard->assume_type = 0;
assumes.clear();
}
void duel::write_buffer(const void* data, int size) {
vector_write_block(message_buffer, data, size);
}
void duel::write_buffer32(uint32 value) {
vector_write<uint32_t>(message_buffer, value);
}
void duel::write_buffer16(uint16 value) {
vector_write<uint16_t>(message_buffer, value);
}
void duel::write_buffer8(uint8 value) {
vector_write<unsigned char>(message_buffer, value);
}
void duel::clear_buffer() {
message_buffer.clear();
}
void duel::set_responsei(uint32 resp) {
game_field->returns.ivalue[0] = resp;
}
void duel::set_responseb(byte* resp) {
std::memcpy(game_field->returns.bvalue, resp, SIZE_RETURN_VALUE);
}
int32 duel::get_next_integer(int32 l, int32 h) {
if (game_field->core.duel_options & DUEL_OLD_REPLAY) {
return random.get_random_integer_old(l, h);
}
else {
return random.get_random_integer(l, h);
}
}