areas implementation
This commit is contained in:
@@ -3,27 +3,41 @@ const rl = @import("raylib");
|
||||
const spawn_area = @import("spawn-area.zig");
|
||||
const structures = @import("structures.zig");
|
||||
|
||||
const Areas = struct {
|
||||
areas: []spawn_area.SpawnArea,
|
||||
pub const Areas = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
areas: []spawn_area.SpawnArea,
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, size: usize) !Areas {
|
||||
const values = std.enums.values(structures.AreaLocation);
|
||||
if (size > values.len) size = values.len;
|
||||
var max_areas = size;
|
||||
if (size > values.len) max_areas = values.len;
|
||||
|
||||
var used_areas = std.ArrayList(structures.AreaLocation).init(allocator);
|
||||
defer used_areas.deinit();
|
||||
|
||||
// here we generate size random AreaLocation enums that aren't in used_areas
|
||||
// the random generator will only generate AreaLocation enums that are not in
|
||||
//
|
||||
// todo implement function which will subtract values from used_areas (after RANDOM)
|
||||
// based on this we will fill data with 0..size data and return the final value
|
||||
var prng = std.Random.DefaultPrng.init(blk: {
|
||||
var seed: u64 = undefined;
|
||||
try std.posix.getrandom(std.mem.asBytes(&seed));
|
||||
break :blk seed;
|
||||
});
|
||||
|
||||
const rand = prng.random();
|
||||
const area_data = try allocator.alloc(spawn_area.SpawnArea, size);
|
||||
|
||||
for (area_data) |*area| {
|
||||
// give us all valid values we can use for AreaLocation
|
||||
const valid_values = try getEnumValuesMinusUsed(allocator, used_areas, values);
|
||||
defer allocator.free(valid_values);
|
||||
|
||||
const num = rand.intRangeAtMost(usize, 0, valid_values.len - 1); // random number
|
||||
// here we create new spawnarea
|
||||
area.* = spawn_area.SpawnArea.init(valid_values[num]);
|
||||
try used_areas.append(valid_values[num]);
|
||||
}
|
||||
|
||||
const data = try allocator.alloc(structures.AreaLocation, size);
|
||||
// todo data must be set before returned
|
||||
return Areas{
|
||||
.areas = data,
|
||||
.allocator = allocator,
|
||||
.areas = area_data,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -31,8 +45,28 @@ const Areas = struct {
|
||||
self.allocator.free(self.areas);
|
||||
}
|
||||
|
||||
pub fn draw(areas: *Areas) void {
|
||||
// todo implement draw
|
||||
_ = areas;
|
||||
pub fn draw(self: *Areas) void {
|
||||
for (self.areas) |area| {
|
||||
area.draw();
|
||||
}
|
||||
}
|
||||
|
||||
fn getEnumValuesMinusUsed(allocator: std.mem.Allocator, used_values: std.ArrayList(structures.AreaLocation), values: []const structures.AreaLocation) ![]structures.AreaLocation {
|
||||
var valuesToRandomise = try allocator.alloc(structures.AreaLocation, values.len - used_values.items.len);
|
||||
|
||||
var i: usize = 0;
|
||||
|
||||
outer: for (values) |value| {
|
||||
for (used_values.items) |used_val| {
|
||||
if (value == used_val) {
|
||||
continue :outer;
|
||||
}
|
||||
}
|
||||
// this value is clean and can get put into values good for randomisation
|
||||
valuesToRandomise[i] = value;
|
||||
i += 1;
|
||||
}
|
||||
|
||||
return valuesToRandomise;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user