System outfit?w

Status
Zamknięty.

Elf

Sing blue silver
Przyjaciel forum
Dołączył
Kwiecień 12, 2008
Posty
409
Liczba reakcji
50
Wiek
35
Cze?? [you], pozwol? sobie zacz?? od wst?pu:

Jest to system pozwalaj?cy w prosty spos?b tworzy? NPC bez znajomo?ci j?zyka skryptowego Lua oraz funkcji otserv. Zosta? on napisany przeze mnie, gdy na WypasOTS postanowi?em rozsypa? sprzedawc? addon?w na ka?dy addon u innego NPC, a nie chcia?o mi si? pisa? czy kopiowa? skrypt?w lub nawet po prostu ?adowa? bazowego pliku do ka?dego z nich. Jednak addony to nie wszystko- system obs?uguje r?wnie? wr?czanie outfit?w samych w sobie, nie tylko addon?w! Wa?ne: zosta? on do??czony do TFSa 0.3.6.



Jak to dzia?a?
Wystarczy, ?e za?adujemy NPC podstawowy skrypt, kt?ry pozwala na korzystanie z systemu autorstwa Jiddo.

Kod:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Varkhal" script="default.lua" walkinterval="2000" floorchange="0">
	<health now="100" max="100"/>
	<look type="134" head="78" body="88" legs="0" feet="88" addons="3"/>
        <parameters>
                <parameter key="module_outfit" value="1"/>
                <parameter key="outfits" value="druid paws,druid bear paws,druid wolf paws"/>
                <parameter key="outfit1" value="10,1,yes;5897,50,wolf paws;5896,50,bear paws"/>
        </parameters>
</npc>

Dzi?ki czemu uzyskujemy takowe efekty...

Gdy chcemy dowiedzie? si?, co oferuje NPC:
Kod:
10:10 Varkhal: Welcome, Elf! I have been expecting you.
10:10 Elf [62]: outfits
10:10 Varkhal: I can dress you into druid paws.

Gdy chcemy zakupi? np. addon:
Kod:
10:12 Varkhal: Welcome, Elf! I have been expecting you.
10:12 Elf [62]: druid paws
10:12 Varkhal: Do you want druid paws addon for 50 wolf paws, 50 bear paws?
10:12 Elf [62]: yes
10:12 Varkhal: You alrady have this addon!

... gdzie dzia?aj? r?wnie? pods?owa kluczowe
Kod:
10:13 Elf [62]: druid bear paws
10:13 Varkhal: Do you want druid bear paws addon for 50 wolf paws, 50 bear paws?

Poza podstawami, mo?emy r?wnie? w prosty spos?b wymaga? r?wnie? pieni?dzy (nie konkretnych monet), sprawdzenia, czy gracz posiada dany outfit (wliczaj?c w to addon, jak np. w przypadku zdobywania addon?w do outfitu wizard), warto?? storage (np. po wykonaniu questa) lub nawet ustawi? graczowi storage (np. by m?g? kontynuowa? questa).


Cze?? techniczna
- Deklaracja modu?u:
Kod:
<parameter key="module_outfit" value="1"/>
Bez tego r?wnie jak bez silnika pr?buj?c odpali? samoch?d

- Deklaracja outfit?w:
Kod:
<parameter key="outfits" value="druid paws,druid bear paws,druid wolf paws"/>
Opcje oddzielamy ?rednikami (;), za? pods?owa kluczowe przecinkami (,), np: "kolczyki zenka,stare kolczyki;spodnie janka".

- Deklaracja wymaga? (czyli najwa?niejsza cz???...):
Kod:
<parameter key="outfit1" value="10,1,yes;5897,50,wolf paws;5896,50,bear paws"/>
Tutaj klucz parametru si? zmienia w zale?no?ci do kt?rego outfitu na li?cie chcemy przypisa? wymagania. Id?c tropem spodni janka, deklaracja wygl?da?aby nastepuj?co:
Kod:
<parameter key="outfit2" value="NUMER OUTFITU,NUMER ADDONU,PREMIUM,P?E?;powtarzaj?ce si? itemy"/>
NUMER OUTFITU- w przypadku TFSa 0.3.5/ otserva 0.6.3 i w g?r?, u?ywamy "id" outfitu, za? w prypadku starszych wersji lookType- oba u?ywane w outfits.xml.
NUMER ADDONU- 0 = outfit, 1 = pierwszy addon, 2 = drugi addon, 3 = oba addony.
PREMIUM- czy aby kupi? wymagane jest premium? yes/no | true/false | 1/0.
P?E?- dla kt?rej p?ci dost?pny jest outfit? 0 female, 1 male.

Przejd?my teraz do item?w...
Kod:
5897,50,wolf paws;5896,50,bear paws
pierwsza warto??: numer (id) itemu;
druga warto??: ilo?? item?w w przypadku item?w stackable (subType);
trzecia warto??: nazwa przedmiotu, kt?rej b?dzie u?ywa? NPC [obs: wiem, mog?o to by? zrobione z automatu, ale osobi?cie uwa?am, ?e lepiej je?li u?ytkownik ma kontrol? nad tym] wypisuj?c wymagania;
czwarta warto?? (nie zosta?a tu wymieniona): ilo?? przedmiot?w w przypadku item?w non-stackable (amount).

Opcje specjalne: money, storagecheck, storageset, outfitid, outfit.
* money- wyja?nia? nie trzeba, u?ywamy jak zwyk?ych item?w
Kod:
money,ILO??,OPIS
* storagecheck- sprawdzanie storage, u?ywamy nast?puj?co:
Kod:
STORAGE_ID,storagecheck,OPIS,STORAGE_VALUE
* storageset- ustawianie storage, u?ywamy nast?puj?co:
Kod:
STORAGE_ID,storageset,[nie u?ywane],STORAGE_VALUE
* outfitid- sprawdzanie outfitu po "id" (jak wspomniane wcze?niej: TFS 0.3.5pl1/ otserv 0.6.3+), u?ywamy nast?puj?co:
Kod:
OUTFIT_ID,outfitid,OPIS,ADDON
* outfit- sprawdzanie outfitu po lookType, u?ywamy nast?puj?co:
Kod:
LOOKTYPE,outfit,OPIS,ADDON


Dalej nie przekonany? Zajmie Ci to zaledwie 5 minut, a potem jaka przyjemno?? z pisania NPC daj?cych addon?w...

Got?w? Jedziemy!

Otw?rz plik data/npc/lib/npcsystem/modules.lua i nad:
Kod:
	Modules = {
		parseableModules = {}
	}

wklej:
Kod:
	-- Constants used for outfit giving mode
	OUTFITMODULE_FUNCTION_OLD = { doPlayerAddOutfit, canPlayerWearOutfit } -- lookType usage
	OUTFITMODULE_FUNCTION_NEW = { doPlayerAddOutfitId, canPlayerWearOutfitId } -- OutfitId usage

	-- Used in outfit module
	OUTFITMODULE_FUNCTION = OUTFITMODULE_FUNCTION_NEW
	if(OUTFITMODULE_FUNCTION[1] == nil or OUTFITMODULE_FUNCTION[2] == nil) then
		OUTFITMODULE_FUNCTION = OUTFITMODULE_FUNCTION_OLD
	end

Teraz znajd?:
Kod:
	ShopModule = {
		npcHandler = nil,
		yesNode = nil,
		noNode = nil,
		noText = '',
		maxCount = 100,
		amount = 0
	}

i nad tym wklej:
Kod:
	OutfitModule = {
		npcHandler = nil,
		outfits = nil,
		yesNode = nil,
		noNode = nil,
	}
	-- Add it to the parseable module list.
	Modules.parseableModules['module_outfit'] = OutfitModule

	function OutfitModule:new()
		if(OUTFITMODULE_FUNCTION[1] == nil or OUTFITMODULE_FUNCTION[2] == nil) then
			return nil
		end

		local obj = {}
		setmetatable(obj, self)
		self.__index = self
		return obj
	end

	function OutfitModule:init(handler)
		self.npcHandler = handler
		self.yesNode = KeywordNode:new(SHOP_YESWORD, OutfitModule.onConfirm, {module = self})
		self.noNode = KeywordNode:new(SHOP_NOWORD, OutfitModule.onDecline, {module = self})

		self.outfits = {}
	end

	-- Parses all known parameters.
	function OutfitModule:parseParameters()
		local ret = NpcSystem.getParameter('outfits')
		if(ret ~= nil) then
			self:parseKeywords(ret)
			for _, word in ipairs({'outfits', 'addons'}) do
				self.npcHandler.keywordHandler:addKeyword({word}, OutfitModule.listOutfits, {module = self})
			end
		end
	end

	function OutfitModule:parseKeywords(data)
		local n = 1
		for outfit in string.gmatch(data, '[^;]+') do
			local i, keywords = 1, {}
			for tmp in string.gmatch(outfit, '[^,]+') do
				table.insert(keywords, tmp)
				i = i + 1
			end

			if(i > 0) then
				local ret = NpcSystem.getParameter('outfit' .. n)
				if(ret ~= nil) then
					self:parseList(keywords, ret)
				else
					print('[Warning] NpcSystem:', 'Missing \'outfit' .. n .. '\' parameter, skipping...')
				end
			else
				print('[Warning] NpcSystem:', 'No keywords found for outfit set #' .. n .. ', skipping...')
			end

			n = n + 1
		end
	end

	function OutfitModule:parseList(keywords, data)
		local outfit, items = nil, {}
		for list in string.gmatch(data, '[^;]+') do
			local a, b, c, d, e = nil, nil, nil, nil, 1
			for tmp in string.gmatch(list, '[^,]+') do
				if(e == 1) then
					a = tmp
				elseif(e == 2) then
					b = tmp
				elseif(e == 3) then
					c = tmp
				elseif(e == 4) then
					d = tmp
				else
					print('[Warning] NpcSystem:', 'Unknown parameter found in outfit list while parsing ' .. (outfit == nil and 'outfit' or 'item') .. '.', tmp, list)
				end

				e = e + 1
			end

			if(outfit == nil) then
				outfit = {tonumber(a), tonumber(b), getBooleanFromString(c), d}
			elseif(a ~= nil) then
				local tmp = tonumber(a)
				if((tmp ~= nil or tostring(a) == "money") and b ~= nil and c ~= nil) then
					a = tmp or 20000
					tmp = tonumber(d)
					if(tmp == nil) then
						tmp = -1
					end

					items[a] = {b, tmp, c}
				else
					print('[Warning] NpcSystem:', 'Missing parameter(s) for outfit items.', b, c, d)
				end
			else
				print('[Warning] NpcSystem:', 'Missing base parameter for outfit items.', a)
			end
		end

		if(type(outfit) == 'table') then
			local tmp = true
			for i = 1, 2 do
				if(outfit[i] == nil) then
					tmp = false
					break
				end
			end

			if(tmp and table.maxn(items) > 0) then
				self:addOutfit(keywords, outfit, items)
			else
				print('[Warning] NpcSystem:', 'Invalid outfit, addon or empty items pool.', data)
			end
		end
	end

	function OutfitModule:addOutfit(keywords, outfit, items)
		table.insert(self.outfits, keywords[1])
		local parameters = {
			outfit = outfit[1],
			addon = outfit[2],
			premium = outfit[3],
			gender = nil,
			items = items,
			module = self
		}

		if(outfit[4] ~= nil) then
			local tmp = string.lower(tostring(outfit[5]))
			if(tmp == 'male' or tmp == '1') then
				parameters.gender = 1
			elseif(tmp == 'female' or tmp == '0') then
				parameters.gender = 0
			end
		end

		for i, name in pairs(keywords) do
			local words = {}
			table.insert(words, name)

			local node = self.npcHandler.keywordHandler:addKeyword(words, OutfitModule.obtain, parameters)
			node:addChildKeywordNode(self.yesNode)
			node:addChildKeywordNode(self.noNode)
		end
	end

	function OutfitModule.obtain(cid, message, keywords, parameters, node)
		local module = parameters.module
		if(not module.npcHandler:isFocused(cid)) then
			return false
		end

		local i, items, size = 0, nil, table.maxn(parameters.items)
		for k, v in pairs(parameters.items) do
			if(v[1] ~= "storageset") then
				i = i + 1
				if(items ~= nil) then
					if(i == size) then
						items = items .. " and "
					else
						items = items .. ", "
					end
				else
					items = ""
				end

				if(tonumber(v[1]) ~= nil and tonumber(v[1]) > 1) then
					items = items .. v[1] .. " "
				end

				items = items .. v[3]
			end
		end
	
		module.npcHandler:say('Do you want ' .. keywords[1] .. ' ' .. (addon == 0 and "outfit" or "addon") .. ' for ' .. items .. '?', cid)
		return true

	end

	function OutfitModule.onConfirm(cid, message, keywords, parameters, node)
		local module = parameters.module
		if(not module.npcHandler:isFocused(cid)) then
			return false
		end

		local parent = node:getParent():getParameters()
		if(isPlayerPremiumCallback(cid) or not parent.premium) then
			if(not OUTFITMODULE_FUNCTION[2](cid, parent.outfit, parent.addon)) then
				if(parent.addon == 0 or OUTFITMODULE_FUNCTION[2](cid, parent.outfit)) then
					if(parent.gender == nil or parent.gender == getPlayerSex(cid)) then
						local found = true
						for k, v in pairs(parent.items) do
							local tmp = tonumber(v[1])
							if(tmp == nil) then
								if(v[1] == "storagecheck") then
									if(getCreatureStorage(cid, k) < v[2]) then
										found = false
									end
								elseif(v[1] == "outfitid") then
									if(not canPlayerWearOutfitId(cid, k, v[2])) then
										found = false
									end
								elseif(v[1] == "outfit") then
									if(not canPlayerWearOutfit(cid, k, v[2])) then
										found = false
									end
								else
									found = false
								end
							elseif(k == 20000) then
								if(getPlayerMoney(cid) < tmp) then
									found = false
								end
							elseif(getPlayerItemCount(cid, k, v[2]) < tmp) then
								found = false
							end

							if(not found) then
								break
							end
						end

						if(found) then
							for k, v in pairs(parent.items) do
								if(tonumber(v[1]) ~= nil) then
									if(k == 20000) then
										doPlayerRemoveMoney(cid, v[1])
									else
										doPlayerRemoveItem(cid, k, v[1], v[2])
									end
								elseif(v[1] == "storageset") then
									doCreatureSetStorage(cid, k, v[2])
								end
							end

							module.npcHandler:say('It was a pleasure to dress you.', cid)
							OUTFITMODULE_FUNCTION[1](cid, parent.outfit, parent.addon)
							doPlayerSetStorageValue(cid, parent.storageId, storage)
						else
							module.npcHandler:say('You don\'t have these items!', cid)
						end
					else
						module.npcHandler:say('Sorry, this ' .. (parent.addon == 0 and 'outfit' or 'addon') .. ' is not for your gender.', cid)
					end
				else
					module.npcHandler:say('I will not dress you with addon of outfit you cannot wear!', cid)
				end
			else
				module.npcHandler:say('You alrady have this ' .. (parent.addon == 0 and 'outfit' or 'addon') .. '!', cid)
			end
		else
			module.npcHandler:say('Sorry, I dress only premium players.', cid)
		end

		module.npcHandler:resetNpc()
		return true
	end

	-- onDecline keyword callback function. Generally called when the player sais 'no' after wanting to buy an item.
	function OutfitModule.onDecline(cid, message, keywords, parameters, node)
		local module = parameters.module
		if(not module.npcHandler:isFocused(cid)) then
			return false
		end

		module.npcHandler:say(module.npcHandler:parseMessage(module.npcHandler:getMessage(MESSAGE_DECLINE), {[TAG_PLAYERNAME] = getCreatureName(cid)}), cid)
		module.npcHandler:resetNpc()
		return true
	end

	function OutfitModule.listOutfits(cid, message, keywords, parameters, node)
		local module = parameters.module
		if(not module.npcHandler:isFocused(cid)) then
			return false
		end

		local msg, size = nil, table.maxn(module.outfits)
		if(size > 0) then
			for i, outfit in ipairs(module.outfits) do
				if(msg ~= nil) then
					if(i == size) then
						msg = msg .. " and "
					else
						msg = msg .. ", "
					end
				else
					msg = "I can dress you into "
				end

				msg = msg .. "{" .. outfit .. "}"
			end
		else
			msg = "Sorry, I have nothing to offer right now."
		end

		module.npcHandler:say(msg .. ".", cid)
		module.npcHandler:resetNpc()
		return true
	end

Teraz wci?nij sobie guzik 'Zapisz' i kurcz?, wszystko gotowe, wiesz?
Jeeli nie jeste? pewny, czy dzia?a: zagarnij sobie powy?szy przyk?ad Varkhala i sprawd?.


To wszystko- ?ycz? mi?ego u?ytku i pozdrawiam,
elf :cool:
 
Odp: System outfit?w

Szkoda, ?e Jiddo wcze?niej nie pomy?la? o dodaniu modu?u outfit?w - strasznie u?atwi?oby to ?ycie. Dobrze, ?e na to wpad?e? i napisa?e? kawa?ek dobrego kodu, przyda si?. Czas pozbawi? Varkhala tego jak?e "kr?tkiego", tera?niejszego skryptu.

Dzi?ki.
 
Odp: System outfit?w

Dobra robota, napewno wykorzystam podczas ota, no ale ja zbytnio skrypt?w nie ogarniam i mam pytanie, jak to mo?na doda? do npc u kt?rego np. mo?emy co? sprzeda?? d;

#Edit
Aha, no tak.. Dzi?ki wielkie ; d
 
Ostatnia edycja:
Odp: System outfit?w

Dobra robota, napewno wykorzystam podczas ota, no ale ja zbytnio skrypt?w nie ogarniam i mam pytanie, jak to mo?na doda? do npc u kt?rego np. mo?emy co? sprzeda?? d;
"Normalnie" <- w parametrach dodatkowe, wspomniane linijki. Dla przyk?adu, u mnie na Wypasie wygl?da to tak:

Kod:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Tom" nameDescription="Tom, the food vendor" script="blank.lua" walkinterval="2000" floorchange="0">
        <health now="100" max="100"/>
        <look type="144" head="132" body="79" legs="132" feet="117" addons="3"/>
        <parameters>
                <parameter key="module_shop" value="1" />
                <parameter key="message_greet" value="Hello |PLAYERNAME|, if you are interested in food we may {trade}, as I may be interested in some dead bodies." />
                <parameter key="shop_buyable" value="cherry,2679,1;blueberry,2677,1;fish,2667,5;cheese,2696,6;brown bread,2691,3;ham,2671,8;carrot,2684,3;meat,2666,5;apple,2674$
                <parameter key="shop_sellable" value="rat,2813,2;rabbit,2992,2" />
                <parameter key="module_outfit" value="1"/>
                <parameter key="outfits" value="druid paws,druid bear paws,druid wolf paws"/>
                <parameter key="outfit1" value="10,1,yes;5897,50,wolf paws;5896,50,bear paws"/>
        </parameters>
</npc>
 
Status
Zamknięty.
Back
Do góry