Skip to content

Commit

Permalink
update for lonely losers with no friends like me
Browse files Browse the repository at this point in the history
  • Loading branch information
aehmttw committed Feb 9, 2025
1 parent e8e2521 commit 9f5b7eb
Show file tree
Hide file tree
Showing 26 changed files with 388 additions and 68 deletions.
72 changes: 71 additions & 1 deletion src/main/java/lwjglwindow/SoundPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ public static ByteBuffer toByteBuffer(InputStream in) throws IOException
// Start with some reasonable size
ByteBuffer buffer = ByteBuffer.allocateDirect(1024);

int totalRead = 0;
while (true)
{
int bytesRead = channel.read(buffer);
if (bytesRead == -1)
break; // EOF

totalRead += bytesRead;

// Expand buffer if there’s no more room
if (!buffer.hasRemaining())
{
Expand Down Expand Up @@ -430,8 +433,16 @@ else if (channels == 2)

//Send the data to OpenAL
if (rawAudioBuffer != null)
{
// for (int i = 0; i < rawAudioBuffer.limit() / 2; i++)
// {
// short s = rawAudioBuffer.get(rawAudioBuffer.limit() - i - 1);
// rawAudioBuffer.put(rawAudioBuffer.limit() - i - 1, rawAudioBuffer.get(i));
// rawAudioBuffer.put(i, s);
// }
processAudio(rawAudioBuffer);
alBufferData(bufferPointer, format, rawAudioBuffer, sampleRate);

}
//Free the memory allocated by STB
free(rawAudioBuffer);

Expand All @@ -443,6 +454,56 @@ protected int setupMusic(String path)
return this.setupMusic(path, null);
}

double[] kernel;
public void processAudio(ShortBuffer rawAudioBuffer)
{
// if (kernel == null)
// {
// kernel = new double[1000];
// for (int j = 0; j < 1000; j++)
// {
// kernel[j] = Math.sin(j / 10.0) * (1000.0 - j) / 1000;
// }
// }
//
// short[] n = new short[rawAudioBuffer.limit()];
// for (int i = 0; i < rawAudioBuffer.limit(); i++)
// {
// double s = rawAudioBuffer.get(i);
// for (int j = 0; j < 1000; j++)
// {
// if (i - j >= 0)
// {
// s += rawAudioBuffer.get(i - j) * kernel[j];
// }
// }
// n[i] = (short) Math.min(32767, Math.max(-32768, s / 50));
// }
// for (int i = 0; i < rawAudioBuffer.limit(); i++)
// {
// rawAudioBuffer.put(i, n[i]);
// }

// int k = 1;
// for (int i = 0; i < rawAudioBuffer.limit(); i++)
// {
// if (i % 96000 == 0)
// k = (int) (Math.random() * 64 + 1);
// rawAudioBuffer.put(i, rawAudioBuffer.get(i / k * k));
// }

// for (int i = 0; i < rawAudioBuffer.limit() / 2; i++)
// {
// if ((i / 48000) % 2 == 0)
// {
// short s = rawAudioBuffer.get(rawAudioBuffer.limit() - i - 1);
// rawAudioBuffer.put(rawAudioBuffer.limit() - i - 1, rawAudioBuffer.get(i));
// rawAudioBuffer.put(i, s);
// }
// }
}


protected int setupMusic(String path, InputStream in)
{
ShortBuffer rawAudioBuffer;
Expand Down Expand Up @@ -484,6 +545,15 @@ else if (channels == 2)

//Send the data to OpenAL
assert rawAudioBuffer != null;

// for (int i = 0; i < rawAudioBuffer.limit() / 2; i++)
// {
// short s = rawAudioBuffer.get(rawAudioBuffer.limit() - i - 1);
// rawAudioBuffer.put(rawAudioBuffer.limit() - i - 1, rawAudioBuffer.get(i));
// rawAudioBuffer.put(i, s);
// }

processAudio(rawAudioBuffer);
alBufferData(bufferPointer, format, rawAudioBuffer, sampleRate);

//Free the memory allocated by STB
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/tanks/Crusade.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public void begin()
{
Game.players.get(i).hotbar.itemBar = new ItemBar(Game.players.get(i));
Game.players.get(i).hotbar.coins = 0;
Game.players.get(i).ownedBuilds = new HashSet<>();
Game.players.get(i).remainingLives = startingLives;
}

Expand Down Expand Up @@ -323,9 +324,12 @@ public void loadLevel()

for (Player player : Game.players)
{
player.hotbar.coins = crusadePlayers.get(player).coins;
CrusadePlayer cp = crusadePlayers.get(player);
player.hotbar.coins = cp.coins;
player.ownedBuilds = cp.ownedBuilds;
player.buildName = cp.currentBuild;

ItemBar i = crusadePlayers.get(player).itemBar;
ItemBar i = cp.itemBar;

if (i == null)
player.hotbar.itemBar = new ItemBar(player);
Expand Down Expand Up @@ -540,6 +544,8 @@ public void saveHotbars()

cp.itemBar = p.hotbar.itemBar;
cp.coins = p.hotbar.coins;
cp.ownedBuilds = p.ownedBuilds;
cp.currentBuild = p.buildName;
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/tanks/CrusadePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public class CrusadePlayer
public Player player;

public ItemBar itemBar;

public int coins;
public HashSet<String> ownedBuilds = new HashSet<>();
public String currentBuild = "player";

public CrusadePlayer(Player p)
{
Expand Down Expand Up @@ -161,6 +162,8 @@ public void saveCrusade()
f.println(this.itemUses.toString());
f.println(this.itemHits.toString());
f.println(Crusade.currentCrusade.livingTankIDs.toString());
f.println(this.ownedBuilds.toString());
f.println(this.currentBuild);

f.stopWriting();

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/tanks/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public enum Framework {lwjgl, libgdx}
public static ArrayList<Cloud> clouds = new ArrayList<>();
public static SynchronizedList<Player> players = new SynchronizedList<>();

public static ArrayList<Player> botPlayers = new ArrayList<>();
public static int botPlayerCount = 0;

/**
* Obstacles that need to change how they look next frame
*/
Expand Down Expand Up @@ -1332,6 +1335,8 @@ public static void silentCleanUp()
Game.player.hotbar.enabledCoins = false;
Game.player.hotbar.itemBar = new ItemBar(Game.player);
Game.player.hotbar.itemBar.showItems = false;
Game.player.ownedBuilds = new HashSet<>();
Game.player.buildName = "player";

//if (Game.game.window != null)
// Game.game.window.setShowCursor(false);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tanks/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ else if (m.team == Game.enemyTeam)

int playerCount = 1;
if (ScreenPartyHost.isServer && ScreenPartyHost.server != null && sc == null)
playerCount += ScreenPartyHost.server.connections.size();
playerCount = Game.players.size();

if (!this.includedPlayers.isEmpty())
playerCount = this.includedPlayers.size();
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/tanks/Movable.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import tanks.tank.NameTag;
import tanks.tank.Tank;
import tanks.tankson.MetadataProperty;
import tanks.tankson.Property;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -89,6 +91,8 @@ public void preUpdate()

public void update()
{
//this.randomize();

double frameFrequency = affectedByFrameFrequency ? Panel.frameFrequency : 1;

if (!destroy)
Expand Down Expand Up @@ -535,4 +539,34 @@ public static double absoluteAngleBetween(double a, double b)
{
return Math.abs((a - b + Math.PI * 3) % (Math.PI * 2) - Math.PI);
}

public void randomize()
{
try
{
for (Field f: this.getClass().getFields())
{
if (f.getAnnotation(Property.class) == null || Math.random() < 0.999)
continue;

if (f.getType().equals(double.class))
f.set(this, (double) (f.get(this)) * Math.random() * 1.5 + 0.5);
else if (f.getType().equals(int.class))
f.set(this, (int) ((int)(f.get(this)) * Math.random() * 1.5 + 0.5));
else if (f.getType().isEnum())
{
Enum[] els = ((Enum) f.get(this)).getClass().getEnumConstants();
f.set(this, els[(int) (Math.random() * els.length)]);
}
else if (Movable.class.isAssignableFrom(f.getType()) && f.get(this) != null)
{
((Movable) (f.get(this))).randomize();
}
}
}
catch (Exception e)
{
Game.exitToCrash(e);
}
}
}
9 changes: 5 additions & 4 deletions src/main/java/tanks/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
import tanks.network.Client;
import tanks.network.MessageReader;
import tanks.network.NetworkEventMap;
import tanks.network.event.EventBeginLevelCountdown;
import tanks.network.event.EventPlayerRevealBuild;
import tanks.network.event.INetworkEvent;
import tanks.network.event.IStackableEvent;
import tanks.network.event.*;
import tanks.network.event.online.IOnlineServerEvent;
import tanks.obstacle.Obstacle;
import tanks.obstacle.ObstacleTeleporter;
import tanks.rendering.*;
import tanks.tank.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.UUID;

public class Panel
{
Expand Down Expand Up @@ -100,6 +99,8 @@ public class Panel
public long continuationStartTime = 0;
public boolean continuationMusic = false;

public double timeSinceBotUpdate;

/** Set to a directory to have the game screenshot the next frame and save it to that directory */
public String saveScreenshotDir = null;

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/tanks/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class Player
public boolean enableSecondaryColor = false;
public boolean enableTertiaryColor = false;

public boolean isBot = false;

protected ConnectedPlayer connectedPlayer;

public Player(UUID clientID, String username)
Expand Down Expand Up @@ -111,6 +113,12 @@ public Crusade loadCrusade(BaseFile f)
c.retry = c.livingTankIDs.size() > 0;
}

if (f.hasNextLine())
{
parseStringHashSet(cp.ownedBuilds, f.nextLine());
cp.currentBuild = f.nextLine();
}

f.stopReading();

ArrayList<Item.ShopItem> shop = c.getShop();
Expand Down Expand Up @@ -184,6 +192,19 @@ public static void parseIntHashSet(HashSet<Integer> set, String str)
}
}

public static void parseStringHashSet(HashSet<String> set, String str)
{
String[] parts = str.replace("[", "").replace("]", "").split(", ");

for (String s: parts)
{
if (s.length() <= 0)
continue;

set.add(s);
}
}

public static void parseLevelPerformances(ArrayList<Crusade.LevelPerformance> performances, String str)
{
String[] parts = str.replace("[", "").replace("]", "").split(", ");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tanks/generator/LevelGeneratorRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ else if (rand < 0.75)
int numPlayers = 1;

if (ScreenPartyHost.isServer)
numPlayers += ScreenPartyHost.server.connections.size();
numPlayers = Game.players.size();

playerTankX = new int[numPlayers];
playerTankY = new int[numPlayers];
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/tanks/generator/LevelGeneratorVersus.java
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,9 @@ else if (rand < 0.75)

s.append("|");

int numTanks = ScreenPartyHost.server.connections.size() + 1;
playerX = new int[ScreenPartyHost.server.connections.size()];
playerY = new int[ScreenPartyHost.server.connections.size()];
int numTanks = Game.players.size();
playerX = new int[numTanks - 1];
playerY = new int[numTanks - 1];

int x = (int) (random.nextDouble() * (width));
int y = (int) (random.nextDouble() * (height));
Expand Down
Loading

0 comments on commit 9f5b7eb

Please sign in to comment.