5 Commits

Author SHA1 Message Date
d32dd4c319 added startScript() 2023-12-30 01:53:07 -06:00
c5de584762 define LuaManager::init 2023-12-30 01:00:27 -06:00
450cc78c9a added globalState; minor styling fixes 2023-12-30 00:56:06 -06:00
95f2920f5c start LuaManager
for now it runs a simple test script on init()
2023-12-13 16:42:40 -06:00
27ccda5b10 added Lua to vendor sources
- TODO: add Lua to our makefile eventually
2023-12-13 16:33:32 -06:00
20 changed files with 88 additions and 59 deletions

3
.gitmodules vendored
View File

@@ -1,3 +1,6 @@
[submodule "tdata"] [submodule "tdata"]
path = tdata path = tdata
url = https://github.com/OpenFusionProject/tabledata.git url = https://github.com/OpenFusionProject/tabledata.git
[submodule "vendor/Lua"]
path = vendor/Lua
url = https://github.com/walterschell/Lua.git

View File

@@ -34,20 +34,28 @@ else()
set(BIN_NAME fusion) set(BIN_NAME fusion)
endif() 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) 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) 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}) set_target_properties(openfusion PROPERTIES OUTPUT_NAME ${BIN_NAME})
# find sqlite3 and use it # find sqlite3 and use it
find_package(SQLite3 REQUIRED) find_package(SQLite3 REQUIRED)
target_include_directories(openfusion PRIVATE ${SQLite3_INCLUDE_DIRS}) 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 # 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}") set_property(TARGET openfusion PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")

View File

@@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2020-2024 OpenFusion Contributors Copyright (c) 2020-2023 OpenFusion Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@@ -66,9 +66,6 @@ motd=Welcome to OpenFusion!
# location of the database # location of the database
#dbpath=database.db #dbpath=database.db
# should there be a score cap for infected zone races?
#izracescorecapped=true
# should tutorial flags be disabled off the bat? # should tutorial flags be disabled off the bat?
disablefirstuseflag=true disablefirstuseflag=true

View File

@@ -16,9 +16,8 @@ EntityRef::EntityRef(CNSocket *s) {
EntityRef::EntityRef(int32_t i) { EntityRef::EntityRef(int32_t i) {
id = i; id = i;
kind = EntityKind::INVALID; assert(NPCManager::NPCs.find(id) != NPCManager::NPCs.end());
if (NPCManager::NPCs.find(id) != NPCManager::NPCs.end()) kind = NPCManager::NPCs[id]->kind;
kind = NPCManager::NPCs[id]->kind;
} }
bool EntityRef::isValid() const { bool EntityRef::isValid() const {

View File

@@ -64,9 +64,6 @@ static void attachGroupData(std::vector<EntityRef>& pcs, std::vector<EntityRef>&
} }
void Groups::addToGroup(Group* group, EntityRef member) { void Groups::addToGroup(Group* group, EntityRef member) {
if (group == nullptr)
return;
if (member.kind == EntityKind::PLAYER) { if (member.kind == EntityKind::PLAYER) {
Player* plr = PlayerManager::getPlayer(member.sock); Player* plr = PlayerManager::getPlayer(member.sock);
plr->group = group; plr->group = group;
@@ -112,9 +109,6 @@ void Groups::addToGroup(Group* group, EntityRef member) {
} }
bool Groups::removeFromGroup(Group* group, EntityRef member) { bool Groups::removeFromGroup(Group* group, EntityRef member) {
if (group == nullptr)
return false;
if (member.kind == EntityKind::PLAYER) { if (member.kind == EntityKind::PLAYER) {
Player* plr = PlayerManager::getPlayer(member.sock); Player* plr = PlayerManager::getPlayer(member.sock);
plr->group = nullptr; // no dangling pointers here muahaahahah plr->group = nullptr; // no dangling pointers here muahaahahah
@@ -174,9 +168,6 @@ bool Groups::removeFromGroup(Group* group, EntityRef member) {
} }
void Groups::disbandGroup(Group* group) { void Groups::disbandGroup(Group* group) {
if (group == nullptr)
return;
// remove everyone from the group!! // remove everyone from the group!!
bool done = false; bool done = false;
while(!done) { while(!done) {
@@ -261,9 +252,6 @@ static void leaveGroup(CNSocket* sock, CNPacketData* data) {
} }
void Groups::sendToGroup(Group* group, void* buf, uint32_t type, size_t size) { void Groups::sendToGroup(Group* group, void* buf, uint32_t type, size_t size) {
if (group == nullptr)
return;
auto players = group->filter(EntityKind::PLAYER); auto players = group->filter(EntityKind::PLAYER);
for (EntityRef ref : players) { for (EntityRef ref : players) {
ref.sock->sendPacket(buf, type, size); ref.sock->sendPacket(buf, type, size);
@@ -271,9 +259,6 @@ void Groups::sendToGroup(Group* group, void* buf, uint32_t type, size_t size) {
} }
void Groups::sendToGroup(Group* group, EntityRef excluded, void* buf, uint32_t type, size_t size) { void Groups::sendToGroup(Group* group, EntityRef excluded, void* buf, uint32_t type, size_t size) {
if (group == nullptr)
return;
auto players = group->filter(EntityKind::PLAYER); auto players = group->filter(EntityKind::PLAYER);
for (EntityRef ref : players) { for (EntityRef ref : players) {
if(ref != excluded) ref.sock->sendPacket(buf, type, size); if(ref != excluded) ref.sock->sendPacket(buf, type, size);
@@ -309,9 +294,6 @@ void Groups::groupTickInfo(CNSocket* sock) {
void Groups::groupKick(Group* group, EntityRef ref) { void Groups::groupKick(Group* group, EntityRef ref) {
if (group == nullptr)
return;
// if you are the group leader, destroy your own group and kick everybody // if you are the group leader, destroy your own group and kick everybody
if (group->members[0] == ref) { if (group->members[0] == ref) {
disbandGroup(group); disbandGroup(group);

View File

@@ -416,9 +416,6 @@ static void itemDeleteHandler(CNSocket* sock, CNPacketData* data) {
Player* plr = PlayerManager::getPlayer(sock); Player* plr = PlayerManager::getPlayer(sock);
if (itemdel->iSlotNum < 0 || itemdel->iSlotNum >= AINVEN_COUNT)
return; // sanity check
resp.eIL = itemdel->eIL; resp.eIL = itemdel->eIL;
resp.iSlotNum = itemdel->iSlotNum; resp.iSlotNum = itemdel->iSlotNum;

View File

@@ -386,9 +386,6 @@ static void taskStart(CNSocket* sock, CNPacketData* data) {
static void taskEnd(CNSocket* sock, CNPacketData* data) { static void taskEnd(CNSocket* sock, CNPacketData* data) {
sP_CL2FE_REQ_PC_TASK_END* missionData = (sP_CL2FE_REQ_PC_TASK_END*)data->buf; sP_CL2FE_REQ_PC_TASK_END* missionData = (sP_CL2FE_REQ_PC_TASK_END*)data->buf;
if (Missions::Tasks.find(missionData->iTaskNum) == Missions::Tasks.end())
return;
TaskData* task = Missions::Tasks[missionData->iTaskNum]; TaskData* task = Missions::Tasks[missionData->iTaskNum];
// handle timed mission failure // handle timed mission failure

View File

@@ -110,7 +110,7 @@ void Nanos::summonNano(CNSocket *sock, int slot, bool silent) {
} }
static void setNanoSkill(CNSocket* sock, sP_CL2FE_REQ_NANO_TUNE* skill) { static void setNanoSkill(CNSocket* sock, sP_CL2FE_REQ_NANO_TUNE* skill) {
if (skill == nullptr || skill->iNanoID >= NANO_COUNT || skill->iNanoID < 0) if (skill->iNanoID >= NANO_COUNT)
return; return;
Player *plr = PlayerManager::getPlayer(sock); Player *plr = PlayerManager::getPlayer(sock);

View File

@@ -106,11 +106,10 @@ static void racingEnd(CNSocket* sock, CNPacketData* data) {
int timeDiff = now - epRace.startTime; int timeDiff = now - epRace.startTime;
int podsCollected = epRace.collectedRings.size(); int podsCollected = epRace.collectedRings.size();
int score = std::exp( int score = std::min(epInfo.maxScore, (int)std::exp(
(epInfo.podFactor * podsCollected) / epInfo.maxPods (epInfo.podFactor * podsCollected) / epInfo.maxPods
- (epInfo.timeFactor * timeDiff) / epInfo.maxTime - (epInfo.timeFactor * timeDiff) / epInfo.maxTime
+ epInfo.scaleFactor); + epInfo.scaleFactor));
score = (settings::IZRACESCORECAPPED && score > epInfo.maxScore) ? epInfo.maxScore : score;
int fm = (1.0 + std::exp(epInfo.scaleFactor - 1.0) * epInfo.podFactor * podsCollected) / epInfo.maxPods; int fm = (1.0 + std::exp(epInfo.scaleFactor - 1.0) * epInfo.podFactor * podsCollected) / epInfo.maxPods;
// we submit the ranking first... // we submit the ranking first...
@@ -133,9 +132,8 @@ static void racingEnd(CNSocket* sock, CNPacketData* data) {
std::vector<int>* rankRewards = &EPRewards[epInfo.EPID].second; std::vector<int>* rankRewards = &EPRewards[epInfo.EPID].second;
// top ranking // top ranking
int maxRank = rankScores->size() - 1;
int topRank = 0; int topRank = 0;
while (topRank < maxRank && rankScores->at(topRank) > topRankingPlayer.Score) while (rankScores->at(topRank) > topRankingPlayer.Score)
topRank++; topRank++;
resp.iEPTopRank = topRank + 1; resp.iEPTopRank = topRank + 1;
@@ -145,7 +143,7 @@ static void racingEnd(CNSocket* sock, CNPacketData* data) {
// this ranking // this ranking
int rank = 0; int rank = 0;
while (rank < maxRank && rankScores->at(rank) > postRanking.Score) while (rankScores->at(rank) > postRanking.Score)
rank++; rank++;
resp.iEPRank = rank + 1; resp.iEPRank = rank + 1;

View File

@@ -50,15 +50,13 @@ time_t getTime();
time_t getTimestamp(); time_t getTimestamp();
void terminate(int); void terminate(int);
// The PROTOCOL_VERSION definition can be defined by the build system. // The PROTOCOL_VERSION definition is defined by the build system.
#if !defined(PROTOCOL_VERSION) #if !defined(PROTOCOL_VERSION)
#define PROTOCOL_VERSION 104
#endif
#if PROTOCOL_VERSION == 104
#include "structs/0104.hpp" #include "structs/0104.hpp"
#elif PROTOCOL_VERSION == 728 #elif PROTOCOL_VERSION == 728
#include "structs/0728.hpp" #include "structs/0728.hpp"
#elif PROTOCOL_VERSION == 104
#include "structs/0104.hpp"
#elif PROTOCOL_VERSION == 1013 #elif PROTOCOL_VERSION == 1013
#include "structs/1013.hpp" #include "structs/1013.hpp"
#else #else

View File

@@ -226,11 +226,10 @@ static void createTables() {
static int getTableSize(std::string tableName) { static int getTableSize(std::string tableName) {
std::lock_guard<std::mutex> lock(dbCrit); // XXX std::lock_guard<std::mutex> lock(dbCrit); // XXX
// you aren't allowed to bind the table name const char* sql = "SELECT COUNT(*) FROM ?";
const char* sql = "SELECT COUNT(*) FROM ";
tableName.insert(0, sql);
sqlite3_stmt* stmt; sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, tableName.c_str(), -1, &stmt, NULL); sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, tableName.c_str(), -1, NULL);
sqlite3_step(stmt); sqlite3_step(stmt);
int result = sqlite3_column_int(stmt, 0); int result = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
@@ -272,12 +271,12 @@ void Database::open() {
int players = getTableSize("Players"); int players = getTableSize("Players");
std::string message = ""; std::string message = "";
if (accounts > 0) { if (accounts > 0) {
message += ": Found " + std::to_string(accounts) + " account(s)"; message += ": Found " + std::to_string(accounts) + " Account";
if (accounts > 1) if (accounts > 1)
message += "s"; message += "s";
} }
if (players > 0) { if (players > 0) {
message += " and " + std::to_string(players) + " player(s)"; message += " and " + std::to_string(players) + " Player Character";
if (players > 1) if (players > 1)
message += "s"; message += "s";
} }

View File

@@ -208,8 +208,6 @@ void Database::addBlock(int playerId, int blockedPlayerId) {
} }
void Database::removeBlock(int playerId, int blockedPlayerId) { void Database::removeBlock(int playerId, int blockedPlayerId) {
std::lock_guard<std::mutex> lock(dbCrit);
const char* sql = R"( const char* sql = R"(
DELETE FROM Blocks DELETE FROM Blocks
WHERE PlayerID = ? AND BlockedPlayerID = ?; WHERE PlayerID = ? AND BlockedPlayerID = ?;

30
src/lua/Manager.cpp Normal file
View 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
View File

@@ -0,0 +1,7 @@
#pragma once
#include "lua/Thread.hpp"
namespace LuaManager {
void init();
}

17
src/lua/Thread.hpp Normal file
View 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);
}
};

View File

@@ -24,6 +24,8 @@
#include "Eggs.hpp" #include "Eggs.hpp"
#include "Rand.hpp" #include "Rand.hpp"
#include "lua/Manager.hpp"
#include "settings.hpp" #include "settings.hpp"
#include "sandbox/Sandbox.hpp" #include "sandbox/Sandbox.hpp"
@@ -138,6 +140,7 @@ int main() {
Trading::init(); Trading::init();
Database::open(); Database::open();
LuaManager::init();
switch (settings::EVENTMODE) { switch (settings::EVENTMODE) {
case 0: break; // no event case 0: break; // no event

View File

@@ -67,9 +67,6 @@ int settings::MONITORINTERVAL = 5000;
// event mode settings // event mode settings
int settings::EVENTMODE = 0; int settings::EVENTMODE = 0;
// race settings
bool settings::IZRACESCORECAPPED = true;
void settings::init() { void settings::init() {
INIReader reader("config.ini"); INIReader reader("config.ini");
@@ -114,7 +111,6 @@ void settings::init() {
EVENTMODE = reader.GetInteger("shard", "eventmode", EVENTMODE); EVENTMODE = reader.GetInteger("shard", "eventmode", EVENTMODE);
DISABLEFIRSTUSEFLAG = reader.GetBoolean("shard", "disablefirstuseflag", DISABLEFIRSTUSEFLAG); DISABLEFIRSTUSEFLAG = reader.GetBoolean("shard", "disablefirstuseflag", DISABLEFIRSTUSEFLAG);
ANTICHEAT = reader.GetBoolean("shard", "anticheat", ANTICHEAT); ANTICHEAT = reader.GetBoolean("shard", "anticheat", ANTICHEAT);
IZRACESCORECAPPED = reader.GetBoolean("shard", "izracescorecapped", IZRACESCORECAPPED);
MONITORENABLED = reader.GetBoolean("monitor", "enabled", MONITORENABLED); MONITORENABLED = reader.GetBoolean("monitor", "enabled", MONITORENABLED);
MONITORPORT = reader.GetInteger("monitor", "port", MONITORPORT); MONITORPORT = reader.GetInteger("monitor", "port", MONITORPORT);
MONITORINTERVAL = reader.GetInteger("monitor", "interval", MONITORINTERVAL); MONITORINTERVAL = reader.GetInteger("monitor", "interval", MONITORINTERVAL);

View File

@@ -38,7 +38,6 @@ namespace settings {
extern int MONITORPORT; extern int MONITORPORT;
extern int MONITORINTERVAL; extern int MONITORINTERVAL;
extern bool DISABLEFIRSTUSEFLAG; extern bool DISABLEFIRSTUSEFLAG;
extern bool IZRACESCORECAPPED;
void init(); void init();
} }

1
vendor/Lua vendored Submodule

Submodule vendor/Lua added at 88246d621a