-
Notifications
You must be signed in to change notification settings - Fork 31
/
duel.h
131 lines (121 loc) · 3.25 KB
/
duel.h
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
/*
* Copyright (c) 2010-2015, Argon Sun (Fluorohydride)
* Copyright (c) 2016-2024, Edoardo Lolletti (edo9300) <[email protected]>
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#ifndef DUEL_H_
#define DUEL_H_
#include <deque>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility> //std::forward
#include <vector>
#include "common.h"
#include "group.h"
#include "interpreter.h"
#include "ocgapi_types.h"
#include "RNG/Xoshiro256.hpp"
class card;
class effect;
class field;
struct loc_info;
struct card_data {
uint32_t code{};
uint32_t alias{};
std::set<uint16_t> setcodes;
uint32_t type{};
uint32_t level{};
uint32_t attribute{};
uint64_t race{};
int32_t attack{};
int32_t defense{};
uint32_t lscale{};
uint32_t rscale{};
uint32_t link_marker{};
card_data(const OCG_CardData& data);
card_data() = default;
};
class duel {
public:
class duel_message {
private:
template<typename T>
void write_internal(T data) {
write(&data, sizeof(T));
}
public:
std::vector<uint8_t> data;
explicit duel_message(uint8_t _message);
void write(const void* buff, size_t size);
void write(loc_info loc);
template<typename T, typename T2>
ForceInline void write(T2 data) {
write_internal<T>(static_cast<T>(data));
}
};
std::vector<uint8_t> buff;
std::vector<uint8_t> query_buffer;
field* game_field;
interpreter* lua;
std::unordered_set<card*> cards;
std::unordered_set<card*> assumes;
std::unordered_set<group*> groups;
std::unordered_set<group*> sgroups;
std::unordered_set<effect*> effects;
std::unordered_set<effect*> uncopy;
std::unordered_map<uint32_t, card_data> data_cache;
enum class SCRIPT_LOAD_STATUS : uint8_t {
NOT_LOADED,
LOAD_SUCCEDED,
LOAD_FAILED,
LOADING,
};
std::unordered_map<uint32_t/* hashed string */, SCRIPT_LOAD_STATUS> loaded_scripts;
duel() = delete;
explicit duel(const OCG_DuelOptions& options);
~duel();
void clear();
card* new_card(uint32_t code);
template<typename... Args>
group* new_group(Args&&... args) {
group* pgroup = new group(this, std::forward<Args>(args)...);
groups.insert(pgroup);
if(lua->call_depth)
sgroups.insert(pgroup);
lua->register_group(pgroup);
return pgroup;
}
effect* new_effect();
void delete_card(card* pcard);
void delete_group(group* pgroup);
void delete_effect(effect* peffect);
void release_script_group();
void restore_assumes();
void generate_buffer();
void write_buffer(const void* data, size_t size);
void clear_buffer();
void set_response(const void* resp, size_t len);
int32_t get_next_integer(int32_t l, int32_t h);
duel_message* new_message(uint8_t message);
const card_data& read_card(uint32_t code);
inline void handle_message(const char* message, OCG_LogTypes type) {
handle_message_callback(handle_message_payload, message, type);
}
inline int read_script(const char* name) {
return read_script_callback(read_script_payload, this, name);
}
private:
std::deque<duel_message> messages;
RNG::Xoshiro256StarStar random;
OCG_DataReader read_card_callback;
OCG_ScriptReader read_script_callback;
OCG_LogHandler handle_message_callback;
OCG_DataReaderDone read_card_done_callback;
void* read_card_payload;
void* read_script_payload;
void* handle_message_payload;
void* read_card_done_payload;
};
#endif /* DUEL_H_ */