diff --git a/assets/cubyz/biomes/bush_lands.json b/assets/cubyz/biomes/bush_lands.json index 89b9e2c7..faae55a6 100644 --- a/assets/cubyz/biomes/bush_lands.json +++ b/assets/cubyz/biomes/bush_lands.json @@ -9,7 +9,7 @@ "hills" : 10, "rivers" : true, - "music" : "sunrise", + "music" : "cubyz:sunrise", "ground_structure" : [ "cubyz:grass", diff --git a/assets/cubyz/biomes/cave/cave.json b/assets/cubyz/biomes/cave/cave.json index 62ec89ef..1253e9fd 100644 --- a/assets/cubyz/biomes/cave/cave.json +++ b/assets/cubyz/biomes/cave/cave.json @@ -2,7 +2,7 @@ "isCave" : true, "maxHeight" : 0, - "music" : "heart-of-the-beast", + "music" : "cubyz:heart-of-the-beast", "ground_structure" : [ "0 to 1 cubyz:gravel", diff --git a/assets/cubyz/biomes/cave/crystal.json b/assets/cubyz/biomes/cave/crystal.json index f66e082f..a5ff7658 100644 --- a/assets/cubyz/biomes/cave/crystal.json +++ b/assets/cubyz/biomes/cave/crystal.json @@ -7,7 +7,7 @@ "caves" : -0.1, "crystals" : 32, - "music" : "la_kubisa_caverna", + "music" : "cubyz:la_kubisa_caverna", "stoneBlock" : "cubyz:stone", "structures" : [ { diff --git a/assets/cubyz/biomes/cave/crystal_forest.json b/assets/cubyz/biomes/cave/crystal_forest.json index ab9f417f..24837e55 100644 --- a/assets/cubyz/biomes/cave/crystal_forest.json +++ b/assets/cubyz/biomes/cave/crystal_forest.json @@ -7,7 +7,7 @@ "caves" : -0.1, "crystals" : 32, - "music" : "leaves", + "music" : "cubyz:leaves", "stoneBlock" : "cubyz:stone", "ground_structure" : [ diff --git a/assets/cubyz/biomes/cave/sky.json b/assets/cubyz/biomes/cave/sky.json index b8311a38..67c2c52c 100644 --- a/assets/cubyz/biomes/cave/sky.json +++ b/assets/cubyz/biomes/cave/sky.json @@ -2,7 +2,7 @@ "isCave" : true, "minHeight" : 0, - "music" : "GymnopedieNo1", + "music" : "cubyz:GymnopedieNo1", "ground_structure" : [], } \ No newline at end of file diff --git a/assets/cubyz/biomes/cave/stalagmite_cave.json b/assets/cubyz/biomes/cave/stalagmite_cave.json index 4c020acf..ebb61ead 100644 --- a/assets/cubyz/biomes/cave/stalagmite_cave.json +++ b/assets/cubyz/biomes/cave/stalagmite_cave.json @@ -7,7 +7,7 @@ "caves" : -0.1, "stalagmites" : 160, - "music" : "heart-of-the-beast", + "music" : "cubyz:heart-of-the-beast", "stoneBlock" : "cubyz:stone" } \ No newline at end of file diff --git a/assets/cubyz/biomes/cave/stone_forest.json b/assets/cubyz/biomes/cave/stone_forest.json index 338de010..ddd99bf6 100644 --- a/assets/cubyz/biomes/cave/stone_forest.json +++ b/assets/cubyz/biomes/cave/stone_forest.json @@ -5,7 +5,7 @@ "caves" : -0.2, - "music" : "heart-of-the-beast", + "music" : "cubyz:heart-of-the-beast", "ground_structure" : [ "0 to 1 cubyz:gravel", diff --git a/assets/cubyz/biomes/cave/void_cavern.json b/assets/cubyz/biomes/cave/void_cavern.json index 56e4a1d7..294029b3 100644 --- a/assets/cubyz/biomes/cave/void_cavern.json +++ b/assets/cubyz/biomes/cave/void_cavern.json @@ -6,6 +6,6 @@ "caves" : -0.1, - "music" : "heart-of-the-beast", + "music" : "cubyz:heart-of-the-beast", "stoneBlock" : "cubyz:void_stone", } \ No newline at end of file diff --git a/assets/cubyz/biomes/clearing.json b/assets/cubyz/biomes/clearing.json index df7fcb19..ca70901b 100644 --- a/assets/cubyz/biomes/clearing.json +++ b/assets/cubyz/biomes/clearing.json @@ -9,7 +9,7 @@ "roughness" : 1, "hills" : 5, - "music" : "sunrise", + "music" : "cubyz:sunrise", "validPlayerSpawn" : true, diff --git a/assets/cubyz/biomes/cold_ocean.json b/assets/cubyz/biomes/cold_ocean.json index 0d842108..b7cc6a4d 100644 --- a/assets/cubyz/biomes/cold_ocean.json +++ b/assets/cubyz/biomes/cold_ocean.json @@ -11,7 +11,7 @@ "roughness" : 20, "hills" : 10, - "music" : "under_the_water_sky", + "music" : "cubyz:under_the_water_sky", "ground_structure" : [ "1 to 2 cubyz:cobblestone" diff --git a/assets/cubyz/biomes/desert.json b/assets/cubyz/biomes/desert.json index 02910736..b343afcb 100644 --- a/assets/cubyz/biomes/desert.json +++ b/assets/cubyz/biomes/desert.json @@ -8,7 +8,7 @@ "hills" : 20, - "music" : "EasternThought", + "music" : "cubyz:EasternThought", "fogDensity" : 20, "fogColor" : 0xe0dcc3, diff --git a/assets/cubyz/biomes/forest.json b/assets/cubyz/biomes/forest.json index 4a88f391..f7d34a2b 100644 --- a/assets/cubyz/biomes/forest.json +++ b/assets/cubyz/biomes/forest.json @@ -8,7 +8,7 @@ "roughness" : 10, "hills" : 10, - "music" : "leaves", + "music" : "cubyz:leaves", "validPlayerSpawn" : true, diff --git a/assets/cubyz/biomes/glass_forest.json b/assets/cubyz/biomes/glass_forest.json index 8af1a322..f4c17496 100644 --- a/assets/cubyz/biomes/glass_forest.json +++ b/assets/cubyz/biomes/glass_forest.json @@ -8,7 +8,7 @@ "roughness" : 10, "hills" : 10, - "music" : "mana-two-part-2", + "music" : "cubyz:mana-two-part-2", "validPlayerSpawn" : false, diff --git a/assets/cubyz/biomes/grassland.json b/assets/cubyz/biomes/grassland.json index 4986d9ff..e88521c5 100644 --- a/assets/cubyz/biomes/grassland.json +++ b/assets/cubyz/biomes/grassland.json @@ -7,7 +7,7 @@ "roughness" : 1, "hills" : 5, - "music" : "sunrise", + "music" : "cubyz:sunrise", "validPlayerSpawn" : true, diff --git a/assets/cubyz/biomes/island.json b/assets/cubyz/biomes/island.json index 4be80f6c..73e11907 100644 --- a/assets/cubyz/biomes/island.json +++ b/assets/cubyz/biomes/island.json @@ -6,7 +6,7 @@ "minHeight" : 4, "maxHeight" : 4, - "music" : "sunrise", + "music" : "cubyz:sunrise", "radius" : 24, diff --git a/assets/cubyz/biomes/island_shelf.json b/assets/cubyz/biomes/island_shelf.json index eb2b76bc..34a271d2 100644 --- a/assets/cubyz/biomes/island_shelf.json +++ b/assets/cubyz/biomes/island_shelf.json @@ -6,7 +6,7 @@ "minHeight" : -10, "maxHeight" : -10, - "music" : "Sincerely", + "music" : "cubyz:Sincerely", "radius" : 64, diff --git a/assets/cubyz/biomes/jungle(TODO).json b/assets/cubyz/biomes/jungle(TODO).json index 5a620951..bd377903 100644 --- a/assets/cubyz/biomes/jungle(TODO).json +++ b/assets/cubyz/biomes/jungle(TODO).json @@ -9,7 +9,7 @@ "roughness" : 10, "hills" : 10, - "music" : "leaves", + "music" : "cubyz:leaves", "validPlayerSpawn" : true, diff --git a/assets/cubyz/biomes/ocean.json b/assets/cubyz/biomes/ocean.json index ddd9ca3a..2d07467f 100644 --- a/assets/cubyz/biomes/ocean.json +++ b/assets/cubyz/biomes/ocean.json @@ -11,7 +11,7 @@ "rivers" : true, - "music" : "under_the_water_sky", + "music" : "cubyz:under_the_water_sky", "ground_structure" : [ "2 to 3 cubyz:gravel" diff --git a/assets/cubyz/biomes/peak.json b/assets/cubyz/biomes/peak.json index 2281e1bf..68002160 100644 --- a/assets/cubyz/biomes/peak.json +++ b/assets/cubyz/biomes/peak.json @@ -7,7 +7,7 @@ "mountains" : 125, - "music" : "DarkTimes", + "music" : "cubyz:DarkTimes", "ground_structure" : [ "3 to 6 cubyz:snow" diff --git a/assets/cubyz/biomes/rainbow_forest.json b/assets/cubyz/biomes/rainbow_forest.json index de48308e..9fca0c60 100644 --- a/assets/cubyz/biomes/rainbow_forest.json +++ b/assets/cubyz/biomes/rainbow_forest.json @@ -10,7 +10,7 @@ "chance" : 0.01, - "music" : "leaves", + "music" : "cubyz:leaves", "validPlayerSpawn" : true, diff --git a/assets/cubyz/biomes/rocky_grassland.json b/assets/cubyz/biomes/rocky_grassland.json index cd69c3e0..feef9fed 100644 --- a/assets/cubyz/biomes/rocky_grassland.json +++ b/assets/cubyz/biomes/rocky_grassland.json @@ -7,7 +7,7 @@ "mountains" : 30, - "music" : "sunrise", + "music" : "cubyz:sunrise", "ground_structure" : [ "cubyz:grass", diff --git a/assets/cubyz/biomes/swamp.json b/assets/cubyz/biomes/swamp.json index 2546464e..f24d9f31 100644 --- a/assets/cubyz/biomes/swamp.json +++ b/assets/cubyz/biomes/swamp.json @@ -9,7 +9,7 @@ "roughness" : 2, "hills" : 2, - "music" : "leaves", + "music" : "cubyz:leaves", "validPlayerSpawn" : true, diff --git a/assets/cubyz/biomes/tall_mountain/base.json b/assets/cubyz/biomes/tall_mountain/base.json index 82961a62..4cf1a542 100644 --- a/assets/cubyz/biomes/tall_mountain/base.json +++ b/assets/cubyz/biomes/tall_mountain/base.json @@ -11,7 +11,7 @@ "chance" : 0, - "music" : "out_of_breath", + "music" : "cubyz:out_of_breath", "parentBiomes" : [ { diff --git a/assets/cubyz/biomes/tall_mountain/peak.json b/assets/cubyz/biomes/tall_mountain/peak.json index cff2f082..e1bafe20 100644 --- a/assets/cubyz/biomes/tall_mountain/peak.json +++ b/assets/cubyz/biomes/tall_mountain/peak.json @@ -10,7 +10,7 @@ "mountains" : 30, "chance" : 0, - "music" : "out_of_breath", + "music" : "cubyz:out_of_breath", "parentBiomes" : [ { diff --git a/assets/cubyz/biomes/tall_mountain/slope1.json b/assets/cubyz/biomes/tall_mountain/slope1.json index 6adfd8fd..125cc537 100644 --- a/assets/cubyz/biomes/tall_mountain/slope1.json +++ b/assets/cubyz/biomes/tall_mountain/slope1.json @@ -11,7 +11,7 @@ "chance" : 0, - "music" : "out_of_breath", + "music" : "cubyz:out_of_breath", "parentBiomes" : [ { diff --git a/assets/cubyz/biomes/tall_mountain/slope2.json b/assets/cubyz/biomes/tall_mountain/slope2.json index c0b1c342..2d319f36 100644 --- a/assets/cubyz/biomes/tall_mountain/slope2.json +++ b/assets/cubyz/biomes/tall_mountain/slope2.json @@ -10,7 +10,7 @@ "chance" : 0, - "music" : "out_of_breath", + "music" : "cubyz:out_of_breath", "parentBiomes" : [ { diff --git a/assets/cubyz/biomes/tall_mountain/slope3.json b/assets/cubyz/biomes/tall_mountain/slope3.json index e4fcc217..b68058c2 100644 --- a/assets/cubyz/biomes/tall_mountain/slope3.json +++ b/assets/cubyz/biomes/tall_mountain/slope3.json @@ -10,7 +10,7 @@ "chance" : 0, - "music" : "out_of_breath", + "music" : "cubyz:out_of_breath", "parentBiomes" : [ { diff --git a/assets/cubyz/biomes/tall_mountain/slope4.json b/assets/cubyz/biomes/tall_mountain/slope4.json index 40b0d4d6..0996173c 100644 --- a/assets/cubyz/biomes/tall_mountain/slope4.json +++ b/assets/cubyz/biomes/tall_mountain/slope4.json @@ -10,7 +10,7 @@ "chance" : 0, - "music" : "out_of_breath", + "music" : "cubyz:out_of_breath", "parentBiomes" : [ { diff --git a/assets/cubyz/biomes/tall_mountain/slope5.json b/assets/cubyz/biomes/tall_mountain/slope5.json index 071283e5..65e226d5 100644 --- a/assets/cubyz/biomes/tall_mountain/slope5.json +++ b/assets/cubyz/biomes/tall_mountain/slope5.json @@ -9,7 +9,7 @@ "interpolation" : "linear", "chance" : 0, - "music" : "out_of_breath", + "music" : "cubyz:out_of_breath", "parentBiomes" : [ { diff --git a/assets/cubyz/biomes/tall_mountain/slope6.json b/assets/cubyz/biomes/tall_mountain/slope6.json index 94e3e2b6..3ba17035 100644 --- a/assets/cubyz/biomes/tall_mountain/slope6.json +++ b/assets/cubyz/biomes/tall_mountain/slope6.json @@ -9,7 +9,7 @@ "interpolation" : "linear", "chance" : 0, - "music" : "out_of_breath", + "music" : "cubyz:out_of_breath", "parentBiomes" : [ { diff --git a/assets/cubyz/biomes/thicket.json b/assets/cubyz/biomes/thicket.json index d8183278..eb1e90f1 100644 --- a/assets/cubyz/biomes/thicket.json +++ b/assets/cubyz/biomes/thicket.json @@ -10,7 +10,7 @@ "roughness" : 10, "hills" : 10, - "music" : "leaves", + "music" : "cubyz:leaves", "validPlayerSpawn" : true, diff --git a/assets/cubyz/biomes/warm_ocean.json b/assets/cubyz/biomes/warm_ocean.json index 0f2a3c52..08f25a02 100644 --- a/assets/cubyz/biomes/warm_ocean.json +++ b/assets/cubyz/biomes/warm_ocean.json @@ -10,7 +10,7 @@ "roughness" : 10, "hills" : 20, - "music" : "tides", + "music" : "cubyz:tides", "ground_structure" : [ "2 to 4 cubyz:sand" diff --git a/src/audio.zig b/src/audio.zig index 983e0133..19df6003 100644 --- a/src/audio.zig +++ b/src/audio.zig @@ -20,18 +20,33 @@ const AudioData = struct { musicId: []const u8, data: []f32 = &.{}, + fn open_vorbis_file_by_id(id: []const u8) ?*c.stb_vorbis { + const colonIndex = std.mem.indexOfScalar(u8, id, ':') orelse { + std.log.err("Invalid music id: {s}. Must be addon:file_name", .{id}); + return null; + }; + const addon = id[0..colonIndex]; + const fileName = id[colonIndex+1..]; + const path1 = std.fmt.allocPrintZ(main.stackAllocator.allocator, "assets/{s}/music/{s}.ogg", .{addon, fileName}) catch unreachable; + defer main.stackAllocator.free(path1); + var err: c_int = 0; + if(c.stb_vorbis_open_filename(path1.ptr, &err, null)) |ogg_stream| return ogg_stream; + const path2 = std.fmt.allocPrintZ(main.stackAllocator.allocator, "serverAssets/{s}/music/{s}.ogg", .{addon, fileName}) catch unreachable; + defer main.stackAllocator.free(path2); + if(c.stb_vorbis_open_filename(path2.ptr, &err, null)) |ogg_stream| return ogg_stream; + std.log.err("Couldn't find music with id \"{s}\". Searched path \"{s}\" and \"{s}\"", .{id, path1, path2}); + return null; + } + fn init(musicId: []const u8) *AudioData { const self = main.globalAllocator.create(AudioData); self.* = .{.musicId = musicId}; - var err: c_int = 0; - const path = std.fmt.allocPrintZ(main.stackAllocator.allocator, "assets/cubyz/music/{s}.ogg", .{musicId}) catch unreachable; - defer main.stackAllocator.free(path); - const ogg_stream = c.stb_vorbis_open_filename(path.ptr, &err, null); - defer c.stb_vorbis_close(ogg_stream); - if(ogg_stream != null) { + + const channels = 2; + if(open_vorbis_file_by_id(musicId)) |ogg_stream| { + defer c.stb_vorbis_close(ogg_stream); const ogg_info: c.stb_vorbis_info = c.stb_vorbis_get_info(ogg_stream); const samples = c.stb_vorbis_stream_length_in_samples(ogg_stream); - const channels = 2; if(sampleRate != @as(f32, @floatFromInt(ogg_info.sample_rate))) { const tempData = main.stackAllocator.alloc(f32, samples*channels); defer main.stackAllocator.free(tempData); @@ -57,7 +72,8 @@ const AudioData = struct { _ = c.stb_vorbis_get_samples_float_interleaved(ogg_stream, channels, self.data.ptr, @as(c_int, @intCast(samples))*ogg_info.channels); } } else { - std.log.err("Couldn't read audio with id {s}", .{musicId}); + self.data = main.globalAllocator.alloc(f32, channels); + @memset(self.data, 0); } return self; } @@ -231,7 +247,7 @@ var curIndex: u16 = 0; var curEndIndex: std.atomic.Value(u16) = .{.value = sampleRate/60 & ~@as(u16, 1)}; fn addMusic(buffer: []f32) void { - const musicId = if(main.game.world) |world| world.playerBiome.load(.monotonic).preferredMusic else "cubyz"; + const musicId = if(main.game.world) |world| world.playerBiome.load(.monotonic).preferredMusic else "cubyz:cubyz"; if(!std.mem.eql(u8, musicId, activeMusicId)) { if(activeMusicId.len == 0) { if(findMusic(musicId)) |musicBuffer| { diff --git a/src/server/terrain/biomes.zig b/src/server/terrain/biomes.zig index 8a04db35..94a2f7b3 100644 --- a/src/server/terrain/biomes.zig +++ b/src/server/terrain/biomes.zig @@ -275,7 +275,7 @@ pub const Biome = struct { .minHeight = json.get(i32, "minHeight", std.math.minInt(i32)), .maxHeight = json.get(i32, "maxHeight", std.math.maxInt(i32)), .supportsRivers = json.get(bool, "rivers", false), - .preferredMusic = main.globalAllocator.dupe(u8, json.get([]const u8, "music", "")), + .preferredMusic = main.globalAllocator.dupe(u8, json.get([]const u8, "music", "cubyz:cubyz")), .isValidPlayerSpawn = json.get(bool, "validPlayerSpawn", false), .chance = json.get(f32, "chance", if(json == .JsonNull) 0 else 1), .maxSubBiomeCount = json.get(f32, "maxSubBiomeCount", std.math.floatMax(f32)),