Advertisement
Guest User

ProjectZomboid(40.43) DeviceData.class

a guest
Oct 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 32.88 KB | None | 0 0
  1. package zombie.radio.devices;
  2.  
  3. import java.io.IOException;
  4. import java.nio.ByteBuffer;
  5. import java.util.ArrayList;
  6. import java.util.Map;
  7. import zombie.GameTime;
  8. import zombie.GameWindow;
  9. import zombie.SandboxOptions;
  10. import zombie.WorldSoundManager;
  11. import zombie.audio.BaseSoundEmitter;
  12. import zombie.characters.IsoPlayer;
  13. import zombie.core.Rand;
  14. import zombie.core.network.ByteBufferWriter;
  15. import zombie.core.raknet.UdpConnection;
  16. import zombie.inventory.InventoryItem;
  17. import zombie.inventory.InventoryItemFactory;
  18. import zombie.inventory.ItemContainer;
  19. import zombie.inventory.types.DrainableComboItem;
  20. import zombie.inventory.types.Radio;
  21. import zombie.iso.IsoGridSquare;
  22. import zombie.iso.IsoObject;
  23. import zombie.iso.IsoWorld;
  24. import zombie.iso.objects.IsoWaveSignal;
  25. import zombie.network.GameClient;
  26. import zombie.network.GameServer;
  27. import zombie.network.PacketTypesShort;
  28. import zombie.radio.ZomboidRadio;
  29. import zombie.vehicles.BaseVehicle;
  30. import zombie.vehicles.VehiclePart;
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. public class DeviceData
  43.   implements Cloneable
  44. {
  45.   private static final float deviceSpeakerSoundMod = 0.4F;
  46.   private static final float deviceButtonSoundVol = 0.05F;
  47.   protected String deviceName;
  48.   protected boolean twoWay;
  49.   protected int transmitRange;
  50.   protected int micRange;
  51.   protected boolean micIsMuted;
  52.   protected float baseVolumeRange;
  53.   protected float deviceVolume;
  54.   protected boolean isPortable;
  55.   protected boolean isTelevision;
  56.   protected boolean isHighTier;
  57.   protected boolean isTurnedOn;
  58.   protected int channel;
  59.   protected int minChannelRange;
  60.   protected int maxChannelRange;
  61.   protected DevicePresets presets;
  62.   protected boolean isBatteryPowered;
  63.   protected boolean hasBattery;
  64.   protected float powerDelta;
  65.   protected float useDelta;
  66.   protected int lastRecordedDistance;
  67.   protected int headphoneType;
  68.   protected WaveSignalDevice parent;
  69.   protected GameTime gameTime;
  70.   protected boolean channelChangedRecently;
  71.   protected BaseSoundEmitter emitter;
  72.   protected ArrayList<Long> soundIDs;
  73.   private float soundCounterStatic;
  74.   protected long radioLoopSound;
  75.   protected boolean doTriggerWorldSound;
  76.   protected long lastMinuteStamp;
  77.   protected int listenCnt;
  78.   float nextStaticSound;
  79.   protected float signalCounter;
  80.   protected float soundCounter;
  81.  
  82.   public DeviceData() { this(null); }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.   public void generatePresets() {
  93.     if (this.presets == null) {
  94.       this.presets = new DevicePresets();
  95.     }
  96.     this.presets.clearPresets();
  97.     if (this.isTelevision) {
  98.       Map map = ZomboidRadio.getInstance().GetChannelList("Television");
  99.       if (map != null) {
  100.         for (Map.Entry entry : map.entrySet()) {
  101.           if (((Integer)entry.getKey()).intValue() >= this.minChannelRange && ((Integer)entry.getKey()).intValue() <= this.maxChannelRange) {
  102.             this.presets.addPreset((String)entry.getValue(), ((Integer)entry.getKey()).intValue());
  103.           }
  104.         }
  105.       }
  106.     } else {
  107.       byte b = this.twoWay ? 100 : 800;
  108.       Map map = ZomboidRadio.getInstance().GetChannelList("Radio");
  109.       if (map != null) {
  110.         for (Map.Entry entry : map.entrySet()) {
  111.           if (((Integer)entry.getKey()).intValue() >= this.minChannelRange && ((Integer)entry.getKey()).intValue() <= this.maxChannelRange && Rand.Next(1000) < b) {
  112.             this.presets.addPreset((String)entry.getValue(), ((Integer)entry.getKey()).intValue());
  113.           }
  114.         }
  115.       }
  116.       if (this.twoWay) {
  117.         map = ZomboidRadio.getInstance().GetChannelList("Amateur");
  118.         if (map != null) {
  119.           for (Map.Entry entry : map.entrySet()) {
  120.             if (((Integer)entry.getKey()).intValue() >= this.minChannelRange && ((Integer)entry.getKey()).intValue() <= this.maxChannelRange && Rand.Next(1000) < b) {
  121.               this.presets.addPreset((String)entry.getValue(), ((Integer)entry.getKey()).intValue());
  122.             }
  123.           }
  124.         }
  125.       }
  126.       if (this.isHighTier) {
  127.         map = ZomboidRadio.getInstance().GetChannelList("Military");
  128.         if (map != null) {
  129.           for (Map.Entry entry : map.entrySet()) {
  130.             if (((Integer)entry.getKey()).intValue() >= this.minChannelRange && ((Integer)entry.getKey()).intValue() <= this.maxChannelRange && Rand.Next(1000) < 10) {
  131.               this.presets.addPreset((String)entry.getValue(), ((Integer)entry.getKey()).intValue());
  132.             }
  133.           }
  134.         }
  135.       }
  136.     }
  137.   }
  138.  
  139.   protected Object clone() throws CloneNotSupportedException {
  140.     DeviceData deviceData = (DeviceData)super.clone();
  141.     deviceData.setDevicePresets((DevicePresets)this.presets.clone());
  142.     deviceData.setParent(null);
  143.     return deviceData;
  144.   }
  145.  
  146.  
  147.   public DeviceData getClone() {
  148.     DeviceData deviceData;
  149.     try {
  150.       deviceData = (DeviceData)clone();
  151.     } catch (Exception exception) {
  152.       System.out.println(exception.getMessage());
  153.       deviceData = new DeviceData();
  154.     }
  155.     return deviceData;
  156.   }
  157.  
  158.  
  159.   public WaveSignalDevice getParent() { return this.parent; }
  160.  
  161.  
  162.   public void setParent(WaveSignalDevice paramWaveSignalDevice) { this.parent = paramWaveSignalDevice; }
  163.  
  164.  
  165.  
  166.   public DevicePresets getDevicePresets() { return this.presets; }
  167.  
  168.   public void setDevicePresets(DevicePresets paramDevicePresets) {
  169.     if (paramDevicePresets == null) {
  170.       paramDevicePresets = new DevicePresets();
  171.     }
  172.     this.presets = paramDevicePresets;
  173.   }
  174.  
  175.  
  176.   public int getMinChannelRange() { return this.minChannelRange; }
  177.  
  178.   public void setMinChannelRange(int paramInt) { this.minChannelRange = (paramInt >= 200 && paramInt <= 1000000) ? paramInt : 200; }
  179.  
  180.   public int getMaxChannelRange() { return this.maxChannelRange; }
  181.  
  182.   public void setMaxChannelRange(int paramInt) { this.maxChannelRange = (paramInt >= 200 && paramInt <= 1000000) ? paramInt : 1000000; }
  183.  
  184.  
  185.   public boolean getIsHighTier() { return this.isHighTier; }
  186.  
  187.   public void setIsHighTier(boolean paramBoolean) { this.isHighTier = paramBoolean; }
  188.  
  189.  
  190.   public boolean getIsBatteryPowered() { return this.isBatteryPowered; }
  191.  
  192.   public void setIsBatteryPowered(boolean paramBoolean) { this.isBatteryPowered = paramBoolean; }
  193.  
  194.   public boolean getHasBattery() { return this.hasBattery; }
  195.  
  196.   public void setHasBattery(boolean paramBoolean) { this.hasBattery = paramBoolean; }
  197.  
  198.   public void addBattery(DrainableComboItem paramDrainableComboItem) {
  199.     if (!this.hasBattery && paramDrainableComboItem != null && paramDrainableComboItem.getFullType().equals("Base.Battery")) {
  200.       ItemContainer itemContainer = paramDrainableComboItem.getContainer();
  201.       if (itemContainer != null) {
  202.         if (itemContainer.getType().equals("floor") && paramDrainableComboItem.getWorldItem() != null && paramDrainableComboItem.getWorldItem().getSquare() != null) {
  203.           paramDrainableComboItem.getWorldItem().getSquare().transmitRemoveItemFromSquare(paramDrainableComboItem.getWorldItem());
  204.           paramDrainableComboItem.getWorldItem().getSquare().getWorldObjects().remove(paramDrainableComboItem.getWorldItem());
  205.           (paramDrainableComboItem.getWorldItem().getSquare()).chunk.recalcHashCodeObjects();
  206.           paramDrainableComboItem.getWorldItem().getSquare().getObjects().remove(paramDrainableComboItem.getWorldItem());
  207.           paramDrainableComboItem.setWorldItem(null);
  208.         }
  209.         this.powerDelta = paramDrainableComboItem.getDelta();
  210.         itemContainer.DoRemoveItem(paramDrainableComboItem);
  211.         this.hasBattery = true;
  212.         transmitDeviceDataState((byte)2);
  213.       }
  214.     }
  215.   }
  216.  
  217.   public InventoryItem getBattery(ItemContainer paramItemContainer) {
  218.     if (this.hasBattery) {
  219.       DrainableComboItem drainableComboItem = (DrainableComboItem)InventoryItemFactory.CreateItem("Base.Battery");
  220.       drainableComboItem.setDelta(this.powerDelta);
  221.       this.powerDelta = 0.0F;
  222.       paramItemContainer.AddItem(drainableComboItem);
  223.       this.hasBattery = false;
  224.       transmitDeviceDataState((byte)2);
  225.       return drainableComboItem;
  226.     }
  227.     return null;
  228.   }
  229.  
  230.  
  231.  
  232.   public void transmitBattryChange() { transmitDeviceDataState((byte)2); }
  233.  
  234.  
  235.   public void addHeadphones(InventoryItem paramInventoryItem) {
  236.     if (this.headphoneType < 0 && (paramInventoryItem.getFullType().equals("Base.Headphones") || paramInventoryItem.getFullType().equals("Base.Earbuds"))) {
  237.       ItemContainer itemContainer = paramInventoryItem.getContainer();
  238.       if (itemContainer != null) {
  239.         if (itemContainer.getType().equals("floor") && paramInventoryItem.getWorldItem() != null && paramInventoryItem.getWorldItem().getSquare() != null) {
  240.           paramInventoryItem.getWorldItem().getSquare().transmitRemoveItemFromSquare(paramInventoryItem.getWorldItem());
  241.           paramInventoryItem.getWorldItem().getSquare().getWorldObjects().remove(paramInventoryItem.getWorldItem());
  242.           (paramInventoryItem.getWorldItem().getSquare()).chunk.recalcHashCodeObjects();
  243.           paramInventoryItem.getWorldItem().getSquare().getObjects().remove(paramInventoryItem.getWorldItem());
  244.           paramInventoryItem.setWorldItem(null);
  245.         }
  246.         byte b = paramInventoryItem.getFullType().equals("Base.Headphones") ? 0 : 1;
  247.         itemContainer.DoRemoveItem(paramInventoryItem);
  248.         setHeadphoneType(b);
  249.         transmitDeviceDataState((byte)6);
  250.       }
  251.     }
  252.   }
  253.  
  254.  
  255.  
  256.   public InventoryItem getHeadphones(ItemContainer paramItemContainer) {
  257.     if (this.headphoneType >= 0) {
  258.       InventoryItem inventoryItem = null;
  259.       if (this.headphoneType == 0) {
  260.         inventoryItem = InventoryItemFactory.CreateItem("Base.Headphones");
  261.       } else if (this.headphoneType == 1) {
  262.         inventoryItem = InventoryItemFactory.CreateItem("Base.Earbuds");
  263.       }
  264.       if (inventoryItem != null) {
  265.         paramItemContainer.AddItem(inventoryItem);
  266.       }
  267.       setHeadphoneType(-1);
  268.       transmitDeviceDataState((byte)6);
  269.     }
  270.     return null;
  271.   }
  272.  
  273.  
  274.   public int getMicRange() { return this.micRange; }
  275.  
  276.  
  277.   public void setMicRange(int paramInt) { this.micRange = paramInt; }
  278.  
  279.   public boolean getMicIsMuted() { return this.micIsMuted; }
  280.  
  281.  
  282.   public void setMicIsMuted(boolean paramBoolean) { this.micIsMuted = paramBoolean; }
  283.  
  284.  
  285.   public DeviceData(WaveSignalDevice paramWaveSignalDevice) {
  286.     this.deviceName = "WaveSignalDevice";
  287.     this.twoWay = false;
  288.     this.transmitRange = 1000;
  289.     this.micRange = 5;
  290.     this.micIsMuted = false;
  291.     this.baseVolumeRange = 15.0F;
  292.     this.deviceVolume = 1.0F;
  293.     this.isPortable = false;
  294.     this.isTelevision = false;
  295.     this.isHighTier = false;
  296.     this.isTurnedOn = false;
  297.     this.channel = 88000;
  298.     this.minChannelRange = 200;
  299.     this.maxChannelRange = 1000000;
  300.     this.presets = null;
  301.     this.isBatteryPowered = true;
  302.     this.hasBattery = true;
  303.     this.powerDelta = 1.0F;
  304.     this.useDelta = 0.001F;
  305.     this.lastRecordedDistance = -1;
  306.     this.headphoneType = -1;
  307.     this.parent = null;
  308.     this.gameTime = null;
  309.     this.channelChangedRecently = false;
  310.     this.emitter = null;
  311.     this.soundIDs = new ArrayList();
  312.     this.soundCounterStatic = 0.0F;
  313.     this.radioLoopSound = 0L;
  314.  
  315.  
  316.    
  317.     this.doTriggerWorldSound = false;
  318.     this.lastMinuteStamp = -1L;
  319.     this.listenCnt = 0;
  320.  
  321.  
  322.     this.nextStaticSound = 0.0F;
  323.  
  324.    
  325.     this.signalCounter = 0.0F;
  326.     this.soundCounter = 0.0F; this.parent = paramWaveSignalDevice; this.presets = new DevicePresets(); this.gameTime = GameTime.getInstance();
  327.   } public int getHeadphoneType() { return this.headphoneType; } public void setHeadphoneType(int paramInt) { this.headphoneType = paramInt; } public float getBaseVolumeRange() { return this.baseVolumeRange; } public void setBaseVolumeRange(float paramFloat) { this.baseVolumeRange = paramFloat; } public float getDeviceVolume() { return this.deviceVolume; } public void setDeviceVolume(float paramFloat) { this.deviceVolume = (paramFloat < 0.0F) ? 0.0F : ((paramFloat > 1.0F) ? 1.0F : paramFloat);
  328.     transmitDeviceDataState((byte)4); } public void setDeviceVolumeRaw(float paramFloat) { this.deviceVolume = (paramFloat < 0.0F) ? 0.0F : ((paramFloat > 1.0F) ? 1.0F : paramFloat); } public boolean getIsTelevision() { return this.isTelevision; } public void doReceiveSignal(int paramInt) { if (this.isTurnedOn)
  329.     { this.lastRecordedDistance = paramInt;
  330.  
  331.  
  332.      
  333.       this.signalCounter = 300.0F;
  334.       this.doTriggerWorldSound = true;
  335.       setNextStaticSound(); }  } public void setIsTelevision(boolean paramBoolean) { this.isTelevision = paramBoolean; } public String getDeviceName() { return this.deviceName; } public void setDeviceName(String paramString) { this.deviceName = paramString; } public boolean getIsTwoWay() { return this.twoWay; } public void setIsTwoWay(boolean paramBoolean) { this.twoWay = paramBoolean; } public int getTransmitRange() { return this.transmitRange; } public void setTransmitRange(int paramInt) { this.transmitRange = (paramInt > 0) ? paramInt : 0; } public boolean getIsPortable() { return this.isPortable; } public void setIsPortable(boolean paramBoolean) { this.isPortable = paramBoolean; } public boolean getIsTurnedOn() { return this.isTurnedOn; } public void setIsTurnedOn(boolean paramBoolean) { if (canBePoweredHere()) { if (!this.isBatteryPowered || this.powerDelta > 0.0F) { this.isTurnedOn = paramBoolean; } else { this.isTurnedOn = false; }  playSoundSend("RadioButton", false); transmitDeviceDataState((byte)0); } else if (this.isTurnedOn) { this.isTurnedOn = false; playSoundSend("RadioButton", false); transmitDeviceDataState((byte)0); }  } public void setTurnedOnRaw(boolean paramBoolean) { this.isTurnedOn = paramBoolean; } public boolean canBePoweredHere() { if (this.isBatteryPowered == true) return true;  if (this.parent instanceof VehiclePart) { VehiclePart vehiclePart = (VehiclePart)this.parent; if (vehiclePart.getItemType() != null && !vehiclePart.getItemType().isEmpty() && vehiclePart.getInventoryItem() == null) return false;  return vehiclePart.hasDevicePower(); }  boolean bool = false; if (GameTime.getInstance().getNightsSurvived() < SandboxOptions.instance.getElecShutModifier()) bool = true;  if (this.parent == null || this.parent.getSquare() == null) { bool = false; } else if (this.parent.getSquare().haveElectricity()) { bool = true; } else if (this.parent.getSquare().getRoom() == null) { bool = false; }  return bool; } public void setRandomChannel() { if (this.presets != null && this.presets.getPresets().size() > 0) { int i = Rand.Next(0, this.presets.getPresets().size()); this.channel = ((PresetEntry)this.presets.getPresets().get(i)).getFrequency(); } else { this.channel = Rand.Next(this.minChannelRange, this.maxChannelRange); this.channel -= this.channel % 200; }  }
  336.   public int getChannel() { return this.channel; }
  337.   public void setChannel(int paramInt) { setChannel(paramInt, true); }
  338.   public void setChannel(int paramInt, boolean paramBoolean) { if (paramInt >= this.minChannelRange && paramInt <= this.maxChannelRange) { this.channel = paramInt; playSoundSend("RadioButton", false); if (this.isTelevision) { playSoundSend("TelevisionZap", true); } else { playSoundSend("RadioZap", true); }  if (this.radioLoopSound > 0L) { this.emitter.stopSound(this.radioLoopSound); this.radioLoopSound = 0L; }  transmitDeviceDataState((byte)1); if (paramBoolean)
  339.         TriggerPlayerListening(true);  }  }
  340.   public boolean isReceivingSignal() { return (this.signalCounter > 0.0F); }
  341.   public void setChannelRaw(int paramInt) { this.channel = paramInt; }
  342.   public float getUseDelta() { return this.useDelta; }
  343.   public void setUseDelta(float paramFloat) { this.useDelta = paramFloat / 60.0F; }
  344.   public float getPower() { return this.powerDelta; } public void setPower(float paramFloat) { if (paramFloat > 1.0F) paramFloat = 1.0F;  if (paramFloat < 0.0F) paramFloat = 0.0F;  this.powerDelta = paramFloat; } public void TriggerPlayerListening(boolean paramBoolean) { if (this.isTurnedOn) ZomboidRadio.getInstance().PlayerListensChannel(this.channel, true, this.isTelevision);  } public void playSoundSend(String paramString, boolean paramBoolean) { playSound(paramString, paramBoolean ? (this.deviceVolume * 0.4F) : 0.05F, true); } public void playSoundLocal(String paramString, boolean paramBoolean) { playSound(paramString, paramBoolean ? (this.deviceVolume * 0.4F) : 0.05F, false); } public void playSound(String paramString, float paramFloat, boolean paramBoolean) { if (GameServer.bServer) return;  setEmitterAndPos(); if (this.emitter != null) { long l = paramBoolean ? this.emitter.playSound(paramString) : this.emitter.playSoundImpl(paramString, false, null); this.emitter.setVolume(l, paramFloat); }  } public void cleanSoundsAndEmitter() { if (this.emitter != null) { this.emitter.stopAll(); IsoWorld.instance.freeEmitters.add(this.emitter); this.emitter = null; this.radioLoopSound = 0L; }  } protected void setEmitterAndPos() { IsoPlayer isoPlayer = null; if (this.parent != null && this.parent instanceof IsoObject) { isoPlayer = (IsoObject)this.parent; } else if (this.parent != null && this.parent instanceof Radio) { isoPlayer = IsoPlayer.getInstance(); }  if (isoPlayer != null) { if (this.emitter == null) { this.emitter = IsoWorld.instance.getFreeEmitter(isoPlayer.getX() + 0.5F, isoPlayer.getY() + 0.5F, (int)isoPlayer.getZ()); IsoWorld.instance.currentEmitters.remove(this.emitter); } else { this.emitter.setPos(isoPlayer.getX() + 0.5F, isoPlayer.getY() + 0.5F, (int)isoPlayer.getZ()); }  if (this.radioLoopSound != 0L) this.emitter.setVolume(this.radioLoopSound, this.deviceVolume * 0.4F);  }  } public int getLastRecordedDistance() { return this.lastRecordedDistance; }
  345.   protected void updateEmitter() { if (GameServer.bServer) return;  if (!this.isTurnedOn) { if (this.emitter != null && this.emitter.isPlaying("RadioButton")) { if (this.radioLoopSound > 0L) this.emitter.stopSound(this.radioLoopSound);  setEmitterAndPos(); this.emitter.tick(); return; }  cleanSoundsAndEmitter(); return; }  setEmitterAndPos(); if (this.emitter != null) { if (this.signalCounter > 0.0F && !this.emitter.isPlaying("RadioTalk")) { if (this.radioLoopSound > 0L) this.emitter.stopSound(this.radioLoopSound);  this.radioLoopSound = this.emitter.playSoundImpl("RadioTalk", false, null); this.emitter.setVolume(this.radioLoopSound, this.deviceVolume * 0.4F); }  String str = !this.isTelevision ? "RadioStatic" : "TelevisionTestBeep"; if (this.radioLoopSound == 0L || (this.signalCounter <= 0.0F && !this.emitter.isPlaying(str))) { if (this.radioLoopSound > 0L) { this.emitter.stopSound(this.radioLoopSound); if (this.isTelevision) { playSoundLocal("TelevisionZap", true); } else { playSoundLocal("RadioZap", true); }  }  this.radioLoopSound = this.emitter.playSoundImpl(str, false, null); this.emitter.setVolume(this.radioLoopSound, this.deviceVolume * 0.4F); }  this.emitter.tick(); }  }
  346.   public void update(boolean paramBoolean1, boolean paramBoolean2) { if (this.lastMinuteStamp == -1L) this.lastMinuteStamp = this.gameTime.getMinutesStamp();  if (this.gameTime.getMinutesStamp() > this.lastMinuteStamp) { long l = this.gameTime.getMinutesStamp() - this.lastMinuteStamp; this.lastMinuteStamp = this.gameTime.getMinutesStamp(); this.listenCnt = (int)(this.listenCnt + l); if (this.listenCnt >= 10)
  347.         this.listenCnt = 0;  if (!GameServer.bServer && this.isTurnedOn && paramBoolean2 && (this.listenCnt == 0 || this.listenCnt == 5))
  348.         TriggerPlayerListening(true);  if (this.isTurnedOn && this.isBatteryPowered && this.powerDelta > 0.0F) { float f = this.powerDelta - this.powerDelta % 0.01F; setPower(this.powerDelta - this.useDelta * (float)l); if (this.listenCnt == 0 || this.powerDelta == 0.0F || this.powerDelta < f)
  349.           if (paramBoolean1 && GameServer.bServer) { transmitDeviceDataStateServer((byte)3, null); } else if (!paramBoolean1 && GameClient.bClient) { transmitDeviceDataState((byte)3); }   }  }  if (this.isTurnedOn && ((this.isBatteryPowered && this.powerDelta <= 0.0F) || !canBePoweredHere())) { this.isTurnedOn = false; if (paramBoolean1 && GameServer.bServer) { transmitDeviceDataStateServer((byte)0, null); } else if (!paramBoolean1 && GameClient.bClient) { transmitDeviceDataState((byte)0); }  }  updateEmitter(); updateSimple(); } public boolean isIsoDevice() { return (getParent() != null && getParent() instanceof IsoWaveSignal); } public void updateSimple() { if (this.signalCounter >= 0.0F) this.signalCounter -= 1.25F * GameTime.getInstance().getMultiplier();  if (this.soundCounter >= 0.0F) this.soundCounter = (float)(this.soundCounter - 1.25D * GameTime.getInstance().getMultiplier());  if (this.signalCounter <= 0.0F && this.lastRecordedDistance >= 0) this.lastRecordedDistance = -1;  updateStaticSounds(); if (GameClient.bClient) updateEmitter();  if (this.doTriggerWorldSound && this.soundCounter <= 0.0F) { if (this.isTurnedOn && this.deviceVolume > 0.0F && (!isInventoryDevice() || this.headphoneType < 0) && ((!GameClient.bClient && !GameServer.bServer) || (GameClient.bClient && isInventoryDevice()) || (GameServer.bServer && !isInventoryDevice()))) { BaseVehicle baseVehicle = null; if (this.parent != null && this.parent instanceof IsoObject) { baseVehicle = (IsoObject)this.parent; } else if (this.parent != null && this.parent instanceof Radio) { IsoPlayer isoPlayer = IsoPlayer.getInstance(); } else if (this.parent instanceof VehiclePart) { baseVehicle = ((VehiclePart)this.parent).getVehicle(); }  if (baseVehicle != null) { int i = (int)(100.0F * this.deviceVolume); int j = getDeviceSoundVolumeRange(); WorldSoundManager.instance.addSound(baseVehicle, (int)baseVehicle.getX(), (int)baseVehicle.getY(), (int)baseVehicle.getZ(), j, i, (i > 50)); }  }  this.doTriggerWorldSound = false; this.soundCounter = (300 + Rand.Next(0, 300)); }  } private void updateStaticSounds() { if (!this.isTurnedOn) return;  float f = GameTime.getInstance().getMultiplier(); this.nextStaticSound -= f; if (this.nextStaticSound <= 0.0F) { if (this.parent != null) { this.parent.AddDeviceText(ZomboidRadio.getInstance().getRandomBzztFzzt(), 1.0F, 1.0F, 1.0F, null, -1); this.doTriggerWorldSound = true; }  setNextStaticSound(); }  } private void setNextStaticSound() { this.nextStaticSound = Rand.Next(250.0F, 1500.0F); }
  350.   public int getDeviceVolumeRange() { return 5 + (int)(this.baseVolumeRange * this.deviceVolume); }
  351.   public int getDeviceSoundVolumeRange() { if (isInventoryDevice()) { Radio radio = (Radio)getParent(); if (radio.getPlayer() != null && radio.getPlayer().getSquare() != null && radio.getPlayer().getSquare().getRoom() != null) return 3 + (int)(this.baseVolumeRange * 0.4F * this.deviceVolume);  return 5 + (int)(this.baseVolumeRange * this.deviceVolume); }  if (isIsoDevice()) { IsoWaveSignal isoWaveSignal = (IsoWaveSignal)getParent(); if (isoWaveSignal.getSquare() != null && isoWaveSignal.getSquare().getRoom() != null) return 3 + (int)(this.baseVolumeRange * 0.5F * this.deviceVolume);  return 5 + (int)(this.baseVolumeRange * 0.75F * this.deviceVolume); }  return 5 + (int)(this.baseVolumeRange / 2.0F * this.deviceVolume); }
  352.   public boolean isInventoryDevice() { return (getParent() != null && getParent() instanceof Radio); }
  353.  
  354.  
  355.   public boolean isVehicleDevice() { return getParent() instanceof VehiclePart; }
  356.  
  357.  
  358.  
  359.   public void transmitPresets() { transmitDeviceDataState((byte)5); }
  360.  
  361.  
  362.  
  363.   private void transmitDeviceDataState(byte paramByte) {
  364.     if (GameClient.bClient) {
  365.      
  366.       try {
  367.         sendDeviceDataStatePacket(GameClient.connection, paramByte);
  368.       }
  369.       catch (Exception exception) {
  370.         System.out.print(exception.getMessage());
  371.       }
  372.     }
  373.   }
  374.  
  375.  
  376.   private void transmitDeviceDataStateServer(byte paramByte, UdpConnection paramUdpConnection) {
  377.     if (GameServer.bServer) {
  378.      
  379.       try {
  380.  
  381.  
  382.        
  383.         for (byte b = 0; b < GameServer.udpEngine.connections.size(); b++) {
  384.           UdpConnection udpConnection = (UdpConnection)GameServer.udpEngine.connections.get(b);
  385.           if (paramUdpConnection == null || paramUdpConnection != udpConnection)
  386.           {
  387.             sendDeviceDataStatePacket(udpConnection, paramByte);
  388.           }
  389.         }
  390.      
  391.       } catch (Exception exception) {
  392.         System.out.print(exception.getMessage());
  393.       }
  394.     }
  395.   }
  396.  
  397.   private void sendDeviceDataStatePacket(UdpConnection paramUdpConnection, byte paramByte) throws IOException
  398.   {
  399.     ByteBufferWriter byteBufferWriter = paramUdpConnection.startPacket();
  400.     PacketTypesShort.doPacket((short)1004, byteBufferWriter);
  401.    
  402.     boolean bool = false;
  403.     if (isIsoDevice()) {
  404.       IsoWaveSignal isoWaveSignal = (IsoWaveSignal)getParent();
  405.       IsoGridSquare isoGridSquare = isoWaveSignal.getSquare();
  406.       if (isoGridSquare != null) {
  407.         byteBufferWriter.putByte((byte)1);
  408.         byteBufferWriter.putInt(isoGridSquare.getX());
  409.         byteBufferWriter.putInt(isoGridSquare.getY());
  410.         byteBufferWriter.putInt(isoGridSquare.getZ());
  411.         byteBufferWriter.putInt(isoGridSquare.getObjects().indexOf(isoWaveSignal));
  412.         bool = true;
  413.       }
  414.     } else if (isInventoryDevice()) {
  415.      
  416.       Radio radio = (Radio)getParent();
  417.  
  418.      
  419.       IsoPlayer isoPlayer = null;
  420.       if (radio.getEquipParent() != null && radio.getEquipParent() instanceof IsoPlayer)
  421.         isoPlayer = (IsoPlayer)radio.getEquipParent();
  422.       if (isoPlayer != null) {
  423.        
  424.         byteBufferWriter.putByte((byte)0);
  425.         if (GameServer.bServer) {
  426.          
  427.           byteBufferWriter.putInt((isoPlayer != null) ? isoPlayer.OnlineID : -1);
  428.         } else {
  429.          
  430.           byteBufferWriter.putByte((byte)isoPlayer.PlayerIndex);
  431.         }
  432.         if (isoPlayer.getPrimaryHandItem() == radio) {
  433.          
  434.           byteBufferWriter.putByte((byte)1);
  435.         } else if (isoPlayer.getSecondaryHandItem() == radio) {
  436.          
  437.           byteBufferWriter.putByte((byte)2);
  438.         } else {
  439.          
  440.           byteBufferWriter.putByte((byte)0);
  441.         }
  442.         bool = true;
  443.       }
  444.     } else if (isVehicleDevice()) {
  445.       VehiclePart vehiclePart = (VehiclePart)getParent();
  446.       byteBufferWriter.putByte((byte)2);
  447.       byteBufferWriter.putShort((vehiclePart.getVehicle()).VehicleID);
  448.       byteBufferWriter.putShort((short)vehiclePart.getIndex());
  449.       bool = true;
  450.     }
  451.    
  452.     if (bool) {
  453.       byteBufferWriter.putByte(paramByte);
  454.       switch (paramByte) {
  455.         case 0:
  456.           byteBufferWriter.putByte(this.isTurnedOn ? 1 : 0);
  457.           break;
  458.         case 1:
  459.           byteBufferWriter.putInt(this.channel);
  460.           break;
  461.         case 2:
  462.           byteBufferWriter.putByte(this.hasBattery ? 1 : 0);
  463.           byteBufferWriter.putFloat(this.powerDelta);
  464.           break;
  465.         case 3:
  466.           byteBufferWriter.putFloat(this.powerDelta);
  467.           break;
  468.         case 4:
  469.           byteBufferWriter.putFloat(this.deviceVolume);
  470.           break;
  471.        
  472.         case 5:
  473.           byteBufferWriter.putInt(this.presets.getPresets().size());
  474.           for (PresetEntry presetEntry : this.presets.getPresets()) {
  475.             GameWindow.WriteString(byteBufferWriter.bb, presetEntry.getName());
  476.             byteBufferWriter.putInt(presetEntry.getFrequency());
  477.           }
  478.           break;
  479.         case 6:
  480.           byteBufferWriter.putInt(this.headphoneType);
  481.           break;
  482.       }
  483.       paramUdpConnection.endPacketImmediate();
  484.     } else {
  485.       paramUdpConnection.cancelPacket();
  486.     }  } public void receiveDeviceDataStatePacket(ByteBuffer paramByteBuffer, UdpConnection paramUdpConnection) throws IOException { byte b1;
  487.     int j;
  488.     float f3, f2, f1;
  489.     boolean bool2;
  490.     int i;
  491.     if (!GameClient.bClient && !GameServer.bServer) {
  492.       return;
  493.     }
  494.     boolean bool = GameServer.bServer;
  495.     boolean bool1 = (isIsoDevice() || isVehicleDevice()) ? 1 : 0;
  496.     byte b = paramByteBuffer.get();
  497.     switch (b) {
  498.       case 0:
  499.         if (bool && bool1) {
  500.           setIsTurnedOn((paramByteBuffer.get() == 1));
  501.         } else {
  502.           this.isTurnedOn = (paramByteBuffer.get() == 1);
  503.         }
  504.         if (bool) {
  505.           transmitDeviceDataStateServer(b, !bool1 ? paramUdpConnection : null);
  506.         }
  507.         break;
  508.       case 1:
  509.         i = paramByteBuffer.getInt();
  510.         if (bool && bool1) {
  511.           setChannel(i);
  512.         } else {
  513.           this.channel = i;
  514.         }
  515.         if (bool) {
  516.           transmitDeviceDataStateServer(b, !bool1 ? paramUdpConnection : null);
  517.         }
  518.         break;
  519.      
  520.       case 2:
  521.         bool2 = (paramByteBuffer.get() == 1) ? 1 : 0;
  522.         f1 = paramByteBuffer.getFloat();
  523.         if (bool && bool1) {
  524.           this.hasBattery = bool2;
  525.           setPower(f1);
  526.         } else {
  527.           this.hasBattery = bool2;
  528.           this.powerDelta = f1;
  529.         }
  530.        
  531.         if (bool) {
  532.           transmitDeviceDataStateServer(b, !bool1 ? paramUdpConnection : null);
  533.         }
  534.         break;
  535.      
  536.       case 3:
  537.         f2 = paramByteBuffer.getFloat();
  538.         if (bool && bool1) {
  539.           setPower(f2);
  540.         } else {
  541.           this.powerDelta = f2;
  542.         }
  543.         if (bool) {
  544.           transmitDeviceDataStateServer(b, !bool1 ? paramUdpConnection : null);
  545.         }
  546.         break;
  547.      
  548.       case 4:
  549.         f3 = paramByteBuffer.getFloat();
  550.         if (bool && bool1) {
  551.           setDeviceVolume(f3);
  552.         } else {
  553.           this.deviceVolume = f3;
  554.         }
  555.         if (bool) {
  556.           transmitDeviceDataStateServer(b, !bool1 ? paramUdpConnection : null);
  557.         }
  558.         break;
  559.      
  560.       case 5:
  561.         j = paramByteBuffer.getInt();
  562.         for (b1 = 0; b1 < j; b1++) {
  563.          
  564.           String str = GameWindow.ReadString(paramByteBuffer);
  565.           int k = paramByteBuffer.getInt();
  566.           if (b1 < this.presets.getPresets().size()) {
  567.             PresetEntry presetEntry = (PresetEntry)this.presets.getPresets().get(b1);
  568.             if (!presetEntry.getName().equals(str) || presetEntry.getFrequency() != k) {
  569.               presetEntry.setName(str);
  570.               presetEntry.setFrequency(k);
  571.             }
  572.           } else {
  573.            
  574.             this.presets.addPreset(str, k);
  575.           }
  576.         }  if (bool) {
  577.           transmitDeviceDataStateServer((byte)5, !bool1 ? paramUdpConnection : null);
  578.         }
  579.         break;
  580.  
  581.      
  582.       case 6:
  583.         this.headphoneType = paramByteBuffer.getInt();
  584.        
  585.         if (bool) {
  586.           transmitDeviceDataStateServer(b, !bool1 ? paramUdpConnection : null);
  587.         }
  588.         break;
  589.     }  }
  590.  
  591.  
  592.  
  593.   public void save(ByteBuffer paramByteBuffer, boolean paramBoolean) throws IOException {
  594.     GameWindow.WriteString(paramByteBuffer, this.deviceName);
  595.     paramByteBuffer.put(this.twoWay ? 1 : 0);
  596.     paramByteBuffer.putInt(this.transmitRange);
  597.     paramByteBuffer.putInt(this.micRange);
  598.     paramByteBuffer.put(this.micIsMuted ? 1 : 0);
  599.     paramByteBuffer.putFloat(this.baseVolumeRange);
  600.     paramByteBuffer.putFloat(this.deviceVolume);
  601.    
  602.     paramByteBuffer.put(this.isPortable ? 1 : 0);
  603.     paramByteBuffer.put(this.isTelevision ? 1 : 0);
  604.     paramByteBuffer.put(this.isHighTier ? 1 : 0);
  605.    
  606.     paramByteBuffer.put(this.isTurnedOn ? 1 : 0);
  607.     paramByteBuffer.putInt(this.channel);
  608.     paramByteBuffer.putInt(this.minChannelRange);
  609.     paramByteBuffer.putInt(this.maxChannelRange);
  610.    
  611.     paramByteBuffer.put(this.isBatteryPowered ? 1 : 0);
  612.     paramByteBuffer.put(this.hasBattery ? 1 : 0);
  613.    
  614.     paramByteBuffer.putFloat(this.powerDelta);
  615.     paramByteBuffer.putFloat(this.useDelta);
  616.    
  617.     paramByteBuffer.putInt(this.headphoneType);
  618.    
  619.     if (this.presets != null) {
  620.       paramByteBuffer.put((byte)1);
  621.       this.presets.save(paramByteBuffer, paramBoolean);
  622.     } else {
  623.       paramByteBuffer.put((byte)0);
  624.     }
  625.   }
  626.  
  627.   public void load(ByteBuffer paramByteBuffer, int paramInt, boolean paramBoolean) throws IOException {
  628.     if (this.presets == null) {
  629.       this.presets = new DevicePresets();
  630.     }
  631.     if (paramInt >= 69) {
  632.       this.deviceName = GameWindow.ReadString(paramByteBuffer);
  633.       this.twoWay = (paramByteBuffer.get() == 1);
  634.       this.transmitRange = paramByteBuffer.getInt();
  635.       this.micRange = paramByteBuffer.getInt();
  636.       this.micIsMuted = (paramByteBuffer.get() == 1);
  637.       this.baseVolumeRange = paramByteBuffer.getFloat();
  638.       this.deviceVolume = paramByteBuffer.getFloat();
  639.      
  640.       this.isPortable = (paramByteBuffer.get() == 1);
  641.       this.isTelevision = (paramByteBuffer.get() == 1);
  642.       this.isHighTier = (paramByteBuffer.get() == 1);
  643.      
  644.       this.isTurnedOn = (paramByteBuffer.get() == 1);
  645.       this.channel = paramByteBuffer.getInt();
  646.       this.minChannelRange = paramByteBuffer.getInt();
  647.       this.maxChannelRange = paramByteBuffer.getInt();
  648.      
  649.       this.isBatteryPowered = (paramByteBuffer.get() == 1);
  650.       this.hasBattery = (paramByteBuffer.get() == 1);
  651.      
  652.       this.powerDelta = paramByteBuffer.getFloat();
  653.       this.useDelta = paramByteBuffer.getFloat();
  654.      
  655.       this.headphoneType = paramByteBuffer.getInt();
  656.      
  657.       if (paramByteBuffer.get() == 1)
  658.         this.presets.load(paramByteBuffer, paramInt, paramBoolean);
  659.     }
  660.   }
  661. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement