Update circuitcore.js, fix error and add destructable circuits

This commit is contained in:
redbirdly 2024-08-07 22:30:30 +08:00 committed by GitHub
parent a298e94c8b
commit ee20718c8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 198 additions and 84 deletions

View File

@ -98,14 +98,22 @@ function createCircuitFrame(pixel, width_, height_, center=true, rotation=0) {
var [rx, ry] = rotateCoordinate(x, y, rotation); var [rx, ry] = rotateCoordinate(x, y, rotation);
var px = pixel.x + rx; var px = pixel.x + rx;
var py = pixel.y + ry; var py = pixel.y + ry;
if (!(0 <= px && px < width && 0 <= py && py < height)) {continue;} if (!(0 <= px && px < width && 0 <= py && py < height)) {continue;}
// Create the pixel
if (!pixelMap[px] || pixelMap[px][py] === undefined) { if (!pixelMap[px] || pixelMap[px][py] === undefined) {
createPixel("circuit_material", px, py); createPixel("circuit_material", px, py);
} }
// Set the core position property
if (pixelMap[px] && pixelMap[px][py] && pixelMap[px][py].element === "circuit_material") {
pixelMap[px][py].corePosition = { x: pixel.x, y: pixel.y };
} }
} }
} }
}
function createPins(pixel, pins, rotation=0) { function createPins(pixel, pins, rotation=0) {
for (var i = 0; i < pins.length; i++) { for (var i = 0; i < pins.length; i++) {
@ -1200,6 +1208,11 @@ elements.very_fast_clock = {
tick: general_clock(8, 4), tick: general_clock(8, 4),
} }
elements.fast_clock = {
color: "#FFAAFF",
tick: general_clock(16, 8),
}
var addDisplayCallback = function(pixel, pins, w, h) { var addDisplayCallback = function(pixel, pins, w, h) {
for (var y = 1; y < h - 1; y++) { for (var y = 1; y < h - 1; y++) {
for (var x = 1; x < w - 1; x++) { for (var x = 1; x < w - 1; x++) {
@ -1324,15 +1337,84 @@ elements.simple_double_seven_segment_display = {
} }
}; };
function malfunction_chip(pixel) {
var emptySpaces = [];
// Search in a 5x5 neighborhood for empty spaces
for (var dy = -2; dy <= 2; dy++) {
for (var dx = -2; dx <= 2; dx++) {
var neighborX = pixel.x + dx;
var neighborY = pixel.y + dy;
if (pixelMap[neighborX] && pixelMap[neighborX][neighborY] === undefined) {
emptySpaces.push({ x: neighborX, y: neighborY });
}
}
}
if (emptySpaces.length > 0) {
// Randomly select one of the empty spaces
var randomSpace = emptySpaces[Math.floor(Math.random() * emptySpaces.length)];
// Determine what to spawn based on probability
var rand = Math.random();
if (rand < 0.7) {
createPixel("electric", randomSpace.x, randomSpace.y);
} else if (rand < 0.99) {
createPixel("fire", randomSpace.x, randomSpace.y);
} else {
createPixel("explosion", randomSpace.x, randomSpace.y);
}
}
}
elements.circuit_material = { elements.circuit_material = {
color: "#444444", color: "#444444",
category: "logic" category: "logic",
state: "solid",
behavior: behaviors.WALL,
hoverStat: function(pixel) {
return `Circuit: ${pixel.corePosition}`;
},
tick: function(pixel) {
// Make it that extreme temperatures can stop the chip from working (for realism)
if (Math.random() < 0.003) { // Chance to check for temperature or nearby particles
// Check temperature
if (pixel.temp > 120) {
// Replace the circuit core with lead if overheating
if (pixel.corePosition && Math.random() < (0.00015) * (pixel.temp - 120)) {
var corePos = pixel.corePosition;
if (pixelMap[corePos.x] && pixelMap[corePos.x][corePos.y]) {
deletePixel(corePos.x, corePos.y);
createPixel("lead", corePos.x, corePos.y);
}
}
// Randomly trigger malfunction if overheating
if (Math.random() < 0.001 * (pixel.temp - 120)) {
malfunction_chip(pixel);
}
// Break the circuit material itself if overheating
if (Math.random() < 0.001 * (pixel.temp - 120)) {
var px = pixel.x;
var py = pixel.y;
deletePixel(px, py);
if (Math.random() < 0.5) {createPixel("lead", px, py);}
}
}
}
}
}; };
elements.input_pin = { elements.input_pin = {
color: "#DDAA33", color: "#DDAA33",
category: "logic", category: "logic",
state: "solid",
active: false, active: false,
stateHigh: "lead",
tempHigh: 570,
behavior: behaviors.WALL,
tick: function(pixel) { tick: function(pixel) {
pixel.active = false; pixel.active = false;
var neighbors = getNeighbors(pixel); var neighbors = getNeighbors(pixel);
@ -1349,7 +1431,11 @@ elements.input_pin = {
elements.output_pin = { elements.output_pin = {
color: "#AAAAAA", color: "#AAAAAA",
category: "logic", category: "logic",
state: "solid",
active: false, active: false,
stateHigh: "lead",
tempHigh: 570,
behavior: behaviors.WALL,
tick: function(pixel) { tick: function(pixel) {
var neighbors = getNeighbors(pixel); var neighbors = getNeighbors(pixel);
for (var i = 0;i < neighbors.length;i++) { for (var i = 0;i < neighbors.length;i++) {
@ -1486,6 +1572,7 @@ var circuits = [
{ circuit: elements.medium_clock }, { circuit: elements.medium_clock },
{ circuit: elements.fast_clock }, { circuit: elements.fast_clock },
{ circuit: elements.very_fast_clock }, { circuit: elements.very_fast_clock },
{ circuit: elements.very_fast_clock },
// Displays/visual circuits: white // Displays/visual circuits: white
{ circuit: elements.simple_seven_segment_display, color: cc_WHITE, size: [5, 9, false] }, { circuit: elements.simple_seven_segment_display, color: cc_WHITE, size: [5, 9, false] },
{ circuit: elements.simple_double_seven_segment_display, color: cc_WHITE, size: [9, 9, false] }, { circuit: elements.simple_double_seven_segment_display, color: cc_WHITE, size: [9, 9, false] },
@ -1497,6 +1584,32 @@ circuits.forEach(circuitInfo => {
circuitInfo.circuit.maxSize = 1; circuitInfo.circuit.maxSize = 1;
circuitInfo.circuit.isCircuitCore = true; circuitInfo.circuit.isCircuitCore = true;
circuitInfo.circuit.previewSize = circuitInfo.size; circuitInfo.circuit.previewSize = circuitInfo.size;
// Exclude circuits without a frame
if (circuitInfo.size) {
var previousCircuitTick = circuitInfo.circuit.tick;
circuitInfo.circuit.tick = function(pixel) {
previousCircuitTick(pixel);
// Don't constantly check
if (Math.random() < 0.1) {
// If there aren't 4 neighboring circuit_material elements then remove the circuit's core
var neighbors = getNeighbors(pixel);
var circuitMaterialCount = 0;
for (var i = 0;i < neighbors.length;i++) {
if (neighbors[i].element == "circuit_material") {
circuitMaterialCount++;
}
}
if (circuitMaterialCount < 2) {
deletePixel(pixel.x, pixel.y);
}
pixel.temp += Math.random(0, 5);
}
}
}
}); });
var circuitRotation = 0; var circuitRotation = 0;
@ -1550,6 +1663,7 @@ runAfterLoad(() => {
drawPixels = function(forceTick=false) { drawPixels = function(forceTick=false) {
originalDrawPixels3(forceTick); originalDrawPixels3(forceTick);
drawCircuitExtras(); drawCircuitExtras();
return true;
}; };
}); });
resetInterval(tps); resetInterval(tps);