package com.example.aicivilization;
import net.fabricmc.api.ModInitializer;
import java.util.*;
/**
* Minecraft AI Civilization
*
* Minecraft: 1.21.1
* Mod Loader: Fabric
* Java: 21
*
* Dieses System bildet die Entscheidungslogik der vier KI-Fraktionen ab.
*
* Die KI darf keine Ressourcen aus dem Nichts erzeugen.
* Jede Ressource muss zuerst erkannt, gefunden und tatsächlich gesammelt werden.
*/
public class MinecraftAICivilizationMod implements ModInitializer {
public static final String MOD_ID = "ai_civilization";
@Override
public void onInitialize() {
System.out.println("Minecraft AI Civilization Mod gestartet!");
AIFactions.createVillage("Village_1");
AIFactions.createVillage("Village_2");
AIFactions.createOutpost("Outpost_1");
System.out.println("KI-Fraktionen wurden gestartet.");
}
// =========================================================
// KI-FRAKTIONEN
// =========================================================
public static class AIFactions {
public static final int VILLAGE_DISTANCE = 1000;
public static final int OUTPOST_DISTANCE = 1500;
public static final Map<String, VillageAI> VILLAGES =
new HashMap<>();
public static final Map<String, PillagerAI> PILLAGER_AIS =
new HashMap<>();
public static final Map<String, PillagerOutpostAI> OUTPOSTS =
new HashMap<>();
public static final PiglinAI PIGLIN_AI =
new PiglinAI("Worldwide_Piglins");
public static final WitherSkeletonAI WITHER_SKELETON_AI =
new WitherSkeletonAI("Worldwide_Wither_Skeletons");
public static void createVillage(String name) {
VILLAGES.put(name, new VillageAI(name));
}
public static void createOutpost(String name) {
OUTPOSTS.put(name, new PillagerOutpostAI(name));
PILLAGER_AIS.put(name, new PillagerAI(name));
}
public static void tickAll() {
for (VillageAI village : VILLAGES.values()) {
village.tick();
}
for (PillagerAI pillager : PILLAGER_AIS.values()) {
pillager.tick();
}
PIGLIN_AI.tick();
WITHER_SKELETON_AI.tick();
}
}
// =========================================================
// AUFGABEN
// =========================================================
public enum TaskType {
OBSERVE,
SEND_INFORMATION,
PLAN,
ASSIGN,
FIND_TARGET,
GATHER,
RETURN_RESOURCES,
CRAFT_TOOLS,
BUILD_HOUSE,
ORGANIZE_BEDS,
BUILD_FACTORY,
MANAGE_RESOURCES,
MANAGE_POPULATION,
EXPLORE,
EXPAND,
DEFEND
}
public enum TaskState {
PLANNED,
ASSIGNED,
TRAVELLING,
WORKING,
RETURNING,
VERIFYING,
COMPLETED,
FAILED
}
public static class Task {
public final TaskType type;
public TaskState state = TaskState.PLANNED;
public String target;
public String assignedWorker;
public int priority;
public int progress;
public Task(TaskType type, int priority) {
this.type = type;
this.priority = priority;
}
public void assign(String worker) {
assignedWorker = worker;
state = TaskState.ASSIGNED;
}
public void travelTo(String target) {
this.target = target;
state = TaskState.TRAVELLING;
}
public void work() {
if (state != TaskState.TRAVELLING &&
state != TaskState.WORKING) {
return;
}
state = TaskState.WORKING;
progress++;
if (progress >= 3) {
state = TaskState.VERIFYING;
}
}
public void verify(boolean successful) {
if (successful) {
state = TaskState.COMPLETED;
} else {
state = TaskState.FAILED;
}
}
}
// =========================================================
// ARBEITER
// =========================================================
public static class Worker {
public final String name;
public boolean busy;
public Worker(String name) {
this.name = name;
}
public boolean canWork() {
return !busy;
}
public void start() {
busy = true;
}
public void finish() {
busy = false;
}
}
// =========================================================
// RESSOURCEN
// =========================================================
public static class Resource {
public final String type;
public int amount;
public int reserved;
public Resource(String type, int amount) {
this.type = type;
this.amount = amount;
}
public int available() {
return Math.max(0, amount - reserved);
}
public boolean reserve(int count) {
if (available() < count) {
return false;
}
reserved += count;
return true;
}
public boolean take(int count) {
if (amount < count) {
return false;
}
amount -= count;
reserved = Math.max(0, reserved - count);
return true;
}
public void add(int count) {
amount += count;
}
}
// =========================================================
// GRUNDKLASSE FÜR ALLE ZIVILISATIONEN
// =========================================================
public static abstract class CivilizationAI {
public final String name;
protected final List<Task> tasks = new ArrayList<>();
protected final List<Worker> workers = new ArrayList<>();
protected final Map<String, Resource> resources = new HashMap<>();
protected int population;
protected int tickCounter;
protected CivilizationAI(String name) {
this.name = name;
workers.add(new Worker(name + "_Worker_1"));
workers.add(new Worker(name + "_Worker_2"));
workers.add(new Worker(name + "_Worker_3"));
resource("food", 20);
resource("wood", 10);
resource("stone", 5);
resource("iron", 0);
resource("gold", 0);
resource("tools", 0);
population = 3;
}
// =====================================================
// 16 SCHRITTE
// =====================================================
public void tick() {
tickCounter++;
// 1. Umgebung erkennen
perceiveEnvironment();
// 2. Informationen an KI senden
sendInformationToAI();
// 3. KI erstellt Aufgaben
createTasks();
// 4. Aufgabe vergeben
assignTasks();
// 5-16 Aufgaben durchführen
executeTasks();
// Nach jeder Runde überprüfen
verifyCivilization();
}
protected void perceiveEnvironment() {
/*
* Echte Fabric-Version:
*
* Hier werden Blöcke, Tiere, Pflanzen,
* Gebäude, Gegner, Ressourcen und andere
* Zivilisationen in der Umgebung erkannt.
*/
}
protected void sendInformationToAI() {
/*
* Die gesammelten Informationen werden
* in den Entscheidungsbereich der KI übertragen.
*/
}
protected abstract void createTasks();
protected void assignTasks() {
for (Task task : tasks) {
if (task.state != TaskState.PLANNED) {
continue;
}
for (Worker worker : workers) {
if (worker.canWork()) {
worker.start();
task.assign(worker.name);
break;
}
}
}
}
protected void executeTasks() {
for (Task task : tasks) {
if (task.state == TaskState.ASSIGNED) {
String target = findTarget(task);
if (target != null) {
task.travelTo(target);
} else {
task.state = TaskState.FAILED;
}
}
if (task.state == TaskState.TRAVELLING) {
task.work();
}
if (task.state == TaskState.WORKING) {
performTask(task);
task.work();
}
if (task.state == TaskState.VERIFYING) {
boolean successful =
verifyTask(task);
task.verify(successful);
freeWorker(task);
}
}
tasks.removeIf(t ->
t.state == TaskState.COMPLETED ||
t.state == TaskState.FAILED);
}
protected String findTarget(Task task) {
if (task.type == TaskType.GATHER) {
return "Nearest_" + task.target;
}
if (task.type == TaskType.BUILD_HOUSE) {
return "Suitable_Building_Site";
}
if (task.type == TaskType.BUILD_FACTORY) {
return "Factory_Site";
}
if (task.type == TaskType.EXPLORE) {
return "Unknown_Area";
}
if (task.type == TaskType.EXPAND) {
return "Expansion_Area";
}
return "Local_Target";
}
protected void performTask(Task task) {
switch (task.type) {
case GATHER ->
gatherResource(task.target);
case CRAFT_TOOLS ->
craftTools();
case BUILD_HOUSE ->
buildHouse();
case ORGANIZE_BEDS ->
organizeBeds();
case BUILD_FACTORY ->
buildFactory();
case MANAGE_RESOURCES ->
manageResources();
case MANAGE_POPULATION ->
managePopulation();
case EXPLORE ->
explore();
case EXPAND ->
expand();
case DEFEND ->
defend();
default -> {
}
}
}
protected boolean verifyTask(Task task) {
return true;
}
protected void freeWorker(Task task) {
for (Worker worker : workers) {
if (worker.name.equals(task.assignedWorker)) {
worker.finish();
return;
}
}
}
// =====================================================
// RESSOURCEN
// =====================================================
protected Resource resource(String name, int amount) {
return resources.computeIfAbsent(
name,
key -> new Resource(key, amount)
);
}
protected void gatherResource(String type) {
Resource resource = resource(type, 0);
/*
* In der echten Minecraft-Version wird hier
* zuerst geprüft, ob das Ziel wirklich existiert.
*/
if (resource.available() > 0) {
resource.take(1);
resource.add(1);
}
}
protected void craftTools() {
Resource wood = resource("wood", 0);
if (wood.available() >= 2) {
wood.take(2);
resource("tools", 0).add(1);
}
}
protected void buildHouse() {
Resource wood = resource("wood", 0);
if (wood.available() >= 5) {
wood.take(5);
}
}
protected void organizeBeds() {
// Freie Betten und Arbeitsplätze werden verteilt.
}
protected void buildFactory() {
if (resource("iron", 0).available() >= 5 &&
resource("wood", 0).available() >= 5) {
resource("iron", 0).take(5);
resource("wood", 0).take(5);
}
}
protected void manageResources() {
for (Resource resource : resources.values()) {
if (resource.amount < 0) {
resource.amount = 0;
}
}
}
protected void managePopulation() {
population = Math.max(0, population);
}
protected void explore() {
// Neue Gebiete werden untersucht.
}
protected void expand() {
// Neue Bauflächen werden gesucht.
}
protected void defend() {
// Gegner erkennen und Verteidiger einsetzen.
}
protected void verifyCivilization() {
/*
* Die KI überprüft:
*
* - Sind genug Lebensmittel vorhanden?
* - Gibt es genug Holz?
* - Gibt es Betten?
* - Gibt es Arbeitsplätze?
* - Funktionieren Fabriken?
* - Ist die Bevölkerung sicher?
* - Gibt es neue Gefahren?
*/
}
}
// =========================================================
// DORFBEWOHNER-KI
// =========================================================
public static class VillageAI extends CivilizationAI {
public VillageAI(String name) {
super(name);
}
@Override
protected void createTasks() {
/*
* Prioritäten:
*
* 1 Nahrung
* 2 Holz
* 3 Stein
* 4 Werkzeuge
* 5 Häuser
* 6 Betten
* 7 Arbeitsplätze
* 8 Lager
* 9 Fabrik
* 10 Handel
* 11 Expansion
* 12 Verteidigung
*/
if (resource("food", 0).available() < 10) {
tasks.add(new Task(TaskType.GATHER, 100));
}
if (resource("wood", 0).available() < 10) {
Task task = new Task(TaskType.GATHER, 90);
task.target = "wood";
tasks.add(task);
}
if (resource("tools", 0).available() < 2) {
tasks.add(new Task(TaskType.CRAFT_TOOLS, 80));
}
if (population > 2) {
tasks.add(new Task(TaskType.BUILD_HOUSE, 70));
tasks.add(new Task(TaskType.ORGANIZE_BEDS, 65));
}
if (tickCounter % 20 == 0) {
tasks.add(new Task(TaskType.EXPLORE, 40));
}
if (tickCounter % 40 == 0) {
tasks.add(new Task(TaskType.BUILD_FACTORY, 50));
}
if (tickCounter % 60 == 0) {
tasks.add(new Task(TaskType.EXPAND, 45));
}
tasks.add(new Task(TaskType.MANAGE_RESOURCES, 30));
tasks.add(new Task(TaskType.MANAGE_POPULATION, 25));
}
}
// =========================================================
// PLÜNDERER-KI
// =========================================================
public static class PillagerAI extends CivilizationAI {
public PillagerAI(String name) {
super(name);
}
@Override
protected void createTasks() {
/*
* Plünderer legen größeren Wert auf:
* Verteidigung, Waffen, Eisen und Expansion.
*/
tasks.add(new Task(TaskType.DEFEND, 100));
if (resource("iron", 0).available() < 5) {
Task task = new Task(TaskType.GATHER, 90);
task.target = "iron";
tasks.add(task);
}
tasks.add(new Task(TaskType.CRAFT_TOOLS, 80));
tasks.add(new Task(TaskType.BUILD_HOUSE, 60));
if (tickCounter % 30 == 0) {
tasks.add(new Task(TaskType.BUILD_FACTORY, 70));
}
if (tickCounter % 50 == 0) {
tasks.add(new Task(TaskType.EXPLORE, 55));
}
if (tickCounter % 80 == 0) {
tasks.add(new Task(TaskType.EXPAND, 65));
}
}
}
// =========================================================
// PIGLIN-KI
// =========================================================
public static class PiglinAI extends CivilizationAI {
public PiglinAI(String name) {
super(name);
}
@Override
protected void createTasks() {
/*
* Eine gemeinsame KI kontrolliert alle Piglins.
*/
if (resource("gold", 0).available() < 10) {
Task gold = new Task(
TaskType.GATHER,
100
);
gold.target = "gold";
tasks.add(gold);
}
tasks.add(new Task(TaskType.CRAFT_TOOLS, 85));
tasks.add(new Task(TaskType.BUILD_HOUSE, 70));
if (tickCounter % 30 == 0) {
tasks.add(new Task(TaskType.BUILD_FACTORY, 75));
}
if (tickCounter % 50 == 0) {
tasks.add(new Task(TaskType.EXPLORE, 60));
}
if (tickCounter % 70 == 0) {
tasks.add(new Task(TaskType.EXPAND, 65));
}
tasks.add(new Task(TaskType.DEFEND, 90));
}
}
// =========================================================
// WITHER-SKELETT-KI
// =========================================================
public static class WitherSkeletonAI
extends CivilizationAI {
public WitherSkeletonAI(String name) {
super(name);
}
@Override
protected void createTasks() {
tasks.add(new Task(TaskType.DEFEND, 100));
Task stone = new Task(
TaskType.GATHER,
90
);
stone.target = "stone";
tasks.add(stone);
tasks.add(new Task(
TaskType.CRAFT_TOOLS,
80
));
tasks.add(new Task(
TaskType.BUILD_HOUSE,
70
));
if (tickCounter % 30 == 0) {
tasks.add(new Task(
TaskType.BUILD_FACTORY,
75
));
}
if (tickCounter % 50 == 0) {
tasks.add(new Task(
TaskType.EXPLORE,
60
));
}
if (tickCounter % 80 == 0) {
tasks.add(new Task(
TaskType.EXPAND,
70
));
}
}
}
// =========================================================
// PLÜNDERER-AUSSENPOSTEN
// =========================================================
public static class PillagerOutpostAI {
public final String name;
public PillagerOutpostAI(String name) {
this.name = name;
}
public void tick() {
/*
* Ein Außenposten wird unabhängig verwaltet.
*
* Außenposten können:
* - Gebäude errichten
* - Ressourcen sammeln
* - Verteidigung organisieren
* - weitere Gebiete kontrollieren
*/
}
}
// =========================================================
// FABRIK
// =========================================================
public static class FactorySystem {
public static boolean canBuildFactory(
CivilizationAI ai) {
return ai.resource("iron", 0).available() >= 5 &&
ai.resource("wood", 0).available() >= 5;
}
public static void buildFactory(
CivilizationAI ai) {
if (!canBuildFactory(ai)) {
return;
}
ai.resource("iron", 0).take(5);
ai.resource("wood", 0).take(5);
}
}
// =========================================================
// FÖRDERBAND
// =========================================================
public static class ConveyorBelt {
private final Queue<String> items =
new ArrayDeque<>();
public boolean motorConnected;
public void connectMotor() {
motorConnected = true;
}
public void addItem(String item) {
if (item != null &&
!item.isBlank()) {
items.add(item);
}
}
public String takeItem() {
if (!motorConnected) {
return null;
}
return items.poll();
}
public int itemCount() {
return items.size();
}
public void transportTo(
ConveyorBelt next) {
if (!motorConnected ||
next == null) {
return;
}
String item = takeItem();
if (item != null) {
next.addItem(item);
}
}
}
// =========================================================
// ROBOTERARM
// =========================================================
public static class RobotArm {
private String sourceBlock;
private String destinationBlock;
public void setSource(
String block) {
sourceBlock = block;
}
public void setDestination(
String block) {
destinationBlock = block;
}
public String transfer(
ConveyorBelt source,
ConveyorBelt destination) {
if (sourceBlock == null ||
destinationBlock == null) {
return null;
}
String item = source.takeItem();
if (item != null &&
destination != null) {
destination.addItem(item);
}
return item;
}
}
// =========================================================
// SPIELER-DÖRFER
// =========================================================
public static class PlayerVillage {
public String name;
public int population;
public int popularity;
public PlayerVillage(String name) {
this.name = name;
}
public void updatePopulation(int value) {
population = Math.max(0, value);
}
public void updatePopularity(int value) {
popularity = Math.max(0, value);
}
}
// =========================================================
// MIGRATION
// =========================================================
public static class VillagerMigration {
public static boolean canMigrate(
PlayerVillage village) {
return village != null &&
village.popularity > 0;
}
public static void migrate(
PlayerVillage village) {
if (canMigrate(village)) {
village.population++;
}
}
}
// =========================================================
// KRIEG
// =========================================================
public static class WarSystem {
public static void declareWar(
CivilizationAI attacker,
CivilizationAI defender) {
if (attacker == null ||
defender == null) {
return;
}
System.out.println(
attacker.name +
" erklärt " +
defender.name +
" den Krieg!"
);
}
}
// =========================================================
// HÄUSER
// =========================================================
public static class HouseConstruction {
public static boolean buildLogHouse(
CivilizationAI ai) {
if (ai.resource("wood", 0).available() < 5) {
return false;
}
ai.resource("wood", 0).take(5);
return true;
}
public static boolean buildShootingHouse(
CivilizationAI ai) {
if (ai.resource("wood", 0).available() < 3 ||
ai.resource("stone", 0).available() < 3) {
return false;
}
ai.resource("wood", 0).take(3);
ai.resource("stone", 0).take(3);
return true;
}
}
// =========================================================
// KABEL
// =========================================================
public static class Cable {
public final String from;
public final String to;
public Cable(String from, String to) {
this.from = from;
this.to = to;
}
}
// =========================================================
// ZOMBIES
// =========================================================
public static class Zombie {
public boolean mutated;
public int strength = 1;
public void mutate() {
if (!mutated) {
mutated = true;
strength++;
}
}
}
// =========================================================
// WASSER
// =========================================================
public static class WaterPhysics {
public static void update() {
/*
* Später wird hier die echte
* Minecraft-Wasserlogik erweitert.
*/
}
}
// =========================================================
// SCHWERKRAFT
// =========================================================
public static class GravitySystem {
public static double GRAVITY = 9.81;
public static double calculateFall(
double time) {
return 0.5 * GRAVITY * time * time;
}
}
// =========================================================
// HAARE
// =========================================================
public static class CreatureHair {
public String type;
public CreatureHair(String type) {
this.type = type;
}
}
// =========================================================
// SPAWNER
// =========================================================
public static class CreatureSpawner {
public String creature;
public CreatureSpawner(String creature) {
this.creature = creature;
}
public void spawn() {
System.out.println(
"Spawn: " + creature
);
}
}
// =========================================================
// GOLIBERT
// =========================================================
public static class Golibert {
public static final int HEALTH_HEARTS = 100;
public void permanentRegeneration() {
// Regeneration III.
}
public void spawnInCave() {
// Einmalige Welt-Spawning-Regel.
}
public void useFloatingTriangleAppearance() {
// Spezielles Erscheinungsbild.
}
}
public static class GolibertHelper {
public void helpGolibert() {
// Hilfs-KI.
}
}
// =========================================================
// WARDEN OF SKELETONS
// =========================================================
public static class WardenOfSkeletons {
public int health = 1000;
public void controlSkeletons() {
// Kontrolliert verbundene Skelette.
}
public void defendArea() {
// Verteidigt sein Gebiet.
}
}
// =========================================================
// WITHER-ENTWICKLUNG
// =========================================================
public static class WitherSkeletonMaster {
public static final int HEALTH_HEARTS = 35;
}
public static class WitherKing {
public static final int HEALTH_HEARTS = 600;
public void summonWithers() {
for (int i = 0; i < 3; i++) {
System.out.println(
"Wither wird beschworen."
);
}
}
}
public static class WitherMaster {
public static final int HEALTH_HEARTS = 6000;
public void summonSkeletons() {
for (int i = 0; i < 15; i++) {
System.out.println(
"Wither-Skelett wird beschworen."
);
}
}
public void regenerate() {
// Regeneration II.
}
}
public static class WitherGod {
public static final int HEALTH_HEARTS = 50000;
public void useAllAbilities() {
// Fähigkeiten der vorherigen Stufen.
}
public void ruleWitherSkeletons() {
// Eine gemeinsame KI kontrolliert
// die Wither-Skelett-Zivilisation.
}
}
}

