Jump to content
Angelix

Simple Functions

Recommended Posts

So the point of this thread is plain and simple: provide short, clean and useful functions to replace all those multiple, messy and commented codes in your server files. I did this because there's literally thousands of lines and they repeat themselves, per item, they only change their value. I though of these while cleaning my scripts, like ItemEffect.lua. I can reduce the amount of lines per file, easier to read, modify (add, delete and alter) and maybe throw in there some useful additions.

 

I'll try to keep this thread update whenever I can make new scripts to reduce lines. It'll be in spoiler format, those spoilers will contain some basic information, the variable lines and the function itself. Please note that you'll have to alter ItemInfo.txt in some/most cases, since these scripts literally replace the older functions. If this thread is on the wrong place, please go ahead and move it to where it corresponds, thanks.

 

Please do note that I'm currently doing this without testing them in-game (too lazy to open it right now), but they should work, if not, let me know.

 

Spoiler

This function can be used to replace all those items used to heal/replenish HP/SP. It can be a fixed amount or by percentage. I include a line so you can add your items and an example as well.

 

Variable.lua


ItemRestoreLife = {}
ItemRestoreLife[0] = {Amount = {HP = 0, SP = 0}, Percentage = {HP = 0, SP = 0}, minLv = nil, maxLv = nil}

Example:


ItemRestoreLife[1847] = {Amount = {HP = 20, SP = 0}, Percentage = {HP = 0, SP = 0}, minLv = nil, maxLv = nil}

Function:


function ItemMedKit(Player, Item)
	local ItemID = GetItemID(Item)
	if ItemRestoreLife[ItemID] == nil then
		SystemNotice(Player, GetItemName(ItemID).." is not usable, please contact administrator.")
		UseItemFailed(Player)  
		return
	end
	if ItemRestoreLife[ItemID].minLv ~= nil and GetChaAttr(Player, ATTR_LV) < ItemRestoreLife[ItemID].minLv then
		SystemNotice(Player, GetItemName(ItemID).." is only usable for players above level "..ItemRestoreLife[ItemID].minLv..".")
		UseItemFailed(Player)  
		return
	end
	if ItemRestoreLife[ItemID].maxLv ~= nil and GetChaAttr(Player, ATTR_LV) > ItemRestoreLife[ItemID].maxLv then
		SystemNotice(Player, GetItemName(ItemID).." is only usable for players below level "..ItemRestoreLife[ItemID].maxLv..".")
		UseItemFailed(Player)  
		return
	end
	if GetChaAttr(Player, ATTR_HP) <= 0 then
		UseItemFailed(Player)  
		return
	end
	local HP = GetChaAttr(Player, ATTR_HP) + ItemRestoreLife[ItemID].Amount.HP + (GetChaAttr(Player, ATTR_MXHP) * (ItemRestoreLife[ItemID].Percentage.HP/100))
	local SP = GetChaAttr(Player, ATTR_SP) + ItemRestoreLife[ItemID].Amount.SP + (GetChaAttr(Player, ATTR_MXSP) * (ItemRestoreLife[ItemID].Percentage.SP/100))
	if HP > GetChaAttr(Player, ATTR_MXHP) then
		HP = GetChaAttr(Player, ATTR_MXHP)
	end
	if SP > GetChaAttr(Player, ATTR_MXSP) then
		SP = GetChaAttr(Player, ATTR_MXSP)
	end
	SetCharaAttr(HP, Player, ATTR_HP)
	SetCharaAttr(SP, Player, ATTR_SP)
end

Usage: You have to change the item's effect to "ItemMedKit", add a new variable with the item's ID instead the 0 placed there and put the amount of recovery and type you want it to replenish.

Spoiler

This function is to replace all those reset or add stat points.

 

Variable:


ItemResetStat = {}
ItemResetStat[0] = {Name = "", Amount = 0, Attribute = nil}

Example:


ItemResetStat[3109] = {Name = "Strength", Amount = -1, Attribute = ATTR_BSTR}
ItemResetStat[3110] = {Name = "Consitution", Amount = -1, Attribute = ATTR_BCON}
ItemResetStat[3111] = {Name = "Agility", Amount = -1, Attribute = ATTR_BAGI}
ItemResetStat[3112] = {Name = "Accuracy", Amount = -1, Attribute = ATTR_DEX}
ItemResetStat[3113] = {Name = "Spirit", Amount = -1, Attribute = ATTR_STA}

Function:


function ItemStat(Player, Item)
	local ItemID = GetItemID(Item)
	if ItemResetStat[ItemID] == nil then
		SystemNotice(Player, GetItemName(ItemID).." is not usable, please contact administrator.")
		UseItemFailed(Player)
		return
	end
	local Stat = GetChaAttr(Player, ItemResetStat[ItemID].Attribute)
	if (Stat + ItemResetStat[ItemID].Amount) < 5 or (Stat + ItemResetStat[ItemID].Amount) > 100 then
		SystemNotice(Player, "You cannot use ["..GetItemName(ItemID).."] to reset your ["..ItemResetStat[ItemID].Name.."], since it will leave you below/above the limit of points.")
		UseItemFailed(Player)
		return
	end
	SystemNotice(Player, "You have successfully used ["..GetItemName(ItemID).."] to alter your ["..ItemResetStat[ItemID].Name.."] by "..ItemResetStat[ItemID].Amount.." point(s).")
	SetCharaAttr((Stat + ItemResetStat[ItemID].Amount), Player, ItemResetStat[ItemID].Attribute)
	if ItemResetStat[ItemID].Amount < 0 then
		local Points = GetChaAttr(Player, ATTR_AP) - ItemResetStat[ItemID].Amount
		SetCharaAttr(Points, Player, ATTR_AP)
	end
end

Usage: Replace your item's effect to "ItemStat", add a new variable line, set the name, whether to add (positive value) or remove (negative value) and the type of attribute (must be base).

Spoiler

This function is to replace all those item effects related to tickets, at least the normal tickets.

 

Variable:


Tickets = {}
Tickets.ID = {}
Tickets.pMap = {}
Tickets.ID[0] = {Name = "", minLv = nil, maxLv = nil}
Tickets.pMap["Default"] = ""

Example:


Tickets.ID[332] = {Name = "Spring Town", minLv = nil, maxLv = nil}
Tickets.pMap["garner2"] = "Chaos Argent"

Function:


function Ticket(Player, Item)
	local ItemID = GetItemID(Item)
	if Tickets.ID[ItemID] == nil then
		SystemNotice(Player, GetItemName(ItemID).." is not usable, please contact administrator.")
		UseItemFailed(Player)  
		return
	end
	if Tickets.ID[ItemID].minLv ~= nil and GetChaAttr(Player, ATTR_LV) < Tickets.ID[ItemID].minLv then
		SystemNotice(Player, GetItemName(ItemID).." is only usable for players above level "..Tickets.ID[ItemID].minLv..".")
		UseItemFailed(Player)  
		return
	end
	if Tickets.ID[ItemID].maxLv ~= nil and GetChaAttr(Player, ATTR_LV) > Tickets.ID[ItemID].maxLv then
		SystemNotice(Player, GetItemName(ItemID).." is only usable for players below level "..Tickets.ID[ItemID].maxLv..".")
		UseItemFailed(Player)  
		return
	end
	if (Hp(Player) < (Mxhp(Player) * 0.5)) or (Sp(Player) <(Mxsp(Player) * 0.5)) or ChaIsBoat(Player) == 1 then 
		SystemNotice(Player, "You must not be in a boat and have at least half of your maximum HP and SP in order to teleport.")
		UseItemFailed(Player)
		return
	end
	if Tickets.pMap[GetChaMapName(Player)] ~= nil then
		SystemNotice(Player, "Cannot use tickets inside "..Tickets.pMap[GetChaMapName(Player)]..".")
		UseItemFailed(Player)
		return
	end
	if DelBagItem(Player, ItemID, 1) == 1 then
		MoveCity(Player, Tickets.ID[ItemID].Name)
	else
		SystemNotice(Player, "Failed to use Ticket to "..Tickets.ID[ItemID].Name..".")
		UseItemFailed(Player)
		return
	end
end

Usage: Replace all your item's effect to "Ticket". You must add a new line with the corresponding ID, the name must be according to your "birth_conf".

 

Edited by Angelix
  • Like 6

Share this post


Link to post
Share on other sites

Great idea!
I was thinking of clearing all the files: blank spaces, indentation, codes that are not being used and this summary of functions that you did.

 

Thanks for sharing.


“So the problem is not so much to see what nobody has yet seen, as to think what nobody has yet thought concerning that which everybody sees.”


― Arthur Schopenhauer

Share this post


Link to post
Share on other sites

This is great idea, especially `ItemEffect.lua` can be reduced to 1/4 length by replacing repeated functions.

 

Here is another snipet to replace the more than 1000 lines with skills (also so many skills unused by the game, anyone has try using them and see what they can do?)

 

For example we have this function for "Dual Shot" skill:

 

-- Dual shot skill book (iteminfo.txt ID = 3190)
function Sk_Script_Lzj ( role , Item )
										
	local sk_add = SK_LZJ  
	local form_sklv = GetSkillLv( role , sk_add ) 

	if form_sklv ~= 0  then 
		UseItemFailed ( role )  
		return 
	end 
	a = AddChaSkill ( role , sk_add, 1 , 1 , 1 ) 
	if a== 0 then 
		UseItemFailed ( role )  
		return 
	end 
end 

 

we need to note down skill book ID in iteminfo.txt, and also the `local sk_add` value. The `sk_add` value is in `variable.lua` and correspond to the skill ID in `skillinfo.txt`. For example, `SK_LZJ` in variable.lua is `SK_LZJ        =    90`, which is the skill ID for Dual Shot in skillinfo.txt.

 

So we can replace all the skill books with an array in `ItemEffect.lua`:

 

Class_Skill_Book = {}
-- Class_Skill_Book[<skill book ID in iteminfo.txt>] = <skill ID in skillinfo.txt> --
Class_Skill_Book[3184] = 73		-- Shield Mastery (unknown)
Class_Skill_Book[3190] = 90		-- Dual Shot
Class_Skill_Book[3193] = 112	-- Meteor Shower
-- etc... --

 

and then replace all the skill book functions with the following:

 

function Class_Skill_Books ( role , Item )
	
	local bk_ID = GetItemID(Item)
	local sk_add = Class_Skill_Book[bk_ID]
	local form_sklv = GetSkillLv( role , sk_add )
	
	if form_sklv ~= 0  then 
		UseItemFailed ( role )  
		return 
	end 
	a = AddChaSkill ( role , sk_add, 1 , 1 , 1 ) 

	if a == 0 then 
		UseItemFailed ( role )  
		return 
	end 
end

 

To use it, in `ItemInfo.txt` we have to replace the skill book's fnction with `Class_Skill_Books`

 

ex.

3190	Dual Shot	book2	10130005	0	0	0	0	0	00	34	0	0	0	0	0	1	1	1	1	1	0	1680	-1	0	-1	0	0	-1	-1	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0,0	0	0	0,0	0	0	0	0	0	0	0	0	0	Class_Skill_Books	0	0	0	0	0	0	Class: Hunter Level Requirement: Lv 29 Prerequisite: Eagle Eye Lv 3, Archery Lv 3

 

Edited by SoundX
  • Like 1

Share this post


Link to post
Share on other sites
On 11/13/2016 at 11:26 AM, Angelix said:

So the point of this thread is plain and simple: provide short, clean and useful functions to replace all those multiple, messy and commented codes in your server files. I did this because there's literally thousands of lines and they repeat themselves, per item, they only change their value. I though of these while cleaning my scripts, like ItemEffect.lua. I can reduce the amount of lines per file, easier to read, modify (add, delete and alter) and maybe throw in there some useful additions.

 

I'll try to keep this thread update whenever I can make new scripts to reduce lines. It'll be in spoiler format, those spoilers will contain some basic information, the variable lines and the function itself. Please note that you'll have to alter ItemInfo.txt in some/most cases, since these scripts literally replace the older functions. If this thread is on the wrong place, please go ahead and move it to where it corresponds, thanks.

 

Please do note that I'm currently doing this without testing them in-game (too lazy to open it right now), but they should work, if not, let me know.

 

  Reveal hidden contents

This function can be used to replace all those items used to heal/replenish HP/SP. It can be a fixed amount or by percentage. I include a line so you can add your items and an example as well.

 

Variable.lua



ItemRestoreLife = {}
ItemRestoreLife[0] = {Amount = {HP = 0, SP = 0}, Percentage = {HP = 0, SP = 0}, minLv = nil, maxLv = nil}

Example:



ItemRestoreLife[1847] = {Amount = {HP = 20, SP = 0}, Percentage = {HP = 0, SP = 0}, minLv = nil, maxLv = nil}

Function:



function ItemMedKit(Player, Item)
	local ItemID = GetItemID(Item)
	if ItemRestoreLife[ItemID] == nil then
		SystemNotice(Player, GetItemName(ItemID).." is not usable, please contact administrator.")
		UseItemFailed(Player)  
		return
	end
	if ItemRestoreLife[ItemID].minLv ~= nil and GetChaAttr(Player, ATTR_LV) < ItemRestoreLife[ItemID].minLv then
		SystemNotice(Player, GetItemName(ItemID).." is only usable for players above level "..ItemRestoreLife[ItemID].minLv..".")
		UseItemFailed(Player)  
		return
	end
	if ItemRestoreLife[ItemID].maxLv ~= nil and GetChaAttr(Player, ATTR_LV) > ItemRestoreLife[ItemID].maxLv then
		SystemNotice(Player, GetItemName(ItemID).." is only usable for players below level "..ItemRestoreLife[ItemID].maxLv..".")
		UseItemFailed(Player)  
		return
	end
	if GetChaAttr(Player, ATTR_HP) <= 0 then
		UseItemFailed(Player)  
		return
	end
	local HP = GetChaAttr(Player, ATTR_HP) + ItemRestoreLife[ItemID].Amount.HP + (GetChaAttr(Player, ATTR_MXHP) * (ItemRestoreLife[ItemID].Percentage.HP/100))
	local SP = GetChaAttr(Player, ATTR_SP) + ItemRestoreLife[ItemID].Amount.SP + (GetChaAttr(Player, ATTR_MXSP) * (ItemRestoreLife[ItemID].Percentage.SP/100))
	if HP > GetChaAttr(Player, ATTR_MXHP) then
		HP = GetChaAttr(Player, ATTR_MXHP)
	end
	if SP > GetChaAttr(Player, ATTR_MXSP) then
		SP = GetChaAttr(Player, ATTR_MXSP)
	end
	SetCharaAttr(HP, Player, ATTR_HP)
	SetCharaAttr(SP, Player, ATTR_SP)
end

Usage: You have to change the item's effect to "ItemMedKit", add a new variable with the item's ID instead the 0 placed there and put the amount of recovery and type you want it to replenish.

  Reveal hidden contents

This function is to replace all those reset or add stat points.

 

Variable:



ItemResetStat = {}
ItemResetStat[0] = {Name = "", Amount = 0, Attribute = nil}

Example:



ItemResetStat[3109] = {Name = "Strength", Amount = -1, Attribute = ATTR_BSTR}
ItemResetStat[3110] = {Name = "Consitution", Amount = -1, Attribute = ATTR_BCON}
ItemResetStat[3111] = {Name = "Agility", Amount = -1, Attribute = ATTR_BAGI}
ItemResetStat[3112] = {Name = "Accuracy", Amount = -1, Attribute = ATTR_DEX}
ItemResetStat[3113] = {Name = "Spirit", Amount = -1, Attribute = ATTR_STA}

Function:



function ItemStat(Player, Item)
	local ItemID = GetItemID(Item)
	if ItemResetStat[ItemID] == nil then
		SystemNotice(Player, GetItemName(ItemID).." is not usable, please contact administrator.")
		UseItemFailed(Player)
		return
	end
	local Stat = GetChaAttr(Player, ItemResetStat[ItemID].Attribute)
	if (Stat + ItemResetStat[ItemID].Amount) < 5 or (Stat + ItemResetStat[ItemID].Amount) > 100 then
		SystemNotice(Player, "You cannot use ["..GetItemName(ItemID).."] to reset your ["..ItemResetStat[ItemID].Name.."], since it will leave you below/above the limit of points.")
		UseItemFailed(Player)
		return
	end
	SystemNotice(Player, "You have successfully used ["..GetItemName(ItemID).."] to alter your ["..ItemResetStat[ItemID].Name.."] by "..ItemResetStat[ItemID].Amount.." point(s).")
	SetCharaAttr((Stat + ItemResetStat[ItemID].Amount), Player, ItemResetStat[ItemID].Attribute)
	if ItemResetStat[ItemID].Amount < 0 then
		local Points = GetChaAttr(Player, ATTR_AP) - ItemResetStat[ItemID].Amount
		SetCharaAttr(Points, Player, ATTR_AP)
	end
end

Usage: Replace your item's effect to "ItemStat", add a new variable line, set the name, whether to add (positive value) or remove (negative value) and the type of attribute (must be base).

  Reveal hidden contents

This function is to replace all those item effects related to tickets, at least the normal tickets.

 

Variable:



Tickets = {}
Tickets.ID = {}
Tickets.pMap = {}
Tickets.ID[0] = {Name = "", minLv = nil, maxLv = nil}
Tickets.pMap["Default"] = ""

Example:



Tickets.ID[332] = {Name = "Spring Town", minLv = nil, maxLv = nil}
Tickets.pMap["garner2"] = "Chaos Argent"

Function:



function Ticket(Player, Item)
	local ItemID = GetItemID(Item)
	if Tickets.ID[ItemID] == nil then
		SystemNotice(Player, GetItemName(ItemID).." is not usable, please contact administrator.")
		UseItemFailed(Player)  
		return
	end
	if Tickets.ID[ItemID].minLv ~= nil and GetChaAttr(Player, ATTR_LV) < Tickets.ID[ItemID].minLv then
		SystemNotice(Player, GetItemName(ItemID).." is only usable for players above level "..Tickets.ID[ItemID].minLv..".")
		UseItemFailed(Player)  
		return
	end
	if Tickets.ID[ItemID].maxLv ~= nil and GetChaAttr(Player, ATTR_LV) > Tickets.ID[ItemID].maxLv then
		SystemNotice(Player, GetItemName(ItemID).." is only usable for players below level "..Tickets.ID[ItemID].maxLv..".")
		UseItemFailed(Player)  
		return
	end
	if (Hp(Player) < (Mxhp(Player) * 0.5)) or (Sp(Player) <(Mxsp(Player) * 0.5)) or ChaIsBoat(Player) == 1 then 
		SystemNotice(Player, "You must not be in a boat and have at least half of your maximum HP and SP in order to teleport.")
		UseItemFailed(Player)
		return
	end
	if Tickets.pMap[GetChaMapName(Player)] ~= nil then
		SystemNotice(Player, "Cannot use tickets inside "..Tickets.pMap[GetChaMapName(Player)]..".")
		UseItemFailed(Player)
		return
	end
	if DelBagItem(Player, ItemID, 1) == 1 then
		MoveCity(Player, Tickets.ID[ItemID].Name)
	else
		SystemNotice(Player, "Failed to use Ticket to "..Tickets.ID[ItemID].Name..".")
		UseItemFailed(Player)
		return
	end
end

Usage: Replace all your item's effect to "Ticket". You must add a new line with the corresponding ID, the name must be according to your "birth_conf".

 

such good idea I was thinking of doing the same but had no opportunity to do so, thanks for sharing!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...