require("color") require("config") class = require("classy") tableHelper = require("tableHelper") require("utils") require("time") logicHandler = require("logicHandler") eventHandler = require("eventHandler") guiHelper = require("guiHelper") animHelper = require("animHelper") speechHelper = require("speechHelper") menuHelper = require("menuHelper") Database = nil Player = nil Cell = nil RecordStore = nil World = nil hourCounter = nil frametimeMultiplier = nil updateTimerId = nil banList = {} pluginList = {} if (config.databaseType ~= nil and config.databaseType ~= "json") and doesModuleExist("luasql." .. config.databaseType) then Database = require("database") Database:LoadDriver(config.databaseType) tes3mp.LogMessage(1, "Using " .. Database.driver._VERSION .. " with " .. config.databaseType .. " driver") Database:Connect(config.databasePath) -- Make sure we enable foreign keys Database:Execute("PRAGMA foreign_keys = ON;") Database:CreatePlayerTables() Database:CreateWorldTables() Player = require("player.sql") Cell = require("cell.sql") RecordStore = require("recordstore.sql") World = require("world.sql") else Player = require("player.json") Cell = require("cell.json") RecordStore = require("recordstore.json") World = require("world.json") end function LoadBanList() tes3mp.LogMessage(2, "Reading banlist.json") banList = jsonInterface.load("banlist.json") if banList.playerNames == nil then banList.playerNames = {} elseif banList.ipAddresses == nil then banList.ipAddresses = {} end if #banList.ipAddresses > 0 then local message = "- Banning manually-added IP addresses:\n" for index, ipAddress in pairs(banList.ipAddresses) do message = message .. ipAddress if index < #banList.ipAddresses then message = message .. ", " end tes3mp.BanAddress(ipAddress) end tes3mp.LogAppend(2, message) end if #banList.playerNames > 0 then local message = "- Banning all IP addresses stored for players:\n" for index, targetName in pairs(banList.playerNames) do message = message .. targetName if index < #banList.playerNames then message = message .. ", " end local targetPlayer = logicHandler.GetPlayerByName(targetName) if targetPlayer ~= nil then for index, ipAddress in pairs(targetPlayer.data.ipAddresses) do tes3mp.BanAddress(ipAddress) end end end tes3mp.LogAppend(2, message) end end function SaveBanList() jsonInterface.save("banlist.json", banList) end function LoadPluginList() tes3mp.LogMessage(2, "Reading pluginlist.json") local jsonPluginList = jsonInterface.load("pluginlist.json") -- Fix numerical keys to print plugins in the correct order tableHelper.fixNumericalKeys(jsonPluginList, true) for listIndex, pluginEntry in ipairs(jsonPluginList) do for entryIndex, hashArray in pairs(pluginEntry) do pluginList[listIndex] = {entryIndex} io.write(("%d, {%s"):format(listIndex, entryIndex)) for _, hash in ipairs(hashArray) do io.write((", %X"):format(tonumber(hash, 16))) table.insert(pluginList[listIndex], tonumber(hash, 16)) end table.insert(pluginList[listIndex], "") io.write("}\n") end end end do local adminsCounter = 0 function IncrementAdminCounter() adminsCounter = adminsCounter + 1 tes3mp.SetRuleValue("adminsOnline", adminsCounter) end function DecrementAdminCounter() adminsCounter = adminsCounter - 1 tes3mp.SetRuleValue("adminsOnline", adminsCounter) end function ResetAdminCounter() adminsCounter = 0 tes3mp.SetRuleValue("adminsOnline", adminsCounter) end end do local previousHourFloor = nil function UpdateTime() if config.passTimeWhenEmpty or tableHelper.getCount(Players) > 0 then hourCounter = hourCounter + (0.0083 * frametimeMultiplier) local hourFloor = math.floor(hourCounter) if previousHourFloor == nil then previousHourFloor = hourFloor elseif hourFloor > previousHourFloor then if hourFloor >= 24 then hourCounter = hourCounter - hourFloor hourFloor = 0 tes3mp.LogMessage(2, "The world time day has been incremented") WorldInstance:IncrementDay() end tes3mp.LogMessage(2, "The world time hour is now " .. hourFloor) WorldInstance.data.time.hour = hourCounter WorldInstance:Save() if tableHelper.getCount(Players) > 0 then WorldInstance:LoadTime(tableHelper.getAnyValue(Players).pid, true) end previousHourFloor = hourFloor end end tes3mp.RestartTimer(updateTimerId, time.seconds(1)) end end function OnServerInit() tes3mp.LogMessage(1, "Called \"OnServerInit\"") local expectedVersionPrefix = "0.7.0" local serverVersion = tes3mp.GetServerVersion() if string.sub(serverVersion, 1, string.len(expectedVersionPrefix)) ~= expectedVersionPrefix then tes3mp.LogAppend(3, "- Version mismatch between server and Core scripts!") tes3mp.LogAppend(3, "- The Core scripts require a server version that starts with " .. expectedVersionPrefix) tes3mp.StopServer(1) end logicHandler.InitializeWorld() local recordStoreTypes = { "armor", "book", "clothing", "creature", "enchantment", "miscellaneous", "npc", "potion", "spell", "weapon" } for _, recordStoreType in ipairs(recordStoreTypes) do logicHandler.LoadRecordStore(recordStoreType) end hourCounter = WorldInstance.data.time.hour frametimeMultiplier = WorldInstance.data.time.timeScale / WorldInstance.defaultTimeScale updateTimerId = tes3mp.CreateTimer("UpdateTime", time.seconds(1)) tes3mp.StartTimer(updateTimerId) logicHandler.PushPlayerList(Players) LoadBanList() LoadPluginList() tes3mp.SetPluginEnforcementState(config.enforcePlugins) end function OnServerPostInit() tes3mp.LogMessage(1, "Called \"OnServerPostInit\"") tes3mp.SetGameMode(config.gameMode) local consoleRuleString = "allowed" if not config.allowConsole then consoleRuleString = "not " .. consoleRuleString end local bedRestRuleString = "allowed" if not config.allowBedRest then bedRestRuleString = "not " .. bedRestRuleString end local wildRestRuleString = "allowed" if not config.allowWildernessRest then wildRestRuleString = "not " .. wildRestRuleString end local waitRuleString = "allowed" if not config.allowWait then waitRuleString = "not " .. waitRuleString end tes3mp.SetRuleString("enforcePlugins", tostring(config.enforcePlugins)) tes3mp.SetRuleValue("difficulty", config.difficulty) tes3mp.SetRuleValue("deathPenaltyJailDays", config.deathPenaltyJailDays) tes3mp.SetRuleString("console", consoleRuleString) tes3mp.SetRuleString("bedResting", bedRestRuleString) tes3mp.SetRuleString("wildernessResting", wildRestRuleString) tes3mp.SetRuleString("waiting", waitRuleString) tes3mp.SetRuleValue("enforcedLogLevel", config.enforcedLogLevel) tes3mp.SetRuleValue("physicsFramerate", config.physicsFramerate) tes3mp.SetRuleString("spawnCell", tostring(config.defaultSpawnCell)) tes3mp.SetRuleString("shareJournal", tostring(config.shareJournal)) tes3mp.SetRuleString("shareFactionRanks", tostring(config.shareFactionRanks)) tes3mp.SetRuleString("shareFactionExpulsion", tostring(config.shareFactionExpulsion)) tes3mp.SetRuleString("shareFactionReputation", tostring(config.shareFactionReputation)) tes3mp.SetRuleString("shareTopics", tostring(config.shareTopics)) tes3mp.SetRuleString("shareBounty", tostring(config.shareBounty)) tes3mp.SetRuleString("shareReputation", tostring(config.shareReputation)) tes3mp.SetRuleString("shareMapExploration", tostring(config.shareMapExploration)) tes3mp.SetRuleString("enablePlacedObjectCollision", tostring(config.enablePlacedObjectCollision)) local respawnCell if config.respawnAtImperialShrine == true then respawnCell = "nearest Imperial shrine" if config.respawnAtTribunalTemple == true then respawnCell = respawnCell .. " or Tribunal temple" end elseif config.respawnAtTribunalTemple == true then respawnCell = "nearest Tribunal temple" else respawnCell = tostring(config.defaultRespawnCell) end tes3mp.SetRuleString("respawnCell", respawnCell) ResetAdminCounter() end function OnServerExit(error) tes3mp.LogMessage(1, "Called \"OnServerExit\"") tes3mp.LogMessage(3, tostring(error)) end function OnRequestPluginList(id, field) id = id + 1 field = field + 1 if #pluginList < id then return "" end return pluginList[id][field] end function OnPlayerConnect(pid) tes3mp.LogMessage(1, "Called \"OnPlayerConnect\" for pid " .. pid) local playerName = tes3mp.GetName(pid) if string.len(playerName) > 35 then playerName = string.sub(playerName, 0, 35) end if logicHandler.IsNameAllowed(playerName) == false then local message = playerName .. " (" .. pid .. ") " .. "joined and tried to use a disallowed name.\n" tes3mp.SendMessage(pid, message, true) return false -- deny player elseif logicHandler.IsPlayerNameLoggedIn(playerName) then local message = playerName .. " (" .. pid .. ") " .. "joined and tried to use an existing player's name.\n" tes3mp.SendMessage(pid, message, true) return false -- deny player else tes3mp.LogAppend(1, "- New player is named " .. playerName) eventHandler.OnPlayerConnect(pid, playerName) return true -- accept player end end function OnLoginTimeExpiration(pid) -- timer-based event, see eventHandler.OnPlayerConnect if logicHandler.AuthCheck(pid) then if Players[pid]:IsModerator() then IncrementAdminCounter() end end end function OnPlayerDisconnect(pid) tes3mp.LogMessage(1, "Called \"OnPlayerDisconnect\" for " .. logicHandler.GetChatName(pid)) local message = logicHandler.GetChatName(pid) .. " left the server.\n" tes3mp.SendMessage(pid, message, true) eventHandler.OnPlayerDisconnect(pid) DecrementAdminCounter() end function OnPlayerResurrect(pid) end function OnPlayerSendMessage(pid, message) return eventHandler.OnPlayerSendMessage(pid, message) end function OnObjectLoopTimeExpiration(loopIndex) eventHandler.OnObjectLoopTimeExpiration(loopIndex) end function OnDeathTimeExpiration(pid) eventHandler.OnDeathTimeExpiration(pid) end function OnPlayerDeath(pid) tes3mp.LogMessage(1, "Called \"OnPlayerDeath\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerDeath(pid) end function OnPlayerAttribute(pid) tes3mp.LogMessage(1, "Called \"OnPlayerAttribute\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerAttribute(pid) end function OnPlayerSkill(pid) eventHandler.OnPlayerSkill(pid) end function OnPlayerLevel(pid) tes3mp.LogMessage(1, "Called \"OnPlayerLevel\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerLevel(pid) end function OnPlayerShapeshift(pid) tes3mp.LogMessage(1, "Called \"OnPlayerShapeshift\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerShapeshift(pid) end function OnPlayerCellChange(pid) tes3mp.LogMessage(1, "Called \"OnPlayerCellChange\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerCellChange(pid) end function OnPlayerEquipment(pid) tes3mp.LogMessage(1, "Called \"OnPlayerEquipment\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerEquipment(pid) end function OnPlayerInventory(pid) tes3mp.LogMessage(1, "Called \"OnPlayerInventory\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerInventory(pid) end function OnPlayerSpellbook(pid) tes3mp.LogMessage(1, "Called \"OnPlayerSpellbook\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerSpellbook(pid) end function OnPlayerQuickKeys(pid) tes3mp.LogMessage(1, "Called \"OnPlayerQuickKeys\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerQuickKeys(pid) end function OnPlayerJournal(pid) tes3mp.LogMessage(1, "Called \"OnPlayerJournal\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerJournal(pid) end function OnPlayerFaction(pid) tes3mp.LogMessage(1, "Called \"OnPlayerFaction\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerFaction(pid) end function OnPlayerTopic(pid) tes3mp.LogMessage(1, "Called \"OnPlayerTopic\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerTopic(pid) end function OnPlayerBounty(pid) tes3mp.LogMessage(1, "Called \"OnPlayerBounty\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerBounty(pid) end function OnPlayerReputation(pid) tes3mp.LogMessage(1, "Called \"OnPlayerReputation\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerReputation(pid) end function OnPlayerBook(pid) tes3mp.LogMessage(1, "Called \"OnPlayerBook\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerBook(pid) end function OnPlayerItemUse(pid) tes3mp.LogMessage(1, "Called \"OnPlayerItemUse\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerItemUse(pid) end function OnPlayerMiscellaneous(pid) tes3mp.LogMessage(1, "Called \"OnPlayerMiscellaneous\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerMiscellaneous(pid) end function OnPlayerEndCharGen(pid) tes3mp.LogMessage(1, "Called \"OnPlayerEndCharGen\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnPlayerEndCharGen(pid) end function OnCellLoad(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnCellLoad\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnCellLoad(pid, cellDescription) end function OnCellUnload(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnCellUnload\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnCellUnload(pid, cellDescription) end function OnCellDeletion(cellDescription) tes3mp.LogMessage(1, "Called \"OnCellDeletion\" for cell " .. cellDescription) eventHandler.OnCellDeletion(cellDescription) end function OnActorList(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnActorList\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnActorList(pid, cellDescription) end function OnActorEquipment(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnActorEquipment\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnActorEquipment(pid, cellDescription) end function OnActorAI(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnActorAI\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnActorAI(pid, cellDescription) end function OnActorDeath(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnActorDeath\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnActorDeath(pid, cellDescription) end function OnActorCellChange(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnActorCellChange\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnActorCellChange(pid, cellDescription) end function OnObjectActivate(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnObjectActivate\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnObjectActivate(pid, cellDescription) end function OnObjectPlace(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnObjectPlace\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnObjectPlace(pid, cellDescription) end function OnObjectSpawn(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnObjectSpawn\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnObjectSpawn(pid, cellDescription) end function OnObjectDelete(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnObjectDelete\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnObjectDelete(pid, cellDescription) end function OnObjectLock(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnObjectLock\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnObjectLock(pid, cellDescription) end function OnObjectTrap(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnObjectTrap\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnObjectTrap(pid, cellDescription) end function OnObjectScale(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnObjectScale\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnObjectScale(pid, cellDescription) end function OnObjectState(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnObjectState\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnObjectState(pid, cellDescription) end function OnDoorState(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnDoorState\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnDoorState(pid, cellDescription) end function OnContainer(pid, cellDescription) tes3mp.LogMessage(1, "Called \"OnContainer\" for " .. logicHandler.GetChatName(pid) .. " and cell " .. cellDescription) eventHandler.OnContainer(pid, cellDescription) end function OnVideoPlay(pid) tes3mp.LogMessage(1, "Called \"OnVideoPlay\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnVideoPlay(pid) end function OnRecordDynamic(pid) tes3mp.LogMessage(1, "Called \"OnRecordDynamic\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnRecordDynamic(pid) end function OnWorldKillCount(pid) tes3mp.LogMessage(1, "Called \"OnWorldKillCount\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnWorldKillCount(pid) end function OnWorldMap(pid) tes3mp.LogMessage(1, "Called \"OnWorldMap\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnWorldMap(pid) end function OnWorldWeather(pid) tes3mp.LogMessage(1, "Called \"OnWorldWeather\" for " .. logicHandler.GetChatName(pid)) eventHandler.OnWorldWeather(pid) end function OnGUIAction(pid, idGui, data) tes3mp.LogMessage(1, "Called \"OnGUIAction\" for " .. logicHandler.GetChatName(pid)) if eventHandler.OnGUIAction(pid, idGui, data) then return end -- if eventHandler.OnGUIAction is called end function OnMpNumIncrement(currentMpNum) eventHandler.OnMpNumIncrement(currentMpNum) end