一. 需求:
传入一个道具id,递归地判断它的获取途径里的其中一个道具是否可以从商店购买得到,如果可以,则返回true,否则返回false
先弄清楚道具的获取途径:
1. 通过商店购买、关卡掉落、xx店获得
2. 由1个道具进化而来
3. 由1个道具染色而来,但是可选的范围可能有N种道具
4. 由N个道具合成而来
但是,还有更复杂的几种情况要考虑:
1. 复杂情况1:某个道具由N个子道具合成而来,但是这N个子道具,又可能分别是进一步由合成、购买或者进化而来...
2. 复杂情况2:某个道具本身不可以购买,所有子道具也不能通过进化、合成获得,但是可以通过染色方式获得,而某一种染色的源道具是可以从商店购买到的
二、数据结构:
gdClothesData = {
[10008]= {
id = 10008,
...
cvt_base=10007
}
}
gdClothesEvolutionData ={
[10065]= {id=10065,num=6,src=10064,},
[10066]= {id=10066,num=4,src=10065,},
}
gdClothesCvtSeriesData ={
[10004]= {[1]=10004,[2]=10005,[3]=10006,},
[10007]= {[1]=10007,[2]=10008,[3]=10009,},
}
gdClothesMergeData ={
[10017]= {
id=10017,
name="浅慵",
clothes= {
[1]= {cloth=50045,num=3,},
[2]= {cloth=10013,num=4,},
},
....
},
...
}
三、实现
于是针对这几种情况,编写了一个比较复杂的递归代码:
cc.FileUtils:getInstance():addSearchPath("src")
cc.FileUtils:getInstance():addSearchPath("res")
-- CC_USE_DEPRECATED_API = true
require "cocos.init"
-- 用于测试 10007 reslut: true
-- 10007 不能购买
-- 10007 <-染- 10008 不能购买
-- 10007 <-染- 10009 能购买
require "kk1.clothes_convert_data"
require "kk1.clothes_data"
require "kk1.clothes_evolution_data"
require "kk1.clothes_merge_data"
--[[
-- 用于测试非修改的原始数据
require "kk.clothes_convert_data"
require "kk.clothes_data"
require "kk.clothes_evolution_data"
require "kk.clothes_merge_data"
]]
-- cclog
local cclog = function(...)
print(string.format(...))
end
-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
cclog("----------------------------------------")
cclog("LUA ERROR: " .. tostring(msg) .. "\n")
cclog(debug.traceback())
cclog("----------------------------------------")
return msg
end
-- 获取某个id的道具对应的染色src id
-- 判断该id的道具是否可以购买得到
function isCanBuy(id)
item = AllClothesResolveObtain[id];
if( (item == nil) or ( table.maxn(item) <= 0 ) ) then
return false;
end;
return item[1].obtainWay == "商店";
end
-- recursion 递归搜索
function checkIsValidItem( id, deep, isIgnoreCheckColor)
ret0 = false;
ret1 = false;
ret2 = false;
ret3 = false;
-- 判断是否可以买到, 如果买得到,立即返回true
ret0 = isCanBuy(id);
if(ret0 == true) then
return true;
end
-- from evolution 进化
item = gdClothesEvolutionData[id];
if(item ~= nil) then
ret1 = checkIsValidItem( item.src, deep + 1, false );
if(ret1 == true) then
return true;
end
end
-- from draw color 染色
item = gdClothesData[id];
if( (isIgnoreCheckColor ~= true) and (item ~= nil) ) then
cvr_base_id = item.cvt_base;
-- 查询出可以相互染色的同类衣服,只要有其中任何一个可以购买到,就认为是有效的
color_src_ids = gdClothesCvtSeriesData[cvr_base_id];
for key,val in pairs(color_src_ids) do
if(id ~= val) then -- 自身不需要检验
ret2 = checkIsValidItem(val, deep + 1, true);
if(ret2 == true) then
return true;
end
end
end
-- 如果同类没有检验到,则检查cvr_base_id
--[[
if(id ~= cvr_base_id) then
ret2 = checkIsValidItem( cvr_base_id, deep + 1, true );
end
]]
if(ret2 == true) then
return true;
end
end
-- from merge 合成
item = gdClothesMergeData[id];
if(item ~= nil) then
merge_src_ids = item.clothes;
for key,val in pairs(merge_src_ids) do
merge_src_id = val.cloth;
if( checkIsValidItem( merge_src_id, deep + 1, false) == true ) then
ret3 = true;
else
ret3 = false;
break;
end;
end
if(ret3 == true) then
return true;
end
end
-- 判断递归深度异常
if(deep > 20) then
print( "error:" .. tostring(id) )
return false;
end
return false;
end
-- @sonikk 2015-6-3 13:50:23
-- @sonikk 2015-6-3 17:16:28
-- @sonikk 2015-6-3 20:47:34
function test_start_search()
--find_id = 10004; -- true
--find_id = 10005; -- 测试染色 src: 10004 true
--find_id = 10065; -- 测试进化 src: 10064 false
--find_id = 10026; --测试合成 src: 10129,10132,10010,10170 (10129 关卡掉落,抽奖店2,抽奖店3) (10010 商店) (10132, 10170 抽奖店2)
find_id = 10007;
deep = 0;
ret = checkIsValidItem(find_id, deep, false);
print( string.format("find id: [%d] result: %s", find_id, tostring(ret) ) )
end
-- 这是主函数
local function main()
test_start_search();
end
local status, msg = xpcall(main, __G__TRACKBACK__)
if not status then
error(msg)
end
n