-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
39 lines (28 loc) · 1.1 KB
/
util.py
File metadata and controls
39 lines (28 loc) · 1.1 KB
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
import random
import colorsys
from collections import deque
from string import ascii_letters, digits
def gen_id(length=10):
chars = ascii_letters + digits
return "".join([random.choice(chars) for x in range(length)])
def delete(target, entities):
entities = filter(lambda x: x.id is target, entities)
return deque(entities)
def distance(a, b):
n = a - b
c = n[0]**2 + n[1]**2
return c
def find_target(searcher, entities):
origin = searcher.shape.position
if searcher.objective in ["eat", "eat!"]:
algae = filter(lambda x: x.type is "algae", entities)
candidates = sorted(algae, key=lambda x: distance(origin, x.shape.position))
return candidates[0] if candidates else None
if searcher.objective is "mate":
mates = filter(lambda x: x.objective is "mate" and x.type is searcher.type and x.id is not searcher.id, entities)
candidates = sorted(mates, key=lambda x: distance(origin, x.shape.position))
return candidates[0] if candidates else None
def hsl_to_rgb(h, s, l):
values = colorsys.hls_to_rgb(h, l, s)
values = [round(x * 255) for x in values]
return values