Skip to content

Commit

Permalink
fix radios in clientside unloaded chunks
Browse files Browse the repository at this point in the history
also:
fix satellite station model
rename satellite station to satellite broadcast station
  • Loading branch information
techno-sam committed Oct 10, 2024
1 parent 6ac177d commit 99b0fee
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 143 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ loader_version=0.16.3
#Fabric api
fabric_version=0.92.2+1.20.1

mod_version = 1.1.0-beta.3
mod_version = 1.1.1-beta.1
maven_group = io.github.foundationgames
archives_base_name = phonos

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.LongConsumer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.longs.LongList;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongSet;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.world.World;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand Down Expand Up @@ -84,7 +87,7 @@ public Delta updateServer(World world) {
radioChanges.add(channel, metadata);
}

final int searchUntil = index;
final int searchUntil = index + 1; // this is intentionally higher than the client version, since the client does more complicated radio logic
RadioLongConsumer[] consumerRef = new RadioLongConsumer[1];
RadioLongConsumer consumer = (child, metadata) -> {
if (emitters.isLoaded(child) && emitters.getEmitter(child) instanceof RadioStorage.RadioEmitter radioEmitter && radioEmitter.isRadio()) {
Expand Down Expand Up @@ -135,11 +138,18 @@ public Delta updateServer(World world) {
// immediate effects regardless of server speed/latency
public void updateClient(World world) {
var emitters = SoundEmitterStorage.getInstance(world);
Int2ObjectOpenHashMap<HashSet<RadioMetadata>> backupSources = new Int2ObjectOpenHashMap<>(radioSources);
radioSources.clear();

int index = 0;

LongSet nonIgnoredRadioEmitters;
LongSet nextNonIgnoredRadioEmitters = new LongOpenHashSet();

while (index < this.levels.size() && !this.levels.get(index).empty()) {
nonIgnoredRadioEmitters = nextNonIgnoredRadioEmitters;
nextNonIgnoredRadioEmitters = new LongOpenHashSet();

if (index + 1 == this.levels.size()) {
this.levels.add(new Level(new LongArrayList(), new LongArrayList()));
}
Expand All @@ -151,14 +161,31 @@ public void updateClient(World world) {
nextLevel.active().clear();
for (long l : nextLevel.inactive()) if (RadioStorage.RADIO_EMITTERS.contains(l)) {
nextLevel.active().add(l);
nextNonIgnoredRadioEmitters.add(l);
}
nextLevel.inactive().removeAll(nextLevel.active());

for (long emId : level.active()) {
if (emitters.isLoaded(emId)) {
HashSet<RadioMetadata> overriddenRadioSources;

var emitter = emitters.getEmitter(emId);
if (emitter instanceof RadioStorage.RadioEmitter radioEmitter && radioEmitter.isRadio())
continue;
if (emitter instanceof RadioStorage.RadioEmitter radioEmitter && radioEmitter.isRadio()) {
if (nonIgnoredRadioEmitters.contains(emId)) {
nextNonIgnoredRadioEmitters.remove(emId);
var sources = backupSources.get(radioEmitter.channel);

if (sources != null) {
radioSources.put(radioEmitter.channel, new HashSet<>(sources));
}

overriddenRadioSources = sources;
} else {
continue;
}
} else {
overriddenRadioSources = null;
}

if (emitter instanceof RadioDevice.Transmitter radioTransmitter) {
int channel = radioTransmitter.getChannel();
Expand All @@ -169,26 +196,40 @@ public void updateClient(World world) {

final int searchUntil = index;
RadioLongConsumer[] consumerRef = new RadioLongConsumer[1];
LongSet finalNextNonIgnoredRadioEmitters = nextNonIgnoredRadioEmitters;

int[] depth = new int[] {0};

RadioLongConsumer consumer = (child, metadata) -> {
if (emitters.isLoaded(child) && emitters.getEmitter(child) instanceof RadioStorage.RadioEmitter radioEmitter && radioEmitter.isRadio()) {
depth[0]++;
radioEmitter.forEachChild(consumerRef[0]);
depth[0]--;
}

if (this.contains(child, searchUntil)) {
return;
}

if (metadata != null && emitter instanceof RadioDevice radioDevice) {
RadioMetadata emitterMetadata = radioDevice.getMetadata();
if (!emitterMetadata.shouldTransmitTo(metadata)) {
return;
if (metadata != null) {
if (emitter instanceof RadioDevice radioDevice) {
RadioMetadata emitterMetadata = radioDevice.getMetadata();
if (!emitterMetadata.shouldTransmitTo(metadata)) {
return;
}
}
if (depth[0] == 0 && emitter instanceof RadioStorage.RadioEmitter && overriddenRadioSources != null) {
if (overriddenRadioSources.stream().noneMatch(source -> source.shouldTransmitTo(metadata))) {
return;
}
}
}

nextLevel.inactive().rem(child);

if (!nextLevel.active().contains(child)) {
nextLevel.active().add(child);
finalNextNonIgnoredRadioEmitters.remove(child);
}
};
consumerRef[0] = consumer;
Expand Down Expand Up @@ -245,9 +286,11 @@ public void accept(SoundSource soundSource) {
public void forEachSource(World world, Consumer<SoundSource> action) {
var emitters = SoundEmitterStorage.getInstance(world);

LongSet deduplication = new LongOpenHashSet();

for (var level : this.levels)
for (long em : level.active) {
if (emitters.isLoaded(em)) {
if (deduplication.add(em) && emitters.isLoaded(em)) {
var emitter = emitters.getEmitter(em);
emitter.forEachSource(emitter instanceof RadioStorage.RadioEmitter radioEmitter && radioEmitter.isRadio()
? new SmartSoundSourceConsumer(action, radioEmitter.channel)
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/assets/phonos/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"itemGroup.phonos.phonos": "Phonos",

"container.phonos.satellite_station": "Satellite Station",
"container.phonos.satellite_station": "Satellite Broadcast Station",

"container.phonos.ender_music_box": "Ender Music Box",
"hint.phonos.ender_music_box.drag": "+ Drag and drop .ogg file to upload",
Expand Down Expand Up @@ -48,7 +48,7 @@
"block.phonos.radio_transceiver": "Radio Transceiver",
"block.phonos.satellite_receiver": "Satellite Receiver",
"block.phonos.radio_loudspeaker": "Radio Loudspeaker",
"block.phonos.satellite_station": "Satellite Station",
"block.phonos.satellite_station": "Satellite Broadcast Station",
"block.phonos.audio_switch": "Audio Switch",
"block.phonos.ender_music_box": "Ender Music Box",
"block.phonos.microphone_base": "Microphone Base",
Expand Down
Loading

0 comments on commit 99b0fee

Please sign in to comment.