diff --git a/scripts/config.lua b/scripts/config.lua index 0ace9c3530..bc7ca3c124 100644 --- a/scripts/config.lua +++ b/scripts/config.lua @@ -24,7 +24,9 @@ config.defaultTimeTable = { year = 427, month = 7, day = 16, hour = 9, -- The chat window instructions that show up when players join the server config.chatWindowInstructions = "Use " .. color.Yellow .. "Y" .. color.White .. " by default to chat or change it" .. " from your client config. Type in " .. color.Yellow .. "/help" .. color.White .. " to see the commands" .. - " available to you. Use " .. color.Yellow .. "F2" .. color.White .. " by default to hide the chat window.\n" + " available to you. Type in " .. color.Yellow .. "/invite " .. color.White .. " to invite a player to your " .. + "team and make your AI followers more forgiving towards them. Use " .. color.Yellow .. "F2" .. color.White .. " by default " .. + " to hide the chat window.\n" -- Whether the world time should continue passing when there are no players on the server config.passTimeWhenEmpty = false diff --git a/scripts/defaultCommands.lua b/scripts/defaultCommands.lua index 4223db2ddc..e1757c6f18 100644 --- a/scripts/defaultCommands.lua +++ b/scripts/defaultCommands.lua @@ -33,7 +33,6 @@ defaultCommands.msg = function(pid, cmd) tes3mp.SendMessage(pid, "You cannot send a blank message.\n") elseif logicHandler.CheckPlayerValidity(pid, cmd[2]) then local targetPid = tonumber(cmd[2]) - local targetName = Players[targetPid].name message = logicHandler.GetChatName(pid) .. " to " .. logicHandler.GetChatName(targetPid) .. ": " message = message .. tableHelper.concatenateFromIndex(cmd, 3) .. "\n" tes3mp.SendMessage(pid, message, false) @@ -44,6 +43,100 @@ end customCommandHooks.registerCommand("msg", defaultCommands.msg) customCommandHooks.registerCommand("message", defaultCommands.msg) +defaultCommands.inviteToTeam = function(pid, cmd) + if pid == tonumber(cmd[2]) then + tes3mp.SendMessage(pid, "You can't invite yourself to a team.\n") + elseif logicHandler.CheckPlayerValidity(pid, cmd[2]) then + + local targetPid = tonumber(cmd[2]) + local senderMessage + + if Players[pid].teamInvitesSent == nil then Players[pid].teamInvitesSent = {} end + if Players[targetPid].teamInvitesReceived == nil then Players[targetPid].teamInvitesReceived = {} end + + if tableHelper.containsValue(Players[pid].data.teamMembers, Players[targetPid].accountName) then + senderMessage = "You are already on the team of " .. logicHandler.GetChatName(targetPid) .. "\n" + elseif tableHelper.containsValue(Players[pid].teamInvitesSent, Players[targetPid].accountName) then + senderMessage = "You have already invited " .. logicHandler.GetChatName(targetPid) .. " to your team.\n" + else + table.insert(Players[pid].teamInvitesSent, Players[targetPid].accountName) + table.insert(Players[targetPid].teamInvitesReceived, Players[pid].accountName) + + senderMessage = "You have invited " .. logicHandler.GetChatName(targetPid) .. " to your team.\n" + local receiverMessage = logicHandler.GetChatName(pid) .. " has invited you to join their team. Write " .. + color.Yellow .. "/join " .. pid .. color.White .. " to accept.\n" + tes3mp.SendMessage(targetPid, receiverMessage, false) + end + + tes3mp.SendMessage(pid, senderMessage, false) + end +end + +customCommandHooks.registerCommand("invite", defaultCommands.inviteToTeam) + +defaultCommands.joinTeam = function(pid, cmd) + if pid == tonumber(cmd[2]) then + tes3mp.SendMessage(pid, "You can't join your own team.\n") + elseif logicHandler.CheckPlayerValidity(pid, cmd[2]) then + + local targetPid = tonumber(cmd[2]) + local senderMessage + + if Players[pid].teamInvitesReceived == nil then Players[pid].teamInvitesReceived = {} end + + if tableHelper.containsValue(Players[pid].data.teamMembers, Players[targetPid].accountName) then + senderMessage = "You are already on the team of " .. logicHandler.GetChatName(targetPid) .. "\n" + elseif tableHelper.containsValue(Players[pid].teamInvitesReceived, Players[targetPid].accountName) then + senderMessage = "You are now on the team of " .. logicHandler.GetChatName(targetPid) .. ". Write " .. + color.Yellow .. "/leave " .. targetPid .. color.White .. " if you later decide to leave it.\n" + local receiverMessage = logicHandler.GetChatName(targetPid) .. " has joined your team.\n" + tes3mp.SendMessage(targetPid, receiverMessage, false) + + table.insert(Players[pid].data.teamMembers, Players[targetPid].accountName) + table.insert(Players[targetPid].data.teamMembers, Players[pid].accountName) + Players[pid]:Save() + Players[pid]:LoadTeam() + Players[targetPid]:Save() + Players[targetPid]:LoadTeam() + else + senderMessage = "You have not yet been invited to the team of " .. logicHandler.GetChatName(targetPid) .. "\n" + end + + tes3mp.SendMessage(pid, senderMessage, false) + end +end + +customCommandHooks.registerCommand("join", defaultCommands.joinTeam) + +defaultCommands.leaveTeam = function(pid, cmd) + if pid == tonumber(cmd[2]) then + tes3mp.SendMessage(pid, "You can't leave your own team.\n") + elseif logicHandler.CheckPlayerValidity(pid, cmd[2]) then + + local targetPid = tonumber(cmd[2]) + local senderMessage + + if tableHelper.containsValue(Players[pid].data.teamMembers, Players[targetPid].accountName) then + senderMessage = "You have now left the team of " .. logicHandler.GetChatName(targetPid) .. "\n" + local receiverMessage = logicHandler.GetChatName(targetPid) .. " has left your team.\n" + tes3mp.SendMessage(targetPid, receiverMessage, false) + + tableHelper.removeValue(Players[pid].data.teamMembers, Players[targetPid].accountName) + tableHelper.removeValue(Players[targetPid].data.teamMembers, Players[pid].accountName) + Players[pid]:Save() + Players[pid]:LoadTeam() + Players[targetPid]:Save() + Players[targetPid]:LoadTeam() + else + senderMessage = "You are not on the team of " .. logicHandler.GetChatName(targetPid) .. "\n" + end + + tes3mp.SendMessage(pid, senderMessage, false) + end +end + +customCommandHooks.registerCommand("leave", defaultCommands.leaveTeam) + defaultCommands.me = function(pid, cmd) local message = logicHandler.GetChatName(pid) .. " " .. tableHelper.concatenateFromIndex(cmd, 2) .. "\n" tes3mp.SendMessage(pid, message, true) diff --git a/scripts/menu/help.lua b/scripts/menu/help.lua index 8e671c4adf..713f2673dd 100644 --- a/scripts/menu/help.lua +++ b/scripts/menu/help.lua @@ -1,5 +1,12 @@ Menus["help player"] = { text = color.Orange .. "Player command list:\n" .. + color.Yellow .. "/invite \n" .. + color.White .. "Invite a player to your team, with your AI followers being more forgiving towards " .. + "players on your team\n" .. + color.Yellow .. "/join \n" .. + color.White .. "Accept an invitation to join a player's team\n" .. + color.Yellow .. "/leave \n" .. + color.White .. "Leave a player's team\n" .. color.Yellow .. "/message \n" .. color.White .. "Send a private message to a player " .. color.Yellow .. "(/msg)\n" .. diff --git a/scripts/player/base.lua b/scripts/player/base.lua index 844a80e3dc..cc2e62b0ab 100644 --- a/scripts/player/base.lua +++ b/scripts/player/base.lua @@ -83,6 +83,7 @@ function BasePlayer:__init(pid, playerName) mapExplored = {}, ipAddresses = {}, recordLinks = {}, + teamMembers = {}, customVariables = {} } @@ -266,9 +267,19 @@ function BasePlayer:FinishLogin() self:LoadClientScriptVariables() WorldInstance:LoadClientScriptVariables(self.pid) + self:LoadTeam() self:LoadCell() self.loggedIn = true + + if self.data.teamMembers == nil then self.data.teamMembers = {} end + + for _, otherAccountName in ipairs(self.data.teamMembers) do + if logicHandler.IsPlayerNameLoggedIn(otherAccountName) then + local otherPlayer = logicHandler.GetPlayerByName(otherAccountName) + otherPlayer:LoadTeam() + end + end customEventHooks.triggerHandlers("OnPlayerFinishLogin", customEventHooks.makeEventStatus(true, true), {self.pid}) customEventHooks.triggerHandlers("OnPlayerAuthentified", customEventHooks.makeEventStatus(true, true), {self.pid}) @@ -1312,6 +1323,22 @@ function BasePlayer:LoadMap() stateHelper:LoadMap(self.pid, self) end +function BasePlayer:LoadTeam() + + if self.data.teamMembers == nil then self.data.teamMembers = {} end + + tes3mp.ClearTeamMembersForPlayer(self.pid) + + for _, otherAccountName in ipairs(self.data.teamMembers) do + if logicHandler.IsPlayerNameLoggedIn(otherAccountName) then + local otherPlayer = logicHandler.GetPlayerByName(otherAccountName) + tes3mp.AddTeamMemberForPlayer(self.pid, otherPlayer.pid) + end + end + + tes3mp.SendTeam(self.pid) +end + function BasePlayer:LoadBooks() if self.data.books == nil then self.data.books = {} end