Downloads containing autoTurret.mut

Downloads
Name Author Game Mode Rating
JJ2+ Only: Szmol Weapon Pack vol. 1 szmol96 Mutator N/A Download file

File preview

#pragma name "Auto-Turret"
#pragma require "autoTurret.j2a"

/**Auto-Turret**/
/**Author: szmol96**/

enum packet_type {packet_getTurretData, packet_createTurret, packet_turretEnergy};

array<uint8> turretEnergy(32, 0);
array<int> turretX(32, 0);
array<int> turretY(32, 0);

const float pi = 3.14159265;
uint customAnimID = 0;
bool keyHeld;

void findCustomAnimID() { //This function is copied from Bunny Must Die! guns
	while (jjAnimSets[ANIM::CUSTOM[customAnimID]].firstAnim != 0) ++customAnimID;
	
	jjAnimSets[ANIM::CUSTOM[customAnimID]].load(0, "autoTurret.j2a");
}

void sendTurretPacket(int posX, int16 posY, int32 playNum) {
	jjSTREAM packet;
	packet.push(uint8(packet_createTurret));
	packet.push(posX);
	packet.push(posY);
	packet.push(playNum);
	jjSendPacket(packet);
}

void sendTurretPacket2(int posX, int16 posY, int32 playNum, uint8 playID) {
	jjSTREAM packet;
	packet.push(uint8(packet_createTurret));
	packet.push(posX);
	packet.push(posY);
	packet.push(playNum);
	jjSendPacket(packet, playID);
}

void sendEnergyPacket(uint8 playNum, int8 energy) {
	jjSTREAM packet;
	packet.push(uint8(packet_turretEnergy));
	packet.push(playNum);
	packet.push(energy);
	jjSendPacket(packet);
}

void aTurret(jjOBJ@ obj) {
	jjPLAYER@ p = jjPlayers[obj.creatorID];
	int nearestPlayer = obj.findNearestPlayer(49152);
	float dx = obj.xPos - jjPlayers[nearestPlayer].xPos;
	float dy = obj.yPos - jjPlayers[nearestPlayer].yPos;
	float angle = 0;
	
	if (obj.state == STATE::START) {
		obj.age = 0;
		obj.energy = 100;
		obj.direction = p.direction;
		obj.frameID = 0;
		obj.state = STATE::FLY;
	}
	
	if (nearestPlayer >= 0 && nearestPlayer != p.playerID) {
		if (p.isEnemy(jjPlayers[nearestPlayer]) && obj.freeze == 0) {
			angle = (512 / pi) * atan2(dx, dy);
			if (obj.age % 15 == 0) {
				jjOBJ@ tBullet = jjObjects[obj.fireBullet(OBJECT::BLASTERBULLETPU)];
				tBullet.state = STATE::FLY;
				tBullet.xPos = obj.xPos + (jjSin(angle) * -20);
				tBullet.yPos = obj.yPos + (jjCos(angle) * -20);
				tBullet.xAcc = 0;
				tBullet.yAcc = 0;
				tBullet.xSpeed = jjSin(angle) * -6;
				tBullet.ySpeed = jjCos(angle) * -6;
				tBullet.points = 20;
				tBullet.creatorType = CREATOR::PLAYER;
				tBullet.creatorID = obj.creatorID;
				tBullet.playerHandling = HANDLING::PLAYERBULLET;
				tBullet.determineCurAnim(ANIM::CUSTOM[customAnimID], 1, true);
				tBullet.killAnim = jjObjectPresets[0].determineCurAnim(ANIM::AMMO, 4, false);
			}
		}
	} else {
		angle = jjSin(obj.age * 2) * 256;
	}
	
	if (!jjMaskedHLine(obj.xPos - 10, 20, obj.yPos + 16) && obj.ySpeed < 7.5) obj.ySpeed += 0.1;
	else obj.ySpeed = 0;
	
	if (turretEnergy[p.playerID] <= 0) {
		if (jjIsServer)	sendEnergyPacket(p.playerID, 0);
		obj.state = STATE::EXPLODE;
	}
	if (obj.state == STATE::EXPLODE) {
		obj.particlePixelExplosion(0);
		obj.behavior = BEHAVIOR::EXPLOSION2;
		obj.frameID = 0;
	}
	
	obj.isTarget = true;
	obj.yPos += obj.ySpeed;
	if (obj.freeze > 0) obj.freeze--;
	else obj.age++;
	
	obj.determineCurFrame(true);
	jjDrawRotatedSpriteFromCurFrame(obj.xPos, obj.yPos, obj.curFrame + 1, angle, 1, 1, SPRITE::NORMAL, 0, 4, 4, -1);
	jjDrawString(obj.xPos - 8, obj.yPos + 24, "" + p.playerID, STRING::SMALL, STRING::NORMAL, 0, 4, 4, -1);
	obj.draw();
}

void addTurret(float x, float y, uint16 crID) {
	jjOBJ@ turret1;
	
	@turret1 = jjObjects[jjAddObject(OBJECT::TNT, x, y, crID, CREATOR::PLAYER)];
	turret1.determineCurAnim(ANIM::CUSTOM[customAnimID], 0, true);
	turret1.killAnim = jjObjectPresets[0].determineCurAnim(ANIM::AMMO, 5, false);
	turret1.playerHandling = HANDLING::SPECIAL;
	turret1.bulletHandling = HANDLING::DETECTBULLET;
	turret1.scriptedCollisions = true;
	turret1.isTarget = true;
	turret1.lightType = LIGHT::NONE;
	turret1.state = STATE::START;
	turret1.behavior = aTurret;
	
	turretEnergy[crID] = 50;
	turretX[crID] = x;
	turretY[crID] = y;
}

void onLevelLoad() {
	findCustomAnimID();
	//jjAnimations[jjAnimSets[ANIM::AMMO] + 59] = jjAnimations[jjAnimSets[ANIM::CUSTOM[customAnimID]] + 2];
}

void onLevelBegin() {
	jjWeapons[7].allowed = true;
	jjWeapons[7].style = WEAPON::MISSILE;
	jjWeapons[7].maximum = 1;
	jjWeapons[7].defaultSample = false;
	
	if (!jjIsServer) {
		jjSTREAM packet;
		packet.push(uint8(packet_getTurretData));
		jjSendPacket(packet);
	}
}

void onPlayerInput(jjPLAYER@ p) {
	if (p.currWeapon == 7) {
		p.noFire = true;
		if (p.keyFire && !keyHeld && turretEnergy[p.playerID] <= 0) {
			if (jjIsServer) {
				addTurret(p.xPos, p.yPos, p.playerID);
				sendTurretPacket(p.xPos, p.yPos, p.playerID);
			} else sendTurretPacket(p.xPos, p.yPos, p.playerID);
			p.ammo[7] = 0;
		}
		if (p.ammo[7] == 0 && !p.keyFire) p.keySelect = true;
	} else p.noFire = false;
	
	keyHeld = p.keyFire;
}

void onObjectHit(jjOBJ@ obj, jjOBJ@ bullet, jjPLAYER@ p, int force) {
	if (obj.eventID == OBJECT::TNT) {
		if (bullet !is null && bullet.points != 20) {
			turretEnergy[jjPlayers[obj.creatorID].playerID] = turretEnergy[jjPlayers[obj.creatorID].playerID] - bullet.animSpeed;
			obj.justHit = 5;
			bullet.delete();
		}
	}
}

void onReceive(jjSTREAM &in packet, int clientID) {
	uint8 type;
	packet.pop(type);
	if (jjIsServer) {
		switch (type) {
			case packet_createTurret:
			{
				int posX;
				int16 posY;
				int32 playNum;
				packet.pop(posX);
				packet.pop(posY);
				packet.pop(playNum);
				
				addTurret(posX, posY, playNum);
				sendTurretPacket(posX, posY, playNum);
			}
			break;
			case packet_getTurretData:
			{
				for (uint8 i = 0; i < 32; ++i) {
					if (turretEnergy[i] != 0) sendTurretPacket2(turretX[i], turretY[i], i, clientID);
				}
			}
			break;
		}
	} else {
		switch (type) {
			case packet_createTurret:
			{
				int posX;
				int16 posY;
				int32 playNum;
				packet.pop(posX);
				packet.pop(posY);
				packet.pop(playNum);
				
				addTurret(posX, posY, playNum);
			}
			break;
			case packet_turretEnergy:
			{
				uint8 playNum;
				int8 energy;
				packet.pop(playNum);
				packet.pop(energy);
				
				turretEnergy[playNum] = energy;
			}
			break;
		}
	}
}