更多源码详见触动官方 Githb:https://github.com/chudongjingling/open_code。
渐开线实现
渐开线在游戏脚本中多用于找怪或者农场类游戏中收割,下面给出两种不同的渐开线在触动精灵中的实现方法:
方形渐开线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
init(1) mSleep(1000) local x = 1010 --起始坐标x local y = 698 --起始坐标y local v = 30 --两点间距离 touchDown(x, y) mSleep(100) for var = 1,20 do j = 0 k = v for _i = 1,2 do for i = 1,10 do x = x + j y = y + k touchMove(x, y) mSleep(20) end j = v k = 0 end v = v * (-1.05) end touchUp(x, y) |
圆形渐开线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
init(1) mSleep(1000) local x0 = 1010 --起始坐标x local y0 = 698 --起始坐标y local rr = 20 --设置递增半径 local l = 10 --设置点间距 local p = 0 --初始化角度 local r = 30 --设置首圈半径 local rn = 10 --设置圈数 touchDown(x0, y0) mSleep(100) for var = 1,rn do while p < math.pi * 2 do x = x0 + r * math.cos(p) y = y0 - r * math.sin(p) touchMove(x, y) mSleep(10) p = p + l/r end p = 0 r = r + rr end touchUp(x0, y0) |
多点模糊比色
在实际游戏脚本制作中,很多界面单靠1个点不容易进行准确的判断,这里封装一个配合TABLE使用的多点模糊比色函数来实现精确判断:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
function multiColor(array,s) s = math.floor(0xff*(100-s)*0.01) keepScreen(true) for var = 1, #array do local lr,lg,lb = getColorRGB(array[var][1],array[var][2]) local r = math.floor(array[var][3]/0x10000) local g = math.floor(array[var][3]%0x10000/0x100) local b = math.floor(array[var][3]%0x100) if math.abs(lr-r) > s or math.abs(lg-g) > s or math.abs(lb-b) > s then keepScreen(false) return false end end keepScreen(false) return true end --用法 g_t_Table = { { 1962, 52, 0xefdccf}, { 2150, 50, 0xefd8d0}, { 1964, 76, 0xe9d1c5}, { 2152, 74, 0xefdcd1}, { 2122, 62, 0xf1ddd1}, { 2146, 1080, 0x893824}, { 1840, 1082, 0x593724}, } if multiColor(g_t_Table,90) then touchDown(100,100) mSleep(50) touchUp(100,100) end |
参数 s 为模糊度,范围 0 - 100,一般使用90即可。
实例中的TABLE格式可使用触动精灵抓色器生成。
随机字符串
产生随机种子
由于lua中的随机函数产生的随机数是伪随机,我们需要设置一个随机种子来解决此问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
--改进方法 math.randomseed(tostring(os.time()):reverse():sub(1, 6)) -- 随机种子 dialog(math.random()) --进阶方法,需加载扩展库中的socket模块 local ts = require("ts") local function get_seed() local t = string.format("%f", socket.gettime()) local st = string.sub(t, string.find(t, "%.") + 1, -1) return tonumber(string.reverse(st)) end math.randomseed(get_seed()) for var = 1,5 do dialog(math.random()) end |
自定义字符串随机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function randomStr(str, num) local ret ='' for i = 1, num do local rchr = math.random(1, string.len(str)) ret = ret .. string.sub(str, rchr, rchr) end return ret end --用法 math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6))) --设置随机种子 for var = 1,5 do s = randomStr("abcdefghijklmnopqrstuvwxyz", 6) --生成6位随机字母 nLog(s) end |
随机大小写字母
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function rndLetter(num) local ret = "" pcall(function() for var = 1,num do if math.random()>0.5 then ret = ret..string.char(math.random(65,90)) else ret = ret..string.char(math.random(97,122)) end end end) return ret end --用法 math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6))) --设置随机种子 for var = 1,5 do nLog(rndLetter(10)) --生成一个10位随机大小写字母的字符串 end |
日期与时间
获取今天是星期几
1 2 3 4 |
local today = tonumber(os.date("%w",os.time())) if today == 0 or today == 6 then --如果是周日或者周六 dialog(today, 0) end |
获取当前日期及时间
1 2 3 4 5 6 7 8 9 10 11 |
local nowTime = os.date("*t",os.time()) --返回一个table dialog(nowTime.year, 0) --年 dialog(nowTime.month, 0) --月 dialog(nowTime.day, 0) --日 dialog(nowTime.hour, 0) --小时 dialog(nowTime.min, 0) --分钟 dialog(nowTime.sec, 0) --秒钟 dialog(nowTime.yday, 0) --显示当前为一年中的第几天 --时间戳格式化当前时间 local nowTime = os.date("%Y-%m-%d %H:%M:%S", os.time()) dialog(nowTime,0) |
UI
一个包含所有控件的默认样式UI实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
local ts = require("ts")--写 showUI 前必须插入这一句 local json = ts.json--写 showUI 前必须插入这一句 w,h = getScreenSize(); MyTable = { ["style"] = "default", ["width"] = w, ["height"] = h, ["config"] = "save_01.dat", ["timer"] = 99, ["orient"] = 0,--需引擎版本 iOS v2.2.5以上版本支持,Android 暂不支持 ["pagetype"] = "multi",--需引擎版本 iOS v2.2.5,Android v2.1.5 以上版本支持 ["title"] = "触动精灵脚本配置",--需引擎版本 iOS v2.2.5,Android v1.2.4 以上版本支持 ["cancelname"] = "取消", ["okname"] = "开始", pages = { { { ["type"] = "Label", ["text"] = "第一页设置", ["size"] = 25, ["align"] = "center", ["color"] = "0,0,0", }, { ["type"] = "RadioGroup", ["list"] = "选项1,选项2,选项3,选项4,选项5,选项6,选项7", ["select"] = "1", }, },{ { ["type"] = "Label", ["text"] = "第二页设置", ["size"] = 25, ["align"] = "center", ["color"] = "0,0,0", }, { ["type"] = "Edit", ["prompt"] = "请输入一个字母", ["text"] = "默认值", ["kbtype"] = "ascii", }, { ["type"] = "Edit", ["prompt"] = "请输入一个数字", ["text"] = "默认值", ["kbtype"] = "number", }, },{ { ["type"] = "Label", ["text"] = "第三页设置", ["size"] = 25, ["align"] = "center", ["color"] = "0,0,0", }, { ["type"] = "CheckBoxGroup", ["list"] = "选项1,选项2,选项3,选项4,选项5,选项6,选项7", ["select"] = "3@5", }, { ["type"] = "ComboBox", ["list"] = "选项1,选项2,选项3", ["select"] = "1", ["data"] = "子选项1,子选项2,子选项3,子选项4#子选项5,子选项6,子选项7#子选项8,子选项9", ["source"] = "test" }, { ["type"] = "ComboBox", ["select"] = "1", ["dataSource"] = "test" }, } } } local MyJsonString = json.encode(MyTable); retTable = {showUI(MyJsonString)}; for var = 1,#retTable do nLog(retTable[var]) --输出每一个返回值 end |
以上实例中使用了两个 ComboBox 控件,并在两个 ComboBox 控件之间建立了数据关联,此控件属性需引擎版本 iOS v2.1.8,Android v1.1.0 以上支持。
以上实例中的 title 属性需引擎版本 iOS v2.2.5,Android v1.2.4 以上版本支持。
以上实例中的 pagetype 属性需引擎版本 iOS v2.2.5,Android v2.1.5 以上版本支持。
以上实例中的 orient 属性需引擎版本 iOS v2.2.5以上版本支持,Android 暂不支持。
以上实例中最后对于 showUI 的调用将返回一个table。
企业版
注入中控器简单的发账号示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
--[[ 简单的发账号示例 script/account.txt内保存账号密码 --]] local ok,account = telib:controller_injection( [[ local f = io.open("script/account.txt", "r") if f then local account_tab = {} local account = f:read() while account do table.insert(account_tab,account) account = f:read() end f:close() if #account_tab > 0 then local f = io.open("script/account.txt", "w") if f then for i = 2,#account_tab do f:write(account_tab[i].."\n") end f:close() end return account_tab[1] else return false end end return false ]] ) assert(ok,account) if account then nLog("获取账号:"..account) toast("获取账号:"..account) else nLog("获取账号失败") toast("获取账号失败") end mSleep(1000) |
封装中控器读写函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
function writeEEFile(str,path,mode) --写入文件内容,路径,类型,默认路径企业版目录scriptData/playLog.text下,自定义路径请把\改为/。默认类型追加,覆盖第三个参数写"wb" mode = mode or "a" local telib = require("ts_enterprise_lib") assert(telib,"无法引入企业专用库") if path==nil then local ok,msg = telib:controller_injection( [[ require("lfs").mkdir("scriptData") local f = io.open("scriptData/playLog.text","a") if f then f:write("]]..str..[[\r\n") f:close() end ]] ) else local ok,msg = telib:controller_injection( [[ local f = io.open("]]..path..[[","]]..mode..[[") if f then f:write("]]..str..[[\r\n") f:close() end ]] ) end end function findEEFile(path,tsType) --读取文件内容,读取方式(string或table) path = path or "scriptData/playLog.text" tsType = tsType or "string" local telib = require("ts_enterprise_lib") assert(telib,"无法引入企业专用库") local ok,account if tsType=="table" then ok,account = telib:controller_injection( [[ local f = io.open("]]..path..[[", "r") if f then local account_tab = {} local account = f:read() while account do table.insert(account_tab,account) account = f:read() end f:close() return account_tab end return false ]] ) else ok,account = telib:controller_injection( [[ local f = io.open("]]..path..[[", "r") if f then local account = f:read("*all") f:close() return account end return false ]] ) end return account end |