Skip to content

Instantly share code, notes, and snippets.

@hemisemidemipresent
Last active November 6, 2023 02:37
Show Gist options
  • Save hemisemidemipresent/99138fe45f14c23b87ae79ebae0fcf8f to your computer and use it in GitHub Desktop.
Save hemisemidemipresent/99138fe45f14c23b87ae79ebae0fcf8f to your computer and use it in GitHub Desktop.
Bad Apple mod on BTD6
// https://www.youtube.com/watch?v=MPuzC4nF3KI
//
using Assets.Scripts.Unity;
using Assets.Scripts.Unity.Bridge;
using MelonLoader;
using UnityEngine;
using Assets.Scripts.Unity.UI_New.InGame;
using Assets.Scripts.Models.Towers;
using System.Collections.Generic;
using Assets.Scripts.Models.Towers.Behaviors;
using System.Drawing;
using System;
namespace slons
{
public class Main : MelonMod
{
public static System.Random random = new System.Random();
static List<TowerToSimulation> ice = new List<TowerToSimulation>();
static List<TowerToSimulation> bomb = new List<TowerToSimulation>();
static int frame = 1;
static int xCount = 22;
static int yCount = 18;
public override void OnApplicationStart() { base.OnApplicationStart(); }
public override void OnUpdate()
{
base.OnUpdate();
if (Input.GetKeyDown(KeyCode.Alpha6))
{
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 18; j++)
{
float x = (i * 213f / 22f) - 141;
float y = (j * 160f / 18f) - 49;
spawnTower(x, y, TowerType.IceMonkey);
spawnTower(x, y, TowerType.BombShooter);
}
}
}
if (Input.GetKeyDown(KeyCode.Alpha7))
{
ice = new List<TowerToSimulation>();
bomb = new List<TowerToSimulation>();
var towers = InGame.instance.bridge.GetAllTowers();
foreach (TowerToSimulation tts in towers)
{
string name = tts.tower.towerModel.name;
if (name.Contains("IceMonkey")) { ice.Add(tts); }
else if (name.Contains("Bomb")) { bomb.Add(tts); }
}
}
if (Input.GetKeyDown(KeyCode.Alpha8)) { reset(); }
if (Input.GetKeyDown(KeyCode.Mouse0))
{
handleImg(frame);
frame ++;
}
}
public void handleImg(int num)
{
string numString = num.ToString("D5");
log(numString);
Bitmap img = new Bitmap("C:\\Users\\drago\\Pictures\\scene" + numString + ".png");
int[] binary = new int[xCount * yCount]; //22*18
int count = 0;
for (int i = 0; i < xCount; i++)
{
for (int j = 0; j < yCount; j++)
{
int x = (int)Math.Round(480 / (float)xCount * i, 0);
int y = (int)Math.Round(360 / (float)yCount * j, 0);
System.Drawing.Color pixel = img.GetPixel(x, y);
if (pixel.R > 128)
{
binary[count] = 1;
}
else { binary[count] = 0; }
count++;
}
}
reset();
moveTowers(binary);
}
public void moveTowers(int[] binary)
{
int bombCount = 0;
int iceCount = 0;
for (int i = 0; i < binary.Length; i++)
{
int j = i / yCount;
int k = i % yCount;
float x = (j * 213f / (float)xCount) - 141;
float y = (k * 160f / (float)yCount) - 49;
/*
float x = (i * 213f / 22f) - 142;
float y = (j * 160f / 18f) - 50;
*/
if (binary[i] == 0)
{
var tts = bomb[bombCount];
tts.tower.PositionTower(new Assets.Scripts.Simulation.SMath.Vector2(x, y));
bombCount++;
}
else
{
var tts = ice[iceCount];
tts.tower.PositionTower(new Assets.Scripts.Simulation.SMath.Vector2(x, y));
iceCount++;
}
}
}
public void reset()
{
var towers = InGame.instance.bridge.GetAllTowers();
foreach (TowerToSimulation tts in towers)
{
tts.tower.PositionTower(new Assets.Scripts.Simulation.SMath.Vector2(420, 420));
}
}
public void log(string s)
{
MelonLogger.Msg(s);
}
static bool towerPlaced = false;
static Il2CppSystem.Action<bool> action2 = (Il2CppSystem.Action<bool>)delegate (bool s)
{
towerPlaced = s;
};
//holy shit NK whyyyy
static void spawnTower(float x, float y, string type)
{
towerPlaced = false;
int attempts = 0;
while (!towerPlaced && attempts < 100)
{
for (int i = -50; i < 200; i++)
{
try
{
TowerModel tm = Game.instance.model.GetTowerFromId(type);
FootprintModel foot = tm.footprint;
foot.doesntBlockTowerPlacement = true;
foot.ignoresPlacementCheck = true;
foot.ignoresTowerOverlap = true;
tm.footprint = foot;
tm.ignoreCoopAreas = true;
tm.ignoreBlockers = true;
tm.towerSize = TowerModel.TowerSize.small;
InGame.instance.bridge.CreateTowerAt(new UnityEngine.Vector2(x, y), tm, i, false, action2);
break;
}
catch
{
}
}
attempts++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment