mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2025-10-20 12:21:04 +00:00
Compare commits
8 Commits
1.5.1
...
lua-more-b
Author | SHA1 | Date | |
---|---|---|---|
d32dd4c319 | |||
c5de584762 | |||
450cc78c9a | |||
95f2920f5c | |||
27ccda5b10 | |||
![]() |
d9ebb4e3ef | ||
![]() |
73c610b471 | ||
![]() |
3e6bfea3fe |
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +1,6 @@
|
||||
[submodule "tdata"]
|
||||
path = tdata
|
||||
url = https://github.com/OpenFusionProject/tabledata.git
|
||||
[submodule "vendor/Lua"]
|
||||
path = vendor/Lua
|
||||
url = https://github.com/walterschell/Lua.git
|
||||
|
@@ -34,20 +34,28 @@ else()
|
||||
set(BIN_NAME fusion)
|
||||
endif()
|
||||
|
||||
# add lua
|
||||
option(LUA_BUILD_COMPILER OFF)
|
||||
option(LUA_ENABLE_SHARED OFF)
|
||||
option(LUA_ENABLE_TESTING OFF)
|
||||
add_subdirectory(vendor/Lua)
|
||||
|
||||
include_directories(src vendor)
|
||||
|
||||
file(GLOB_RECURSE SOURCES src/**.[ch]pp vendor/**.[ch]pp vendor/**.[ch] version.h)
|
||||
file(GLOB_RECURSE VENDOR_SOURCES vendor/bcrypt/**.[ch] vendor/mingw/**.h vendor/INIReader.hpp vendor/JSON.hpp)
|
||||
|
||||
file(GLOB_RECURSE SOURCES src/**.[ch]pp version.h)
|
||||
|
||||
configure_file(version.h.in ${CMAKE_SOURCE_DIR}/version.h @ONLY)
|
||||
|
||||
add_executable(openfusion ${SOURCES})
|
||||
add_executable(openfusion ${SOURCES} ${VENDOR_SOURCES})
|
||||
|
||||
set_target_properties(openfusion PROPERTIES OUTPUT_NAME ${BIN_NAME})
|
||||
|
||||
# find sqlite3 and use it
|
||||
find_package(SQLite3 REQUIRED)
|
||||
target_include_directories(openfusion PRIVATE ${SQLite3_INCLUDE_DIRS})
|
||||
target_link_libraries(openfusion PRIVATE ${SQLite3_LIBRARIES})
|
||||
target_link_libraries(openfusion PRIVATE lua_static ${SQLite3_LIBRARIES})
|
||||
|
||||
# Makes it so config, tdata, etc. get picked up when starting via the debugger in VS
|
||||
set_property(TARGET openfusion PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
|
||||
|
@@ -94,20 +94,49 @@ void NPCManager::sendToViewable(Entity *npc, void *buf, uint32_t type, size_t si
|
||||
static void npcBarkHandler(CNSocket* sock, CNPacketData* data) {
|
||||
sP_CL2FE_REQ_BARKER* req = (sP_CL2FE_REQ_BARKER*)data->buf;
|
||||
|
||||
// get bark IDs from task data
|
||||
TaskData* td = Missions::Tasks[req->iMissionTaskID];
|
||||
std::vector<int> barks;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (td->task["m_iHBarkerTextID"][i] != 0) // non-zeroes only
|
||||
barks.push_back(td->task["m_iHBarkerTextID"][i]);
|
||||
int taskID = req->iMissionTaskID;
|
||||
// ignore req->iNPC_ID as it is often fixated on a single npc in the region
|
||||
|
||||
if (Missions::Tasks.find(taskID) == Missions::Tasks.end()) {
|
||||
std::cout << "mission task not found: " << taskID << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (barks.empty())
|
||||
return; // no barks
|
||||
TaskData* td = Missions::Tasks[taskID];
|
||||
auto& barks = td->task["m_iHBarkerTextID"];
|
||||
|
||||
Player* plr = PlayerManager::getPlayer(sock);
|
||||
std::vector<std::pair<int32_t, int32_t>> npcLines;
|
||||
|
||||
for (Chunk* chunk : plr->viewableChunks) {
|
||||
for (auto ent = chunk->entities.begin(); ent != chunk->entities.end(); ent++) {
|
||||
if (ent->kind != EntityKind::SIMPLE_NPC)
|
||||
continue;
|
||||
|
||||
BaseNPC* npc = (BaseNPC*)ent->getEntity();
|
||||
if (npc->type < 0 || npc->type >= NPCData.size())
|
||||
continue; // npc unknown ?!
|
||||
|
||||
int barkType = NPCData[npc->type]["m_iBarkerType"];
|
||||
if (barkType < 1 || barkType > 4)
|
||||
continue; // no barks
|
||||
|
||||
int barkID = barks[barkType - 1];
|
||||
if (barkID == 0)
|
||||
continue; // no barks
|
||||
|
||||
npcLines.push_back(std::make_pair(npc->id, barkID));
|
||||
}
|
||||
}
|
||||
|
||||
if (npcLines.size() == 0)
|
||||
return; // totally no barks
|
||||
|
||||
auto& [npcID, missionStringID] = npcLines[Rand::rand(npcLines.size())];
|
||||
|
||||
INITSTRUCT(sP_FE2CL_REP_BARKER, resp);
|
||||
resp.iNPC_ID = req->iNPC_ID;
|
||||
resp.iMissionStringID = barks[Rand::rand(barks.size())];
|
||||
resp.iNPC_ID = npcID;
|
||||
resp.iMissionStringID = missionStringID;
|
||||
sock->sendPacket(resp, P_FE2CL_REP_BARKER);
|
||||
}
|
||||
|
||||
|
30
src/lua/Manager.cpp
Normal file
30
src/lua/Manager.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "lua/Manager.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace LuaManager;
|
||||
|
||||
static lua_State *globalState = nullptr;
|
||||
|
||||
void LuaManager::init() {
|
||||
lua_State *L = luaL_newstate();
|
||||
luaL_openlibs(L);
|
||||
|
||||
static LuaThread *startScript(const char *script) {
|
||||
lua_State *L = lua_newthread(globalState);
|
||||
LuaThread *T = new LuaThread(L, luaL_ref(L, LUA_REGISTRYINDEX));
|
||||
|
||||
if (luaL_dostring(L, script)) {
|
||||
std::cout << "Lua error: " << lua_tostring(L, -1) << std::endl;
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
return T;
|
||||
}
|
||||
|
||||
void LuaManager::init() {
|
||||
globalState = luaL_newstate();
|
||||
luaL_openlibs(globalState);
|
||||
|
||||
delete startScript("print(\"Hello from Lua!\")");
|
||||
}
|
7
src/lua/Manager.hpp
Normal file
7
src/lua/Manager.hpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "lua/Thread.hpp"
|
||||
|
||||
namespace LuaManager {
|
||||
void init();
|
||||
}
|
17
src/lua/Thread.hpp
Normal file
17
src/lua/Thread.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
extern "C" {
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
#include "lauxlib.h"
|
||||
}
|
||||
|
||||
struct LuaThread {
|
||||
lua_State *L;
|
||||
int ref;
|
||||
|
||||
LuaThread(lua_State *L, int ref) : L(L), ref(ref) {}
|
||||
~LuaThread() {
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, ref);
|
||||
}
|
||||
};
|
@@ -24,6 +24,8 @@
|
||||
#include "Eggs.hpp"
|
||||
#include "Rand.hpp"
|
||||
|
||||
#include "lua/Manager.hpp"
|
||||
|
||||
#include "settings.hpp"
|
||||
#include "sandbox/Sandbox.hpp"
|
||||
|
||||
@@ -138,6 +140,7 @@ int main() {
|
||||
Trading::init();
|
||||
|
||||
Database::open();
|
||||
LuaManager::init();
|
||||
|
||||
switch (settings::EVENTMODE) {
|
||||
case 0: break; // no event
|
||||
|
1
vendor/Lua
vendored
Submodule
1
vendor/Lua
vendored
Submodule
Submodule vendor/Lua added at 88246d621a
Reference in New Issue
Block a user