summarylogtreecommitdiffstats
path: root/iastfyp.py
blob: cf6a5e2d2ada6e402c2e6bd9384d73d66be01ad4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import turtle
import random
import math
import tkinter as tk
import colorsys
import logging
import uuid
from datetime import datetime
from logging.handlers import RotatingFileHandler

# -----------------------
# LOGGING
# -----------------------
log_filename = f"iastfyp_turtle_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
handler = RotatingFileHandler(log_filename, maxBytes=5 * 1024 * 1024, backupCount=2)
handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(message)s", datefmt="%H:%M:%S"))
log = logging.getLogger()
log.setLevel(logging.DEBUG)
log.addHandler(handler)
log.info("IaSTFyP started")

# -----------------------
# TK ROOT
# -----------------------
root = tk.Tk()
root.title("IaSTFyP Controller")

# -----------------------
# CONFIG STORAGE
# -----------------------
MODE = None

config = {
    "MAX_TURTLES": 35,
    "MIN_SPEED": 35,
    "MAX_SPEED": 50,
    "REPRO_CHANCE": 0.3,
    "DEATH_CHANCE": 0.01,
    "MEET_DISTANCE": 10
}

# -----------------------
# MODE SELECT MENU
# -----------------------
def select_mode():
    global MODE

    win = tk.Toplevel(root)
    win.title("Select Mode")
    win.geometry("300x300")

    tk.Label(win, text="Choose Mode", font=("Arial", 14, "bold")).pack(pady=10)

    def set_mode(m):
        global MODE
        MODE = m
        win.destroy()

    tk.Button(win, text="supah_ez", command=lambda: set_mode("supah_ez")).pack(fill="x", padx=20, pady=5)
    tk.Button(win, text="easy", command=lambda: set_mode("easy")).pack(fill="x", padx=20, pady=5)
    tk.Button(win, text="normal", command=lambda: set_mode("normal")).pack(fill="x", padx=20, pady=5)
    tk.Button(win, text="extreme", command=lambda: set_mode("extreme")).pack(fill="x", padx=20, pady=5)
    tk.Button(win, text="custom", command=lambda: set_mode("custom")).pack(fill="x", padx=20, pady=5)

    win.transient(root)
    win.grab_set()
    root.wait_window(win)

# -----------------------
# CUSTOM CONFIG MENU
# -----------------------
def custom_menu():
    win = tk.Toplevel(root)
    win.title("Custom Settings")
    win.geometry("350x400")

    entries = {}

    tk.Label(win, text="Edit Simulation Parameters", font=("Arial", 12, "bold")).pack(pady=10)

    def add_field(name):
        frame = tk.Frame(win)
        frame.pack(fill="x", padx=10, pady=3)
        tk.Label(frame, text=name, width=18, anchor="w").pack(side="left")
        e = tk.Entry(frame)
        e.insert(0, str(config[name]))
        e.pack(side="right", fill="x", expand=True)
        entries[name] = e

    for key in config:
        add_field(key)

    def apply():
        try:
            config["MAX_TURTLES"] = int(entries["MAX_TURTLES"].get())
            config["MIN_SPEED"] = int(entries["MIN_SPEED"].get())
            config["MAX_SPEED"] = int(entries["MAX_SPEED"].get())
            config["REPRO_CHANCE"] = float(entries["REPRO_CHANCE"].get())
            config["DEATH_CHANCE"] = float(entries["DEATH_CHANCE"].get())
            config["MEET_DISTANCE"] = int(entries["MEET_DISTANCE"].get())
        except ValueError:
            print("Invalid input — expected numbers.")
            return
        win.destroy()

    tk.Button(win, text="Start Simulation", command=apply).pack(pady=10)

    win.transient(root)
    win.grab_set()
    root.wait_window(win)

# -----------------------
# START UI FLOW
# -----------------------
select_mode()

if MODE is None:
    raise SystemExit("No mode selected")

if MODE == "supah_ez":
    config.update({
        "MAX_TURTLES": 10,
        "MIN_SPEED": 7,
        "MAX_SPEED": 10,
        "REPRO_CHANCE": 0.2,
        "DEATH_CHANCE": 0.002,
        "MEET_DISTANCE": 20
    })
elif MODE == "easy":
    config.update({
        "MAX_TURTLES": 15,
        "MIN_SPEED": 13,
        "MAX_SPEED": 15,
        "REPRO_CHANCE": 0.3,
        "DEATH_CHANCE": 0.001,
        "MEET_DISTANCE": 25
    })
elif MODE == "normal":
    config.update({
        "MAX_TURTLES": 50,
        "MIN_SPEED": 20,
        "MAX_SPEED": 30,
        "REPRO_CHANCE": 0.4,
        "DEATH_CHANCE": 0.0003,
        "MEET_DISTANCE": 30
    })
elif MODE == "extreme":
    config.update({
        "MAX_TURTLES": 5000,
        "MIN_SPEED": 100,
        "MAX_SPEED": 100,
        "REPRO_CHANCE": 1.0,
        "DEATH_CHANCE": 0.000001,
        "MEET_DISTANCE": 100
    })
elif MODE == "custom":
    custom_menu()

log.info(f"Mode: {MODE}")
log.info(f"Config: {config}")

# -----------------------
# TURTLE SETUP
# -----------------------
screen = turtle.Screen()
screen.tracer(0)
screen.colormode(1.0)

# Query actual screen bounds
BOUND_X = screen.window_width() // 2 - 10
BOUND_Y = screen.window_height() // 2 - 10

# TurtleEntity wraps turtle + stable UUID-keyed age
class TurtleEntity:
    def __init__(self):
        self.uid = uuid.uuid4()
        self.age = 0
        self.t = turtle.Turtle()
        self.t.shape("circle")
        self.t.speed(0)
        self.t.penup()
        self.t.goto(random.randint(-BOUND_X, BOUND_X), random.randint(-BOUND_Y, BOUND_Y))
        self.t.setheading(random.randint(0, 360))
        self.t.pendown()

    def destroy(self):
        self.t.hideturtle()
        self.t.clear()

    def xcor(self):
        return self.t.xcor()

    def ycor(self):
        return self.t.ycor()

def age_color(age):
    hue = (age % 300) / 300.0
    return colorsys.hsv_to_rgb(hue, 1.0, 1.0)

def distance(a, b):
    return math.hypot(a.xcor() - b.xcor(), a.ycor() - b.ycor())

# Spatial grid for O(n) proximity checks instead of O(n²)
class SpatialGrid:
    def __init__(self, cell_size):
        self.cell_size = cell_size
        self.grid = {}

    def _cell(self, x, y):
        return int(x // self.cell_size), int(y // self.cell_size)

    def build(self, entities):
        self.grid = {}
        for e in entities:
            c = self._cell(e.xcor(), e.ycor())
            self.grid.setdefault(c, []).append(e)

    def neighbors(self, e):
        cx, cy = self._cell(e.xcor(), e.ycor())
        result = []
        for dx in (-1, 0, 1):
            for dy in (-1, 0, 1):
                result.extend(self.grid.get((cx + dx, cy + dy), []))
        return result

turtles = []
pending_kills = 0
paused = False

for _ in range(2):
    turtles.append(TurtleEntity())

# -----------------------
# CONTROL UI
# -----------------------
btn_frame = tk.Frame(root)
btn_frame.pack()

def add_turtle():
    if len(turtles) < config["MAX_TURTLES"]:
        turtles.append(TurtleEntity())

def kill_turtle():
    global pending_kills
    pending_kills += 1

def toggle_pause():
    global paused
    paused = not paused
    pause_btn.config(text="Resume" if paused else "Pause")

tk.Button(btn_frame, text="Add", command=add_turtle).pack(side="left")
tk.Button(btn_frame, text="Kill", command=kill_turtle).pack(side="left")
pause_btn = tk.Button(btn_frame, text="Pause", command=toggle_pause)
pause_btn.pack(side="left")

pop_label = tk.Label(root, text="Population: 2")
pop_label.pack()

# -----------------------
# SIM LOOP
# -----------------------
grid = SpatialGrid(config["MEET_DISTANCE"])

def step():
    global turtles, pending_kills

    if paused:
        root.after(50, step)
        return

    alive = []
    new_turtles = []

    for e in turtles:
        t = e.t

        if abs(t.xcor()) > BOUND_X or abs(t.ycor()) > BOUND_Y:
            t.setheading(t.towards(0, 0))
            t.forward(20)

        t.forward(random.randint(config["MIN_SPEED"], config["MAX_SPEED"]))
        t.right(random.randint(-30, 30))

        e.age += 1
        t.color(age_color(e.age))

        if random.random() < config["DEATH_CHANCE"]:
            e.destroy()
        else:
            alive.append(e)

    turtles[:] = alive

    # O(n) reproduction via spatial grid
    grid.build(turtles)
    seen_pairs = set()
    for e in turtles:
        if len(turtles) + len(new_turtles) >= config["MAX_TURTLES"]:
            break
        for neighbor in grid.neighbors(e):
            if neighbor is e:
                continue
            pair = frozenset((id(e), id(neighbor)))
            if pair in seen_pairs:
                continue
            seen_pairs.add(pair)
            if distance(e, neighbor) < config["MEET_DISTANCE"]:
                if len(turtles) + len(new_turtles) < config["MAX_TURTLES"]:
                    if random.random() < config["REPRO_CHANCE"]:
                        new_turtles.append(TurtleEntity())

    turtles.extend(new_turtles)

    # kills — snapshot pending_kills atomically
    kills = pending_kills
    pending_kills = 0
    for _ in range(kills):
        if turtles:
            e = turtles.pop(random.randrange(len(turtles)))
            e.destroy()

    pop_label.config(text=f"Population: {len(turtles)}")
    screen.update()
    root.after(50, step)

# -----------------------
# START
# -----------------------
step()
root.mainloop()