-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvillager.py
361 lines (307 loc) · 12.7 KB
/
villager.py
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import pygame
import time
from utils.logger import logger
import random
from utils.agent import Agent
from langchain_core.language_models import BaseLanguageModel
from utils.agentmemory import AgentMemory
from dotenv import load_dotenv
import os
load_dotenv()
SPEED = 2
class Villager:
"""
Represents a villager in the game.
Attributes:
killed_villagers (list): List to keep track of killed villagers.
agent_id (str): The ID of the villager.
x (int): X-coordinate of the villager's position.
y (int): Y-coordinate of the villager's position.
meeting_location (tuple): The meeting location coordinates.
background_texts (str): Background information for the villager.
agent (Agent): The agent representing the villager.
current_task (str): The current task assigned to the villager.
task_location (tuple): The location of the current task.
task_complete_function (function): The function to call when the task is completed.
task_start_time (float): The start time of the task.
task_end_time (float): The end time of the task.
task_doing (bool): Indicates if the villager is currently doing a task.
time_to_complete_task (float): The time required to complete the task.
last_talk_attempt_time (float): The last time the villager attempted to talk.
talking (bool): Indicates if the villager is currently talking.
paths (list): The paths the villager can move on.
font (pygame.font.Font): The font used for rendering text.
alive (bool): Indicates if the villager is alive.
observation_countdown (float): Countdown timer for observations.
location_observation_countdown (float): Countdown timer for location observations.
"""
killed_villagers = []
def __init__(self, name, x, y, background_texts, llm: BaseLanguageModel, memory: AgentMemory, occupation="", meeting_location=(0, 0), paths=[]):
self.agent_id = name
self.x = x
self.y = y
self.meeting_location = meeting_location
self.background_texts = background_texts
self.agent = Agent(name=name, status=occupation, memory=memory, llm=llm, description=background_texts)
self.current_task = None
self.task_location = None
self.task_complete_function = None
self.task_start_time = None
self.task_end_time = 111717403133
self.task_doing = False
self.time_to_complete_task = None
self.last_talk_attempt_time = 0
self.talking = False
self.paths = paths
self.font = pygame.font.SysFont(None, 24)
self.alive = True
self.observation_countdown = time.time()
self.location_observation_countdown = time.time()
def assign_task(self, task, location, time_to_complete_task, task_complete_function):
"""
Assign a task to the villager.
Parameters:
task (str): The task to assign.
location (tuple): The location of the task.
time_to_complete_task (float): The time required to complete the task.
task_complete_function (function): The function to call when the task is completed.
"""
if not self.alive:
print(f"{self.agent_id} is dead hence can't be assigned a task")
return
self.current_task = task
self.task_complete_function = task_complete_function
self.task_location = (location.x, location.y)
self.time_to_complete_task = time_to_complete_task / float(os.getenv("SPEED"))
self.task_doing = False
self.task_start_time = None
self.task_end_time = 111717403133
def task_complete(self):
"""
Check if the current task is complete.
Returns:
bool: True if the task is complete, False otherwise.
"""
if not self.alive:
return False
return self.current_task is not None and time.time() >= self.task_end_time
def start_task(self):
"""
Start the current task.
"""
if not self.alive:
return
if not self.task_doing:
logger.info(f"{self.agent_id} has started to do the task '{self.current_task}'!")
self.task_doing = True
self.task_start_time = time.time()
self.task_end_time = self.task_start_time + self.time_to_complete_task
def update(self):
"""
Update the villager's state.
"""
if not self.alive or self.talking:
return
if self.task_doing:
if self.task_complete():
logger.info(f"{self.agent_id} has completed the task '{self.current_task}'!")
self.task_complete_function()
self.current_task = None
self.time_to_complete_task = None
self.task_doing = False
else:
if self.current_task is not None:
directions = [
(0, -1), (0, 1), (-1, 0), (1, 0),
(-1, -1), (-1, 1), (1, -1), (1, 1)
]
current_distance = self.distance_to_target(self.x, self.y)
moved = False
for direction in directions:
next_x = self.x + SPEED * direction[0]
next_y = self.y + SPEED * direction[1]
if self.distance_to_target(next_x, next_y) < current_distance:
self.x = next_x
self.y = next_y
moved = True
break
if not moved:
next_x = self.x + SPEED
next_y = self.y
if self.is_on_path(next_x, next_y, self.paths):
self.x = next_x
self.y = next_y
if current_distance <= 2:
self.start_task()
def is_on_path(self, x, y, paths):
"""
Check if the given coordinates are on a path.
Parameters:
x (int): X-coordinate to check.
y (int): Y-coordinate to check.
paths (list): List of paths.
Returns:
bool: True if the coordinates are on a path, False otherwise.
"""
for path in paths:
if path.rect.collidepoint(x, y):
return True
return False
def distance_to_target(self, x, y):
"""
Calculate the distance to the target task location.
Parameters:
x (int): X-coordinate of the current position.
y (int): Y-coordinate of the current position.
Returns:
float: Distance to the target task location.
"""
dx = self.task_location[0] - x
dy = self.task_location[1] - y
return (dx ** 2 + dy ** 2) ** 0.5
def interrupt_task(self):
"""
Interrupt the current task.
"""
self.current_task = None
self.task_doing = False
self.last_talk_attempt_time = time.time()
def draw(self, screen):
"""
Draw the villager on the screen.
Parameters:
screen (pygame.Surface): The screen to draw on.
"""
color = (0, 0, 0) if self.task_doing else (255, 0, 0)
pygame.draw.circle(screen, color, (int(self.x), int(self.y)), 5)
agent_id_text = self.font.render(self.agent_id, True, (0, 0, 0))
screen.blit(agent_id_text, (self.x - 25, self.y - 40))
vil_image = pygame.image.load(f'images/{self.agent_id.lower()}.png')
vil_image = pygame.transform.scale(vil_image, (60, 60))
if not self.alive:
vil_image = pygame.transform.rotate(vil_image, 90)
screen.blit(vil_image, (self.x - 25, self.y - 25))
class Werewolf(Villager):
"""
Represents a werewolf in the game, inheriting from Villager.
Attributes:
is_werewolf (bool): Indicates if the villager is a werewolf.
kill_cooldown (float): Cooldown timer for the werewolf's kill ability.
"""
def __init__(self, agent_id, x, y, background_texts, llm: BaseLanguageModel, memory: AgentMemory, occupation="", meeting_location=(0, 0)):
super().__init__(agent_id, x, y, background_texts, llm, memory, occupation, meeting_location)
self.is_werewolf = True
self.kill_cooldown = time.time()
def update(self):
"""
Update the werewolf's state.
"""
if self.talking:
return
if self.task_doing:
if self.task_complete():
logger.info(f"{self.agent_id} (Werewolf) sabotaged the task '{self.current_task}'!")
self.task_complete_function()
self.current_task = None
self.time_to_complete_task = None
self.task_doing = False
else:
if self.current_task is not None:
directions = [
(0, -1), (0, 1), (-1, 0), (1, 0),
(-1, -1), (-1, 1), (1, -1), (1, 1)
]
current_distance = self.distance_to_target(self.x, self.y)
moved = False
for direction in directions:
next_x = self.x + SPEED * direction[0]
next_y = self.y + SPEED * direction[1]
if self.distance_to_target(next_x, next_y) < current_distance :
self.x = next_x
self.y = next_y
moved = True
break
if not moved:
next_x = self.x + SPEED
next_y = self.y
if self.is_on_path(next_x, next_y, self.paths):
self.x = next_x
self.y = next_y
if current_distance <= 2:
self.start_task()
# Player class update to handle team tasks
class Player(Villager):
"""
Represents a player in the game, inheriting from Villager.
Attributes:
speed (int): The speed at which the player moves.
is_werewolf (bool): Indicates if the player is a werewolf.
"""
def __init__(self, name, x, y, background_texts, llm: BaseLanguageModel, memory: AgentMemory, occupation="", meeting_location=(0, 0), paths=[], is_werewolf=False):
super().__init__(name, x, y, background_texts, llm, memory, occupation, meeting_location, paths=paths)
self.speed = 2
self.is_werewolf = is_werewolf
def handle_input(self):
"""
Handle player input for movement.
"""
if not self.alive:
return
keys = pygame.key.get_pressed()
dx, dy = 0, 0
if keys[pygame.K_LEFT]:
dx -= self.speed
if keys[pygame.K_RIGHT]:
dx += self.speed
if keys[pygame.K_UP]:
dy -= self.speed
if keys[pygame.K_DOWN]:
dy += self.speed
self.x += dx
self.y += dy
def update(self):
"""
Update the player's state.
"""
self.handle_input()
super().update()
# Check if near a task location to complete it
if self.current_task is not None and not self.task_doing and self.distance_to_target(self.x, self.y) <= 2:
self.start_task()
def distance_to_task(self, x, y):
"""
Calculate the distance to the target task location.
Parameters:
x (int): X-coordinate of the current position.
y (int): Y-coordinate of the current position.
Returns:
float: Distance to the target task location.
"""
dx = self.x - x
dy = self.y - y
return (dx ** 2 + dy ** 2) ** 0.5
def distance_to_villager(self,vil):
"""
Calculate the distance to the target task location.
Parameters:
x (int): X-coordinate of the current position.
y (int): Y-coordinate of the current position.
Returns:
float: Distance to the target task location.
"""
dx = self.x - vil.x
dy = self.y - vil.y
return (dx ** 2 + dy ** 2) ** 0.5
def draw(self, screen):
"""
Draw the player on the screen.
Parameters:
screen (pygame.Surface): The screen to draw on.
"""
color = (0, 255, 0) if not self.is_werewolf else (255, 0, 0)
pygame.draw.circle(screen, color, (int(self.x), int(self.y)), 5)
agent_id_text = self.font.render(self.agent_id, True, (0, 0, 0))
screen.blit(agent_id_text, (self.x - 25, self.y - 40))
vil_image = pygame.image.load('images/akio.png') if not self.is_werewolf else pygame.image.load('images/werewolf.png')
vil_image = pygame.transform.scale(vil_image, (60, 60))
screen.blit(vil_image, (self.x - 25, self.y - 25))