Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create NPC /n #3059

Open
2 of 5 tasks
Kadeosek opened this issue Nov 3, 2024 · 1 comment
Open
2 of 5 tasks

Create NPC /n #3059

Kadeosek opened this issue Nov 3, 2024 · 1 comment
Labels
Priority: Medium This issue may be impactful and needs some attention. Status: Pending Test This PR or Issue requires more testing Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors.

Comments

@Kadeosek
Copy link

Kadeosek commented Nov 3, 2024

Priority

Medium

Area

  • Datapack
  • Source
  • Map
  • Other

What happened?

Command in talkactions is bugger
Everytime if use /n is info There is not enough room.

I fix it.

-- Add new NPC entry
local textToAdd = string.format('\t\n\t\t\n\t', position.x, position.y, position.z, name, position.z)
local newFileContent = fileContent:gsub(endTag, textToAdd .. "\n" .. endTag)

Full script create_npc.lua
-- To summon a temporary NPC use /n npcname
-- To summon a permanent NPC use /n npcname,true

local createNpc = TalkAction("/n")

function createNpc.onSay(player, words, param)
-- create log
logCommand(player, words, param)

if param == "" then
	player:sendCancelMessage("Command parameter required: /n npcname or /n npcname,true for permanent NPC.")
	return true
end

-- Split parameters
local split = param:split(",")
local name = split[1]
local permanentStr = split[2]

-- Check if there's enough room for NPC creation
local position = player:getPosition()
local npc = Game.createNpc(name, position)

if npc then
	npc:setMasterPos(position)
	position:sendMagicEffect(CONST_ME_MAGIC_RED)

	-- Adding permanent NPC to XML
	if permanentStr and permanentStr == "true" then
		local mapName = configManager.getString(configKeys.MAP_NAME)
		local mapNpcsPath = mapName .. "-npc.xml"
		local filePath = string.format("%s/world/%s", DATA_DIRECTORY, mapNpcsPath)

		-- Open NPC file for reading
		local npcsFile = io.open(filePath, "r")
		if not npcsFile then
			player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Error: NPC file not found.")
			return true
		end

		-- Read and modify NPC file content
		local fileContent = npcsFile:read("*all")
		npcsFile:close()

		local endTag = "</npcs>"
		if not fileContent:find(endTag, 1, true) then
			player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Error: NPC file format is incorrect. Missing end tag ".. endTag ..".")
			return true
		end

		-- Add new NPC entry
		local textToAdd = string.format('\t<npc centerx="%i" centery="%i" centerz="%i" radius="1">\n\t\t<npc name="%s" x="0" y="0" z="%i" spawntime="60" />\n\t</npc>', position.x, position.y, position.z, name, position.z)
		local newFileContent = fileContent:gsub(endTag, textToAdd .. "\n" .. endTag)

		-- Write updated content back to file
		npcsFile = io.open(filePath, "w")
		if not npcsFile then
			player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Error: Could not write to the NPC file.")
			return true
		end
		npcsFile:write(newFileContent)
		npcsFile:close()

		player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Permanent NPC added successfully.")
	end
else
	player:sendCancelMessage("There is not enough room to summon NPC.")
	position:sendMagicEffect(CONST_ME_POFF)
end
return true

end

createNpc:separator(" ")
createNpc:groupType("god")
createNpc:register()

What OS are you seeing the problem on?

Linux

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Kadeosek Kadeosek added the Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors. label Nov 3, 2024
@github-actions github-actions bot added Priority: Medium This issue may be impactful and needs some attention. Status: Pending Test This PR or Issue requires more testing labels Nov 3, 2024
@lBaah
Copy link

lBaah commented Dec 8, 2024

Yes, theres a issue with new NPCs that you have added and not spawned by the startup (map load). These changes bring /n to these NPC.

Npc.cpp

std::shared_ptr<Npc> Npc::createNpc(const std::string &name, bool create) {
	auto npcType = g_npcs().getNpcType(name);
	if (!npcType && !create) {
		return nullptr;
	} else if (!npcType && create) {
		npcType = g_npcs().getNpcType(name, create);
	}

	if (!npcType) {
		return nullptr;
	}

	return std::make_shared<Npc>(npcType);
}

game_functions.cpp

int GameFunctions::luaGameCreateNpc(lua_State* L) {
	// Game.createNpc(npcName, position[, extended = false[, force = false]])
	const auto &npc = Npc::createNpc(getString(L, 1), true);
	if (!npc) {
		g_logger().error("[{}] NPC is null.", __FUNCTION__);
		lua_pushnil(L);
		return 1;
	}

	const Position &position = getPosition(L, 2);
	const bool extended = getBoolean(L, 3, false);
	const bool force = getBoolean(L, 4, false);
	if (g_game().placeCreature(npc, position, extended, force)) {
		pushUserdata<Npc>(L, npc);
		setMetatable(L, -1, "Npc");
	} else {
		g_logger().error("[{}] Cannot place npc.", __FUNCTION__);
		lua_pushnil(L);
	}
	return 1;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: Medium This issue may be impactful and needs some attention. Status: Pending Test This PR or Issue requires more testing Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors.
Projects
None yet
Development

No branches or pull requests

2 participants