Just some dumb refactoring

This commit is contained in:
Knaapchen 2022-11-11 23:53:44 +01:00
parent 46d77d4f89
commit 440a89ffae
6 changed files with 316 additions and 264 deletions

View File

@ -37,6 +37,12 @@ namespace flyff {
Recovery = 14, Recovery = 14,
Quest = 25, Quest = 25,
}; };
static std::ostream &operator<<(std::ostream &os, const ItemKind2 &kind) {
os << "ItemKind2(" << static_cast<unsigned>(kind) << ")";
return os;
}
}; };

View File

@ -80,7 +80,14 @@ namespace flyff {
Knuckle = 203, Knuckle = 203,
Shield = 300, Shield = 300,
HealthPill = 1404
}; };
static std::ostream &operator<<(std::ostream &os, const ItemKind3 &kind) {
os << "ItemKind3(" << static_cast<unsigned>(kind) << ")";
return os;
}
}; };

View File

@ -418,34 +418,36 @@ void on_keyup_hook(int event_type, const EmscriptenKeyboardEvent *event, void *u
// client.show_announcement("Hello world!", {0xFF, 0, 0}); // client.show_announcement("Hello world!", {0xFF, 0, 0});
std::cout << "Player object ID: " << std::hex << client.player->object_id << std::dec << std::endl; // std::cout << "Player object ID: " << std::hex << client.player->object_id << std::dec << std::endl;
auto current = client.movers.first; // auto current = client.movers.first;
do { // do {
const auto &mover = current->mover; // const auto &mover = current->mover;
if (mover->type == flyff::ObjectType::Item) {
std::cout << "Item "
<< std::hex << mover->object_id << std::dec
<< " at " << mover->position
<< " (" << std::hex << static_cast<u32>(mover) << std::dec << ")"
<< std::endl;
}
current = current->next;
} while (current);
// auto items = client.player->inventory.begin();
// std::cout << "Player has " << client.player->get_inventory_slots() << " slots" << std::endl;
// for (auto i = 0; i < client.player->get_inventory_slots(); i++) {
// if (items[i].property) {
// const auto &item = items[i].property;
// //
// std::cout << client.get_text(item->name) << ": " // if (mover->type == flyff::ObjectType::Item) {
// << reinterpret_cast<const void *>(static_cast<u32>(item)) << ", " // std::cout << "Item "
// << std::hex << mover->object_id << std::dec
// << " at " << mover->position
// << " (" << std::hex << static_cast<u32>(mover) << std::dec << ")"
// << std::endl; // << std::endl;
// }
// //
// } // current = current->next;
// } // } while (current);
auto items = client.player->inventory.begin();
std::cout << "Player has " << client.player->get_inventory_slots() << " slots" << std::endl;
for (auto i = 0; i < client.player->get_inventory_slots(); i++) {
if (items[i].property) {
const auto &item = items[i].property;
std::cout << client.get_text(item->name) << ": "
<< reinterpret_cast<const void *>(static_cast<u32>(item)) << ", "
<< "Ik2: " << item->item_kind2 << ", "
<< "Ik3: " << item->item_kind3 << ", "
<< std::endl;
}
}
} }
} }
} }
@ -578,10 +580,10 @@ void attacker_script() {
// attack_target() // attack_target()
if (neuz.player->get_hp_percent() != 100) { if (neuz.player->get_hp_percent() != 100) {
auto food_item = flyff::Pointer<flyff::InventoryItem>(nullptr);
{
auto items = player->inventory.begin(); auto items = player->inventory.begin();
int32_t smallest_difference = std::numeric_limits<int32_t>::max(); int32_t smallest_difference = std::numeric_limits<int32_t>::max();
auto food_item = flyff::Pointer<flyff::InventoryItem>(nullptr);
for (auto i = 0; i < player->get_inventory_slots(); i++) { for (auto i = 0; i < player->get_inventory_slots(); i++) {
const auto &item_property = items[i].property; const auto &item_property = items[i].property;
if (item_property && if (item_property &&
@ -598,18 +600,51 @@ void attacker_script() {
} }
} }
} }
}
if (food_item) { if (food_item) {
std::string str = "Eating "; std::string str = "Eating ";
str.append(neuz.get_text(food_item->property->name)); str.append(neuz.get_text(food_item->property->name));
str.append(" because it has the smallest difference of ");
str.append(std::to_string(smallest_difference));
neuz.show_message(str); neuz.show_message(str);
client.send_use_item_in_inventory(food_item->index); client.send_use_item_in_inventory(food_item->index);
} else {
// HP is not full but no food item found.
auto pill_item = flyff::Pointer<flyff::InventoryItem>(nullptr);
{
auto items = player->inventory.begin();
int32_t smallest_difference = std::numeric_limits<int32_t>::max();
for (auto i = 0; i < player->get_inventory_slots(); i++) {
const auto &item_property = items[i].property;
if (item_property &&
player->can_use_item(item_property) &&
item_property->item_kind3 == flyff::ItemKind3::HealthPill) {
auto hp = client.player->get_hp();
auto max_hp = client.player->get_max_hp();
// How much HP is left over after healing with this item.
int32_t difference = max_hp - (hp + item_property->heal_hp);
if (difference >= 0 && std::abs(difference) < std::abs(smallest_difference)) {
pill_item = flyff::Pointer<flyff::InventoryItem>(&items[i]);
smallest_difference = difference;
} }
} }
}
}
if(pill_item) {
std::string str = "Eating ";
str.append(neuz.get_text(pill_item->property->name));
str.append(" because food item was not ready.");
neuz.show_message(str);
client.send_use_item_in_inventory(pill_item->index);
}
}
}
if (neuz.player->get_move_state() == flyff::Object::MoveState::Stand && if (neuz.player->get_move_state() == flyff::Object::MoveState::Stand &&
!neuz.player->is_attacking()) { !neuz.player->is_attacking()) {

View File

@ -274,7 +274,8 @@ WASM_IMPORT_IMPL(env, _glUniform3fv) = [](u32 a, u32 b, u32 c) {
/* import: 'a' 'K' */ /* import: 'a' 'K' */
// "K": "_glTexImage2D", // "K": "_glTexImage2D",
WASM_IMPORT_IMPL(env, _glTexImage2D) = [](u32 target, u32 level, u32 internalformat, u32 d, u32 e, u32 f, u32 g, u32 h, u32 i) { WASM_IMPORT_IMPL(env, _glTexImage2D) = [](u32 target, u32 level, u32 internalformat, u32 d, u32 e, u32 f, u32 g, u32 h,
u32 i) {
const auto &pixels = fugg::ModulePointer<const void>(i); const auto &pixels = fugg::ModulePointer<const void>(i);
#if TRACE_GL_CALLS #if TRACE_GL_CALLS
@ -788,7 +789,8 @@ WASM_IMPORT_IMPL(env, _glUniform4f) = [](u32 a, f32 b, f32 c, f32 d, f32 e) {
}; };
/* import: 'a' 'lb' */ /* import: 'a' 'lb' */
// "lb": "_glCompressedTexImage2D", // "lb": "_glCompressedTexImage2D",
WASM_IMPORT_IMPL(env, _glCompressedTexImage2D) = [](u32 target, u32 level, u32 internalformat, u32 width, u32 height, u32 border, u32 image_size, u32 h) { WASM_IMPORT_IMPL(env, _glCompressedTexImage2D) = [](u32 target, u32 level, u32 internalformat, u32 width, u32 height,
u32 border, u32 image_size, u32 h) {
const auto &data = fugg::ModulePointer<const void>(h); const auto &data = fugg::ModulePointer<const void>(h);
#if TRACE_GL_CALLS #if TRACE_GL_CALLS
@ -1155,7 +1157,8 @@ WASM_IMPORT_IMPL(env, _emscripten_webgl_create_context) = [](u32 a, u32 b) {
#endif #endif
auto attr = static_cast<const EmscriptenWebGLContextAttributes *>(attributes); auto attr = static_cast<const EmscriptenWebGLContextAttributes *>(attributes);
std::cout << "Trying to create a context with version " << attr->majorVersion << "." << attr->minorVersion << std::endl; std::cout << "Trying to create a context with version " << attr->majorVersion << "." << attr->minorVersion
<< std::endl;
return static_cast<u32>( return static_cast<u32>(
emscripten_webgl_create_context(target, attributes) emscripten_webgl_create_context(target, attributes)

View File

@ -1,10 +1,12 @@
#include <wasm-rt.h> #include <wasm-rt.h>
#include <client.h> #include <client.h>
#include <fugg.h> #include <fugg.h>
#include <flyff.h>
#include <emscripten/bind.h> #include <emscripten/bind.h>
#include <iostream> #include <iostream>
#include <utility>
#include "embind.h" #include "embind.h"
@ -24,7 +26,7 @@ void platform_text_edited_hook(u32 ptr, u32 selection_start, u32 selection_end)
} }
void platform_got_devicemotion_permission_hook() { void platform_got_devicemotion_permission_hook() {
using PlatformGotDeviceMotionPermissionFn = void (*)(void); using PlatformGotDeviceMotionPermissionFn = void (*)();
auto platform_got_devicemotion_permission = fugg::function_ref<PlatformGotDeviceMotionPermissionFn>( auto platform_got_devicemotion_permission = fugg::function_ref<PlatformGotDeviceMotionPermissionFn>(
fugg::embind::kFunctionMap["platform_got_devicemotion_permission"]->fn fugg::embind::kFunctionMap["platform_got_devicemotion_permission"]->fn
); );
@ -32,13 +34,13 @@ void platform_got_devicemotion_permission_hook() {
platform_got_devicemotion_permission(); platform_got_devicemotion_permission();
} }
void platform_captcha_completed_hook(std::string token) { void platform_captcha_completed_hook(const std::string& token) {
struct WireType { struct WireType {
size_t length; size_t length;
char data[1]; char data[1];
}; };
WireType* wt = (WireType*)malloc(sizeof(size_t) + token.length()); auto* wt = (WireType*)malloc(sizeof(size_t) + token.length());
wt->length = token.length(); wt->length = token.length();
memcpy(wt->data, token.data(), token.length()); memcpy(wt->data, token.data(), token.length());
@ -57,7 +59,6 @@ void platform_captcha_completed_hook(std::string token) {
platform_captcha_completed, platform_captcha_completed,
fugg::RuntimePointer<WireType>(wt) fugg::RuntimePointer<WireType>(wt)
); );
} }
using PlatformWebsocketFn = void (*)(emscripten::val event); using PlatformWebsocketFn = void (*)(emscripten::val event);
@ -69,7 +70,7 @@ void platform_websocket_open_hook(emscripten::val event) {
fugg::embind::kFunctionMap["platform_websocket_open"]->fn fugg::embind::kFunctionMap["platform_websocket_open"]->fn
); );
platform_websocket_open(event); platform_websocket_open(std::move(event));
} }
void platform_websocket_close_hook(emscripten::val event) { void platform_websocket_close_hook(emscripten::val event) {
@ -79,10 +80,10 @@ void platform_websocket_close_hook(emscripten::val event) {
fugg::embind::kFunctionMap["platform_websocket_close"]->fn fugg::embind::kFunctionMap["platform_websocket_close"]->fn
); );
platform_websocket_close(event); platform_websocket_close(std::move(event));
} }
void platform_websocket_message_hook(emscripten::val event) { void platform_websocket_message_hook(const emscripten::val& event) {
// std::cout << "platform_websocket_message: Got " << event["data"]["byteLength"].as<unsigned long>() << " bytes" << std::endl; // std::cout << "platform_websocket_message: Got " << event["data"]["byteLength"].as<unsigned long>() << " bytes" << std::endl;
struct Test { struct Test {

View File

@ -54,7 +54,7 @@ void free(void *ptr_) {
emscripten_builtin_free(ptr_); emscripten_builtin_free(ptr_);
#if TRACE_ALLOC #if TRACE_ALLOC
std::cout << "FUGG: Free'd OUTSIDE pointer at " << reinterpret_cast<void*>(ptr) << std::endl; std::cout << "FUGG: Free'd OUTSIDE pointer at " << reinterpret_cast<void*>(ptr_) << std::endl;
#endif #endif
} }
} }