天龙八部服务端是 Lua4,很多数学函数历史包袱比较重:老脚本直接用全局 sin() / cos(),而且三角函数还是角度制。但 Lua5 开始,官方数学库已经统一走 math.xxx,并且三角函数改成了弧度制。
我的做法是 B 方案:
- 原来的全局函数完全不动(老脚本不炸)
- 新增一套
math.xxx(Lua5 风格):三角函数按 弧度工作,补齐 math.pi / math.huge / math.fmod / math.modf 等常用字段
下面只写 Lua 端怎么用。
| 新增函数 |
作用 |
table.concat(list[, sep[, i[, j]]]) |
把 list[i..j] 拼成一个字符串,中间用 sep 连接(默认空串)。 |
table.insert(list, [pos,] value) |
往数组表里插入元素:不写 pos 就追加到末尾,写了就插入并整体后移。 |
table.remove(list[, pos]) |
删除并返回 pos 位置元素:不写 pos 默认删末尾,同时把后面的元素前移。 |
table.sort(list[, comp]) |
原地排序 list[1..table.getn(list)];可选比较函数 comp(a,b)。 |
table.pack(...) |
把所有参数打包成表:t[1]=...,并写入 t.n=参数个数(支持中间有 nil)。 |
table.unpack(list[, i[, j]]) |
展开返回 list[i..j];i 默认 1;j 默认 table.getn(list)。 |
table.getn(t) |
取长度(以 Lua4 规则为准:通常依赖 t.n 或内部长度策略)。 |
table.setn(t, n) |
设置表长度( Lua4 风格:写 t["n"]=n)。 |
table.foreach(t, f) |
遍历 table:对每个 k,v 调用 f(k,v);若返回非 nil 就立刻停止并返回该值。 |
table.foreachi(t, f) |
遍历数组部分:按 1..getn(t) 调用 f(i, t[i]);若返回非 nil 就立刻停止并返回该值。 |
0) 重点:角度 vs 弧度(别踩坑)
- 老脚本:
sin(90) —— 角度制(保持不变)
- 新写法:
math.sin(math.pi/2) —— 弧度制(Lua5) (lua.org)
如果你手里是角度(比如 90 度),Lua5 风格写法就是:
print(math.sin(math.rad(90))) -- 1
1) math.pi
作用:圆周率 π
print(math.pi) -- 3.1415926535...
2) math.huge
作用:一个非常大的数(HUGE_VAL),基本当作 “∞” 用 (lua.org)
print(math.huge)
3) 三角函数(Lua5 风格:弧度)
math.sin(x) / math.cos(x) / math.tan(x)
print(math.sin(math.pi/2)) -- 1
print(math.cos(0)) -- 1
print(math.tan(math.pi/4)) -- 1
math.asin(x) / math.acos(x) / math.atan(x) / math.atan2(y, x)
返回值也是 弧度。
print(math.asin(1)) -- 1.570796...
print(math.atan2(1, 1)) -- 0.785398...
4) 角度/弧度转换
math.rad(deg):角度 → 弧度
math.deg(rad):弧度 → 角度
(Lua5 常用)(lua.org)
print(math.rad(180)) -- 3.141592...
print(math.deg(math.pi)) -- 180
5) 取余
math.fmod(x, y)(Lua5 常用)(lua.org)
math.mod(x, y)(和fmod同一个逻辑,方便兼容老写法)
print(math.fmod(10, 3)) -- 1
print(math.mod(10, 3)) -- 1
6) math.modf(x)
作用:把数字拆成 整数部分 和 小数部分(两个返回值)
local ip, fp = math.modf(12.34)
print(ip, fp) -- 12 0.34(左右,浮点会有误差)
7) math.pow(x, y)
作用:乘方(Lua5 风格表函数)
print(math.pow(2, 10)) -- 1024
8) 常用基础函数(基本都跟 Lua5 一致)
print(math.abs(-3.5)) -- 3.5
print(math.ceil(3.1)) -- 4
print(math.floor(3.9)) -- 3
print(math.sqrt(9)) -- 3
print(math.min(3,9,2,7)) -- 2
print(math.max(3,9,2,7)) -- 9
print(math.log(10)) -- ln(10)
print(math.log10(1000)) -- 3
print(math.exp(1)) -- e
9) 随机数
math.random() / math.random(u) / math.random(l, u)
math.randomseed(12345)
print(math.random()) -- 0~1
print(math.random(10)) -- 1~10
print(math.random(5, 8)) -- 5~8
综合测试脚本(验证 math 是否生效)
print("=== math basic ===")
print(math.abs(-3.5))
print(math.ceil(3.1))
print(math.floor(3.9))
print(math.sqrt(9))
print(math.min(3,9,2,7))
print(math.max(3,9,2,7))
print(math.pow(2,10))
print("\n=== math pi/huge ===")
print(math.pi)
print(math.huge)
print("\n=== trig (radians) ===")
print(math.sin(math.pi/2))
print(math.cos(0))
print(math.tan(math.pi/4))
print(math.deg(math.pi))
print(math.rad(180))
print("\n=== fmod/mod ===")
print(math.mod(10,3))
print(math.fmod(10,3))
print("\n=== modf ===")
local ip, fp = math.modf(12.34)
print(ip, fp)
print("\n=== random ===")
math.randomseed(12345)
print(math.random())
print(math.random(10))
print(math.random(5,8))
输出结果:
=== math basic ===
3.5
4
3
3
2
9
1024
=== math pi/huge ===
3.141592653589793
inf
=== trig (radians) ===
1
1
0.9999999999999999
180
3.141592653589793
=== fmod/mod ===
1
1
=== modf ===
12 0.3399999999999999
=== random ===
0.1783953044462927
4
5
老脚本照跑,新脚本按 Lua5 写也舒服。(lua.org)