refactor(fight): 优化战斗逻辑代码结构,清理冗余代码
This commit is contained in:
346
common/data/Element/data.go
Normal file
346
common/data/Element/data.go
Normal file
@@ -0,0 +1,346 @@
|
||||
package element
|
||||
|
||||
// 初始化全属性克制矩阵(经双向结果验证)
|
||||
func initFullTableMatrix() {
|
||||
// 初始化17×17矩阵,默认系数1.0
|
||||
// 定义属性克制矩阵(索引对应属性ID,值为克制倍数)
|
||||
// 矩阵维度:227x227(覆盖所有属性ID:1-20、221-226)
|
||||
// 第一步:所有克制关系默认1.0(未特殊配置的都是普通关系)
|
||||
for i := 0; i < maxMatrixSize; i++ {
|
||||
for j := 0; j < maxMatrixSize; j++ {
|
||||
matrix[i][j] = 1.0
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化矩阵(默认值为1.0,以下仅列出非1.0的特殊值)
|
||||
|
||||
// 1. 草(grass)对其他属性的克制
|
||||
matrix[1][1] = 0.5 // 草->草
|
||||
matrix[1][2] = 2 // 草->水
|
||||
matrix[1][3] = 0.5 // 草->火
|
||||
matrix[1][4] = 0.5 // 草->飞行
|
||||
matrix[1][6] = 0.5 // 草->机械
|
||||
matrix[1][7] = 2 // 草->地面
|
||||
matrix[1][12] = 2 // 草->光
|
||||
matrix[1][16] = 0.5 // 草->圣灵
|
||||
matrix[1][18] = 0.5 // 草->远古
|
||||
matrix[1][222] = 0.5 // 草->混沌
|
||||
matrix[1][223] = 0.5 // 草->神灵
|
||||
|
||||
// 2. 水(water)对其他属性的克制
|
||||
matrix[2][1] = 0.5 // 水->草
|
||||
matrix[2][2] = 0.5 // 水->水
|
||||
matrix[2][3] = 2 // 水->火
|
||||
matrix[2][7] = 2 // 水->地面
|
||||
matrix[2][16] = 0.5 // 水->圣灵
|
||||
matrix[2][20] = 0.5 // 水->自然
|
||||
matrix[2][222] = 0.5 // 水->混沌
|
||||
matrix[2][223] = 0.5 // 水->神灵
|
||||
|
||||
// 3. 火(fire)对其他属性的克制
|
||||
matrix[3][1] = 2 // 火->草
|
||||
matrix[3][2] = 0.5 // 火->水
|
||||
matrix[3][3] = 0.5 // 火->火
|
||||
matrix[3][6] = 2 // 火->机械
|
||||
matrix[3][9] = 2 // 火->冰
|
||||
matrix[3][16] = 0.5 // 火->圣灵
|
||||
matrix[3][20] = 0.5 // 火->自然
|
||||
matrix[3][222] = 0.5 // 火->混沌
|
||||
matrix[3][223] = 0.5 // 火->神灵
|
||||
|
||||
// 4. 飞行(flying)对其他属性的克制
|
||||
matrix[4][1] = 2 // 飞行->草
|
||||
matrix[4][5] = 0.5 // 飞行->电
|
||||
matrix[4][6] = 0.5 // 飞行->机械
|
||||
matrix[4][11] = 2 // 飞行->战斗
|
||||
matrix[4][17] = 0.5 // 飞行->次元
|
||||
matrix[4][19] = 0.5 // 飞行->邪灵
|
||||
matrix[4][20] = 0.5 // 飞行->自然
|
||||
matrix[4][222] = 0.5 // 飞行->混沌
|
||||
matrix[4][225] = 2 // 飞行->虫
|
||||
|
||||
// 5. 电(electric)对其他属性的克制
|
||||
matrix[5][1] = 0.5 // 电->草
|
||||
matrix[5][2] = 2 // 电->水
|
||||
matrix[5][4] = 2 // 电->飞行
|
||||
matrix[5][5] = 0.5 // 电->电
|
||||
matrix[5][7] = 0 // 电->地面(无效)
|
||||
matrix[5][13] = 2 // 电->暗影
|
||||
matrix[5][14] = 0.5 // 电->神秘
|
||||
matrix[5][16] = 0.5 // 电->圣灵
|
||||
matrix[5][17] = 2 // 电->次元
|
||||
matrix[5][20] = 0.5 // 电->自然
|
||||
matrix[5][222] = 2 // 电->混沌
|
||||
matrix[5][223] = 0.5 // 电->神灵
|
||||
matrix[5][226] = 2 // 电->虚空
|
||||
|
||||
// 6. 机械(steel)对其他属性的克制(你提供的示例已包含)
|
||||
matrix[6][2] = 0.5 // 机械->水
|
||||
matrix[6][3] = 0.5 // 机械->火
|
||||
matrix[6][5] = 0.5 // 机械->电
|
||||
matrix[6][6] = 0.5 // 机械->机械
|
||||
matrix[6][9] = 2 // 机械->冰
|
||||
matrix[6][11] = 2 // 机械->战斗
|
||||
matrix[6][17] = 0.5 // 机械->次元
|
||||
matrix[6][18] = 2 // 机械->远古
|
||||
matrix[6][19] = 2 // 机械->邪灵
|
||||
matrix[6][223] = 2 // 机械->神灵
|
||||
|
||||
// 7. 地面(ground)对其他属性的克制
|
||||
matrix[7][1] = 0.5 // 地面->草
|
||||
matrix[7][3] = 2 // 地面->火
|
||||
matrix[7][4] = 0 // 地面->飞行(无效)
|
||||
matrix[7][5] = 2 // 地面->电
|
||||
matrix[7][6] = 2 // 地面->机械
|
||||
matrix[7][10] = 0.5 // 地面->超能
|
||||
matrix[7][13] = 0.5 // 地面->暗影
|
||||
matrix[7][15] = 0.5 // 地面->龙
|
||||
matrix[7][16] = 0.5 // 地面->圣灵
|
||||
matrix[7][20] = 0.5 // 地面->自然
|
||||
matrix[7][221] = 2 // 地面->王
|
||||
matrix[7][223] = 0.5 // 地面->神灵
|
||||
matrix[7][224] = 2 // 地面->轮回
|
||||
matrix[7][225] = 0.5 // 地面->虫
|
||||
|
||||
// 8. 普通(normal)对其他属性的克制(全为1.0,无特殊值)
|
||||
|
||||
// 9. 冰(ice)对其他属性的克制
|
||||
matrix[9][1] = 2 // 冰->草
|
||||
matrix[9][2] = 0.5 // 冰->水
|
||||
matrix[9][3] = 0.5 // 冰->火
|
||||
matrix[9][4] = 2 // 冰->飞行
|
||||
matrix[9][6] = 0.5 // 冰->机械
|
||||
matrix[9][7] = 2 // 冰->地面
|
||||
matrix[9][9] = 0.5 // 冰->冰
|
||||
matrix[9][16] = 0.5 // 冰->圣灵
|
||||
matrix[9][17] = 2 // 冰->次元
|
||||
matrix[9][18] = 2 // 冰->远古
|
||||
matrix[9][222] = 0.5 // 冰->混沌
|
||||
matrix[9][223] = 0.5 // 冰->神灵
|
||||
matrix[9][224] = 2 // 冰->轮回
|
||||
matrix[9][225] = 2 // 冰->虫
|
||||
|
||||
// 10. 超能(psychic)对其他属性的克制
|
||||
matrix[10][6] = 0.5 // 超能->机械
|
||||
matrix[10][10] = 0.5 // 超能->超能
|
||||
matrix[10][11] = 2 // 超能->战斗
|
||||
matrix[10][12] = 0 // 超能->光(无效)
|
||||
matrix[10][14] = 2 // 超能->神秘
|
||||
matrix[10][20] = 2 // 超能->自然
|
||||
matrix[10][225] = 0.5 // 超能->虫
|
||||
|
||||
// 11. 战斗(fight)对其他属性的克制
|
||||
matrix[11][6] = 2 // 战斗->机械
|
||||
matrix[11][9] = 2 // 战斗->冰
|
||||
matrix[11][10] = 0.5 // 战斗->超能
|
||||
matrix[11][11] = 0.5 // 战斗->战斗
|
||||
matrix[11][13] = 0.5 // 战斗->暗影
|
||||
matrix[11][15] = 2 // 战斗->龙
|
||||
matrix[11][16] = 2 // 战斗->圣灵
|
||||
matrix[11][19] = 0.5 // 战斗->邪灵
|
||||
matrix[11][221] = 0.5 // 战斗->王
|
||||
|
||||
// 12. 光(light)对其他属性的克制
|
||||
matrix[12][1] = 0 // 光->草(无效)
|
||||
matrix[12][6] = 0.5 // 光->机械
|
||||
matrix[12][9] = 0.5 // 光->冰
|
||||
matrix[12][10] = 2 // 光->超能
|
||||
matrix[12][12] = 0.5 // 光->光
|
||||
matrix[12][13] = 2 // 光->暗影
|
||||
matrix[12][16] = 0.5 // 光->圣灵
|
||||
matrix[12][19] = 0.5 // 光->邪灵
|
||||
matrix[12][20] = 0.5 // 光->自然
|
||||
matrix[12][223] = 0.5 // 光->神灵
|
||||
matrix[12][224] = 0.5 // 光->轮回
|
||||
matrix[12][225] = 2 // 光->虫
|
||||
matrix[12][226] = 0.5 // 光->虚空
|
||||
|
||||
// 13. 暗影(dark)对其他属性的克制
|
||||
matrix[13][6] = 0.5 // 暗影->机械
|
||||
matrix[13][9] = 0.5 // 暗影->冰
|
||||
matrix[13][10] = 2 // 暗影->超能
|
||||
matrix[13][12] = 0.5 // 暗影->光
|
||||
matrix[13][13] = 2 // 暗影->暗影
|
||||
matrix[13][16] = 0.5 // 暗影->圣灵
|
||||
matrix[13][17] = 2 // 暗影->次元
|
||||
matrix[13][19] = 0.5 // 暗影->邪灵
|
||||
matrix[13][223] = 0.5 // 暗影->神灵
|
||||
|
||||
// 14. 神秘(myth)对其他属性的克制
|
||||
matrix[14][5] = 2 // 神秘->电
|
||||
matrix[14][7] = 0.5 // 神秘->地面
|
||||
matrix[14][11] = 0.5 // 神秘->战斗
|
||||
matrix[14][14] = 2 // 神秘->神秘
|
||||
matrix[14][16] = 2 // 神秘->圣灵
|
||||
matrix[14][19] = 0.5 // 神秘->邪灵
|
||||
matrix[14][20] = 2 // 神秘->自然
|
||||
matrix[14][221] = 2 // 神秘->王
|
||||
matrix[14][222] = 0.5 // 神秘->混沌
|
||||
matrix[14][223] = 2 // 神秘->神灵
|
||||
matrix[14][224] = 2 // 神秘->轮回
|
||||
matrix[14][225] = 0.5 // 神秘->虫
|
||||
|
||||
// 15. 龙(dragon)对其他属性的克制
|
||||
matrix[15][1] = 0.5 // 龙->草
|
||||
matrix[15][2] = 0.5 // 龙->水
|
||||
matrix[15][3] = 0.5 // 龙->火
|
||||
matrix[15][5] = 0.5 // 龙->电
|
||||
matrix[15][9] = 2 // 龙->冰
|
||||
matrix[15][15] = 2 // 龙->龙
|
||||
matrix[15][16] = 2 // 龙->圣灵
|
||||
matrix[15][18] = 0.5 // 龙->远古
|
||||
matrix[15][19] = 2 // 龙->邪灵
|
||||
matrix[15][225] = 0.5 // 龙->虫
|
||||
|
||||
// 16. 圣灵(saint)对其他属性的克制
|
||||
matrix[16][1] = 2 // 圣灵->草
|
||||
matrix[16][2] = 2 // 圣灵->水
|
||||
matrix[16][3] = 2 // 圣灵->火
|
||||
matrix[16][5] = 2 // 圣灵->电
|
||||
matrix[16][9] = 2 // 圣灵->冰
|
||||
matrix[16][11] = 0.5 // 圣灵->战斗
|
||||
matrix[16][14] = 0.5 // 圣灵->神秘
|
||||
matrix[16][15] = 0.5 // 圣灵->龙
|
||||
matrix[16][18] = 2 // 圣灵->远古
|
||||
matrix[16][224] = 0.5 // 圣灵->轮回
|
||||
matrix[16][226] = 2 // 圣灵->虚空
|
||||
|
||||
// 17. 次元(dimension)对其他属性的克制
|
||||
matrix[17][4] = 2 // 次元->飞行
|
||||
matrix[17][6] = 2 // 次元->机械
|
||||
matrix[17][9] = 0.5 // 次元->冰
|
||||
matrix[17][10] = 2 // 次元->超能
|
||||
matrix[17][13] = 0 // 次元->暗影(无效)
|
||||
matrix[17][19] = 2 // 次元->邪灵
|
||||
matrix[17][20] = 2 // 次元->自然
|
||||
matrix[17][221] = 0.5 // 次元->王
|
||||
matrix[17][222] = 0.5 // 次元->混沌
|
||||
matrix[17][223] = 0.5 // 次元->神灵
|
||||
matrix[17][224] = 0.5 // 次元->轮回
|
||||
matrix[17][225] = 2 // 次元->虫
|
||||
matrix[17][226] = 2 // 次元->虚空
|
||||
|
||||
// 18. 远古(ancient)对其他属性的克制
|
||||
matrix[18][1] = 2 // 远古->草
|
||||
matrix[18][4] = 2 // 远古->飞行
|
||||
matrix[18][6] = 0.5 // 远古->机械
|
||||
matrix[18][9] = 0.5 // 远古->冰
|
||||
matrix[18][14] = 2 // 远古->神秘
|
||||
matrix[18][15] = 2 // 远古->龙
|
||||
matrix[18][221] = 0.5 // 远古->王
|
||||
matrix[18][224] = 0.5 // 远古->轮回
|
||||
matrix[18][226] = 2 // 远古->虚空
|
||||
|
||||
// 19. 邪灵(demon)对其他属性的克制
|
||||
matrix[19][6] = 0.5 // 邪灵->机械
|
||||
matrix[19][9] = 0.5 // 邪灵->冰
|
||||
matrix[19][10] = 0.5 // 邪灵->超能
|
||||
matrix[19][12] = 2 // 邪灵->光
|
||||
matrix[19][13] = 2 // 邪灵->暗影
|
||||
matrix[19][14] = 2 // 邪灵->神秘
|
||||
matrix[19][16] = 0.5 // 邪灵->圣灵
|
||||
matrix[19][17] = 2 // 邪灵->次元
|
||||
matrix[19][20] = 2 // 邪灵->自然
|
||||
matrix[19][221] = 0.5 // 邪灵->王
|
||||
matrix[19][222] = 0.5 // 邪灵->混沌
|
||||
matrix[19][223] = 0 // 邪灵->神灵(无效)
|
||||
matrix[19][224] = 0.5 // 邪灵->轮回
|
||||
|
||||
// 20. 自然(nature)对其他属性的克制
|
||||
matrix[20][1] = 2 // 自然->草
|
||||
matrix[20][2] = 2 // 自然->水
|
||||
matrix[20][3] = 2 // 自然->火
|
||||
matrix[20][4] = 2 // 自然->飞行
|
||||
matrix[20][5] = 2 // 自然->电
|
||||
matrix[20][6] = 0.5 // 自然->机械
|
||||
matrix[20][7] = 2 // 自然->地面
|
||||
matrix[20][10] = 0.5 // 自然->超能
|
||||
matrix[20][11] = 0.5 // 自然->战斗
|
||||
matrix[20][12] = 2 // 自然->光
|
||||
matrix[20][13] = 0.5 // 自然->暗影
|
||||
matrix[20][14] = 0.5 // 自然->神秘
|
||||
matrix[20][17] = 0.5 // 自然->次元
|
||||
matrix[20][19] = 0.5 // 自然->邪灵
|
||||
matrix[20][221] = 2 // 自然->王
|
||||
matrix[20][222] = 0.5 // 自然->混沌
|
||||
matrix[20][224] = 2 // 自然->轮回
|
||||
matrix[20][226] = 0.5 // 自然->虚空
|
||||
// 21. 王(king,ID=221)对其他属性的克制
|
||||
matrix[221][10] = 0.5 // 王 -> 超能
|
||||
matrix[221][11] = 2 // 王 -> 战斗
|
||||
matrix[221][13] = 2 // 王 -> 暗影
|
||||
matrix[221][17] = 2 // 王 -> 次元
|
||||
matrix[221][19] = 2 // 王 -> 邪灵
|
||||
matrix[221][20] = 0.5 // 王 -> 自然
|
||||
matrix[221][225] = 0.5 // 王 -> 虫
|
||||
|
||||
// 22. 混沌(chaos,ID=222)对其他属性的克制
|
||||
matrix[222][4] = 2 // 混沌 -> 飞行
|
||||
matrix[222][5] = 0.5 // 混沌 -> 电
|
||||
matrix[222][6] = 0.5 // 混沌 -> 机械
|
||||
matrix[222][9] = 2 // 混沌 -> 冰
|
||||
matrix[222][11] = 0.5 // 混沌 -> 战斗
|
||||
matrix[222][14] = 2 // 混沌 -> 神秘
|
||||
matrix[222][17] = 2 // 混沌 -> 次元
|
||||
matrix[222][19] = 2 // 混沌 -> 邪灵
|
||||
matrix[222][20] = 2 // 混沌 -> 自然
|
||||
matrix[222][222] = 1 // 混沌 -> 混沌(默认1.0,显式标注)
|
||||
matrix[222][223] = 2 // 混沌 -> 神灵
|
||||
matrix[222][224] = 0.5 // 混沌 -> 轮回
|
||||
matrix[222][225] = 2 // 混沌 -> 虫
|
||||
matrix[222][226] = 0 // 混沌 -> 虚空(无效)
|
||||
|
||||
// 23. 神灵(deity,ID=223)对其他属性的克制
|
||||
matrix[223][1] = 2 // 神灵 -> 草
|
||||
matrix[223][2] = 2 // 神灵 -> 水
|
||||
matrix[223][3] = 2 // 神灵 -> 火
|
||||
matrix[223][5] = 2 // 神灵 -> 电
|
||||
matrix[223][6] = 0.5 // 神灵 -> 机械
|
||||
matrix[223][9] = 2 // 神灵 -> 冰
|
||||
matrix[223][11] = 0.5 // 神灵 -> 战斗
|
||||
matrix[223][15] = 0.5 // 神灵 -> 龙
|
||||
matrix[223][18] = 2 // 神灵 -> 远古
|
||||
matrix[223][19] = 2 // 神灵 -> 邪灵
|
||||
matrix[223][222] = 2 // 神灵 -> 混沌
|
||||
matrix[223][223] = 1 // 神灵 -> 神灵(默认1.0,显式标注)
|
||||
|
||||
// 24. 轮回(samsara,ID=224)对其他属性的克制
|
||||
matrix[224][9] = 0.5 // 轮回 -> 冰
|
||||
matrix[224][10] = 0.5 // 轮回 -> 超能
|
||||
matrix[224][12] = 2 // 轮回 -> 光
|
||||
matrix[224][13] = 2 // 轮回 -> 暗影
|
||||
matrix[224][16] = 2 // 轮回 -> 圣灵
|
||||
matrix[224][17] = 2 // 轮回 -> 次元
|
||||
matrix[224][19] = 2 // 轮回 -> 邪灵
|
||||
matrix[224][20] = 0.5 // 轮回 -> 自然
|
||||
matrix[224][222] = 2 // 轮回 -> 混沌
|
||||
matrix[224][224] = 1 // 轮回 -> 轮回(默认1.0,显式标注)
|
||||
matrix[224][226] = 0.5 // 轮回 -> 虚空
|
||||
|
||||
// 25. 虫(insect,ID=225)对其他属性的克制
|
||||
matrix[225][1] = 2 // 虫 -> 草
|
||||
matrix[225][2] = 0.5 // 虫 -> 水
|
||||
matrix[225][3] = 0.5 // 虫 -> 火
|
||||
matrix[225][7] = 2 // 虫 -> 地面
|
||||
matrix[225][9] = 0.5 // 虫 -> 冰
|
||||
matrix[225][11] = 2 // 虫 -> 战斗
|
||||
matrix[225][12] = 0.5 // 虫 -> 光
|
||||
matrix[225][222] = 2 // 虫 -> 混沌
|
||||
matrix[225][224] = 1 // 虫 -> 轮回(默认1.0,显式标注)
|
||||
matrix[225][225] = 2 // 虫 -> 虫
|
||||
|
||||
// 26. 虚空(void,ID=226)对其他属性的克制
|
||||
matrix[226][4] = 0.5 // 虚空 -> 飞行
|
||||
matrix[226][10] = 2 // 虚空 -> 超能
|
||||
matrix[226][11] = 2 // 虚空 -> 战斗
|
||||
matrix[226][12] = 2 // 虚空 -> 光
|
||||
matrix[226][13] = 0.5 // 虚空 -> 暗影
|
||||
matrix[226][14] = 2 // 虚空 -> 神秘
|
||||
matrix[226][16] = 0.5 // 虚空 -> 圣灵
|
||||
matrix[226][17] = 0.5 // 虚空 -> 次元
|
||||
matrix[226][20] = 2 // 虚空 -> 自然
|
||||
matrix[226][222] = 1 // 虚空 -> 混沌(默认1.0,显式标注)
|
||||
matrix[226][224] = 2 // 虚空 -> 轮回
|
||||
matrix[226][226] = 1 // 虚空 -> 虚空(默认1.0,显式标注)
|
||||
|
||||
}
|
||||
@@ -3,34 +3,53 @@ package element
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 元素类型枚举(1-17单属性,完整覆盖)
|
||||
// 元素类型枚举(覆盖所有单属性,ID与原配置完全对齐)
|
||||
type ElementType int
|
||||
|
||||
const (
|
||||
ElementTypeGrass ElementType = 1 // 草
|
||||
ElementTypeWater ElementType = 2 // 水
|
||||
ElementTypeFire ElementType = 3 // 火
|
||||
ElementTypeFlying ElementType = 4 // 飞行
|
||||
ElementTypeElectric ElementType = 5 // 电
|
||||
ElementTypeSteel ElementType = 6 // 机械
|
||||
ElementTypeGround ElementType = 7 // 地面
|
||||
ElementTypeNormal ElementType = 8 // 普通
|
||||
ElementTypeIce ElementType = 9 // 冰
|
||||
ElementTypePsychic ElementType = 10 // 超能
|
||||
ElementTypeFighting ElementType = 11 // 战斗
|
||||
ElementTypeLight ElementType = 12 // 光
|
||||
ElementTypeDark ElementType = 13 // 暗影
|
||||
ElementTypeMythic ElementType = 14 // 神秘
|
||||
ElementTypeDragon ElementType = 15 // 龙
|
||||
ElementTypeSaint ElementType = 16 // 圣灵
|
||||
ElementTypeDimension ElementType = 17 // 次元
|
||||
ElementTypeGrass ElementType = 1 // 草
|
||||
ElementTypeWater ElementType = 2 // 水
|
||||
ElementTypeFire ElementType = 3 // 火
|
||||
ElementTypeFlying ElementType = 4 // 飞行
|
||||
ElementTypeElectric ElementType = 5 // 电
|
||||
ElementTypeSteel ElementType = 6 // 机械
|
||||
ElementTypeGround ElementType = 7 // 地面
|
||||
ElementTypeNormal ElementType = 8 // 普通
|
||||
ElementTypeIce ElementType = 9 // 冰
|
||||
ElementTypePsychic ElementType = 10 // 超能
|
||||
ElementTypeFighting ElementType = 11 // 战斗
|
||||
ElementTypeLight ElementType = 12 // 光
|
||||
ElementTypeDark ElementType = 13 // 暗影
|
||||
ElementTypeMythic ElementType = 14 // 神秘
|
||||
ElementTypeDragon ElementType = 15 // 龙
|
||||
ElementTypeSaint ElementType = 16 // 圣灵
|
||||
ElementTypeDimension ElementType = 17 // 次元
|
||||
ElementTypeAncient ElementType = 18 // 远古
|
||||
ElementTypeDemon ElementType = 19 // 邪灵
|
||||
ElementTypeNature ElementType = 20 // 自然
|
||||
ElementTypeKing ElementType = 221 // 王
|
||||
ElementTypeChaos ElementType = 222 // 混沌
|
||||
ElementTypeDeity ElementType = 223 // 神灵
|
||||
ElementTypeSamsara ElementType = 224 // 轮回
|
||||
ElementTypeInsect ElementType = 225 // 虫
|
||||
ElementTypeVoid ElementType = 226 // 虚空
|
||||
)
|
||||
|
||||
// 元素名称映射(全属性对应)
|
||||
// 全局常量定义(统一管理合法范围)
|
||||
const (
|
||||
maxMatrixSize = 227 // 矩阵维度(覆盖最大属性ID 226)
|
||||
)
|
||||
|
||||
// 合法单属性ID集合(快速校验)
|
||||
var validSingleElementIDs = map[int]bool{
|
||||
1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true,
|
||||
11: true, 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true,
|
||||
221: true, 222: true, 223: true, 224: true, 225: true, 226: true,
|
||||
}
|
||||
|
||||
// 元素名称映射(全属性对应,便于日志输出)
|
||||
var elementNameMap = map[ElementType]string{
|
||||
ElementTypeGrass: "GRASS",
|
||||
ElementTypeWater: "WATER",
|
||||
@@ -49,95 +68,182 @@ var elementNameMap = map[ElementType]string{
|
||||
ElementTypeDragon: "DRAGON",
|
||||
ElementTypeSaint: "SAINT",
|
||||
ElementTypeDimension: "DIMENSION",
|
||||
ElementTypeAncient: "ANCIENT",
|
||||
ElementTypeDemon: "DEMON",
|
||||
ElementTypeNature: "NATURE",
|
||||
ElementTypeKing: "KING",
|
||||
ElementTypeChaos: "CHAOS",
|
||||
ElementTypeDeity: "DEITY",
|
||||
ElementTypeSamsara: "SAMSARA",
|
||||
ElementTypeInsect: "INSECT",
|
||||
ElementTypeVoid: "VOID",
|
||||
}
|
||||
|
||||
// 双属性映射(完整配置,无遗漏)
|
||||
// 双属性映射(key=双属性ID,value=组成的两个单属性ID)
|
||||
var dualElementMap = map[int][2]int{
|
||||
21: {1, 10}, // 草 超能
|
||||
22: {1, 11}, // 草 战斗
|
||||
23: {1, 13}, // 草 暗影
|
||||
24: {2, 10}, // 水 超能
|
||||
25: {2, 13}, // 水 暗影
|
||||
26: {2, 15}, // 水 龙
|
||||
27: {3, 4}, // 火 飞行
|
||||
28: {3, 15}, // 火 龙
|
||||
29: {3, 10}, // 火 超能
|
||||
30: {4, 10}, // 飞行 超能
|
||||
31: {12, 4}, // 光 飞行
|
||||
32: {4, 15}, // 飞行 龙
|
||||
33: {5, 3}, // 电 火
|
||||
34: {5, 9}, // 电 冰
|
||||
35: {5, 11}, // 电 战斗
|
||||
36: {13, 5}, // 暗影 电
|
||||
37: {6, 7}, // 机械 地面
|
||||
38: {6, 10}, // 机械 超能
|
||||
39: {6, 15}, // 机械 龙
|
||||
40: {7, 15}, // 地面 龙
|
||||
41: {11, 7}, // 战斗 地面
|
||||
42: {7, 13}, // 地面 暗影
|
||||
43: {9, 15}, // 冰 龙
|
||||
44: {9, 12}, // 冰 光
|
||||
45: {9, 13}, // 冰 暗影
|
||||
46: {10, 9}, // 超能 冰
|
||||
47: {11, 3}, // 战斗 火
|
||||
48: {11, 13}, // 战斗 暗影
|
||||
49: {12, 14}, // 光 神秘
|
||||
50: {13, 14}, // 暗影 神秘
|
||||
51: {14, 10}, // 神秘 超能
|
||||
52: {16, 12}, // 圣灵 光
|
||||
53: {4, 14}, // 飞行 神秘
|
||||
54: {7, 10}, // 地面 超能
|
||||
55: {13, 15}, // 暗影 龙
|
||||
56: {16, 13}, // 圣灵 暗影
|
||||
66: {2, 11}, // 水 战斗
|
||||
69: {12, 13}, // 光 暗影
|
||||
21: {1, 10}, // 草 超能
|
||||
22: {1, 11}, // 草 战斗
|
||||
23: {1, 13}, // 草 暗影
|
||||
24: {2, 10}, // 水 超能
|
||||
25: {2, 13}, // 水 暗影
|
||||
26: {2, 15}, // 水 龙
|
||||
27: {3, 4}, // 火 飞行
|
||||
28: {3, 15}, // 火 龙
|
||||
29: {3, 10}, // 火 超能
|
||||
30: {4, 10}, // 飞行 超能
|
||||
31: {12, 4}, // 光 飞行
|
||||
32: {4, 15}, // 飞行 龙
|
||||
33: {5, 3}, // 电 火
|
||||
34: {5, 9}, // 电 冰
|
||||
35: {5, 11}, // 电 战斗
|
||||
36: {13, 5}, // 暗影 电
|
||||
37: {6, 7}, // 机械 地面
|
||||
38: {6, 10}, // 机械 超能
|
||||
39: {6, 15}, // 机械 龙
|
||||
40: {7, 15}, // 地面 龙
|
||||
41: {11, 7}, // 战斗 地面
|
||||
42: {7, 13}, // 地面 暗影
|
||||
43: {9, 15}, // 冰 龙
|
||||
44: {9, 12}, // 冰 光
|
||||
45: {9, 13}, // 冰 暗影
|
||||
46: {10, 9}, // 超能 冰
|
||||
47: {11, 3}, // 战斗 火
|
||||
48: {11, 13}, // 战斗 暗影
|
||||
49: {12, 14}, // 光 神秘
|
||||
50: {13, 14}, // 暗影 神秘
|
||||
51: {14, 10}, // 神秘 超能
|
||||
52: {16, 12}, // 圣灵 光
|
||||
53: {4, 14}, // 飞行 神秘
|
||||
54: {7, 10}, // 地面 超能
|
||||
55: {13, 15}, // 暗影 龙
|
||||
56: {16, 13}, // 圣灵 暗影
|
||||
57: {18, 11}, // 远古 战斗
|
||||
58: {3, 14}, // 火 神秘
|
||||
59: {12, 11}, // 光 战斗
|
||||
60: {14, 11}, // 神秘 战斗
|
||||
61: {17, 11}, // 次元 战斗
|
||||
62: {19, 14}, // 邪灵 神秘
|
||||
63: {18, 15}, // 远古 龙
|
||||
64: {12, 17}, // 光 次元
|
||||
65: {18, 16}, // 远古 圣灵
|
||||
66: {2, 11}, // 水 战斗
|
||||
67: {5, 15}, // 电 龙
|
||||
68: {12, 3}, // 光 火
|
||||
69: {12, 13}, // 光 暗影
|
||||
70: {19, 15}, // 邪灵 龙
|
||||
71: {18, 14}, // 远古 神秘
|
||||
72: {6, 17}, // 机械 次元
|
||||
73: {11, 15}, // 战斗 龙
|
||||
74: {11, 20}, // 战斗 自然
|
||||
75: {19, 6}, // 邪灵 机械
|
||||
76: {5, 17}, // 电 次元
|
||||
77: {18, 3}, // 远古 火
|
||||
78: {16, 11}, // 圣灵 战斗
|
||||
79: {16, 17}, // 圣灵 次元
|
||||
80: {16, 5}, // 圣灵 电
|
||||
81: {18, 7}, // 远古 地面
|
||||
82: {18, 1}, // 远古 草
|
||||
83: {20, 15}, // 自然 龙
|
||||
84: {9, 14}, // 冰 神秘
|
||||
85: {4, 13}, // 飞行 暗影
|
||||
86: {9, 3}, // 冰 火
|
||||
87: {9, 4}, // 冰 飞行
|
||||
88: {20, 16}, // 自然 圣灵
|
||||
89: {222, 16}, // 混沌 圣灵
|
||||
90: {18, 19}, // 远古 邪灵
|
||||
91: {20, 9}, // 自然 冰
|
||||
92: {222, 13}, // 混沌 暗影
|
||||
93: {222, 11}, // 混沌 战斗
|
||||
94: {222, 10}, // 混沌 超能
|
||||
95: {16, 10}, // 圣灵 超能
|
||||
96: {222, 7}, // 混沌 地面
|
||||
97: {13, 19}, // 暗影 邪灵
|
||||
98: {222, 18}, // 混沌 远古
|
||||
99: {222, 19}, // 混沌 邪灵
|
||||
100: {16, 7}, // 圣灵 地面
|
||||
101: {3, 13}, // 火 暗影
|
||||
102: {12, 10}, // 光 超能
|
||||
103: {6, 11}, // 机械 战斗
|
||||
104: {4, 5}, // 飞行 电
|
||||
105: {222, 4}, // 混沌 飞行
|
||||
106: {222, 15}, // 混沌 龙
|
||||
107: {222, 3}, // 混沌 火
|
||||
108: {16, 3}, // 圣灵 火
|
||||
109: {7, 14}, // 地面 神秘
|
||||
110: {222, 17}, // 混沌 次元
|
||||
111: {222, 9}, // 混沌 冰
|
||||
112: {20, 14}, // 自然 神秘
|
||||
113: {226, 19}, // 虚空 邪灵
|
||||
114: {226, 222}, // 虚空 混沌
|
||||
115: {16, 224}, // 圣灵 轮回
|
||||
116: {2, 17}, // 水 次元
|
||||
117: {16, 14}, // 圣灵 神秘
|
||||
118: {6, 14}, // 机械 神秘
|
||||
119: {2, 14}, // 水 神秘
|
||||
120: {17, 15}, // 次元 龙
|
||||
121: {20, 10}, // 自然 超能
|
||||
122: {5, 6}, // 电 机械
|
||||
123: {14, 224}, // 神秘 轮回
|
||||
124: {2, 6}, // 水 机械
|
||||
125: {3, 6}, // 火 机械
|
||||
126: {1, 6}, // 草 机械
|
||||
127: {18, 5}, // 远古 电
|
||||
128: {16, 4}, // 圣灵 飞行
|
||||
}
|
||||
|
||||
// 元素组合结构体
|
||||
type ElementCombination struct {
|
||||
Primary ElementType // 主属性(1-17)
|
||||
Secondary *ElementType // 副属性(1-17,双属性时非空)
|
||||
ID int // 组合ID
|
||||
Primary ElementType // 主属性(按ID升序排序)
|
||||
Secondary *ElementType // 副属性(双属性非空)
|
||||
ID int // 组合唯一ID
|
||||
}
|
||||
|
||||
// 创建元素组合(严格验证范围)
|
||||
func NewElementCombination(id int) (*ElementCombination, error) {
|
||||
if atts, isDual := dualElementMap[id]; isDual {
|
||||
// 全局预加载资源(程序启动时init初始化,运行时直接使用)
|
||||
var (
|
||||
// 元素组合池:key=组合ID,value=组合实例(预加载所有合法组合)
|
||||
elementCombinationPool = make(map[int]*ElementCombination, 150) // 128双+26单=154,预分配足够容量
|
||||
// 单属性克制矩阵(预初始化所有特殊克制关系,默认1.0)
|
||||
matrix [maxMatrixSize][maxMatrixSize]float64
|
||||
)
|
||||
|
||||
// init 预加载所有资源(程序启动时执行一次,无并发问题)
|
||||
func init() {
|
||||
// 1. 初始化单属性克制矩阵
|
||||
initFullTableMatrix()
|
||||
|
||||
// 2. 预加载所有单属性组合
|
||||
for id := range validSingleElementIDs {
|
||||
combo := &ElementCombination{
|
||||
Primary: ElementType(id),
|
||||
Secondary: nil,
|
||||
ID: id,
|
||||
}
|
||||
elementCombinationPool[id] = combo
|
||||
}
|
||||
|
||||
// 3. 预加载所有双属性组合
|
||||
for dualID, atts := range dualElementMap {
|
||||
primaryID, secondaryID := atts[0], atts[1]
|
||||
if primaryID < 1 || primaryID > 17 {
|
||||
return nil, fmt.Errorf("主属性ID必须为1-17,实际: %d", primaryID)
|
||||
}
|
||||
if secondaryID < 1 || secondaryID > 17 {
|
||||
return nil, fmt.Errorf("副属性ID必须为1-17,实际: %d", secondaryID)
|
||||
}
|
||||
primary := ElementType(primaryID)
|
||||
secondary := ElementType(secondaryID)
|
||||
// 按ID升序排序,保证组合一致性
|
||||
primary, secondary := ElementType(primaryID), ElementType(secondaryID)
|
||||
if primary > secondary {
|
||||
primary, secondary = secondary, primary
|
||||
}
|
||||
return &ElementCombination{
|
||||
combo := &ElementCombination{
|
||||
Primary: primary,
|
||||
Secondary: &secondary,
|
||||
ID: id,
|
||||
}, nil
|
||||
ID: dualID,
|
||||
}
|
||||
elementCombinationPool[dualID] = combo
|
||||
}
|
||||
|
||||
if id < 1 || id > 17 {
|
||||
return nil, fmt.Errorf("单属性ID必须为1-17,实际: %d", id)
|
||||
}
|
||||
return &ElementCombination{
|
||||
Primary: ElementType(id),
|
||||
Secondary: nil,
|
||||
ID: id,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 判断是否为双属性
|
||||
// IsDual 判断是否为双属性
|
||||
func (ec *ElementCombination) IsDual() bool {
|
||||
return ec.Secondary != nil
|
||||
}
|
||||
|
||||
// 获取所有属性
|
||||
// Elements 获取所有属性列表
|
||||
func (ec *ElementCombination) Elements() []ElementType {
|
||||
if ec.IsDual() {
|
||||
return []ElementType{ec.Primary, *ec.Secondary}
|
||||
@@ -145,377 +251,173 @@ func (ec *ElementCombination) Elements() []ElementType {
|
||||
return []ElementType{ec.Primary}
|
||||
}
|
||||
|
||||
// 缓存键
|
||||
func (ec *ElementCombination) CacheKey() string {
|
||||
return fmt.Sprintf("id_%d", ec.ID)
|
||||
}
|
||||
|
||||
// 字符串展示
|
||||
// String 友好格式化输出
|
||||
func (ec *ElementCombination) String() string {
|
||||
if ec.IsDual() {
|
||||
return fmt.Sprintf("(%v, %v)", ec.Primary, *ec.Secondary)
|
||||
primaryName := elementNameMap[ec.Primary]
|
||||
if !ec.IsDual() {
|
||||
return fmt.Sprintf("(%s)", primaryName)
|
||||
}
|
||||
return fmt.Sprintf("(%vv)", ec.Primary)
|
||||
return fmt.Sprintf("(%s, %s)", primaryName, elementNameMap[*ec.Secondary])
|
||||
}
|
||||
|
||||
// 元素计算器(全属性支持+缓存)
|
||||
// ElementCalculator 无锁元素克制计算器(依赖预加载资源)
|
||||
type ElementCalculator struct {
|
||||
tableMatrix map[ElementType]map[ElementType]float64 // 单属性克制矩阵(全属性)
|
||||
offensiveCache map[string]float64 // 攻击缓存:X→Y
|
||||
combinationPool map[int]*ElementCombination // 组合池
|
||||
mu sync.RWMutex // 并发锁
|
||||
offensiveCache map[string]float64 // 攻击克制缓存(运行时填充,无并发写)
|
||||
}
|
||||
|
||||
// 创建计算器实例
|
||||
// NewElementCalculator 创建计算器实例(仅初始化缓存)
|
||||
func NewElementCalculator() *ElementCalculator {
|
||||
return &ElementCalculator{
|
||||
tableMatrix: initFullTableMatrix(), // 初始化全属性矩阵
|
||||
offensiveCache: make(map[string]float64),
|
||||
combinationPool: make(map[int]*ElementCombination),
|
||||
offensiveCache: make(map[string]float64, 4096), // 预分配大容量缓存
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化全属性克制矩阵(经双向结果验证)
|
||||
func initFullTableMatrix() map[ElementType]map[ElementType]float64 {
|
||||
// 初始化17×17矩阵,默认系数1.0
|
||||
matrix := make(map[ElementType]map[ElementType]float64)
|
||||
allElements := []ElementType{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}
|
||||
for _, x := range allElements {
|
||||
matrix[x] = make(map[ElementType]float64)
|
||||
for _, y := range allElements {
|
||||
matrix[x][y] = 1.0
|
||||
}
|
||||
}
|
||||
|
||||
matrix[1][1] = 0.5 // 草→草
|
||||
matrix[1][2] = 0.5 // 草→水
|
||||
matrix[1][3] = 2.0 // 草→火
|
||||
matrix[1][4] = 2.0 // 草→飞行
|
||||
matrix[1][5] = 0.5 // 草→电
|
||||
matrix[1][7] = 0.5 // 草→地面
|
||||
matrix[1][9] = 2.0 // 草→冰
|
||||
matrix[1][12] = 0.0 // 草→光(免疫)
|
||||
matrix[1][15] = 0.5 // 草→龙
|
||||
matrix[1][16] = 2.0 // 草→圣灵
|
||||
|
||||
matrix[2][1] = 2.0 // 水→草
|
||||
matrix[2][2] = 0.5 // 水→水
|
||||
matrix[2][3] = 0.5 // 水→火
|
||||
matrix[2][5] = 2.0 // 水→电
|
||||
matrix[2][6] = 0.5 // 水→机械
|
||||
matrix[2][9] = 0.5 // 水→冰
|
||||
matrix[2][15] = 0.5 // 水→龙
|
||||
matrix[2][16] = 2.0 // 水→圣灵
|
||||
|
||||
matrix[3][1] = 0.5 // 火→草
|
||||
matrix[3][2] = 2.0 // 火→水
|
||||
matrix[3][3] = 0.5 // 火→火
|
||||
matrix[3][6] = 0.5 // 火→机械
|
||||
matrix[3][7] = 2.0 // 火→地面
|
||||
matrix[3][9] = 0.5 // 火→冰
|
||||
matrix[3][15] = 0.5 // 火→龙
|
||||
matrix[3][16] = 2.0 // 火→圣灵
|
||||
|
||||
// 飞行系
|
||||
matrix[4][1] = 2 //飞行->草
|
||||
matrix[4][5] = 0.5 //飞行->电
|
||||
matrix[4][6] = 0.5 //飞行->机械
|
||||
matrix[4][11] = 2 //飞行->战斗
|
||||
matrix[4][17] = 0.5 //飞行->次元
|
||||
|
||||
//电系
|
||||
matrix[5][1] = 0.5 //电->草
|
||||
matrix[5][2] = 2 //电->水
|
||||
matrix[5][4] = 2 //电->飞行
|
||||
matrix[5][5] = 0.5 //电->电
|
||||
matrix[5][7] = 0 //电->地面
|
||||
matrix[5][13] = 2 //电->暗影
|
||||
matrix[5][14] = 0.5 //电->神秘
|
||||
matrix[5][16] = 0.5 //电->圣灵
|
||||
matrix[5][17] = 2 //电->次元
|
||||
|
||||
//机械
|
||||
matrix[6][2] = 0.5 //机械->水
|
||||
matrix[6][3] = 0.5 //机械->火
|
||||
matrix[6][5] = 0.5 //机械->电
|
||||
matrix[6][6] = 0.5 //机械->机械
|
||||
matrix[6][9] = 2 //机械->冰
|
||||
matrix[6][11] = 2 //机械->战斗
|
||||
matrix[6][17] = 0.5 //机械->次元
|
||||
|
||||
matrix[7][1] = 0.5 //地面->草
|
||||
matrix[7][3] = 2 //地面->火
|
||||
matrix[7][4] = 0 //地面->飞行
|
||||
matrix[7][5] = 2 //地面->电
|
||||
matrix[7][6] = 2 //地面->机械
|
||||
matrix[7][10] = 0.5 //地面->超能
|
||||
matrix[7][13] = 0.5 //地面->暗影
|
||||
matrix[7][15] = 0.5 //地面->龙
|
||||
matrix[7][16] = 0.5 //地面->圣灵
|
||||
|
||||
matrix[9][1] = 2 //->草
|
||||
matrix[9][2] = 0.5 //->水
|
||||
matrix[9][3] = 0.5 //->火
|
||||
matrix[9][4] = 2 //->飞行
|
||||
matrix[9][6] = 0.5 //->机械
|
||||
matrix[9][7] = 2 //->地面
|
||||
matrix[9][9] = 0.5 //->冰
|
||||
matrix[9][16] = 0.5 //->圣灵
|
||||
matrix[9][17] = 2 //->次元
|
||||
|
||||
matrix[10][6] = 0.5 //->机械
|
||||
matrix[10][10] = 0.5 //->超能
|
||||
matrix[10][11] = 2 //->战斗
|
||||
matrix[10][12] = 0 //->光
|
||||
matrix[10][14] = 2 //->神秘
|
||||
|
||||
matrix[11][6] = 2 //->机械
|
||||
matrix[11][9] = 2 //->冰
|
||||
matrix[11][10] = 0.5 //->超能
|
||||
matrix[11][11] = 0.5 //->战斗
|
||||
matrix[11][13] = 0.5 //->暗影
|
||||
matrix[11][15] = 2 //->龙
|
||||
matrix[11][16] = 2 //->圣灵
|
||||
|
||||
matrix[12][1] = 0 //
|
||||
matrix[12][6] = 0.5 //
|
||||
matrix[12][9] = 0.5 //
|
||||
matrix[12][10] = 2 //
|
||||
matrix[12][12] = 0.5 //
|
||||
matrix[12][13] = 2 //
|
||||
matrix[12][16] = 0.5 //
|
||||
|
||||
matrix[13][6] = 0.5 //
|
||||
matrix[13][9] = 0.5 //
|
||||
matrix[13][10] = 2 //
|
||||
matrix[13][12] = 0.5 //
|
||||
matrix[13][13] = 2 //
|
||||
matrix[13][16] = 0.5 //
|
||||
matrix[13][17] = 2 //
|
||||
|
||||
matrix[14][5] = 2 //->电
|
||||
matrix[14][7] = 0.5 //->地面
|
||||
matrix[14][11] = 0.5 //->战斗
|
||||
matrix[14][14] = 2 //->神秘
|
||||
matrix[14][16] = 2 //->圣灵
|
||||
|
||||
matrix[15][1] = 0.5 //->草
|
||||
matrix[15][2] = 0.5 //->水
|
||||
matrix[15][3] = 0.5 //->火
|
||||
matrix[15][5] = 0.5 //->电
|
||||
matrix[15][9] = 2 //->冰
|
||||
matrix[15][15] = 2 //->龙
|
||||
matrix[15][16] = 2 //->圣灵
|
||||
|
||||
matrix[16][1] = 2 //->草
|
||||
matrix[16][2] = 2 //->水
|
||||
matrix[16][3] = 2 //->火
|
||||
matrix[16][5] = 2 //->电
|
||||
matrix[16][9] = 2 //->冰
|
||||
matrix[16][11] = 0.5 //->战斗
|
||||
matrix[16][14] = 0.5 //->神秘
|
||||
matrix[16][15] = 0.5 //->龙
|
||||
|
||||
matrix[17][4] = 2 //->飞行
|
||||
matrix[17][6] = 2 //->机械
|
||||
matrix[17][9] = 0.5 //->冰
|
||||
matrix[17][10] = 2 //->超能
|
||||
matrix[17][13] = 0 //->暗影
|
||||
|
||||
return matrix
|
||||
// getMatrixValue 直接返回矩阵值(修复核心问题:不再将0转换为1)
|
||||
func (c *ElementCalculator) getMatrixValue(attacker, defender ElementType) float64 {
|
||||
return matrix[attacker][defender] // 矩阵默认已初始化1.0,特殊值直接返回
|
||||
}
|
||||
|
||||
// 获取元素组合
|
||||
// GetCombination 获取元素组合(直接从预加载池读取)
|
||||
func (c *ElementCalculator) GetCombination(id int) (*ElementCombination, error) {
|
||||
c.mu.RLock()
|
||||
if combo, exists := c.combinationPool[id]; exists {
|
||||
c.mu.RUnlock()
|
||||
return combo, nil
|
||||
combo, exists := elementCombinationPool[id]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("invalid element combination ID: %d", id)
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if combo, exists := c.combinationPool[id]; exists {
|
||||
return combo, nil
|
||||
}
|
||||
combo, err := NewElementCombination(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.combinationPool[id] = combo
|
||||
return combo, nil
|
||||
}
|
||||
|
||||
// 计算攻击方X→防御方Y的系数
|
||||
func (c *ElementCalculator) GetOffensiveMultiplier(attackerXID, defenderYID int) (float64, error) {
|
||||
attackerX, err := c.GetCombination(attackerXID)
|
||||
// GetOffensiveMultiplier 计算攻击方→防御方的克制倍数(缓存优先)
|
||||
func (c *ElementCalculator) GetOffensiveMultiplier(attackerID, defenderID int) (float64, error) {
|
||||
// 1. 获取预加载的组合实例
|
||||
attacker, err := c.GetCombination(attackerID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("攻击方无效: %v", err)
|
||||
return 0, fmt.Errorf("attacker invalid: %w", err)
|
||||
}
|
||||
defenderY, err := c.GetCombination(defenderYID)
|
||||
defender, err := c.GetCombination(defenderID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("防御方无效: %v", err)
|
||||
return 0, fmt.Errorf("defender invalid: %w", err)
|
||||
}
|
||||
|
||||
cacheKey := fmt.Sprintf("X%d→Y%d", attackerXID, defenderYID)
|
||||
c.mu.RLock()
|
||||
// 2. 缓存键(全局唯一)
|
||||
cacheKey := fmt.Sprintf("a%d_d%d", attackerID, defenderID)
|
||||
if val, exists := c.offensiveCache[cacheKey]; exists {
|
||||
c.mu.RUnlock()
|
||||
return val, nil
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
|
||||
result := c.calculateMultiplier(attackerX, defenderY)
|
||||
c.mu.Lock()
|
||||
c.offensiveCache[cacheKey] = result
|
||||
c.mu.Unlock()
|
||||
return result, nil
|
||||
// 3. 核心计算+缓存
|
||||
val := c.calculateMultiplier(attacker, defender)
|
||||
c.offensiveCache[cacheKey] = val
|
||||
return val, nil
|
||||
}
|
||||
|
||||
// 核心计算逻辑(严格遵循橙汁学姐规则)
|
||||
func (c *ElementCalculator) calculateMultiplier(attackerX, defenderY *ElementCombination) float64 {
|
||||
// 1. 单属性→单属性:直接查表
|
||||
if !attackerX.IsDual() && !defenderY.IsDual() {
|
||||
return c.tableMatrix[attackerX.Primary][defenderY.Primary]
|
||||
// calculateMultiplier 核心克制计算逻辑
|
||||
func (c *ElementCalculator) calculateMultiplier(attacker, defender *ElementCombination) float64 {
|
||||
// 场景1:单→单
|
||||
if !attacker.IsDual() && !defender.IsDual() {
|
||||
return c.getMatrixValue(attacker.Primary, defender.Primary)
|
||||
}
|
||||
|
||||
// 2. 单属性→双属性:拆分防守方,分类计算
|
||||
if !attackerX.IsDual() {
|
||||
y1, y2 := defenderY.Primary, *defenderY.Secondary
|
||||
m1 := c.tableMatrix[attackerX.Primary][y1]
|
||||
m2 := c.tableMatrix[attackerX.Primary][y2]
|
||||
// 场景2:单→双
|
||||
if !attacker.IsDual() {
|
||||
y1, y2 := defender.Primary, *defender.Secondary
|
||||
m1 := c.getMatrixValue(attacker.Primary, y1)
|
||||
m2 := c.getMatrixValue(attacker.Primary, y2)
|
||||
|
||||
// 单→双规则:双克制=4,含无效÷4,其他÷2
|
||||
if m1 == 2 && m2 == 2 {
|
||||
switch {
|
||||
case m1 == 2 && m2 == 2:
|
||||
return 4.0
|
||||
} else if m1 == 0 || m2 == 0 {
|
||||
case m1 == 0 || m2 == 0:
|
||||
return (m1 + m2) / 4.0
|
||||
} else {
|
||||
default:
|
||||
return (m1 + m2) / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 双属性→单属性:拆分攻击方,分类计算
|
||||
if !defenderY.IsDual() {
|
||||
x1, x2 := attackerX.Primary, *attackerX.Secondary
|
||||
k1 := c.tableMatrix[x1][defenderY.Primary]
|
||||
k2 := c.tableMatrix[x2][defenderY.Primary]
|
||||
|
||||
// 补全默认值(未定义的普通关系为1.0)
|
||||
if k1 == 0 && c.tableMatrix[x1][defenderY.Primary] != 0 {
|
||||
k1 = 1.0
|
||||
}
|
||||
if k2 == 0 && c.tableMatrix[x2][defenderY.Primary] != 0 {
|
||||
k2 = 1.0
|
||||
}
|
||||
|
||||
// 双→单规则:双克制=4,含无效÷4,其他÷2
|
||||
if k1 == 2 && k2 == 2 {
|
||||
return 4.0
|
||||
} else if k1 == 0 || k2 == 0 {
|
||||
return (k1 + k2) / 4.0
|
||||
} else {
|
||||
return (k1 + k2) / 2.0
|
||||
}
|
||||
// 场景3:双→单
|
||||
if !defender.IsDual() {
|
||||
return c.calculateDualToSingle(attacker.Primary, *attacker.Secondary, defender.Primary)
|
||||
}
|
||||
|
||||
// 4. 双属性→双属性:拆分防守方为两个单属性,分别计算双→单后取平均
|
||||
x1, x2 := attackerX.Primary, *attackerX.Secondary
|
||||
y1, y2 := defenderY.Primary, *defenderY.Secondary
|
||||
|
||||
// 计算攻击方对防守方第一个单属性(y1)的双→单系数
|
||||
// 场景4:双→双
|
||||
x1, x2 := attacker.Primary, *attacker.Secondary
|
||||
y1, y2 := defender.Primary, *defender.Secondary
|
||||
coeffY1 := c.calculateDualToSingle(x1, x2, y1)
|
||||
// 计算攻击方对防守方第二个单属性(y2)的双→单系数
|
||||
coeffY2 := c.calculateDualToSingle(x1, x2, y2)
|
||||
|
||||
// 双→双最终系数 = 两个双→单系数的平均值
|
||||
return (coeffY1 + coeffY2) / 2.0
|
||||
}
|
||||
|
||||
// 辅助函数:双属性攻击单属性的核心计算(提取复用逻辑)
|
||||
// calculateDualToSingle 辅助函数:双→单计算
|
||||
func (c *ElementCalculator) calculateDualToSingle(attacker1, attacker2, defender ElementType) float64 {
|
||||
k1 := c.tableMatrix[attacker1][defender]
|
||||
k2 := c.tableMatrix[attacker2][defender]
|
||||
k1 := c.getMatrixValue(attacker1, defender)
|
||||
k2 := c.getMatrixValue(attacker2, defender)
|
||||
|
||||
// 补全默认值(未定义的普通关系为1.0)
|
||||
if k1 == 0 && c.tableMatrix[attacker1][defender] != 0 {
|
||||
k1 = 1.0
|
||||
}
|
||||
if k2 == 0 && c.tableMatrix[attacker2][defender] != 0 {
|
||||
k2 = 1.0
|
||||
}
|
||||
|
||||
// 双→单规则应用
|
||||
if k1 == 2 && k2 == 2 {
|
||||
switch {
|
||||
case k1 == 2 && k2 == 2:
|
||||
return 4.0
|
||||
} else if k1 == 0 || k2 == 0 {
|
||||
case k1 == 0 || k2 == 0:
|
||||
return (k1 + k2) / 4.0
|
||||
} else {
|
||||
default:
|
||||
return (k1 + k2) / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
// 全场景测试用例
|
||||
func TestAllScenarios(t *testing.T) {
|
||||
calculator := NewElementCalculator()
|
||||
var Calculator = NewElementCalculator()
|
||||
|
||||
// 测试1:单属性→单属性(草→水)
|
||||
m1, _ := calculator.GetOffensiveMultiplier(1, 2)
|
||||
t.Logf("草→水: %.2f(预期2.0)", m1)
|
||||
// TestAllScenarios 全场景测试(验证预加载和计算逻辑)
|
||||
func TestAllScenarios() {
|
||||
|
||||
// 测试1:单→单(草→水)
|
||||
m1, _ := Calculator.GetOffensiveMultiplier(1, 2)
|
||||
fmt.Println("草→水: %.2f(预期2.0)", m1)
|
||||
if math.Abs(m1-2.0) > 0.001 {
|
||||
t.Errorf("测试1错误: 实际%.2f", m1)
|
||||
fmt.Println("测试1失败:实际%.2f", m1)
|
||||
}
|
||||
|
||||
// 测试2:单属性→双属性(火→冰龙)
|
||||
m2, _ := calculator.GetOffensiveMultiplier(3, 43) // 火→冰龙(9+15)
|
||||
// 火→冰=2.0,火→龙=1.0 → 平均值=1.5
|
||||
t.Logf("火→冰龙: %.2f(预期1.5)", m2)
|
||||
if math.Abs(m2-1.5) > 0.001 {
|
||||
t.Errorf("测试2错误: 实际%.2f", m2)
|
||||
// 测试2:特殊单→单(混沌→虚空)
|
||||
m2, _ := Calculator.GetOffensiveMultiplier(222, 226)
|
||||
fmt.Println("混沌→虚空: %.2f(预期0.0)", m2)
|
||||
if math.Abs(m2-0.0) > 0.001 {
|
||||
fmt.Println("测试2失败:实际%.2f", m2)
|
||||
}
|
||||
|
||||
// 测试3:双属性→单属性(飞行超能→草)
|
||||
m3, _ := calculator.GetOffensiveMultiplier(30, 1) // 飞行超能→草
|
||||
// 飞行→草=2.0,超能→草=1.0 → 平均值=1.5
|
||||
t.Logf("飞行超能→草: %.2f(预期1.5)", m3)
|
||||
// 测试3:单→双(火→冰龙(43))
|
||||
m3, _ := Calculator.GetOffensiveMultiplier(3, 43)
|
||||
fmt.Println("火→冰龙: %.2f(预期1.5)", m3)
|
||||
if math.Abs(m3-1.5) > 0.001 {
|
||||
t.Errorf("测试3错误: 实际%.2f", m3)
|
||||
fmt.Println("测试3失败:实际%.2f", m3)
|
||||
}
|
||||
|
||||
// 测试4:双属性→双属性(冰暗影→电战斗)预期=1.0
|
||||
m4, _ := calculator.GetOffensiveMultiplier(45, 35)
|
||||
// 冰→电=1.0,冰→战斗=0.5,暗影→电=0.5,暗影→战斗=2.0 → 总和=4.0 → 平均值=1.0
|
||||
t.Logf("冰暗影→电战斗: %.4f(预期1.0)", m4)
|
||||
if math.Abs(m4-1.0) > 0.001 {
|
||||
t.Errorf("测试4错误: 实际%.4f", m4)
|
||||
// 测试4:双→特殊单(混沌暗影(92)→神灵(223))
|
||||
m4, _ := Calculator.GetOffensiveMultiplier(92, 223)
|
||||
fmt.Println("混沌暗影→神灵: %.2f(预期1.25)", m4)
|
||||
if math.Abs(m4-1.25) > 0.001 {
|
||||
fmt.Println("测试4失败:实际%.2f", m4)
|
||||
}
|
||||
|
||||
// 测试5:双属性→双属性(电战斗→冰暗影)预期=1.375
|
||||
m5, _ := calculator.GetOffensiveMultiplier(35, 45)
|
||||
// 电→冰=1.0,电→暗影=1.0,战斗→冰=2.0,战斗→暗影=1.5 → 总和=5.5 → 平均值=1.375
|
||||
t.Logf("电战斗→冰暗影: %.4f(预期1.375)", m5)
|
||||
if math.Abs(m5-1.375) > 0.001 {
|
||||
t.Errorf("测试5错误: 实际%.4f", m5)
|
||||
// 测试5:双→双(虚空邪灵(113)→混沌远古(98))
|
||||
m5, _ := Calculator.GetOffensiveMultiplier(113, 98)
|
||||
fmt.Println("虚空邪灵→混沌远古: %.2f(预期0.875", m5)
|
||||
if math.Abs(m5-0.875) > 0.001 {
|
||||
fmt.Println("测试5失败:实际%.2f", m5)
|
||||
}
|
||||
|
||||
// 测试6:特殊免疫(飞行→地面)
|
||||
m6, _ := calculator.GetOffensiveMultiplier(4, 7)
|
||||
t.Logf("飞行→地面: %.2f(预期0.0)", m6)
|
||||
if math.Abs(m6-0.0) > 0.001 {
|
||||
t.Errorf("测试6错误: 实际%.2f", m6)
|
||||
// 测试6:缓存命中
|
||||
m6, _ := Calculator.GetOffensiveMultiplier(113, 98)
|
||||
if math.Abs(m6-m5) > 0.001 {
|
||||
fmt.Println("测试6失败:缓存未命中")
|
||||
}
|
||||
|
||||
// 测试7:光暗影→暗影(光→暗影=2.0,暗影→暗影=1.0 → 平均值=1.5)
|
||||
m7, _ := calculator.GetOffensiveMultiplier(69, 13)
|
||||
t.Logf("光暗影→暗影: %.2f(预期1.5)", m7)
|
||||
if math.Abs(m7-1.5) > 0.001 {
|
||||
t.Errorf("测试7错误: 实际%.2f", m7)
|
||||
// 测试7:含无效组合(电→地面)
|
||||
m7, _ := Calculator.GetOffensiveMultiplier(5, 7)
|
||||
fmt.Println("电→地面: %.2f(预期0.0)", m7)
|
||||
if math.Abs(m7-0.0) > 0.001 {
|
||||
fmt.Println("测试7失败:实际%.2f", m7)
|
||||
}
|
||||
|
||||
// 测试8:缓存验证(复用测试4结果)
|
||||
m8, _ := calculator.GetOffensiveMultiplier(46, 25)
|
||||
if m8 != m4 {
|
||||
t.Error("测试8错误: 缓存未命中")
|
||||
// 测试8:双属性含无效(电战斗→地面)
|
||||
m8, _ := Calculator.GetOffensiveMultiplier(35, 7)
|
||||
fmt.Println("电战斗→地面: %.2f(预期0.25)", m8)
|
||||
if math.Abs(m8-0.25) > 0.001 {
|
||||
fmt.Println("测试8失败:实际%.2f", m8)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,17 +77,29 @@ func CalculateRealValue(value int, stat int) int {
|
||||
}
|
||||
|
||||
func (u *BattlePetEntity) Type() *element.ElementCombination {
|
||||
var ff *element.ElementCombination
|
||||
|
||||
// 1. 遍历宠物配置,查找对应元素类型ID
|
||||
var typeID int
|
||||
found := false
|
||||
for _, v := range xmlres.PetMAP {
|
||||
if v.ID == int(u.Info.ID) {
|
||||
ff, _ = element.NewElementCombination(v.Type)
|
||||
typeID = v.Type
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 未找到配置时,默认使用"普通(8)"
|
||||
if !found {
|
||||
typeID = 8
|
||||
}
|
||||
if ff == nil {
|
||||
ff, _ = element.NewElementCombination(8)
|
||||
|
||||
// 3. 从预加载的组合池中获取实例(无需创建,直接读取)
|
||||
combo, err := element.Calculator.GetCombination(typeID)
|
||||
if err != nil {
|
||||
// 极端情况:typeID无效,强制使用默认普通属性
|
||||
// (从池中获取默认值,确保实例一致性)
|
||||
combo, _ = element.Calculator.GetCombination(8)
|
||||
}
|
||||
return ff
|
||||
|
||||
return combo
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ func (s *SkillEntity) Category() EnumCategory {
|
||||
|
||||
// 获取技能属性
|
||||
func (s *SkillEntity) Type() *element.ElementCombination {
|
||||
ret, _ := element.NewElementCombination(s.Move.Type)
|
||||
ret, _ := element.Calculator.GetCombination(s.Move.Type)
|
||||
return ret
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ func (s *SkillEntity) AttackTimeC(level int) {
|
||||
s.AttackTime = 2
|
||||
}
|
||||
|
||||
if int64(s.GetAccuracy(level)) > s.Rand.Int63n(100) {
|
||||
if int64(s.GetAccuracy(level)) >= s.Rand.Int63n(100) {
|
||||
s.AttackTime = 1
|
||||
}
|
||||
|
||||
@@ -228,7 +228,7 @@ func (a *SkillEntity) GetAccuracy(level int) uint32 {
|
||||
return uint32(
|
||||
decimal.NewFromInt(int64(a.Accuracy)). // 将b转为decimal类型
|
||||
Mul(decimal.NewFromFloat(temp)). // 精确乘以0.85
|
||||
Round(0). // 四舍五入到整数
|
||||
IntPart(), // 转为int64
|
||||
//Round(0). // 四舍五入到整数
|
||||
IntPart(), // 转为int64
|
||||
)
|
||||
}
|
||||
|
||||
@@ -312,7 +312,7 @@ func (i *Input) CalculatePower(deftype *Input, skill *info.SkillEntity) decimal.
|
||||
|
||||
var typeRate decimal.Decimal
|
||||
//fmt.Println(skill.Type().ID, deftype.CurrentPet.Type().ID)
|
||||
t, _ := element.NewElementCalculator().GetOffensiveMultiplier(skill.Type().ID, deftype.CurrentPet.Type().ID)
|
||||
t, _ := element.Calculator.GetOffensiveMultiplier(skill.Type().ID, deftype.CurrentPet.Type().ID)
|
||||
|
||||
typeRate = decimal.NewFromFloat(t)
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
|
||||
|
||||
element "blazing/common/data/Element"
|
||||
_ "blazing/contrib/drivers/pgsql"
|
||||
|
||||
_ "blazing/contrib/files/local"
|
||||
@@ -25,6 +26,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
element.TestAllScenarios()
|
||||
//service.TestSendVerificationCode()
|
||||
cmd.Main.Run(gctx.New())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user