Jump to content

[Help] How to write this with a better lua code


Go to solution Solved by Sherer,

Recommended Posts

Hello,

This is a quest to upgrade your skill, from M1 to M2, M3, M4 ecc till G1.

The quest itself works well with no problem, but it's just a terrible code, so long and ugly. 

How this could be made shorter and nicer, but that works at the same way?

I have the same script to upgrade the skill from G1 to G2,G3 ecc till P

Spoiler

quest skill_book begin
    state start begin
	
----------********** book m1 warrior **********----------		
		
		when 52101.use begin
			if pc.get_job() > 0 then
				say_title ("Warrior Book")
				say("")
				say("This book isn't for your race")
				say("")
				return
			end	
			if pc.get_skill_group() == 0 then
				say_title ("warrior book")
				say("")
				say("you have no class yet")
				say("")
				return
			end		
			local vnum_list, name_list = skill_libri.GetSkillList2(20)
			if table.getn(vnum_list) == 0 then
				say_title ("book warrior")
				say("")
				say_reward ("No skill to upgrade")
				say("")
				return
			end
			say_title ("warrior book")
			say("")
			say("choose the skill to upgrade:")
			say("")
			table.insert(name_list, "Annulla") 
			local s = select_table(name_list)
			if s == table.getn(name_list) then
				return
			end		
			local skill_name = name_list[s]
			local skill_vnum = vnum_list[s]
			local skill_level = pc.get_skill_level(skill_vnum)
			say_title ("warrior book")
			say("")
			say("you choose: "..skill_name)
			say("are you sure to upgrade this?")
			local a = select("yes","No")
			if a == 2 then
				return
			end
			pc.set_skill_level (skill_vnum, skill_level+1)
			pc.remove_item(52101, 1)
		end
		

----------********** book M1 Assassin **********----------		
				when 52102.use begin
			if pc.get_job() > 1 or pc.get_job() < 1 then
			-- repeat the same quest for the Assassin	



----------********** book M1 Sura **********----------		
				when 52103.use begin
				if pc.get_job() > 2 or pc.get_job() < 2 then
  			-- repeat the same quest for the Sura

  
  
----------********** book M1 Shaman **********----------			
		when 52104.use begin
  			if pc.get_job() < 3 then
			-- repeat the same quest for the shaman --

  
  
--**
--** book skill function
--**

function GetSkillList2(min_level)
			local skill_list = special.active_skill_list[pc.get_job()+1][pc.get_skill_group()]
			local vnum_list = {}
			local name_list = {}
			for i = 1,table.getn(skill_list) do
				local skill_vnum = skill_list[i]
				local skill_level = pc.get_skill_level(skill_vnum)
				if skill_level >= min_level and skill_level < 30 then
					table.insert(vnum_list, skill_list[i])
					table.insert(name_list, locale.GM_SKILL_NAME_DICT[skill_vnum])
				end
			end
			return vnum_list, name_list
end
end
end

 

 

Link to comment
Share on other sites

  • Bronze
  • Solution

Try this one:

quest skill_book begin
    state start begin
		function GetSkillList2(min_level)
			local skill_list = special.active_skill_list[pc.get_job()+1][pc.get_skill_group()]
			local vnum_list = {}
			local name_list = {}
			for i = 1,table.getn(skill_list) do
				local skill_vnum = skill_list[i]
				local skill_level = pc.get_skill_level(skill_vnum)
				if skill_level >= min_level and skill_level < 30 then
					table.insert(vnum_list, skill_list[i])
					table.insert(name_list, locale.GM_SKILL_NAME_DICT[skill_vnum])
				end
			end
			return vnum_list, name_list
		end
		when 52101.use or 52102.use or 52103.use or 52104.use with pc.can_warp() begin -- don't forget about checking warp status, otherwise one's can bug is using f.e trade glitch
			local id_to_job = {[52101] = 0, [52102] = 1, [52103] = 2, [52104] = 3}
			if pc.get_job() > id_to_job[item.get_vnum()] then
				say_title (item.get_name()) -- just print book's name
				say("")
				say("This book isn't for your race")
				say("")
				return
			end	
			if pc.get_skill_group() == 0 then
				say_title (item.get_name()) -- just print book's name
				say("")
				say("you have no class yet")
				say("")
				return
			end		
			local vnum_list, name_list = skill_book.GetSkillList2(20)
			if table.getn(vnum_list) == 0 then
				say_title (item.get_name()) -- just print book's name
				say("")
				say_reward ("No skill to upgrade")
				say("")
				return
			end
			say_title (item.get_name()) -- just print book's name
			say("")
			say("choose the skill to upgrade:")
			say("")
			table.insert(name_list, "Annulla") 
			local s = select_table(name_list)
			if s == table.getn(name_list) then
				return
			end		
			local skill_name = name_list[s]
			local skill_vnum = vnum_list[s]
			local skill_level = pc.get_skill_level(skill_vnum)
			say_title (item.get_name()) -- just print book's name
			say("")
			say("you choose: "..skill_name)
			say("are you sure to upgrade this?")
			local a = select("yes","No")
			if a == 2 then
				return
			end
			pc.set_skill_level (skill_vnum, skill_level+1)
			pc.remove_item(item.get_vnum(), 1)
		end
	end
end

 

  • Love 1
Link to comment
Share on other sites

Thank you very much, it is great and works :)

There is just a small problem, the race warrior can use all the items, the race sura can use 3 items, assassin 2 items and shaman 1 items

I think the problem is here

			local id_to_job = {[52101] = 0, [52102] = 1, [52103] = 2, [52104] = 3}
			if pc.get_job() > id_to_job[item.get_vnum()] then
				say_title (item.get_name()) -- just print book's name
				say("")
				say("This book isn't for your race")
				say("")
				return
			end

 

I should change it to this

			if pc.get_job() ~= id_to_job[item.get_vnum()] then

 

Link to comment
Share on other sites

  • Bronze
Dnia 28.06.2019 o 12:04, Cripplez napisał:

Thank you very much, it is great and works :)

There is just a small problem, the race warrior can use all the items, the race sura can use 3 items, assassin 2 items and shaman 1 items

I think the problem is here


			local id_to_job = {[52101] = 0, [52102] = 1, [52103] = 2, [52104] = 3}
			if pc.get_job() > id_to_job[item.get_vnum()] then
				say_title (item.get_name()) -- just print book's name
				say("")
				say("This book isn't for your race")
				say("")
				return
			end

 

I should change it to this


			if pc.get_job() ~= id_to_job[item.get_vnum()] then

 

Yes you are right. It was already late I didn't realize that mistake :P

  • Love 1
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.