diff --git a/zigs/src/main.zig b/zigs/src/main.zig index 989c56d..962a110 100644 --- a/zigs/src/main.zig +++ b/zigs/src/main.zig @@ -111,29 +111,25 @@ const Namer = struct { } var s: u32 = 0; - var q_len: i32 = -1; inline for (0..96) |i| { const m = ((val[i] *% 181) +% 160); if (m >= 89 and m < 217) { name_base[s] = m & 63; s += 1; - if (q_len == 30) { - break; - } else { - q_len += 1; - } + } + if (s > 30) { + break; } } - if (q_len < 31) { + if (s < 31) { inline for (96..256) |i| { const m = ((val[i] *% 181) +% 160); if (m >= 89 and m < 217) { name_base[s] = m & 63; - q_len += 1; s += 1; - if (q_len > 30) { - break; - } + } + if (s > 30) { + break; } } } @@ -162,9 +158,82 @@ const Namer = struct { .skl_freq = skl_freq, }; } + + pub fn update_skill(self: *Namer) void { + self.skl_id = [40]u8{ + 0, 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, + }; + // 全量更新 name_base + var s: u8 = 0; + for (0..256) |i| { + const m = ((self.val[i] *% 181) +% 160); + if (m >= 89 and m < 217) { + self.name_base[s] = m & 63; + s += 1; + } + } + var a: u8 = 0; + var b: u8 = 0; + s = 0; + for (0..2) |_| { + for (0..40) |i| { + a += 1; + b +%= self.val[a]; + // swap a, b + const tmp = self.val[a]; + self.val[a] = self.val[b]; + self.val[b] = tmp; + const u = self.val[self.val[a] +% self.val[b]]; + a += 1; + b +%= self.val[a]; + // swap a, b + const tmp2 = self.val[a]; + self.val[a] = self.val[b]; + self.val[b] = tmp2; + const t = self.val[self.val[a] +% self.val[b]]; + const rnd: u8 = @truncate((@as(u32, u) << 8 | @as(u32, t)) % 40); + s = (s + rnd + self.skl_id[i]) % 40; + // swap i, s + const tmp3 = self.skl_id[i]; + self.skl_id[i] = self.skl_id[s]; + self.skl_id[s] = tmp3; + } + } + var last: i32 = -1; + var j: u8 = 0; + var i: u8 = 0; + while (i < 128) { + const p = @min( + @min(self.name_base[i], self.name_base[i + 1]), + @min(self.name_base[i + 2], self.name_base[i + 3]), + ); + if (p > 10 and self.skl_id[j] < 35) { + self.skl_freq[j] = p - 10; + if (self.skl_id[j] < 25) { + last = j; + } else { + self.skl_freq[j] = 0; + } + } + j += 1; + i += 4; + } + if (last != -1) { + self.skl_freq[@intCast(last)] <<= 1; + } + if (self.skl_freq[14] != 0 and last != 14) { + self.skl_freq[14] += @min(@min(self.name_base[60], self.name_base[61]), self.skl_freq[14]); + } + if (self.skl_freq[15] != 0 and last != 15) { + self.skl_freq[15] += @min(@min(self.name_base[62], self.name_base[63]), self.skl_freq[15]); + } + } }; -test "val_test" { +test "val_simple_test" { // 从 rust 移植过来的测试 const team = Teamer.new("x"); const namer = Namer.new(team, "x"); @@ -188,4 +257,51 @@ test "val_test" { } } -test "name_test" {} +test "name_test" { + const team = Teamer.new("x"); + var namer = Namer.new(team, "x"); + + const base_name = [128]u8{ + 53, 0, 40, 4, 58, 61, 37, 46, 56, 51, 21, 20, 27, 17, 15, 26, 13, 30, 52, 63, 36, 30, 57, 34, 22, 37, 35, 6, 12, 25, + 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }; + const full_name = [128]u8{ + 53, 0, 40, 4, 58, 61, 37, 46, 56, 51, 21, 20, 27, 17, 15, 26, 13, 30, 52, 63, 36, 30, 57, 34, 22, 37, 35, 6, 12, 25, + 50, 49, 59, 23, 49, 27, 51, 58, 39, 28, 60, 20, 31, 36, 41, 11, 7, 29, 24, 24, 61, 62, 57, 4, 28, 48, 55, 50, 38, 29, + 10, 40, 42, 15, 23, 47, 42, 62, 47, 1, 60, 5, 43, 21, 1, 46, 45, 9, 9, 14, 38, 13, 56, 0, 31, 59, 39, 6, 35, 41, + 55, 5, 34, 3, 7, 33, 33, 45, 16, 16, 32, 43, 18, 44, 22, 14, 17, 10, 11, 53, 18, 44, 19, 52, 2, 32, 12, 8, 2, 54, + 26, 48, 8, 3, 63, 54, 19, 25, + }; + + for (0..128) |i| { + try std.testing.expectEqual(base_name[i], namer.name_base[i]); + } + namer.update_skill(); + for (0..128) |i| { + try std.testing.expectEqual(full_name[i], namer.name_base[i]); + } +} + +test "skill_test" { + const team = Teamer.new("x"); + var namer = Namer.new(team, "x"); + namer.update_skill(); + + const skl_id = [40]u8{ + 9, 13, 12, 38, 4, 27, 26, 15, 16, 32, 24, 5, 7, 21, 18, 10, 37, 2, 6, 20, 39, 1, 14, 3, 11, 29, 22, 33, 19, 0, 30, + 31, 17, 28, 34, 35, 23, 8, 25, 36, + }; + const skl_prop = [40]u8{ + 13, 0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 1, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + }; + + std.debug.print("skl_id: {any}\n", .{namer.skl_id}); + for (0..40) |i| { + try std.testing.expectEqual(skl_id[i], namer.skl_id[i]); + try std.testing.expectEqual(skl_prop[i], namer.skl_freq[i]); + } +}