147 lines
3.1 KiB
Zig
147 lines
3.1 KiB
Zig
const std = @import("std");
|
|
const print = std.debug.print;
|
|
const math = std.math;
|
|
|
|
const Endian = std.builtin.Endian;
|
|
const ArrayList = std.ArrayList;
|
|
const AutoHashMap = std.AutoHashMap;
|
|
const Allocator = std.mem.Allocator;
|
|
const StringHashMap = std.StringHashMap;
|
|
|
|
const pnt = @import("point.zig");
|
|
const Pnt = pnt.Pnt;
|
|
const Activity = @import("activity.zig").Activity;
|
|
const SoundSettings = @import("settings.zig").SoundSettings;
|
|
const FreqAmpBuffer = @import("freqamp.zig").FreqAmpBuffer;
|
|
|
|
const utility = @import("utility.zig");
|
|
const prnt = utility.prnt;
|
|
|
|
|
|
pub const SoundNode = struct {
|
|
|
|
allocator: Allocator,
|
|
|
|
name: []const u8 = "soundnode",
|
|
uid: usize = 0,
|
|
|
|
air_in: ArrayList(*SoundNode),
|
|
wire_in: ArrayList(*SoundNode),
|
|
|
|
air_targets: ArrayList(*SoundNode),
|
|
wire_targets: ArrayList(*SoundNode),
|
|
|
|
activities: ArrayList(*Activity),
|
|
props: StringHashMap(f64),
|
|
|
|
x: f32 = 0,
|
|
y: f32 = 0,
|
|
z: f32 = 0,
|
|
|
|
fab: FreqAmpBuffer,
|
|
active: u8 = 1,
|
|
|
|
pub fn create(allocator: Allocator, name: []const u8, uid: usize) !*SoundNode {
|
|
|
|
const air_in = ArrayList(*SoundNode).init(allocator);
|
|
const wire_in = ArrayList(*SoundNode).init(allocator);
|
|
const air_targets = ArrayList(*SoundNode).init(allocator);
|
|
const wire_targets = ArrayList(*SoundNode).init(allocator);
|
|
const activities = ArrayList(*Activity).init(allocator);
|
|
const fab = FreqAmpBuffer{};
|
|
const props = StringHashMap(f64).init(allocator);
|
|
|
|
const sn = try allocator.create(SoundNode);
|
|
|
|
sn.* = .{
|
|
.uid = uid,
|
|
.allocator = allocator,
|
|
.name = name,
|
|
.air_in = air_in,
|
|
.wire_in = wire_in,
|
|
.air_targets = air_targets,
|
|
.wire_targets = wire_targets,
|
|
.activities = activities,
|
|
.fab = fab,
|
|
.props = props,
|
|
.x = 0,
|
|
.y = 0,
|
|
.z = 0,
|
|
.active = 0,
|
|
};
|
|
|
|
return sn;
|
|
|
|
}
|
|
|
|
pub fn init(allocator: Allocator, name: []const u8) !SoundNode {
|
|
|
|
const air_in = ArrayList(*SoundNode).init(allocator);
|
|
const wire_in = ArrayList(*SoundNode).init(allocator);
|
|
const activities = ArrayList(*Activity).init(allocator);
|
|
const fab = try FreqAmpBuffer.init(allocator);
|
|
const props = StringHashMap(f64).init(allocator);
|
|
|
|
return .{
|
|
.allocator = allocator,
|
|
.name = name,
|
|
.air_in = air_in,
|
|
.wire_in = wire_in,
|
|
.activities = activities,
|
|
.fab = fab,
|
|
.props = props,
|
|
.x = 0,
|
|
.y = 0,
|
|
.z = 0,
|
|
};
|
|
|
|
}
|
|
|
|
pub fn deinit(self: *SoundNode) void {
|
|
|
|
self.air_in.deinit();
|
|
self.wire_in.deinit();
|
|
self.activities.deinit();
|
|
self.fab.deinit();
|
|
self.props.deinit();
|
|
|
|
}
|
|
|
|
pub fn current_tick(self: *SoundNode) u32 {
|
|
|
|
return self.fab.current_tick;
|
|
|
|
}
|
|
|
|
pub fn g(self: *SoundNode, key: []const u8) f64 {
|
|
|
|
const value = self.props.get(key);
|
|
|
|
if (value) |v| {
|
|
return v;
|
|
} else {
|
|
return @as(f64, 0.0);
|
|
}
|
|
}
|
|
|
|
pub fn s(self: *SoundNode, key: []const u8, value: f64) !void {
|
|
|
|
try self.props.put(key, value);
|
|
|
|
}
|
|
|
|
pub fn distance(self: *SoundNode, other: *SoundNode) f64 {
|
|
|
|
if ((self.x == other.x) and (self.y == other.y) and (self.z == other.z)) {
|
|
return 0;
|
|
}
|
|
|
|
const dx: f32 = self.x - other.x;
|
|
const dy: f32 = self.y - other.y;
|
|
const dz: f32 = self.z - other.z;
|
|
|
|
return math.sqrt(dx*dx + dy*dy + dz*dz);
|
|
|
|
}
|
|
|
|
};
|