mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 03:06:55 -04:00
Resolves: #1457 --------- Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>
This commit is contained in:
parent
b79784a0aa
commit
2e705d9cc9
@ -330,6 +330,9 @@ pub const Blueprint = struct {
|
||||
};
|
||||
|
||||
pub const Pattern = struct {
|
||||
const weightSeparator = '%';
|
||||
const expressionSeparator = ',';
|
||||
|
||||
blocks: AliasTable(Entry),
|
||||
|
||||
const Entry = struct {
|
||||
@ -338,26 +341,28 @@ pub const Pattern = struct {
|
||||
};
|
||||
|
||||
pub fn initFromString(allocator: NeverFailingAllocator, source: []const u8) !@This() {
|
||||
var specifiers = std.mem.splitScalar(u8, source, ',');
|
||||
var specifiers = std.mem.splitScalar(u8, source, expressionSeparator);
|
||||
var totalWeight: f32 = 0;
|
||||
|
||||
var weightedEntries: ListUnmanaged(struct {block: Block, weight: f32}) = .{};
|
||||
defer weightedEntries.deinit(main.stackAllocator);
|
||||
|
||||
while(specifiers.next()) |specifier| {
|
||||
var iterator = std.mem.splitScalar(u8, specifier, '%');
|
||||
var blockId = specifier;
|
||||
var weight: f32 = 1.0;
|
||||
|
||||
var weight: f32 = undefined;
|
||||
var block = main.blocks.parseBlock(iterator.rest());
|
||||
if(std.mem.containsAtLeastScalar(u8, specifier, 1, weightSeparator)) {
|
||||
var iterator = std.mem.splitScalar(u8, specifier, weightSeparator);
|
||||
const weightString = iterator.first();
|
||||
blockId = iterator.rest();
|
||||
|
||||
const first = iterator.first();
|
||||
weight = std.fmt.parseFloat(f32, weightString) catch return error.@"Weight not a valid number";
|
||||
if(weight <= 0) return error.@"Weight must be greater than 0";
|
||||
}
|
||||
|
||||
_ = main.blocks.getBlockById(blockId) catch return error.@"Block not found";
|
||||
const block = main.blocks.parseBlock(blockId);
|
||||
|
||||
weight = std.fmt.parseFloat(f32, first) catch blk: {
|
||||
// To distinguish somehow between mistyped numeric values and actual block IDs we check for addon name separator.
|
||||
if(!std.mem.containsAtLeastScalar(u8, first, 1, ':')) return error.PatternSyntaxError;
|
||||
block = main.blocks.parseBlock(first);
|
||||
break :blk 1.0;
|
||||
};
|
||||
totalWeight += weight;
|
||||
weightedEntries.append(main.stackAllocator, .{.block = block, .weight = weight});
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ pub fn execute(args: []const u8, source: *User) void {
|
||||
return source.sendMessage("#ff0000Position 2 isn't set", .{});
|
||||
};
|
||||
const pattern = Pattern.initFromString(main.stackAllocator, args) catch |err| {
|
||||
source.sendMessage("#ff0000Error parsing pattern: {}", .{err});
|
||||
source.sendMessage("#ff0000Error parsing pattern: {s}", .{@errorName(err)});
|
||||
return;
|
||||
};
|
||||
defer pattern.deinit(main.stackAllocator);
|
||||
|
Loading…
x
Reference in New Issue
Block a user