112 lines
3.0 KiB
C++
112 lines
3.0 KiB
C++
#include "protocol.h"
|
|
#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 {6, slot};
|
|
}
|
|
|
|
Bytes attack(std::uint8_t type, std::uint32_t victim, std::uint8_t process_crc, std::uint8_t file_crc) {
|
|
Bytes out{225, type};
|
|
u32(out, victim);
|
|
out.push_back(process_crc);
|
|
out.push_back(file_crc);
|
|
return out;
|
|
}
|
|
|
|
Bytes pickup(std::uint32_t vid) {
|
|
Bytes out{15};
|
|
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{7, 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{11, 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{13, 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{224, 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{231};
|
|
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{19};
|
|
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 entergame(std::uint8_t language) {
|
|
return {10,language};
|
|
}
|
|
|
|
Bytes on_click(std::uint32_t vid) {
|
|
Bytes out{26};
|
|
u32(out, vid);
|
|
return out;
|
|
}
|
|
|
|
Bytes warp() {
|
|
return {65};
|
|
}
|
|
|
|
Bytes fly_targeting(std::uint32_t target_vid, std::int32_t x, std::int32_t y) {
|
|
Bytes out{51};
|
|
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
|