171 lines
5.9 KiB
C++
171 lines
5.9 KiB
C++
#include "protocol.h"
|
|
#include "eter_net/Packet.h"
|
|
#include <algorithm>
|
|
#include <stdexcept>
|
|
#include <cstring>
|
|
|
|
namespace ava2::wire {
|
|
namespace {
|
|
void u16(Bytes& out, std::uint16_t v) {
|
|
out.push_back(static_cast<std::uint8_t>(v & 0xFF));
|
|
out.push_back(static_cast<std::uint8_t>((v >> 8) & 0xFF));
|
|
}
|
|
void u32(Bytes& out, std::uint32_t v) {
|
|
for (int shift=0; shift<32; shift+=8) out.push_back(static_cast<std::uint8_t>(v >> shift));
|
|
}
|
|
}
|
|
|
|
Bytes select_character(std::uint8_t slot) {
|
|
if (slot >= 5) throw std::out_of_range("AVA2 character slot must be 0..4");
|
|
return {HEADER_CG_CHARACTER_SELECT, slot};
|
|
}
|
|
|
|
namespace {
|
|
// strncpy into a zeroed char[width]: at most width-1 bytes keep the terminator (CHARACTER_NAME_MAX_LEN+1 field).
|
|
void fixed(Bytes& out, const std::string& value, std::size_t width, std::size_t copy) {
|
|
auto start=out.size(); out.resize(start+width,0);
|
|
std::copy_n(value.begin(), std::min({value.size(), copy, width}), out.begin()+start);
|
|
}
|
|
}
|
|
|
|
Bytes create_character(std::uint8_t slot, const std::string& name, std::uint16_t job, std::uint8_t shape,
|
|
std::uint8_t con, std::uint8_t intelligence, std::uint8_t str, std::uint8_t dex) {
|
|
Bytes out{HEADER_CG_PLAYER_CREATE, slot};
|
|
// strncpy(name, name, CHARACTER_NAME_MAX_LEN): the 25th byte stays 0.
|
|
fixed(out, name, CHARACTER_NAME_MAX_LEN+1, CHARACTER_NAME_MAX_LEN);
|
|
u16(out, job);
|
|
out.insert(out.end(), {shape, con, intelligence, str, dex});
|
|
return out;
|
|
}
|
|
|
|
Bytes destroy_character(std::uint8_t slot, const std::string& private_code) {
|
|
Bytes out{HEADER_CG_PLAYER_DESTROY, slot};
|
|
fixed(out, private_code, 8, 8-1); // strncpy(szPrivateCode, code, PRIVATE_CODE_LENGTH-1)
|
|
return out;
|
|
}
|
|
|
|
Bytes select_empire(std::uint8_t empire) { return {HEADER_CG_EMPIRE, empire}; }
|
|
|
|
Bytes change_name(std::uint8_t slot, const std::string& name) {
|
|
Bytes out{HEADER_CG_CHANGE_NAME, slot};
|
|
fixed(out, name, CHARACTER_NAME_MAX_LEN+1, CHARACTER_NAME_MAX_LEN);
|
|
return out;
|
|
}
|
|
|
|
Bytes attack(std::uint8_t type, std::uint32_t victim, std::uint8_t process_crc, std::uint8_t file_crc) {
|
|
Bytes out{HEADER_CG_ATTACK, type};
|
|
u32(out, victim);
|
|
out.push_back(process_crc);
|
|
out.push_back(file_crc);
|
|
return out;
|
|
}
|
|
|
|
Bytes pickup(std::uint32_t vid) {
|
|
Bytes out{HEADER_CG_ITEM_PICKUP};
|
|
u32(out, vid);
|
|
return out;
|
|
}
|
|
|
|
Bytes character_move(std::uint8_t func, std::uint8_t arg, std::uint8_t rot, std::int32_t x, std::int32_t y, std::uint32_t time) {
|
|
Bytes out{HEADER_CG_MOVE, func, arg, rot};
|
|
u32(out, static_cast<std::uint32_t>(x));
|
|
u32(out, static_cast<std::uint32_t>(y));
|
|
u32(out, time);
|
|
return out;
|
|
}
|
|
|
|
Bytes item_use(std::uint8_t window_type, std::uint16_t cell) {
|
|
Bytes out{HEADER_CG_ITEM_USE, window_type};
|
|
u16(out, cell);
|
|
return out;
|
|
}
|
|
|
|
Bytes item_move(std::uint8_t src_window, std::uint16_t src_cell, std::uint8_t dst_window, std::uint16_t dst_cell, std::uint32_t count) {
|
|
Bytes out{HEADER_CG_ITEM_MOVE, src_window};
|
|
u16(out, src_cell);
|
|
out.push_back(dst_window);
|
|
u16(out, dst_cell);
|
|
u32(out, count);
|
|
return out;
|
|
}
|
|
|
|
Bytes item_drop(std::uint8_t window_type, std::uint16_t cell, std::uint32_t count) {
|
|
Bytes out{HEADER_CG_ITEM_DROP, window_type};
|
|
u16(out, cell);
|
|
u32(out, count);
|
|
return out;
|
|
}
|
|
|
|
Bytes chat(const std::string& text, std::uint8_t type) {
|
|
if(text.empty() || text.size()>=512) return {};
|
|
Bytes out{HEADER_CG_CHAT};
|
|
std::uint16_t size = static_cast<std::uint16_t>(5 + text.size());
|
|
u16(out, size);
|
|
out.push_back(type);
|
|
out.insert(out.end(), text.begin(), text.end());
|
|
out.push_back(0);
|
|
return out;
|
|
}
|
|
|
|
Bytes whisper(const std::string& name, const std::string& text) {
|
|
if(text.size()>=255) return {};
|
|
Bytes out{HEADER_CG_WHISPER};
|
|
std::uint16_t size = static_cast<std::uint16_t>(3 + 25 + text.size() + 1);
|
|
u16(out, size);
|
|
char nameBuf[25] = {0};
|
|
std::strncpy(nameBuf, name.c_str(), sizeof(nameBuf) - 1);
|
|
out.insert(out.end(), nameBuf, nameBuf + sizeof(nameBuf));
|
|
out.insert(out.end(), text.begin(), text.end());
|
|
out.push_back(0);
|
|
return out;
|
|
}
|
|
|
|
Bytes mobile_message(const std::string& name,const std::string& text) {
|
|
// AVA2 TPacketCGSMS: BYTE + WORD + char[25], followed by NUL text.
|
|
if(text.size()>65535-29) return {}; // Do not emit a wrapped WORD frame.
|
|
Bytes out{HEADER_CG_SMS};u16(out,static_cast<std::uint16_t>(29+text.size()));
|
|
char recipient[25]{};std::strncpy(recipient,name.c_str(),24);
|
|
out.insert(out.end(),recipient,recipient+25);
|
|
out.insert(out.end(),text.begin(),text.end());out.push_back(0);return out;
|
|
}
|
|
Bytes attribute67(int material,int support,int support_pos,int item_pos) {
|
|
Bytes out{HEADER_CG_67_ATTR,uint8_t(material),uint8_t(support)};
|
|
u16(out,uint16_t(support_pos));u16(out,uint16_t(item_pos));return out;
|
|
}
|
|
Bytes calendar(uint8_t subheader,int day,int event,const std::string& start,const std::string& end) {
|
|
Bytes out{HEADER_CG_CALENDAR_SYSTEM,subheader};
|
|
u32(out,uint32_t(day));u32(out,uint32_t(event));
|
|
for(const auto& text:{start,end}) {
|
|
char value[6]{};std::strncpy(value,text.c_str(),6);out.insert(out.end(),value,value+6);
|
|
}
|
|
return out;
|
|
}
|
|
Bytes catch_king(uint8_t subheader,int argument) {
|
|
Bytes out{HEADER_CG_MINI_GAME_CATCH_KING,uint8_t(subheader==0||subheader==2?5:4),0,subheader};
|
|
if(subheader==0 || subheader==2)out.push_back(uint8_t(argument));return out;
|
|
}
|
|
|
|
Bytes entergame(std::uint8_t language) {
|
|
return {HEADER_CG_ENTERGAME, language};
|
|
}
|
|
|
|
Bytes on_click(std::uint32_t vid) {
|
|
Bytes out{HEADER_CG_ON_CLICK};
|
|
u32(out, vid);
|
|
return out;
|
|
}
|
|
|
|
Bytes warp() {
|
|
return {HEADER_CG_WARP};
|
|
}
|
|
|
|
Bytes fly_targeting(std::uint32_t target_vid, std::int32_t x, std::int32_t y) {
|
|
Bytes out{HEADER_CG_FLY_TARGETING};
|
|
u32(out, target_vid);
|
|
u32(out, static_cast<std::uint32_t>(x));
|
|
u32(out, static_cast<std::uint32_t>(y));
|
|
return out;
|
|
}
|
|
|
|
} // namespace ava2::wire
|