<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title>Форум QUIK [тема: Проблемы с math.random]</title>
		<link>http://forum.quik.ru</link>
		<description>Новое в теме Проблемы с math.random форума  на сайте Форум QUIK [forum.quik.ru]</description>
		<language>ru</language>
		<docs>http://backend.userland.com/rss2</docs>
		<pubDate>Sat, 02 May 2026 19:25:53 +0300</pubDate>
		<item>
			<title>Проблемы с math.random</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message35071/topic4042/">Проблемы с math.random</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			<br />====quote====<br /><a class="blog-p-user-name" id="bp_unNIPZVT" href="/user/17/" bx-tooltip-user-id="17">Sergey Gorokhov</a> написал:<br />тогда так:<br />=============<br /><p>Еще раз спасибо за помощь. &nbsp;Проверил - на положительных числах &nbsp;работает правильно. Только надо помнить, что если шаг цены=1, то &nbsp;надо задавать P=0 (а не &nbsp;1). &nbsp;</p><p>Я сделал по другому – см. код ниже. &nbsp;При этом в качестве Р надо всегда задавать шаг цены, например если шаг цены=1, то &nbsp;надо задавать P=1. &nbsp;К остатку remnant добавляется +1.E-9 для корректировки. Дело в том, что иногда remnant принимает значения типа 0.3199999999999 &nbsp;вместо 0,32. Именно так получилось для числа 78.82 при step=0.5. &nbsp;Если +1.E-9 не добавлять, то в случаях типа 0.3199999999999 &nbsp;могут получиться неверные (заниженные) результаты. </p>
====code====
<pre>function main()
 step=0.5
 v1=FROUND(78.15,step) -- дает 78
 v2=FROUND(78.25,step) -- дает 78.5 
 v3=FROUND(78.43,step) -- дает 78.5
 v4=FROUND(78.61,step) -- дает 78.5
 v5=FROUND(78.75,step) -- дает 79
 v6=FROUND(78.82,step) -- дает 79
end

function FROUND(v,step)
&nbsp;&nbsp;remnant=math.fmod(v, step)
&nbsp;&nbsp;v=v-remnant
&nbsp;&nbsp;if (remnant+1.E-9)/step&#62;=0.5 then 
&nbsp;&nbsp; v=v+step
&nbsp;&nbsp;end
&nbsp;&nbsp;return v 
end
</pre>
============= <br />
			<i>28.11.2018 12:28:51, Виктор Столетов.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message35071/topic4042/</link>
			<guid>http://forum.quik.ru/messages/forum10/message35071/topic4042/</guid>
			<pubDate>Wed, 28 Nov 2018 12:28:51 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Проблемы с math.random</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message35063/topic4042/">Проблемы с math.random</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			<br />====quote====<br /><a class="blog-p-user-name" id="bp_9T9RMU8b" href="/user/2675/" bx-tooltip-user-id="2675">Виктор Столетов</a> написал:<br />Но что если шаг цены равен к примеру 0,5? Тогда число 78.61 следовало бы округлить до 78,5, но при p=1 оно округлится до 78,6, а при p=0 до 79. Хотя это уже мои проблемы, если найду решение, то напишу.<br />=============<br /><br />тогда так:<br />
====code====
<pre>
function roundEX(V, P)
if math.fmod(P,1) == 0 then
&nbsp;&nbsp;&nbsp;if (V &#62;= 0) then return math.floor(V * (10 ^ P) + 0.5) / (10 ^ P)
&nbsp;&nbsp;&nbsp;else return math.ceil(V * (10 ^ P) - 0.5) / (10 ^ P) end
else
&nbsp;&nbsp; if (V &#62;= 0) then return math.floor(V / P + 0.5) * P
&nbsp;&nbsp; else return math.ceil(V / P - 0.5) * P end
end
end
</pre>
=============
<br /><br />можете переделать по своему. <br />
			<i>28.11.2018 06:07:54, Sergey Gorokhov.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message35063/topic4042/</link>
			<guid>http://forum.quik.ru/messages/forum10/message35063/topic4042/</guid>
			<pubDate>Wed, 28 Nov 2018 06:07:54 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Проблемы с math.random</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message35059/topic4042/">Проблемы с math.random</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			<noindex><a href="http://www.bot4sale.ru/blog-menu/qpile/6-round-to-step.html" target="_blank" rel="nofollow">http://www.bot4sale.ru/blog-menu/qpile/6-round-to-step.html</a></noindex><br /><br />перепишите на луа, это просто. <br />
			<i>27.11.2018 20:14:23, s_mike@rambler.ru.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message35059/topic4042/</link>
			<guid>http://forum.quik.ru/messages/forum10/message35059/topic4042/</guid>
			<pubDate>Tue, 27 Nov 2018 20:14:23 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Проблемы с math.random</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message35058/topic4042/">Проблемы с math.random</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			<br />====quote====<br /><a class="blog-p-user-name" id="bp_v7yRUt68" href="/user/17/" bx-tooltip-user-id="17">Sergey Gorokhov</a> написал:<br />Позвольте немного скорректировать.<br />=============<br /><p>Спасибо вам, Майк и Сергей. Да такого кода я бы точно не додумался.</p><p>Отрицательных чисел у меня нет, поэтому я написал по варианту &nbsp;Майка и немного упростил - убрал tonumber, а &nbsp;вместо (p or 0) написал просто p. Получилось так:</p>
====code====
<pre>function round(value, p)
&nbsp;&nbsp; local e = 10 ^ p 
&nbsp;&nbsp; return math.floor(value * e + 0.5) / e
end
x0=round(123.456,0) -- дает 123
x2=round(123.456,2) -- дает 123,46
</pre>
=============
<p> Правда моя задача немного сложнее - надо округлить выражение (high+low+close)/3 до шага цены. Если шаг цены равен 1 или 0,01 или 0,0001, то проблем нет, т.к. каждому шагу цены легко сопоставить параметр p, например: шаг=1 - p=0; шаг=0,01 - p=2; шаг=0,0001 - p=4.</p><p>Но что если шаг цены равен к примеру 0,5? Тогда число 78.61 следовало бы округлить до 78,5, но при p=1 оно округлится до 78,6, а при p=0 до 79. Хотя это уже мои проблемы, если найду решение, то напишу.</p> <br />
			<i>27.11.2018 19:53:10, Виктор Столетов.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message35058/topic4042/</link>
			<guid>http://forum.quik.ru/messages/forum10/message35058/topic4042/</guid>
			<pubDate>Tue, 27 Nov 2018 19:53:10 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Проблемы с math.random</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message35034/topic4042/">Проблемы с math.random</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			<br />====quote====<br /><a class="blog-p-user-name" id="bp_As1TBuLJ" href="/user/47/" bx-tooltip-user-id="47">s_mike@rambler.ru</a> написал:<br />--------------------------------------------------------------------<br />-- round (number, p = 0)<br />-- Округление до требуемого знака.<br />-- round(123.456,-2) = 100<br />-- round(123.456,1) = 123.5<br />-- round(123.456) = 123<br />function math.round(value, p)<br /><br />	local e = 10 ^ (p or 0)<br />	return math.floor(tonumber(value) * e + 0.5) / e<br />end<br />=============<br /><br />Позвольте немного скорректировать.<br />дело в том что при округлении отрицательных чисел, эта функция может допустить ошибку.<br />так &nbsp;round(-123.55,1) вернет -123.5 а должен вернуть -123.6<br />или &nbsp;round(-123.5,0) вернет -123 а должен вернуть -124<br /><br />для того чтобы избежать этого, надо немного исправить функцию для случая отрицательных чисел, вот так:<br />
====code====
<pre>
function math.round(value, p)
&nbsp;&nbsp;&nbsp;local e = 10 ^ (p or 0)
&nbsp;&nbsp;&nbsp;if value &#62;= 0 then return math.floor(tonumber(value) * e + 0.5) / e
&nbsp;&nbsp;&nbsp;else return math.ceil(tonumber(value) * e - 0.5) / e end
end
</pre>
============= <br />
			<i>27.11.2018 05:53:53, Sergey Gorokhov.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message35034/topic4042/</link>
			<guid>http://forum.quik.ru/messages/forum10/message35034/topic4042/</guid>
			<pubDate>Tue, 27 Nov 2018 05:53:53 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Проблемы с math.random</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message35024/topic4042/">Проблемы с math.random</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			<noindex><a href="https://forum.quik.ru/user/2675/" target="_blank" rel="nofollow">Виктор Столетов</a></noindex>, в Lua нет встроенной функции математического округления. <br />
			<i>26.11.2018 21:37:35, Enfernuz.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message35024/topic4042/</link>
			<guid>http://forum.quik.ru/messages/forum10/message35024/topic4042/</guid>
			<pubDate>Mon, 26 Nov 2018 21:37:35 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Проблемы с math.random</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message35023/topic4042/">Проблемы с math.random</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			--------------------------------------------------------------------<br />-- round (number, p = 0)<br />-- Округление до требуемого знака.<br />-- round(123.456,-2) = 100<br />-- round(123.456,1) = 123.5<br />-- round(123.456) = 123<br />function math.round(value, p)<br /><br />	local e = 10 ^ (p or 0)<br />	return math.floor(tonumber(value) * e + 0.5) / e<br />end <br />
			<i>26.11.2018 21:35:20, s_mike@rambler.ru.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message35023/topic4042/</link>
			<guid>http://forum.quik.ru/messages/forum10/message35023/topic4042/</guid>
			<pubDate>Mon, 26 Nov 2018 21:35:20 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Проблемы с math.random</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message35020/topic4042/">Проблемы с math.random</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			<p>У меня тоже вопрос по математическим функциям. В руководстве не нашел функции округления до целого. Например, как сделать, чтобы 5,7 округлялась до 6, а 5,2 - до 5? </p><p>math.ceil(5.2) округляет вверх и дает 6 (а неплохо бы иметь 5) </p><p>math.floor(5.7) округляет вниз и дает 5 (а неплохо бы иметь 6) </p><p>выражение 5.2-5.2%1 отбрасывает дробную часть и тоже дает 5.</p><p> </p><p>Или надо &nbsp;самому писать функцию округления по нормальным &nbsp;математическим правилам? </p> <br />
			<i>26.11.2018 19:30:47, Виктор Столетов.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message35020/topic4042/</link>
			<guid>http://forum.quik.ru/messages/forum10/message35020/topic4042/</guid>
			<pubDate>Mon, 26 Nov 2018 19:30:47 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Проблемы с math.random</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message34597/topic4042/">Проблемы с math.random</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			В полный код даже не смотрел.<br />Сделай так<br />math.randomseed(os.clock());<br /> &nbsp;parametr1 = math.random(2,5);<br /> &nbsp; parametr2 = math.random(2,5);<br /> &nbsp; &nbsp;parametr3 = math.random(2,5);<br /> &nbsp; &nbsp;...<br /> &nbsp; &nbsp;.....<br /> &nbsp; &nbsp; parametr... = math.random(2,5);<br /><br />parametr1 - это то, что ты получаешь.<br />Если выведешь остальные результаты, то увидишь, что разброс начинается со второго. И чем дальше, тем сильнее.<br />Я пользуюсь, начиная с третьего. &nbsp; <br />
			<i>26.10.2018 22:28:59, Игорь Б.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message34597/topic4042/</link>
			<guid>http://forum.quik.ru/messages/forum10/message34597/topic4042/</guid>
			<pubDate>Fri, 26 Oct 2018 22:28:59 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Проблемы с math.random</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message34581/topic4042/">Проблемы с math.random</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			Добрый день. Проблемы с math.random(возможно что-то иное) в lua игры.<br /><br />Если пробросить диапазон от 2 до 5 - math.random(2,5), почему то постоянно выпадает 2.<br />Перед функцией пытался вызывать math.randomseed(os.clock()), не помогло.<br /><br />строки:<br />local ItemNum = math.random(2,5)<br />local ObjNum = math.random(2,5)<br /><br /><br />Полный код:<br />&#91;SPOILER&#93;gac_gas_require &quot;activity/quest/QuestMgrInc&quot;<br />gac_gas_require &quot;item/store_room_cfg/StoreRoomCfg&quot;<br />gac_gas_require &quot;activity/npc/CheckNpcDropSetting&quot;<br /><br />local os = os<br />local EAssignMode =<br />{<br />	eAM_FreePickup 		 &nbsp; = 0, --自由拾取<br />	eAM_AverageAssign 	 = 1, --平均分配<br />	eAM_RespectivePickup = 2, --各自拾取<br />	eAM_AuctionAssign 	 = 3, --拍卖模式<br />	eAM_NeedAssign			= 4, --需求分配<br />}<br />local EAuctionStandard =<br />{<br />	eAS_GrayStandard &nbsp; = 0, --灰色<br />	eAS_WhiteStandard &nbsp;= 1, --白色<br />	eAS_GreenStandard &nbsp;= 2, --绿色<br />	eAS_BlueStandard &nbsp; = 3, --蓝色<br />	eAS_PurpleStandard = 4, --紫色<br />	eAS_OrangeStandard = 5, --橙色<br />	eAS_YellowStandard = 6, --黄色<br />	eAS_CyanStandard &nbsp; = 7, --青色<br />}<br />local QuestState = {<br />	init	= 1,<br />	failure	= 2,<br />	finish	= 3<br />}<br /><br />local EClass = EClass<br />local ECamp = ECamp<br />local ShareArea = 38<br />local DropTeamNum = 30<br />local TeamItemNum = 30<br />local DropProbability = 100000000<br />local DropItemProtectTime = 20<br />local g_ItemInfoMgr = CItemInfoMgr:new()<br />local g_NpcDropObjTable = g_NpcDropObjTable<br />local g_NpcDropItemTable = g_NpcDropItemTable<br />local g_QuestDropItemMgr = g_QuestDropItemMgr<br />--local NpcDropItem_Common = NpcDropItem_Common<br />local DropTeam_Server = DropTeam_Server<br />local g_WhereGiveQuestMgr = g_WhereGiveQuestMgr<br />local DropItemObjTeam_Server = DropItemObjTeam_Server<br />local RoleQuestDB = RequireDbBox(&quot;RoleQuestDB&quot;)<br />local RoomMgrDB = RequireDbBox(&quot;GasRoomMgrDB&quot;)<br />local LoginServerDB = RequireDbBox(&quot;LoginServerDB&quot;)<br />local GasTeamDB = RequireDbBox(&quot;GasTeamDB&quot;)<br />local CharacterMediatorDB = RequireDbBox(&quot;CharacterMediatorDB&quot;)<br />local AreaDB = RequireDbBox(&quot;AreaDB&quot;)<br />local g_NpcDropObjTeamRate = g_NpcDropObjTeamRate<br />local g_NpcDropItemTeamRate = g_NpcDropItemTeamRate<br />local g_RepeatQuestMgr = g_RepeatQuestMgr<br />local event_type_tbl = event_type_tbl<br /><br />local NpcDropItemDB = CreateDbBox(...)<br /><br />local function Get2PosDistance(pos1, pos2)<br />	return math.max( math.abs( pos1.x - pos2.x ), math.abs( pos1.y - pos2.y ) )<br />end<br /><br />--从ItemTeam随机物品<br />local function RandomTeamItems(PlayerId, teamindex)<br />	local ClassNameTbl = {<br />		&#91;EClass.eCL_Warrior&#93;				= &quot;大剑&quot;,<br />		&#91;EClass.eCL_MagicWarrior&#93;		= &quot;魔剑&quot;,<br />		&#91;EClass.eCL_Paladin&#93;				= &quot;骑士&quot;,<br />		&#91;EClass.eCL_NatureElf&#93;			= &quot;法师&quot;,<br />		&#91;EClass.eCL_EvilElf&#93;				= &quot;邪魔&quot;,<br />		&#91;EClass.eCL_Priest&#93;					= &quot;牧师&quot;,<br />		&#91;EClass.eCL_DwarfPaladin&#93;		= &quot;矮骑&quot;,<br />		&#91;EClass.eCL_ElfHunter&#93;			= &quot;精灵弓&quot;,<br />		&#91;EClass.eCL_OrcWarrior&#93;			= &quot;兽战&quot;,<br />	}<br />	local CampNameTbl = {<br />		&#91;ECamp.eCamp_AmyEmpire&#93;			= &quot;艾米&quot;,<br />		&#91;ECamp.eCamp_WestEmpire&#93;		= &quot;神圣&quot;,<br />		&#91;ECamp.eCamp_DevilIsland&#93;		= &quot;修斯&quot;,<br />	}<br />	if string.find(teamindex, &quot;#class#&quot;) then	<br />		local FightSkillDB = RequireDbBox(&quot;FightSkillDB&quot;)<br />		local ClassID = FightSkillDB.Dbs_SelectPlayerClass(PlayerId)<br />		teamindex = string.gsub(teamindex, &quot;#class#&quot;, ClassNameTbl&#91;ClassID&#93;)<br />	end<br />	if string.find(teamindex, &quot;#camp#&quot;) then<br />		local CharacterMediatorDB = RequireDbBox(&quot;CharacterMediatorDB&quot;)<br />		local Camp = CharacterMediatorDB.GetCamp(PlayerId)<br />		teamindex = string.gsub(teamindex, &quot;#camp#&quot;, CampNameTbl&#91;Camp&#93;)<br />	end<br />	local AllRateSum=g_NpcDropItemTeamRate&#91;teamindex&#93;<br />	local Items_List={}<br /><br />--	for i=1,TeamItemNum do<br />--		local DropRate=&quot;DropRate&quot;..i<br />--		local Rate = DropTeam_Server(teamindex, &quot;DropRate&quot;)<br />--		if Rate == &quot;&quot; or Rate == nil or not IsNumber(Rate) then<br />--			Rate = 0<br />--		end<br />--		AllRateSum=AllRateSum+Rate &nbsp;--以每一组的物品概率值相加组成总概率<br />--	end<br />	<br />	local RandomItemRate=math.random(0,AllRateSum) &nbsp;--随机一个概率值<br />	local ItemRateIndex=0<br />	<br />	local droprate1 = DropTeam_Server(teamindex,&quot;DropRate1&quot;)<br />	if droprate1 == &quot;&quot; or droprate1 == nil then<br />		droprate1 = 0<br />	else<br />		droprate1 = tonumber(droprate1)<br />	end<br />	--比较随机得到的概率值和物品概率值<br />	if RandomItemRate&gt;=0 and RandomItemRate&lt;=droprate1 then<br />		ItemRateIndex=1<br />	else<br />		local droprate1 = 0<br />		local droprate2 = DropTeam_Server(teamindex,&quot;DropRate1&quot;)<br />		if droprate2 == &quot;&quot; or droprate2 == nil then<br />			droprate2 = 0<br />		else<br />			droprate2 = tonumber(droprate2)<br />		end<br />		<br />		for i=2,TeamItemNum do	<br />			local dropr1 = DropTeam_Server(teamindex,&quot;DropRate&quot;..(i-1))<br />			if dropr1==&quot;&quot; or not dropr1 then<br />				dropr1 = 0<br />			else<br />				dropr1 = tonumber(dropr1)<br />			end<br />			local dropr2 = DropTeam_Server(teamindex,&quot;DropRate&quot;..i)<br />			if dropr2==&quot;&quot; or not dropr2 then<br />				dropr2 = 0<br />			else<br />				dropr2 = tonumber(dropr2)<br />			end<br />			<br />			droprate2 = dropr2 + droprate2<br />			droprate1 = dropr1 + droprate1<br />			if RandomItemRate&gt;droprate1 and RandomItemRate&lt;=droprate2 then<br />				ItemRateIndex=i<br />				break<br />			end<br />		end<br />		<br />	end<br />	<br />	if ItemRateIndex&gt;0 and ItemRateIndex&lt;=TeamItemNum then<br />		local ItemTypeIndex=&quot;DropType&quot;..ItemRateIndex<br />		local ItemNameIndex=&quot;DropName&quot;..ItemRateIndex<br />		local ItemType=DropTeam_Server(teamindex,ItemTypeIndex)<br />		local ItemName=DropTeam_Server(teamindex,ItemNameIndex)<br />		local DropMinNum = DropTeam_Server(teamindex,&quot;DropMinNum&quot;)<br />		local DropMaxNum = DropTeam_Server(teamindex,&quot;DropMaxNum&quot;)<br />		math.randomseed(os.clock())<br />		local ItemNum = math.random(2,5)<br />		--Items_List = {ItemType,ItemName,ItemNum}<br />		local Only = g_ItemInfoMgr:GetItemInfo( ItemType,ItemName,&quot;Only&quot; )<br />		if Only ~= 1 then<br />			local FoldLimit = g_ItemInfoMgr:GetItemInfo( ItemType,ItemName,&quot;FoldLimit&quot; ) or 1<br />			if FoldLimit == 1 then<br />				for i = 1, ItemNum do<br />					table.insert(Items_List, {ItemType,ItemName,1})<br />				end<br />			else<br />				table.insert(Items_List, {ItemType,ItemName,ItemNum})<br />			end<br />		elseif Only == 1 then<br />			local havenum = RoomMgrDB.GetItemCount(PlayerId, ItemType,ItemName)<br />			if havenum == 0 then<br />				if g_WhereGiveQuestMgr&#91;&quot;Goods&quot;&#93;&#91;ItemName&#93; and ItemType == 16 then<br />					local questname = g_WhereGiveQuestMgr&#91;&quot;Goods&quot;&#93;&#91;ItemName&#93;&#91;1&#93;<br />					local queststate = RoleQuestDB.GetQuestState({PlayerId,questname})<br />					if queststate == nil or (queststate == QuestState.finish and g_RepeatQuestMgr&#91;questname&#93;) then<br />						table.insert(Items_List, {ItemType,ItemName,1,&#91;&quot;QuestDropOwner&quot;&#93; = PlayerId})<br />					end<br />				end<br />			end<br />		end<br />	end<br />	return Items_List<br />end<br /><br />--随机产生掉落OBJ表<br />local function RandomTeamObjs(PlayerId, teamindex)<br />	local ClassNameTbl = {<br />		&#91;EClass.eCL_Warrior&#93;				= &quot;大剑&quot;,<br />		&#91;EClass.eCL_MagicWarrior&#93;		= &quot;魔剑&quot;,<br />		&#91;EClass.eCL_Paladin&#93;				= &quot;骑士&quot;,<br />		&#91;EClass.eCL_NatureElf&#93;			= &quot;法师&quot;,<br />		&#91;EClass.eCL_EvilElf&#93;				= &quot;邪魔&quot;,<br />		&#91;EClass.eCL_Priest&#93;					= &quot;牧师&quot;,<br />		&#91;EClass.eCL_DwarfPaladin&#93;		= &quot;矮骑&quot;,<br />		&#91;EClass.eCL_ElfHunter&#93;			= &quot;精灵弓&quot;,<br />		&#91;EClass.eCL_OrcWarrior&#93;			= &quot;兽战&quot;,<br />	}<br />	local CampNameTbl = {<br />		&#91;ECamp.eCamp_AmyEmpire&#93;			= &quot;艾米&quot;,<br />		&#91;ECamp.eCamp_WestEmpire&#93;		= &quot;神圣&quot;,<br />		&#91;ECamp.eCamp_DevilIsland&#93;		= &quot;修斯&quot;,<br />	}<br />	if string.find(teamindex, &quot;#class#&quot;) then	<br />		local FightSkillDB = RequireDbBox(&quot;FightSkillDB&quot;)<br />		local ClassID = FightSkillDB.Dbs_SelectPlayerClass(PlayerId)<br />		teamindex = string.gsub(teamindex, &quot;#class#&quot;, ClassNameTbl&#91;ClassID&#93;)<br />	end<br />	if string.find(teamindex, &quot;#camp#&quot;) then<br />		local CharacterMediatorDB = RequireDbBox(&quot;CharacterMediatorDB&quot;)<br />		local Camp = CharacterMediatorDB.GetCamp(PlayerId)<br />		teamindex = string.gsub(teamindex, &quot;#camp#&quot;, CampNameTbl&#91;Camp&#93;)<br />	end<br />	local AllRateSum=g_NpcDropObjTeamRate&#91;teamindex&#93;<br />	local Objs_List={}<br />--	for i=1,TeamItemNum do<br />--		local DropRate=&quot;DropRate&quot;..i<br />--		local Rate = DropItemObjTeam_Server&#91;teamindex&#93;&#91;DropRate&#93;<br />--		if Rate == &quot;&quot; or Rate == nil or not IsNumber(Rate) then<br />--			Rate = 0<br />--		end<br />--		AllRateSum=AllRateSum+Rate<br />--	end<br />	<br />	local RandomItemRate=math.random(0,AllRateSum)<br />	local ItemRateIndex=0<br />	<br />	local droprate1 = DropItemObjTeam_Server(teamindex,&quot;DropRate1&quot;)<br />	if droprate1 == &quot;&quot; or droprate1 == nil then<br />		droprate1 = 0<br />	else<br />		droprate1 = tonumber(droprate1)<br />	end<br />	<br />	if RandomItemRate&gt;=0 and RandomItemRate&lt;=droprate1 then<br />		ItemRateIndex=1<br />	else<br />		local droprate1 = 0<br />		local droprate2 = DropItemObjTeam_Server(teamindex,&quot;DropRate1&quot;)<br />		if droprate2 == &quot;&quot; or droprate2 == nil then<br />			droprate2 = 0<br />		else<br />			droprate2 = tonumber(droprate2)<br />		end<br />		<br />		for i=2,TeamItemNum do	<br />			local dropr1 = DropItemObjTeam_Server(teamindex,&quot;DropRate&quot;..(i-1))<br />			if dropr1==&quot;&quot; or not dropr1 then<br />				dropr1 = 0<br />			else<br />				dropr1 = tonumber(dropr1)<br />			end<br />			local dropr2 = DropItemObjTeam_Server(teamindex,&quot;DropRate&quot;..i)<br />			if dropr2==&quot;&quot; or not dropr2 then<br />				dropr2 = 0<br />			else<br />				dropr2 = tonumber(dropr2)<br />			end<br /><br />			droprate2 = dropr2 + droprate2<br />			droprate1 = dropr1 + droprate1<br />			if RandomItemRate&gt;droprate1 and RandomItemRate&lt;=droprate2 then<br />				ItemRateIndex=i<br />				break<br />			end<br />		end<br />		<br />	end<br />	if ItemRateIndex&gt;0 and ItemRateIndex&lt;=TeamItemNum then<br />		local ObjNameIndex = &quot;DropName&quot;..ItemRateIndex<br />		local ObjName = DropItemObjTeam_Server(teamindex,ObjNameIndex)<br />		local DropMinNum = DropItemObjTeam_Server(teamindex,&quot;DropMinNum&quot;)<br />		local DropMaxNum = DropItemObjTeam_Server(teamindex,&quot;DropMaxNum&quot;)<br />		math.randomseed(os.clock())<br /> &nbsp; 		local ObjNum = math.random(2,5)<br />--		print(&quot;DropObjName &quot;,ObjName)<br />		if RoleQuestDB.CountObjDropLimitByName(ObjName) &lt;= 0 then<br />			if g_WhereGiveQuestMgr&#91;&quot;Obj&quot;&#93;&#91;ObjName&#93; then<br />				local questname = g_WhereGiveQuestMgr&#91;&quot;Obj&quot;&#93;&#91;ObjName&#93;&#91;1&#93;<br />				local queststate = RoleQuestDB.GetQuestState({PlayerId,questname})<br />				if queststate == nil or (queststate == QuestState.finish and g_RepeatQuestMgr&#91;questname&#93;) then<br />					table.insert(Objs_List, {ObjName,1})<br />				end<br />			else<br />				for i = 1, ObjNum do<br />					table.insert(Objs_List, {ObjName,1})<br />				end<br />			end<br />		end<br />	end<br />	return Objs_List<br />end<br /><br />--杀死怪后,产生掉落物品的tal表<br />local function NpcDeadDropItem(PlayerId,NpcName,NpcLevel,TeamID,CanSharePlayerTbl)<br />	local DropItemTbl = {}<br />	DropItemTbl.m_Items_List = {}<br />	local NpcDropItemTbl = g_NpcDropItemTable&#91;NpcName&#93;<br />	local CharacterMediatorDB = RequireDbBox(&quot;CharacterMediatorDB&quot;)<br />	local PlayerLevel = CharacterMediatorDB.GetPlayerLevel(PlayerId)<br />	if NpcDropItemTbl ~= nil then<br />		for i = 1, table.getn(NpcDropItemTbl) do<br />			---对NpcDropItem_Common&#91;NpcName&#93;的每一Team进行随机<br />			local dropLevel = NpcDropItemTbl&#91;i&#93;&#91;&quot;Level&quot;&#93;<br />			if dropLevel == nil or dropLevel == &quot;&quot; or (dropLevel&#91;1&#93; &lt;= NpcLevel and dropLevel&#91;2&#93; &gt;= NpcLevel) then<br />				local dropr = NpcDropItemTbl&#91;i&#93;&#91;&quot;DropRate&quot;&#93;(NpcLevel, PlayerLevel)<br />				if (dropr*DropProbability) &gt;= math.random(0,DropProbability) then<br />					local team_index = NpcDropItemTbl&#91;i&#93;&#91;&quot;DropTeam&quot;&#93;<br />					if team_index ~=&quot;&quot; then<br />						--从Team中随机物品<br />						local tbl = RandomTeamItems(PlayerId,team_index)<br />						for j = 1, table.getn(tbl) do<br />							table.insert(DropItemTbl.m_Items_List, tbl&#91;j&#93; )<br />						end<br />					end<br />				end<br />			end<br />		end<br />	end<br />--	if NpcDropItem_Common&#91;NpcName&#93;~=nil then<br />--		for i=1,DropTeamNum do<br />--			---对NpcDropItem_Common&#91;NpcName&#93;的每一Team进行随机<br />----		 &nbsp;local dropLevel = NpcDropItem_Common&#91;NpcName&#93;&#91;&quot;DropTeamLevel&quot;..i&#93;<br />--			local dropLevel = GetCfgTransformValue(false, &quot;ObjRandomDropItem_Server&quot;, NpcName, &quot;DropTeamLevel&quot;..i)<br />--			if dropLevel == nil or dropLevel == &quot;&quot; or (dropLevel&#91;1&#93; &lt;= NpcLevel and dropLevel&#91;2&#93; &gt;= NpcLevel) then<br />-- &nbsp; 			--local RandomRate = math.random(0,DropProbability)<br />--				local dropr = NpcDropItem_Common&#91;NpcName&#93;&#91;&quot;DropTeamRate&quot;..i&#93;<br />--				if dropr ~= nil and dropr ~= &quot;&quot; and (dropr*DropProbability) &gt;= math.random(0,DropProbability) then<br />--					local team_index = NpcDropItem_Common&#91;NpcName&#93;&#91;&quot;DropTeam&quot;..i&#93;<br />--					if team_index ~=&quot;&quot; then<br />--						--从Team中随机物品<br />--						local tbl = RandomTeamItems(PlayerId,team_index)<br />--						for j = 1, table.getn(tbl) do<br />--							table.insert(DropItemTbl.m_Items_List, tbl&#91;j&#93; )<br />--						end<br />--					end<br />--				end<br />--			end<br />--		end<br />--	end<br /><br />	DropItemTbl.m_OwnerId = PlayerId<br />	DropItemTbl.m_CreateTime = os.time()<br />	if table.getn(CanSharePlayerTbl) &gt; 1 then<br />		DropItemTbl.m_BelongToTeam = TeamID<br />		DropItemTbl.m_CanSharePlayer = CanSharePlayerTbl<br />	end<br />	<br />	return DropItemTbl<br />end<br /><br />local function QuestDropObj(NpcName,ShowName,DropObjTbl,CanSharePlayerTbl,sceneName)<br />	local AddItemResTbl = {}<br />	local DropInfo = g_QuestDropItemMgr&#91;NpcName&#93; or g_QuestDropItemMgr&#91;ShowName&#93;<br />	if not DropInfo then<br />		return AddItemResTbl<br />	end<br />	<br />	for questname , p in pairs(DropInfo) do<br />		for i = 1, table.getn(p) do<br />			local droptype,dropname,droprate = p&#91;i&#93;.Type,p&#91;i&#93;.Object,p&#91;i&#93;.Rate<br />			local RandomRate = math.random(0,DropProbability) &nbsp;<br />			if droprate*DropProbability &gt;= RandomRate then<br />				for j = 1, table.getn(CanSharePlayerTbl) do<br />					local PlayerId = CanSharePlayerTbl&#91;j&#93;<br />					local state = RoleQuestDB.GetQuestState({PlayerId, questname})<br />					if state == QuestState.init then			<br />						--填加任务物品(直接向背包中添加)<br />						local param = {}<br />						param&#91;&quot;char_id&quot;&#93; = PlayerId<br />						param&#91;&quot;nType&quot;&#93; = droptype<br />						param&#91;&quot;sName&quot;&#93; = dropname<br />						param&#91;&quot;nCount&quot;&#93; = 2<br />						param&#91;&quot;CheckQuestNeed&quot;&#93; = true<br />						param&#91;&quot;sceneName&quot;&#93; = sceneName<br />						param&#91;&quot;createType&quot;&#93; = event_type_tbl&#91;&quot;任务掉落物品&quot;&#93;<br />						local AddItemRes = CharacterMediatorDB.AddItem(param)<br />						local tbl = {AddItemRes,&#91;&quot;PlayerId&quot;&#93;=PlayerId}<br />						if not IsNumber(AddItemRes) then &nbsp;--不是数字添加成功<br />							tbl&#91;&quot;ItemType&quot;&#93;=droptype<br />							tbl&#91;&quot;ItemName&quot;&#93;=dropname<br />							tbl&#91;&quot;ItemNum&quot;&#93;=2<br />							table.insert(AddItemResTbl,tbl)<br />						else<br />							---暂时屏蔽<br />--							if AddItemRes == 3 then --添加不成功的原因是背包满，则转为传奇掉落<br />--								local tbl1 = {dropname,1,&#91;&quot;QuestDropOwner&quot;&#93; = PlayerId}<br />--								table.insert(DropObjTbl.m_Objs_List, tbl1)<br />--							end<br />							table.insert(AddItemResTbl,tbl)<br />						end<br />					end<br />				end<br />			end<br />		end<br />	end<br />	return AddItemResTbl<br />end<br /><br />--杀死怪后,掉落Obj的tal表<br />local function NpcDeadDropObj(PlayerId,NpcName,NpcLevel,TeamID,CanSharePlayerTbl, TempTeamMemberID)<br />	local DropObjTbl = {}<br />	DropObjTbl.m_Objs_List = {}<br />	local NpcDropItemObjTbl = g_NpcDropObjTable&#91;NpcName&#93;<br />	local CharacterMediatorDB = RequireDbBox(&quot;CharacterMediatorDB&quot;)<br />	local PlayerLevel = CharacterMediatorDB.GetPlayerLevel(PlayerId)<br />	if NpcDropItemObjTbl~=nil then<br />		for i = 1, table.getn(NpcDropItemObjTbl) do<br />			local dropLevel = NpcDropItemObjTbl&#91;i&#93;&#91;&quot;Level&quot;&#93;<br />			if dropLevel == nil or dropLevel == &quot;&quot; or (dropLevel&#91;1&#93; &lt;= NpcLevel and dropLevel&#91;2&#93; &gt;= NpcLevel) then<br />				local dropr = NpcDropItemObjTbl&#91;i&#93;&#91;&quot;DropRate&quot;&#93;(NpcLevel, PlayerLevel)<br />				if (dropr*DropProbability) &gt;= math.random(0,DropProbability) then<br />					local team_index = NpcDropItemObjTbl&#91;i&#93;&#91;&quot;DropTeam&quot;&#93;<br />					if team_index ~=&quot;&quot; then<br />						local tbl = RandomTeamObjs(PlayerId,team_index)<br />						for j = 1, table.getn(tbl) do<br />							table.insert(DropObjTbl.m_Objs_List, tbl&#91;j&#93; )<br />						end<br />					end<br />				end<br />			end<br />		end<br />	end<br />	DropObjTbl.m_OwnerId = PlayerId<br />	DropObjTbl.m_TempTeamMemberID = TempTeamMemberID<br />	if table.getn(CanSharePlayerTbl) &gt; 1 then<br />		local eAM = GasTeamDB.GetAssignMode(TeamID) or EAssignMode.eAM_NeedAssign &nbsp; &nbsp; --按小队分配<br />		local AuctionStandard = GasTeamDB.GetAuctionStandard(TeamID) or EAuctionStandard.eAS_GreenStandard --默认为0 &nbsp; --拍卖方式<br />		local AuctionBasePrice = GasTeamDB.GetAuctionBasePrice(TeamID) or 0 --默认为0 &nbsp; --拍卖方式<br />		---分配模式为各自拾取时就不用指定队伍归属和可共享者列表了<br />		DropObjTbl.m_CanSharePlayer = CanSharePlayerTbl<br />		if eAM ~= EAssignMode.eAM_RespectivePickup then &nbsp;--不属于自动拾取<br />			DropObjTbl.m_BelongToTeam = TeamID<br />			DropObjTbl.m_AssignMode = eAM<br />			DropObjTbl.m_AuctionStandard = AuctionStandard<br />			DropObjTbl.m_AuctionBasePrice = AuctionBasePrice<br />		end<br />	end<br />	return DropObjTbl<br />end<br /><br />function NpcDropItemDB.NpcDeadDrop(PlayerId,data,CanShareTeamMateTbl,sceneName)<br />	<br />	--获取符合分配条件的SharePlayerTbl<br />	local AreaName = data&#91;&quot;AreaName&quot;&#93;<br />	local CanSharePlayerTbl = {}<br />	table.insert(CanSharePlayerTbl,PlayerId)<br />	for i = 1, table.getn(CanShareTeamMateTbl) do<br />		table.insert(CanSharePlayerTbl,CanShareTeamMateTbl&#91;i&#93;)<br />	end<br />	local NpcName = data&#91;&quot;NpcName&quot;&#93;<br />	local NpcLevel = data&#91;&quot;MonsterLevel&quot;&#93;<br />	local ShowName = data&#91;&quot;ShowName&quot;&#93;<br />	local TeamID = data&#91;&quot;TeamID&quot;&#93;<br />	local DropObjTbl = NpcDeadDropObj(PlayerId,NpcName,NpcLevel,TeamID,CanSharePlayerTbl, data&#91;&quot;TempTeamMemberID&quot;&#93;)<br />	local DropItemTbl = NpcDeadDropItem(PlayerId,NpcName,NpcLevel,TeamID,CanSharePlayerTbl)<br />	local AddItemResTbl = QuestDropObj(NpcName,ShowName,DropObjTbl,CanSharePlayerTbl,sceneName )<br />	return DropObjTbl,DropItemTbl,AddItemResTbl<br />end<br /><br />local	function GetCanPickUpIndexTbl(PlayerId, ItemList)<br />	local PickUpIndex = {}<br />	local RoleQuestDB = RequireDbBox(&quot;RoleQuestDB&quot;)<br />	for index = 1,table.maxn(ItemList) do<br />		if ItemList&#91;index&#93; then<br />			local ItemType,ItemName ,ItemNum = ItemList&#91;index&#93;&#91;1&#93;,ItemList&#91;index&#93;&#91;2&#93;,ItemList&#91;index&#93;&#91;3&#93;<br />			local itemtype = tonumber(ItemType)<br />			if (not ItemList&#91;index&#93;&#91;&quot;QuestDropOwner&quot;&#93;) then<br />				--不是任务掉落，也不是等待拍卖的物品，可以拾取<br />				table.insert(PickUpIndex, index)<br />			elseif ItemList&#91;index&#93;&#91;&quot;QuestDropOwner&quot;&#93; == PlayerId <br />				and RoleQuestDB.CheckNeedItemNum(PlayerId, ItemType, ItemName, true)&#91;1&#93; then<br />				--是任务掉落，而且掉落归属自己，并且通过任务检测<br />				table.insert(PickUpIndex, index)<br />			end<br />		end<br />	end<br />	return PickUpIndex &nbsp;--返回可以拾取的物品Index表<br />end<br /><br />function NpcDropItemDB.ShowNpcDropItem(data)<br />	local PlayerId = data&#91;&quot;PlayerId&quot;&#93;<br />	local Items_List = data&#91;&quot;DropItemTbl&quot;&#93;.m_Items_List<br />	return GetCanPickUpIndexTbl(PlayerId, Items_List)<br />end<br /><br />function NpcDropItemDB.SelAllDropItems(data)<br />	local DropItemTbl = data&#91;&quot;DropItemTbl&quot;&#93;<br />	local ItemList = DropItemTbl.m_Items_List<br />	local RemoveIndexTbl = {}<br />	local AddResTbl = {}<br />	local param = {}<br />	param&#91;&quot;char_id&quot;&#93; = data&#91;&quot;PlayerId&quot;&#93;<br />	param&#91;&quot;createType&quot;&#93; = event_type_tbl&#91;&quot;点npc尸体掉落(点所有格子)&quot;&#93;<br />	param&#91;&quot;sceneName&quot;&#93; = data&#91;&quot;sceneName&quot;&#93;<br />	<br />	if DropItemTbl.m_PickUpIndex then<br />		for index = 1, table.getn(DropItemTbl.m_PickUpIndex) do<br />			local GetIndex = DropItemTbl.m_PickUpIndex&#91;index&#93;<br />			local itemtype = ItemList&#91;GetIndex&#93;&#91;1&#93;<br />			local itemname = ItemList&#91;GetIndex&#93;&#91;2&#93;<br />			local itemnum = ItemList&#91;GetIndex&#93;&#91;3&#93;<br />			<br />			param&#91;&quot;nType&quot;&#93;	= itemtype<br />			param&#91;&quot;sName&quot;&#93; 	= itemname<br />			param&#91;&quot;nCount&quot;&#93;	= itemnum<br />			if ItemList&#91;GetIndex&#93;&#91;&quot;QuestDropOwner&quot;&#93; then<br />				param&#91;&quot;CheckQuestNeed&quot;&#93;	= true<br />			end<br />			local ResTbl = NpcDropItemDB.AddOneGridItem(param,true)<br />			if ResTbl&#91;1&#93; then<br />				table.insert(RemoveIndexTbl,GetIndex)<br />			end<br />			ResTbl&#91;&quot;nType&quot;&#93;	= itemtype<br />			ResTbl&#91;&quot;sName&quot;&#93; 	= itemname<br />			ResTbl&#91;&quot;nCount&quot;&#93;	= itemnum<br />			table.insert(AddResTbl,ResTbl)<br />		end<br />	end<br />	return {AddResTbl,RemoveIndexTbl}<br />end<br /><br />--添加物品,方法1<br />function NpcDropItemDB.AddOneGridItem(data,IsTran)<br />	local CharacterMediatorDB = RequireDbBox(&quot;CharacterMediatorDB&quot;)<br />	local res1 = CharacterMediatorDB.AddItem(data)<br />	if IsNumber(res1) then<br />		if not IsTran then<br />			CancelTran()<br />		end<br />		local RoomMgr = RequireDbBox(&quot;GasRoomMgrDB&quot;)<br />		return {false,res1}<br />	end<br />	return {true,res1}<br />end<br /><br />function NpcDropItemDB.SelOneGridDropItem(data)<br />----	local PlayerId = data&#91;&quot;PlayerId&quot;&#93;<br />----	local DropItemTbl = data&#91;&quot;DropItemTbl&quot;&#93;<br />----	local GetIndex = data&#91;&quot;GetIndex&quot;&#93;<br />----	local ItemList = DropItemTbl.m_Items_List<br />--	local Param = {}<br />--	<br />--	if not ItemList&#91;GetIndex&#93;&#91;&quot;QuestDropOwner&quot;&#93; then &nbsp;<br />--		Param&#91;&quot;CheckQuestNeed&quot;&#93; = true<br />--	end<br />--	<br />--	Param&#91;&quot;char_id&quot;&#93;	= PlayerId<br />--	Param&#91;&quot;nType&quot;&#93;	= ItemList&#91;GetIndex&#93;&#91;1&#93;<br />--	Param&#91;&quot;sName&quot;&#93;	= ItemList&#91;GetIndex&#93;&#91;2&#93;<br />--	Param&#91;&quot;nCount&quot;&#93;	= ItemList&#91;GetIndex&#93;&#91;3&#93;<br />	data&#91;&quot;createType&quot;&#93; = event_type_tbl&#91;&quot;点npc尸体掉落(点一个格子)&quot;&#93;<br />	return NpcDropItemDB.AddOneGridItem(data)<br />end<br /><br />SetDbLocalFuncType(NpcDropItemDB.ShowNpcDropItem)<br />SetDbLocalFuncType(NpcDropItemDB.SelOneGridDropItem)<br />SetDbLocalFuncType(NpcDropItemDB.SelAllDropItems)<br />return NpcDropItemDB<br />&#91;/SPOILER&#93; <br />
			<i>26.10.2018 14:42:17, Mercenar.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message34581/topic4042/</link>
			<guid>http://forum.quik.ru/messages/forum10/message34581/topic4042/</guid>
			<pubDate>Fri, 26 Oct 2018 14:42:17 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
	</channel>
</rss>
