Downloads containing xlmdragon.j2as

Downloads
Name Author Game Mode Rating
TSF with JJ2+ Only: The Dragon EyrieFeatured Download PurpleJazz Capture the flag 8.7 Download file

File preview

#include "MLLE-Include-1.4.asc"
const bool MLLESetupSuccessful = MLLE::Setup();
#pragma require "xlmdragon-MLLE-Data-1.j2l"
#pragma require "xlmdragon.j2l"

bool mist = true;
const string Filename = "mist.asdat";

funcdef jjPALCOLOR ColorFunction(jjPALCOLOR);

void onLevelLoad() {
	jjUseLayer8Speeds = true;
	
	generateCustomSpringSprites(jjAnimSets[ANIM::CUSTOM[0]], array<uint> = {40});
	turnIntoCustomSpring(jjObjectPresets[OBJECT::FROZENSPRING], 0, 19.75f, false);

	jjPIXELMAP rain(32,32);
	for (uint x = 0; x < rain.width; ++x) {
		for (uint y = 0; y < rain.height; ++y) {
			if (x == 16) {
				if (y <= 16) rain[x,y] = 75;
				else if (y > 16 && y <= 24) rain[x,y] = 74;
				else rain[x,y] = 0;
			} else {
				rain[x,y] = 0;
			}
		}
	}
	
	jjANIMATION@ anim = jjAnimations[jjAnimSets[ANIM::COMMON].firstAnim + 2];
	for (uint frameID = 0; frameID < anim.frameCount; ++frameID) {
		jjANIMFRAME@ frame = jjAnimFrames[anim.firstFrame + frameID];
		rain.save(frame);
		frame.hotSpotX = -frame.width/2;
		frame.hotSpotY = -frame.height;
	}
	
	jjObjectPresets[OBJECT::BOUNCERPOWERUP].direction = 0;
	jjObjectPresets[OBJECT::RFPOWERUP].direction = 1;
	
	jjObjectPresets[OBJECT::AMBIENTSOUND].behavior = Emitter();
	
	for (int i = 1; i < 255; i++) {
		if (jjObjectPresets[i].playerHandling == HANDLING::PICKUP && jjObjectPresets[i].behavior == BEHAVIOR::PICKUP) {
			jjObjectPresets[i].behavior = CannotBeBuried();
		}
	}
	
	jjSTREAM savedSettings(Filename);
	while (!savedSettings.isEmpty()) {
		savedSettings.pop(mist);
	}
	
	jjAnimSets[ANIM::DEVILDEVAN].load();
	

}

void onLevelReload() {
	MLLE::Palette.apply();
}

void onLevelBegin() {
	array<jjLAYER@> layers = jjLayerOrderGet();
	
	if (layers[6].hasTileMap) {
		darkenLayer(layers[6], array<uint8>(), function(color) {
			color.setHSL(color.getHue(), color.getSat(), color.getLight() * 6 / 10);
			return color;
		});
	}
	
	for (int i = 1; i < jjObjectCount; i++) {
		if (jjObjects[i].eventID == OBJECT::CTFBASE || jjObjects[i].behavior == BEHAVIOR::FLAG) jjObjects[i].yOrg -= 12;
	}
}

jjANIMSET@ customSpringSprite;
array<int> fastCustomSpringSpeeds(jjLocalPlayerCount);
bool generateCustomSpringSprites(jjANIMSET@ anim, const array<uint> &in colors) {
	int length = colors.length();
	bool success = (@customSpringSprite = anim).allocate(array<uint>(length * 3, 5)) !is null;
	if (success) {
		uint srcSet = jjAnimSets[ANIM::SPRING];
		for (int i = 0; i < length; i++) {
			uint color = colors[i];
			uint destAnimOffset = anim + i * 3;
			for (int j = 0; j < 3; j++) {
				uint srcAnim = jjAnimations[srcSet + j];
				uint destAnim = jjAnimations[destAnimOffset + j];
				for (int k = 0; k < 5; k++) {
					jjPIXELMAP image(jjAnimFrames[destAnim + k] = jjAnimFrames[srcAnim + k]);
					int width = image.width;
					int height = image.height;
					for (int l = 0; l < height; l++) {
						for (int m = 0; m < width; m++) {
							int pixel = image[m, l];
							if (pixel >= 32 && pixel < 40)
								image[m, l] = color + (pixel & 7);
						}
					}
					if (!image.save(jjAnimFrames[destAnim + k]))
						return false;
				}
			}
		}
	}
	return success;
}
void initializeCustomSpring(jjOBJ@ obj) {
	int anim = obj.curAnim;
	obj.behave(obj.behavior = BEHAVIOR::SPRING, false);
	if (obj.curAnim != anim) {
		obj.curAnim = anim + 2;
		obj.determineCurFrame();
	}
	obj.draw();
}
void turnIntoCustomSpring(jjOBJ@ obj, uint color, float power, bool horizontal) {
	if (horizontal) {
		obj.xSpeed = power;
		obj.ySpeed = 0.f;
	} else {
		obj.xSpeed = 0.f;
		obj.ySpeed = -power;
		if (obj.state == STATE::START && obj.creatorType == CREATOR::LEVEL) {
			int x = int(obj.xPos) >> 5;
			int y = int(obj.yPos) >> 5;
			if (jjParameterGet(x, y, 0, 1) != 0) {
				jjParameterSet(x, y, 0, 1, 0);
				obj.yPos -= 4.f;
				obj.ySpeed = power;
			}
		}
	}
	obj.behavior = initializeCustomSpring;
	obj.curAnim = customSpringSprite + color * 3 + (horizontal ? 1 : 0);
	obj.energy = obj.frameID = obj.freeze = obj.justHit = obj.light = obj.points = 0;
	obj.isBlastable = obj.isTarget = obj.scriptedCollisions = obj.triggersTNT = false;
	obj.deactivates = obj.isFreezable = true;
	obj.bulletHandling = HANDLING::IGNOREBULLET;
	obj.playerHandling = HANDLING::SPECIAL;
	obj.lightType = LIGHT::NORMAL;
	obj.determineCurFrame();
}

class Emitter : jjBEHAVIORINTERFACE {
	void onBehave(jjOBJ@ emitter) {
		emitter.behave(jjLowDetail && jjGameTicks >= 2? BEHAVIOR::INACTIVE : BEHAVIOR::AMBIENTSOUND, false);
		if (!jjLowDetail) {
			jjPARTICLE@ smoke = jjAddParticle(PARTICLE::SMOKE);
			if (smoke !is null) {
				smoke.xPos = int(emitter.xPos + 8);
				smoke.yPos = int(emitter.yPos - 28);
				smoke.ySpeed = -0.2;
			}
		}
	}
}

class CannotBeBuried : jjBEHAVIORINTERFACE {
	void onBehave(jjOBJ@ obj) {
		obj.behave(BEHAVIOR::PICKUP);
		if (jjMaskedPixel(int(obj.xPos), int(obj.yPos) + 8)) {
			obj.state = STATE::FLOAT;
		}
	}
}

void onPlayer(jjPLAYER@ play) {
	for (int i = 0; i < 1024; i++) {
		jjPARTICLE@ particle = jjParticles[i];
		if (particle.type == PARTICLE::RAIN) {
			particle.xSpeed = 0;
			particle.ySpeed = play.ySpeed < 0? 10 : int(10 + play.ySpeed);
		}
	}
}

void darkenLayer(jjLAYER@ layer, array<uint8>& freePaletteIndices, ColorFunction@ func) {
	array<int> tileIDs, uniqueTileIDs;
	for (int i = 0; i < layer.height; i++) {
		for (int j = 0; j < layer.width; j++) {
			int tileID = layer.tileGet(j, i);
			if (tileID != 0)
				tileIDs.insertLast(tileID);
		}
	}
	int prev = 0;
	tileIDs.sortAsc();
	for (uint i = 0; i < tileIDs.length(); i++) {
		if (tileIDs[i] != prev)
			uniqueTileIDs.insertLast(prev = tileIDs[i]);
	}
	uint firstNewTile = jjTileCount;
	jjTilesFromTileset(jjTilesetFileName, 1, uniqueTileIDs.length());
	array<uint8> mapping(256);
	for (uint i = 0; i < uniqueTileIDs.length(); i++) {
		jjPIXELMAP tile(uniqueTileIDs[i]);
		for (int j = 0; j < 32; j++) {
			for (int k = 0; k < 32; k++) {
				uint8 pixel = tile[k, j];
				if (pixel != 0) {
					if (mapping[pixel] == 0) {
						jjPALCOLOR color = func(jjPalette.color[pixel]);
						uint8 bestMatch = jjPalette.findNearestColor(color);
						if (!freePaletteIndices.isEmpty()) {
							jjPALCOLOR other = jjPalette.color[bestMatch];
							int red = other.red - color.red;
							int green = other.green - color.green;
							int blue = other.blue - color.blue;
							int distance = red * red + green * green + blue * blue;
							if (distance > 16) {
								bestMatch = freePaletteIndices[freePaletteIndices.length() - 1];
								jjPalette.color[bestMatch] = color;
								freePaletteIndices.removeLast();
							}
						}
						mapping[pixel] = bestMatch;
					}
				tile[k, j] = mapping[pixel];
				}
			}
		}
		tile.save(firstNewTile + i);
	}
	layer.generateSettableTileArea();
	for (int i = 0; i < layer.height; i++) {
		for (int j = 0; j < layer.widthReal; j++) {
			int tileID = layer.tileGet(j, i);
			if (tileID != 0)
				layer.tileSet(j, i, firstNewTile + uniqueTileIDs.find(tileID));
		}
	}
}

void onMain() {
	if (jjGameTicks == 210 && (jjFPS < 59 || (jjFPS > 60 && jjFPS < 90)) && mist && jjColorDepth == 16 && !jjLowDetail) {
		jjConsole("|You may want to toggle the mist with ||!mist|||||| to address performance issues.");
	}

	jjTexturedBGFadePositionY = jjLowDetail? 0.5:0.4;

	array<jjLAYER@> layers = jjLayerOrderGet();
	
	if (jjColorDepth == 16 && mist) layers[7].hasTiles = true;
	else layers[7].hasTiles = false;
	
	if (jjColorDepth == 16 && !mist) layers[8].hasTiles = true;
	else layers[8].hasTiles = false;

	layers[7].yOffset += 0.1f;
	layers[11].xOffset += 0.4f;
	layers[12].xOffset += 0.25f;
	if (layers[7].yOffset % (layers[7].height*32) == 0) layers[7].yOffset = 0;
	if (layers[11].xOffset % (layers[11].widthReal*32) == 0) layers[11].xOffset = 0;
	if (layers[12].xOffset % (layers[12].widthReal*32) == 0) layers[12].xOffset = 0;
	
	jjIsSnowing = !jjLowDetail;
	
	for (int i = 1; i < jjObjectCount; i++) {
		jjOBJ@ obj = jjObjects[i];
		if (obj.eventID == OBJECT::STEADYLIGHT) {
			obj.xPos = obj.xOrg + 8;
			obj.yPos = obj.yOrg - 4;
			jjDrawSprite(obj.xPos + 1, obj.yPos, ANIM::AMMO, 13, jjGameTicks >> 3, 0, SPRITE::NORMAL, 0, 5, 5);
		}
	}
}

bool onLocalChat(string &in text, CHAT::Type type) {
	if (jjRegexMatch(text, "!mist", true)) {
		if (mist) {
			jjConsole("Mist disabled!");
			mist = false;
			jjSTREAM stream;
			stream.push(mist);
			stream.save(Filename);
		}
		else {
			jjConsole("Mist enabled!");
			mist = true;
			jjSTREAM stream;
			stream.push(mist);
			stream.save(Filename);
		}
	return true;
	}
	else return false;
}

class Dragon {
	private float xPos, yPos, xOrg, yOrg, minScale, maxScale;
	private int direction, color;
	
	void setParameters(Dragon@ dragon, float xOrg, float yOrg, float minScale, float maxScale, int direction, int color) {
		dragon.xOrg = xOrg;
		dragon.yOrg = yOrg;
		dragon.minScale = minScale;
		dragon.maxScale = maxScale;
		dragon.direction = direction;
		dragon.color = color;
	}
	
	void getPosition(Dragon@ dragon) {
		dragon.xPos = jjSin(jjGameTicks)*64*dragon.direction + dragon.xOrg;
		dragon.yPos = jjCos(jjGameTicks)*16*dragon.direction + dragon.yOrg;
	}
	
	void draw(Dragon@ dragon, jjCANVAS@ canvas) {
		canvas.drawResizedSprite(int(dragon.xPos), int(dragon.yPos), ANIM::DEVILDEVAN, dragon.yPos > (dragon.yOrg + 0.24) && dragon.yPos < (dragon.yOrg + 2.8)? 9:5, jjGameTicks >> 3, dragon.yPos >= (dragon.yOrg + 2.8)? maxScale:-minScale, dragon.yPos >= (dragon.yOrg + 2.8)? maxScale:minScale, SPRITE::SINGLECOLOR, dragon.color);
	}
}

void onDrawLayer6(jjPLAYER@ play, jjCANVAS@ canvas) {
	Dragon dragon;
	
	dragon.setParameters(dragon, 560, 320, 0.3, 0.45, -1, 229);
	dragon.getPosition(dragon);
	
	dragon.draw(dragon, canvas);
}

void onDrawLayer7(jjPLAYER@ play, jjCANVAS@ canvas) {
	Dragon dragon;

	dragon.setParameters(dragon, 100, 130, 0.18, 0.25, 1, 95);
	dragon.getPosition(dragon);
	
	dragon.draw(dragon, canvas);
}