/* ============================================================================================== ATOM SMILE (atom_smile.pde) - VEXO CLIENT FORK Physik-Simulator: Partikel-Instanzen mit 8-Byte Eigenschaften pro Zelle. Speicherung: sim[Run]/[Partikel][ID]/[Schicht][Partikel][Frame].txt Inkl. passiver Vorbereitung für ZIP/Container-Kompression großer Sternenkarten. ============================================================================================== */ import processing.net.*; import javax.swing.JOptionPane; import java.net.HttpURLConnection; import java.net.URL; import java.io.*; import java.util.ArrayList; import java.util.zip.*; // VORBEREITUNG FÜR CONTAINER (ZIP) // Passiver Netzwerk-Socket-Header Client myClient01, myClient0r, myClient0g, myClient0b, myClient0x, myClient0y; Server myServer01, myServer0r, myServer0g, myServer0b, myServer0x, myServer0y; // Globale Variablen für Raster-Daten float feld = 10.0f; int num = 30; // Wenn num = 4 -> 5x5x5 Gitter (0,1,2,3,4) int num1 = 30; int level = 0; // Z-Achse (welche Schicht in 2D angezeigt wird) int ordner = 0; // Fungiert als "Run" (sim0, sim1, ...) int iv = 1; int lastSavedFrame = 1; // --- DATENSTRUKTUR FÜR ATOM-MODUS --- class Particle { int id; int type; int x, y, z; int[] props = new int[8]; // 8 Bytes (-128 bis +127) } ArrayList particles = new ArrayList(); int[] typeCounters = new int[8]; // Das visuelle Gitter int[][][][] gridData = new int[105][105][105][8]; // Partikel Definitionen String[] particleNames = {"nix", "proton", "neutron", "elektron", "photon", "positron", "neutrino", "quark"}; color[] particleColors = { color(255, 255, 255), // 0: Nix (Vakuum) color(255, 50, 50), // 1: Proton (Rot) color(50, 255, 50), // 2: Neutron (Grün) color(50, 50, 255), // 3: Elektron (Blau) color(255, 255, 50), // 4: Photon (Gelb) color(255, 50, 255), // 5: Positron (Magenta) color(50, 255, 255), // 6: Neutrino (Cyan) color(255, 150, 50) // 7: Quark (Orange) }; int currentParticle = 1; // UI Schieberegler-Werte (0-255 in UI -> -128 bis 127 in TXT) int val1 = 128; int val2 = 128; int val3 = 128; int val4 = 128; // Zustand & Flags int dom = 1; boolean show3DView = true; boolean showHelperGrid = true; boolean isPlaying = false; boolean atomMode = true; int playTimer = 0; // Online-Modus Variablen boolean onlineActive = false; boolean remoteFrameAvailable = false; String serverBaseUrl = ""; int saveFlashTimer = 0; void setup() { size(1200, 850); background(255); ensureDir("sim0/"); updateGridFromParticles(); drawBlankGrid(); } void ensureDir(String path) { File f = new File(sketchPath(path)); if (!f.exists()) f.mkdirs(); } void clearMemoryGrid() { for (int l = 0; l < 105; l++) { for (int y = 0; y < 105; y++) { for (int x = 0; x < 105; x++) { for (int p = 0; p < 8; p++) gridData[x][y][l][p] = 0; } } } } void updateGridFromParticles() { clearMemoryGrid(); for (Particle p : particles) { if (p.x >= 0 && p.x <= num && p.y >= 0 && p.y <= num && p.z >= 0 && p.z <= num) { for(int i=0; i<8; i++) { gridData[p.x][p.y][p.z][i] = p.props[i]; } } } } boolean frameExistsLocally(int targetFrame) { String baseDir = "sim" + ordner + "/"; File dir = new File(sketchPath(baseDir)); if (!dir.exists() || !dir.isDirectory()) return false; String frameStr = nf(targetFrame, 4); File[] subs = dir.listFiles(); if (subs == null || subs.length == 0) return false; for (File sub : subs) { if (sub.isDirectory()) { String sName = sub.getName(); String pName = sName.replaceAll("[0-9]", ""); File testF = new File(sub, "0" + pName + frameStr + ".txt"); if (testF.exists()) return true; } } return false; } void updateRemoteStatus() { if (!onlineActive) { remoteFrameAvailable = false; return; } if (serverBaseUrl == null || serverBaseUrl.equals("")) loadServerConfig(); if (serverBaseUrl != null && !serverBaseUrl.equals("")) { String frameStr = nf(iv, 4); remoteFrameAvailable = checkUrlExists(serverBaseUrl + "/smile/sim" + ordner + "/nix0/0nix" + frameStr + ".txt"); } else { remoteFrameAvailable = false; } } boolean checkUrlExists(String urlString) { try { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("HEAD"); conn.setConnectTimeout(2000); conn.setReadTimeout(2000); return (conn.getResponseCode() == 200); } catch (Exception e) { return false; } } void loadServerConfig() { File f = new File(sketchPath("server.txt")); if (f.exists()) { String[] lines = loadStrings("server.txt"); if (lines != null && lines.length > 0) serverBaseUrl = trim(lines[0]); } } void drawTopBar() { fill(255); noStroke(); rect(0, 0, 1200, 30); if (saveFlashTimer > 0) { fill(100, 255, 100); rect(5, 3, 75, 24, 4); saveFlashTimer--; } else if (!frameExistsLocally(iv)) { fill(255, 255, 100); rect(5, 3, 75, 24, 4); } else { fill(245); rect(5, 3, 75, 24, 4); } fill(show3DView ? color(200, 230, 255) : color(240)); stroke(150); rect(235, 3, 95, 24, 4); fill(240); stroke(150); rect(335, 3, 100, 24, 4); fill(isPlaying ? color(100, 255, 100) : color(255, 100, 100)); stroke(150); rect(440, 3, 70, 24, 4); fill(atomMode ? color(255, 180, 100) : color(240)); stroke(150); rect(520, 3, 60, 24, 4); fill(!onlineActive ? color(255) : (remoteFrameAvailable ? color(100, 255, 100) : color(255, 255, 100))); stroke(150); rect(1020, 3, 70, 24, 4); fill(showHelperGrid ? color(200, 230, 255) : color(240)); stroke(150); rect(1100, 3, 90, 24, 4); fill(0); textSize(18); textAlign(LEFT, CENTER); text("Datei", 10, 15); text("Frame: " + iv, 90, 15); text("3D-Modus", 242, 15); text("ClearFrame", 342, 15); text(isPlaying ? "Sim: ON" : "Sim: OFF", 445, 15); text("Atom", 525, 15); text("Size: " + num, 590, 15); text("Run: " + ordner, 690, 15); text("Level: " + level, 785, 15); // Level entspricht dem Z-Wert der 2D Ebene! textAlign(CENTER, CENTER); text("Online", 1055, 15); text(showHelperGrid ? "Gitter: AN" : "Gitter: AUS", 1145, 15); textAlign(LEFT, CENTER); } void draw() { processNetworkPassive(); if (isPlaying && atomMode) { playTimer++; if (playTimer > 5) { playTimer = 0; updateAtomSimulation(); } } drawTopBar(); drawPropertyControls(); if (dom == 1) { dom = 0; drawBlankGrid(); } if (show3DView) draw3DBlockArea(); else { fill(255); stroke(180); rect(630, 30, 560, 600); } if (!isPlaying) { if (mouseX >= 0 && mouseX < 600 && mouseY >= 30 && mouseY < 630) { int cellX = floor(mouseX / feld); int cellY = floor((mouseY - 30) / feld); if (mousePressed && mouseButton == LEFT) { if (cellX < 105 && cellY < 105) { for (int i = particles.size() - 1; i >= 0; i--) { Particle p = particles.get(i); if (p.x == cellX && p.y == cellY && p.z == level) particles.remove(i); } if (currentParticle != 0) { Particle p = new Particle(); p.type = currentParticle; p.id = typeCounters[currentParticle]++; p.x = cellX; p.y = cellY; p.z = level; // Z-Wert entspricht aktuellem Level p.props[0] = p.type; p.props[1] = val1 - 128; p.props[2] = val2 - 128; p.props[3] = val3 - 128; p.props[4] = val4 - 128; particles.add(p); } updateGridFromParticles(); } color pCol = particleColors[currentParticle]; fill(pCol); stroke(200); rect(cellX * feld, 30 + cellY * feld, feld, feld); } if (mousePressed && mouseButton == RIGHT) { if (cellX < 105 && cellY < 105) { currentParticle = gridData[cellX][cellY][level][0]; val1 = gridData[cellX][cellY][level][1] + 128; val2 = gridData[cellX][cellY][level][2] + 128; val3 = gridData[cellX][cellY][level][3] + 128; val4 = gridData[cellX][cellY][level][4] + 128; } } } } } // Physik Simulation: Random Walk in 3D void updateAtomSimulation() { for (Particle p : particles) { int nx = constrain(p.x + (int)random(-2, 2), 0, num); int ny = constrain(p.y + (int)random(-2, 2), 0, num); int nz = constrain(p.z + (int)random(-2, 2), 0, num); boolean collision = false; for (Particle other : particles) { if (other != p && other.x == nx && other.y == ny && other.z == nz) { collision = true; break; } } if (!collision) { p.x = nx; p.y = ny; p.z = nz; } } updateGridFromParticles(); loadLevelToScreen(); } void loadLevelToScreen() { fill(220); stroke(180); rect(0, 30, 600, 600); feld = 600.0f / (num + 1); for (int y = 0; y <= num; y++) { for (int x = 0; x <= num; x++) { int pType = gridData[x][y][level][0]; fill(particleColors[pType]); stroke(200); rect(x * feld, 30 + y * feld, feld, feld); } } } void drawBlankGrid() { fill(220); stroke(180); rect(0, 30, 600, 600); feld = 600.0f / (num + 1); for (int y = 0; y <= num; y++) { for (int x = 0; x <= num; x++) { fill(255); stroke(200); rect(x * feld, 30 + y * feld, feld, feld); } } } void drawIsoCube(float sx, float sy, float S, color c) { fill(c); stroke(40, 60); beginShape(); vertex(sx, sy); vertex(sx + S, sy + S * 0.5f); vertex(sx, sy + S); vertex(sx - S, sy + S * 0.5f); endShape(CLOSE); fill(red(c) * 0.75f, green(c) * 0.75f, blue(c) * 0.75f); stroke(40, 60); beginShape(); vertex(sx - S, sy + S * 0.5f); vertex(sx, sy + S); vertex(sx, sy + S + S); vertex(sx - S, sy + S * 0.5f + S); endShape(CLOSE); fill(red(c) * 0.55f, green(c) * 0.55f, blue(c) * 0.55f); stroke(40, 60); beginShape(); vertex(sx, sy + S); vertex(sx + S, sy + S * 0.5f); vertex(sx + S, sy + S * 0.5f + S); vertex(sx, sy + S + S); endShape(CLOSE); } void draw3DBlockArea() { fill(245); stroke(180); rect(630, 30, 560, 600); fill(0); textSize(14); textAlign(LEFT, CENTER); text("3D Atom-Ansicht (Gesamtes Universum)", 645, 48); float originX = 910; float originY = 220; float S = 10.0f; if (showHelperGrid) { stroke(180, 120); noFill(); for (int y = 0; y <= num; y++) { float x1 = originX + (0 - y) * S; float y1 = originY + (0 + y) * (S * 0.5f); float x2 = originX + (num - y) * S; float y2 = originY + (num + y) * (S * 0.5f); line(x1, y1, x2, y2); } for (int x = 0; x <= num; x++) { float x1 = originX + (x - 0) * S; float y1 = originY + (x + 0) * (S * 0.5f); float x2 = originX + (x - num) * S; float y2 = originY + (x + num) * (S * 0.5f); line(x1, y1, x2, y2); } } // Zeichne ALLE Ebenen, nicht nur die aktuelle! for (int l = 0; l <= num; l++) { for (int y = 0; y <= num; y++) { for (int x = 0; x <= num; x++) { int pType = gridData[x][y][l][0]; if (pType != 0) { color c = particleColors[pType]; // Wenn Helper-Grid an ist, heben wir die aktuell in 2D gewählte Z-Ebene leicht hervor if (showHelperGrid && l != level) { c = color(red(c), green(c), blue(c), 100); // andere Ebenen etwas transparenter } float sx = originX + (x - y) * S; float sy = originY + (x + y) * (S * 0.5f) - (l * S * 1.0f); drawIsoCube(sx, sy, S, c); } } } } } void drawPropertyControls() { noStroke(); for (int i = 0; i <= 255; i++) { stroke(i, 0, 0); line(i, 680, i, 700); stroke(0, i, 0); line(i, 710, i, 730); stroke(0, 0, i); line(i, 740, i, 760); stroke(i); line(i, 800, i, 820); } stroke(255); strokeWeight(2); line(val1, 678, val1, 702); line(val2, 708, val2, 732); line(val3, 738, val3, 762); stroke(255, 0, 255); line(val4, 798, val4, 822); strokeWeight(1); fill(255); noStroke(); rect(258, 678, 140, 92); fill(255); rect(258, 798, 140, 24); fill(0); textSize(14); textAlign(LEFT, CENTER); text("Feld 1: " + (val1 - 128), 265, 695); text("Feld 2: " + (val2 - 128), 265, 725); text("Feld 3: " + (val3 - 128), 265, 755); text("Zusatz: " + (val4 - 128), 265, 810); fill(particleColors[currentParticle]); stroke(0); rect(0, 770, 255, 25); fill(currentParticle == 0 ? 0 : 255); textAlign(CENTER, CENTER); text(particleNames[currentParticle].toUpperCase(), 127, 782); fill(240); stroke(180); rect(410, 680, 480, 140, 6); fill(0); textSize(14); textAlign(LEFT, CENTER); text("Partikel-Palette (Klick zum Auswählen):", 425, 700); for (int i = 0; i < 8; i++) { float hx = 425 + (i * 55); float hy = 715; fill(particleColors[i]); stroke(0); rect(hx, hy, 45, 45, 4); fill(0); textSize(10); textAlign(CENTER, CENTER); text(particleNames[i], hx + 22, hy + 55); if (currentParticle == i) { noFill(); stroke(0, 255, 0); strokeWeight(3); rect(hx - 2, hy - 2, 49, 49, 6); strokeWeight(1); } } } void mousePressed() { if (mouseX >= 0 && mouseX <= 255) { if (mouseY >= 680 && mouseY <= 700) { val1 = mouseX; return; } if (mouseY >= 710 && mouseY <= 730) { val2 = mouseX; return; } if (mouseY >= 740 && mouseY <= 760) { val3 = mouseX; return; } if (mouseY >= 800 && mouseY <= 820) { val4 = mouseX; return; } } if (mouseY >= 715 && mouseY <= 760 && mouseX >= 425 && mouseX <= 425 + (8 * 55)) { int clickedIdx = floor((mouseX - 425) / 55.0f); if (clickedIdx >= 0 && clickedIdx < 8) { currentParticle = clickedIdx; return; } } if (mouseY >= 0 && mouseY <= 30) { if (mouseX >= 235 && mouseX <= 330) { show3DView = !show3DView; return; } if (mouseX >= 440 && mouseX <= 510) { isPlaying = !isPlaying; return; } if (mouseX >= 520 && mouseX <= 580) { atomMode = !atomMode; return; } if (mouseX >= 1100 && mouseX <= 1190) { showHelperGrid = !showHelperGrid; return; } if (mouseX >= 1020 && mouseX <= 1090) { if (!onlineActive) { File consentFile = new File(sketchPath("online_zugriff.txt")); if (!consentFile.exists()) { int choice = JOptionPane.showConfirmDialog(null, "DISCLAMER: Kosten, Warnungen...\nBedingungen akzeptieren?", "Online-Zugriff", JOptionPane.YES_NO_OPTION); if (choice == JOptionPane.YES_OPTION) { try { PrintWriter pw = createWriter("online_zugriff.txt"); pw.println("erlaubt"); pw.flush(); pw.close(); } catch (Exception e) {} } else return; } File serverFile = new File(sketchPath("server.txt")); if (!serverFile.exists()) { String inputUrl = JOptionPane.showInputDialog(null, "Server-Adresse eingeben:", "Server", JOptionPane.QUESTION_MESSAGE); if (inputUrl != null && !inputUrl.trim().equals("")) { inputUrl = trim(inputUrl); if (checkUrlExists(inputUrl + "/smile/smile_pde_erlaubt.txt")) { try { PrintWriter pw = createWriter("server.txt"); pw.println(inputUrl); pw.flush(); pw.close(); serverBaseUrl = inputUrl; } catch (Exception e) {} } else { JOptionPane.showMessageDialog(null, "Fehler!", "Fehler", JOptionPane.ERROR_MESSAGE); return; } } else return; } else loadServerConfig(); onlineActive = true; updateRemoteStatus(); } else { onlineActive = false; remoteFrameAvailable = false; } return; } if (mouseX >= 90 && mouseX <= 220 && !isPlaying) { if (mouseButton == LEFT) iv = max(1, iv - 1); if (mouseButton == RIGHT) iv++; if (frameExistsLocally(iv)) loadFrameData(iv); else { particles.clear(); updateGridFromParticles(); drawBlankGrid(); } updateRemoteStatus(); } if (mouseX >= 590 && mouseX <= 670 && !isPlaying) { if (mouseButton == LEFT) num = min(100, num + 1); if (mouseButton == RIGHT) num = max(2, num - 1); num1 = num; drawBlankGrid(); } if (mouseX >= 335 && mouseX <= 435 && !isPlaying) { if (mouseButton == LEFT) { particles.clear(); updateGridFromParticles(); drawBlankGrid(); } } if (mouseX >= 690 && mouseX <= 780 && !isPlaying) { if (mouseButton == LEFT) ordner++; if (mouseButton == RIGHT) ordner = max(0, ordner - 1); if (frameExistsLocally(iv)) loadFrameData(iv); else { particles.clear(); updateGridFromParticles(); drawBlankGrid(); } updateRemoteStatus(); } if (mouseX >= 785 && mouseX <= 895 && !isPlaying) { if (mouseButton == LEFT) level = min(num, level + 1); // Z-Achse Rauf if (mouseButton == RIGHT) level = max(0, level - 1); // Z-Achse Runter loadLevelToScreen(); updateRemoteStatus(); } if (mouseX >= 0 && mouseX <= 80 && !isPlaying) { if (mouseButton == RIGHT) { saveFrameData(); saveFlashTimer = 20; iv++; } else if (mouseButton == LEFT) { iv = lastSavedFrame; loadFrameData(iv); updateRemoteStatus(); } } } } // ================================================================= // DATEI EXPORT ENGINE // ================================================================= void saveFrameData() { String baseDir = "sim" + ordner + "/"; ensureDir(baseDir); String frameStr = nf(iv, 4); if (particles.isEmpty()) { String subFolder = "nix0/"; ensureDir(baseDir + subFolder); for (int l = 0; l <= num; l++) { PrintWriter out = createWriter(baseDir + subFolder + l + "nix" + frameStr + ".txt"); for (int x = 0; x <= num; x++) { for (int y = 0; y <= num; y++) out.println("0 0 0 0 0 0 0 0"); out.println("eol"); } out.println("eof"); out.flush(); out.close(); } } else { for (Particle p : particles) { String pName = particleNames[p.type]; String subFolder = pName + p.id + "/"; ensureDir(baseDir + subFolder); for (int l = 0; l <= num; l++) { PrintWriter out = createWriter(baseDir + subFolder + l + pName + frameStr + ".txt"); for (int x = 0; x <= num; x++) { for (int y = 0; y <= num; y++) { if (p.x == x && p.y == y && p.z == l) { out.println(p.props[0] + " " + p.props[1] + " " + p.props[2] + " " + p.props[3] + " " + p.props[4] + " " + p.props[5] + " " + p.props[6] + " " + p.props[7]); } else { out.println("0 0 0 0 0 0 0 0"); } } out.println("eol"); } out.println("eof"); out.flush(); out.close(); } } } lastSavedFrame = max(lastSavedFrame, iv); println("Sim-Block gespeichert in " + baseDir + " | Frame " + frameStr); } void loadFrameData(int targetFrame) { String baseDir = "sim" + ordner + "/"; String frameStr = nf(targetFrame, 4); particles.clear(); for (int i=0; i<8; i++) typeCounters[i] = 0; File dir = new File(sketchPath(baseDir)); if (!dir.exists() || !dir.isDirectory()) return; boolean numDetermined = false; for (File subFolder : dir.listFiles()) { if (subFolder.isDirectory()) { String sName = subFolder.getName(); int pType = -1; String pNameStr = ""; for (int i=0; i<8; i++) { if (sName.startsWith(particleNames[i])) { pType = i; pNameStr = particleNames[i]; break; } } if (pType != -1) { int pId = 0; try { pId = Integer.parseInt(sName.substring(pNameStr.length())); } catch(Exception e){} Particle p = new Particle(); p.type = pType; p.id = pId; boolean pFound = false; if (!numDetermined) { String[] l0 = loadStrings(baseDir + sName + "/0" + pNameStr + frameStr + ".txt"); if (l0 != null) { int rowLen = 0; for(int i=0; i 0) { num = rowLen - 1; num1 = num; numDetermined = true; } } } for (int l = 0; l <= num; l++) { String[] layerLines = loadStrings(baseDir + sName + "/" + l + pNameStr + frameStr + ".txt"); if (layerLines != null) { int lx = 0, ly = 0; for (int i=0; i= 8) { p.x = lx; p.y = ly; p.z = l; for(int j=0; j<8; j++) p.props[j] = int(parts[j]); pFound = true; } } ly++; } } } } if (pFound && pType != 0) { particles.add(p); typeCounters[pType] = max(typeCounters[pType], pId + 1); } } } } updateGridFromParticles(); loadLevelToScreen(); } void processNetworkPassive() {} // ================================================================= // PASSIVE FUNKTION FÜR ZUKÜNFTIGE ZIP-KOMPRESSION // ================================================================= void exportToZipPassive(String sourceDirPath, String zipFilePath) { /* // Später aktivieren: Packt die gesamte Ordnerstruktur eines Runs // effizient zusammen, um Gigabytes an "0 0 0"-Textdateien massiv zu verkleinern. try { FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); // ... Logik zum Hinzufügen der Dateien ... zos.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } */ }