Skip to content

Commit 22cb576

Browse files
committed
auto gen macros
1 parent 002a5f6 commit 22cb576

3 files changed

Lines changed: 113 additions & 68 deletions

File tree

client/src/ui/tooltips/AbilityTooltip.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ pub fn update(self: *AbilityTooltip, params: tooltip.ParamsFor(AbilityTooltip))
101101
.{params.data.cooldown},
102102
) catch "Buffer overflow");
103103
} else {
104-
const mana_icon = comptime game_data.StatIncreaseData.toControlCode(.{ .max_mp = undefined });
105-
const health_icon = comptime game_data.StatIncreaseData.toControlCode(.{ .max_hp = undefined });
106-
const gold_icon = "&img=\"misc,0\"";
104+
const mana_icon = comptime game_data.Stat.max_mp.icon().comptimeControlCode();
105+
const health_icon = comptime game_data.Stat.max_hp.icon().comptimeControlCode();
106+
const gold_icon = comptime game_data.Currency.gold.icon().comptimeControlCode();
107107

108108
if (has_health_cost and has_mana_cost) {
109109
self.subtext.text_data.setText(std.fmt.bufPrint(

client/src/ui/tooltips/ItemTooltip.zig

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -380,19 +380,22 @@ pub fn update(self: *ItemTooltip, params: tooltip.ParamsFor(ItemTooltip)) void {
380380
defer i += 1;
381381
if (i == 0) text = std.fmt.bufPrint(self.getMainBuffer(), line_base ++ "On Equip: ", .{text}) catch text;
382382

383-
const amount = incr.amount();
384-
if (amount > 0) {
385-
text = std.fmt.bufPrint(
386-
self.getMainBuffer(),
387-
"{s}+" ++ decimal_fmt ++ " {s}{s}",
388-
.{ text, amount, incr.toControlCode(), if (i == stat_increases.len - 1) "" else ", " },
389-
) catch text;
390-
} else {
391-
text = std.fmt.bufPrint(
392-
self.getMainBuffer(),
393-
"{s}" ++ decimal_fmt ++ " {s}{s}",
394-
.{ text, amount, incr.toControlCode(), if (i == stat_increases.len - 1) "" else ", " },
395-
) catch text;
383+
switch (incr) {
384+
inline else => |val, tag| {
385+
if (val.amount > 0) {
386+
text = std.fmt.bufPrint(
387+
self.getMainBuffer(),
388+
"{s}+" ++ decimal_fmt ++ " {s}{s}",
389+
.{ text, val.amount, comptime tag.icon().comptimeControlCode(), if (i == stat_increases.len - 1) "" else ", " },
390+
) catch text;
391+
} else {
392+
text = std.fmt.bufPrint(
393+
self.getMainBuffer(),
394+
"{s}" ++ decimal_fmt ++ " {s}{s}",
395+
.{ text, val.amount, comptime tag.icon().comptimeControlCode(), if (i == stat_increases.len - 1) "" else ", " },
396+
) catch text;
397+
}
398+
},
396399
}
397400
};
398401

@@ -417,7 +420,7 @@ pub fn update(self: *ItemTooltip, params: tooltip.ParamsFor(ItemTooltip)) void {
417420
};
418421

419422
if (data.mana_cost) |cost| {
420-
const mana_icon = comptime game_data.StatIncreaseData.toControlCode(.{ .max_mp = undefined });
423+
const mana_icon = comptime game_data.Stat.max_mp.icon().comptimeControlCode();
421424
text = std.fmt.bufPrint(
422425
self.getMainBuffer(),
423426
line_base ++ float_fmt ++ "% chance to consume " ++ decimal_fmt ++ "&space{s}",
@@ -426,7 +429,7 @@ pub fn update(self: *ItemTooltip, params: tooltip.ParamsFor(ItemTooltip)) void {
426429
}
427430

428431
if (data.health_cost) |cost| {
429-
const health_icon = comptime game_data.StatIncreaseData.toControlCode(.{ .max_hp = undefined });
432+
const health_icon = comptime game_data.Stat.max_hp.icon().comptimeControlCode();
430433
text = std.fmt.bufPrint(
431434
self.getMainBuffer(),
432435
line_base ++ float_fmt ++ "% chance to consume " ++ decimal_fmt ++ "&space{s}",
@@ -435,7 +438,7 @@ pub fn update(self: *ItemTooltip, params: tooltip.ParamsFor(ItemTooltip)) void {
435438
}
436439

437440
if (data.gold_cost) |cost| {
438-
const gold_icon = "&img=\"misc,0\"";
441+
const gold_icon = comptime game_data.Currency.gold.icon().comptimeControlCode();
439442
text = std.fmt.bufPrint(
440443
self.getMainBuffer(),
441444
line_base ++ float_fmt ++ "% chance to consume " ++ decimal_fmt ++ "&space{s}",

shared/src/game_data.zig

Lines changed: 91 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,26 @@ const ziggy = @import("ziggy");
55
const network_data = @import("network_data.zig");
66
const utils = @import("utils.zig");
77

8-
const macro_mappings = [_]struct { base: []const u8, replace: []const u8 }{
9-
.{ .base = "$hptxt", .replace = "&type=\"bold_it\"&col=\"20AC20\"" },
10-
.{ .base = "$mptxt", .replace = "&type=\"bold_it\"&col=\"1C40FF\"" },
11-
.{ .base = "$strtxt", .replace = "&type=\"bold_it\"&col=\"FF6C32\"" },
12-
.{ .base = "$deftxt", .replace = "&type=\"bold_it\"&col=\"FF9670\"" },
13-
.{ .base = "$wittxt", .replace = "&type=\"bold_it\"&col=\"A15AFF\"" },
14-
.{ .base = "$restxt", .replace = "&type=\"bold_it\"&col=\"D65BFF\"" },
15-
.{ .base = "$statxt", .replace = "&type=\"bold_it\"&col=\"C45860\"" },
16-
.{ .base = "$inttxt", .replace = "&type=\"bold_it\"&col=\"6080FF\"" },
17-
.{ .base = "$spdtxt", .replace = "&type=\"bold_it\"&col=\"C45860\"" },
18-
.{ .base = "$hsttxt", .replace = "&type=\"bold_it\"&col=\"60FFAC\"" },
19-
.{ .base = "$multitxt", .replace = "&type=\"bold_it\"&col=\"FFE770\"" },
20-
.{ .base = "$footnotetxt", .replace = "&type=\"med_it\"&size=\"10\"&col=\"736562\"" },
21-
.{ .base = "$hpicon", .replace = "&space" ++ StatIncreaseData.toControlCode(.{ .max_hp = undefined }) },
22-
.{ .base = "$mpicon", .replace = "&space" ++ StatIncreaseData.toControlCode(.{ .max_mp = undefined }) },
23-
.{ .base = "$stricon", .replace = "&space" ++ StatIncreaseData.toControlCode(.{ .strength = undefined }) },
24-
.{ .base = "$deficon", .replace = "&space" ++ StatIncreaseData.toControlCode(.{ .defense = undefined }) },
25-
.{ .base = "$witicon", .replace = "&space" ++ StatIncreaseData.toControlCode(.{ .wit = undefined }) },
26-
.{ .base = "$resicon", .replace = "&space" ++ StatIncreaseData.toControlCode(.{ .resistance = undefined }) },
27-
.{ .base = "$staicon", .replace = "&space" ++ StatIncreaseData.toControlCode(.{ .stamina = undefined }) },
28-
.{ .base = "$inticon", .replace = "&space" ++ StatIncreaseData.toControlCode(.{ .intelligence = undefined }) },
29-
.{ .base = "$spdicon", .replace = "&space" ++ StatIncreaseData.toControlCode(.{ .speed = undefined }) },
30-
.{ .base = "$hsticon", .replace = "&space" ++ StatIncreaseData.toControlCode(.{ .haste = undefined }) },
8+
const ReplacePair = struct { base: []const u8, replace: []const u8 };
9+
const macro_mappings = b: {
10+
var ret: []const ReplacePair = &.{};
11+
for (@typeInfo(Stat).@"enum".fields) |field| {
12+
const stat = @field(Stat, field.name);
13+
const shorthand = stat.shorthand();
14+
ret = ret ++ &[_]ReplacePair{.{
15+
.base = "$" ++ shorthand ++ "txt",
16+
.replace = std.fmt.comptimePrint("&type=\"bold_it\"&col=\"{X:0>6}\"", .{stat.color()}),
17+
}};
18+
ret = ret ++ &[_]ReplacePair{.{
19+
.base = "$" ++ shorthand ++ "icon",
20+
.replace = "&space" ++ stat.icon().comptimeControlCode(),
21+
}};
22+
}
23+
ret = ret ++ &[_]ReplacePair{
24+
.{ .base = "$multitxt", .replace = "&type=\"bold_it\"&col=\"FFE770\"" },
25+
.{ .base = "$footnotetxt", .replace = "&type=\"med_it\"&size=\"10\"&col=\"736562\"" },
26+
};
27+
break :b ret;
3128
};
3229

3330
pub var resource: Maps(ResourceData) = .{};
@@ -232,6 +229,14 @@ pub const TextureData = struct {
232229
sheet: []const u8,
233230
index: u16,
234231

232+
pub fn controlCode(self: TextureData, buf: []u8) ![]const u8 {
233+
return try std.fmt.bufPrint(buf, "&img=\"{s},{d}\"", .{ self.sheet, self.index });
234+
}
235+
236+
pub fn comptimeControlCode(self: TextureData) []const u8 {
237+
return std.fmt.comptimePrint("&img=\"{s},{d}\"", .{ self.sheet, self.index });
238+
}
239+
235240
pub const ziggy_options = struct {
236241
pub fn parse(parser: *ziggy.Parser, first_tok: ziggy.Tokenizer.Token) ziggy.Parser.Error!TextureData {
237242
const map = try parser.parseValue(ziggy.dynamic.Map(u16), first_tok);
@@ -508,19 +513,19 @@ pub const GroundData = struct {
508513
animations: ?[]const FrameData = null,
509514
};
510515

511-
pub const StatIncreaseData = union(enum) {
512-
max_hp: struct { amount: u16 },
513-
max_mp: struct { amount: u16 },
514-
strength: struct { amount: u16 },
515-
wit: struct { amount: u16 },
516-
defense: struct { amount: u16 },
517-
resistance: struct { amount: u16 },
518-
speed: struct { amount: u16 },
519-
stamina: struct { amount: u16 },
520-
intelligence: struct { amount: u16 },
521-
haste: struct { amount: u16 },
522-
523-
pub fn toString(self: StatIncreaseData) []const u8 {
516+
pub const Stat = enum(u8) {
517+
max_hp = 0,
518+
max_mp = 1,
519+
strength = 2,
520+
wit = 3,
521+
defense = 4,
522+
resistance = 5,
523+
speed = 6,
524+
stamina = 7,
525+
intelligence = 8,
526+
haste = 9,
527+
528+
pub fn name(self: Stat) []const u8 {
524529
return switch (self) {
525530
.max_hp => "Max HP",
526531
.max_mp => "Max MP",
@@ -535,26 +540,63 @@ pub const StatIncreaseData = union(enum) {
535540
};
536541
}
537542

538-
pub fn toControlCode(self: StatIncreaseData) []const u8 {
543+
pub fn shorthand(self: Stat) []const u8 {
539544
return switch (self) {
540-
.max_hp => "&img=\"misc_big,0\"",
541-
.max_mp => "&img=\"misc_big,1\"",
542-
.strength => "&img=\"misc_big,2\"",
543-
.wit => "&img=\"misc_big,3\"",
544-
.defense => "&img=\"misc_big,4\"",
545-
.resistance => "&img=\"misc_big,5\"",
546-
.stamina => "&img=\"misc_big,6\"",
547-
.intelligence => "&img=\"misc_big,7\"",
548-
.speed => "&img=\"misc_big,8\"",
549-
.haste => "&img=\"misc_big,9\"",
545+
.max_hp => "hp",
546+
.max_mp => "mp",
547+
.strength => "str",
548+
.wit => "wit",
549+
.defense => "def",
550+
.resistance => "res",
551+
.speed => "spd",
552+
.stamina => "sta",
553+
.intelligence => "int",
554+
.haste => "hst",
550555
};
551556
}
552557

553-
pub fn amount(self: StatIncreaseData) u16 {
558+
pub fn color(self: Stat) u24 {
554559
return switch (self) {
555-
inline else => |inner| inner.amount,
560+
.max_hp => 0x20AC20,
561+
.max_mp => 0x1C40FF,
562+
.strength => 0xFF6C32,
563+
.wit => 0xA15AFF,
564+
.defense => 0xFF9670,
565+
.resistance => 0xD65BFF,
566+
.speed => 0xC45860,
567+
.stamina => 0xC45860,
568+
.intelligence => 0x6080FF,
569+
.haste => 0x60FFAC,
556570
};
557571
}
572+
573+
pub fn icon(self: Stat) TextureData {
574+
return switch (self) {
575+
.max_hp => .{ .sheet = "misc_big", .index = 0 },
576+
.max_mp => .{ .sheet = "misc_big", .index = 1 },
577+
.strength => .{ .sheet = "misc_big", .index = 2 },
578+
.wit => .{ .sheet = "misc_big", .index = 3 },
579+
.defense => .{ .sheet = "misc_big", .index = 4 },
580+
.resistance => .{ .sheet = "misc_big", .index = 5 },
581+
.stamina => .{ .sheet = "misc_big", .index = 6 },
582+
.intelligence => .{ .sheet = "misc_big", .index = 7 },
583+
.speed => .{ .sheet = "misc_big", .index = 8 },
584+
.haste => .{ .sheet = "misc_big", .index = 9 },
585+
};
586+
}
587+
};
588+
589+
pub const StatIncreaseData = union(Stat) {
590+
max_hp: struct { amount: u16 },
591+
max_mp: struct { amount: u16 },
592+
strength: struct { amount: u16 },
593+
wit: struct { amount: u16 },
594+
defense: struct { amount: u16 },
595+
resistance: struct { amount: u16 },
596+
speed: struct { amount: u16 },
597+
stamina: struct { amount: u16 },
598+
intelligence: struct { amount: u16 },
599+
haste: struct { amount: u16 },
558600
};
559601

560602
pub const StatIncreaseDataPerc = union(enum) {

0 commit comments

Comments
 (0)