-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
151 lines (111 loc) · 4.58 KB
/
main.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
import glfw
from OpenGL.GL import *
from gfx.MazeTexture import MazeTexture
from model.direction_map.DirectionMap import DirectionMap
from model.environment.line import Point
from gfx.AgentManager import AgentManager
from resources.handling.reading import load_direction_from_file, load_map_from_file
from random import randint
if not glfw.init():
exit(1)
# global intensity
global global_intensity
global_intensity = 50
window = glfw.create_window(1280, 720, "Modelowanie i Symulacja Systemów - Symulacja (0 FPS)", None, None)
glfw.make_context_current(window)
simulation_running = True
if not window:
glfw.terminate()
exit(1)
map_filename = "resources/ready/galeria_krakowska_maze100x100.txt"
maze_original = load_map_from_file(map_filename)
maze = load_map_from_file(map_filename)
exit_points = []
for i in range(40, 60):
exit_points.append(Point(99, i))
# directions = direction_map(maze, exit_points, 1)
directions_1 = load_direction_from_file("resources/ready/GK_directionmap_one_100x100.txt")
directions_2 = load_direction_from_file("resources/ready/GK_directionmap_two_100x100.txt")
directions_3 = load_direction_from_file("resources/ready/GK_directionmap_three_100x100.txt")
directions_4 = load_direction_from_file("resources/ready/GK_directionmap_four_100x100.txt")
direct = [DirectionMap(directions_1), DirectionMap(directions_2), DirectionMap(directions_3),
DirectionMap(directions_4)]
w_prev = 1280
h_prev = 720
offset = 20
tile_size = [(w_prev - 2 * (offset + 1)) / len(maze[0]), (h_prev - 2 * (offset + 1)) / len(maze)]
agents = AgentManager(tile_size, w_prev, h_prev, offset, exit_points, maze, direct)
mazeTexture = MazeTexture(maze_original, w_prev, h_prev, offset, tile_size)
def mouse_button_callback(window, button, action, mods):
if button == glfw.MOUSE_BUTTON_LEFT and action == glfw.PRESS:
pos_x, pos_y = glfw.get_cursor_pos(window)
pos_x -= offset
pos_y -= offset
pos = [-1, -1]
for it in range(len(maze)):
if tile_size[1] * it < pos_y < tile_size[1] * (it + 1):
pos[0] = it
for it in range(len(maze[0])):
if tile_size[0] * it < pos_x < tile_size[0] * (it + 1):
pos[1] = it
if pos[0] != -1 and pos[1] != -1 and maze[pos[0]][pos[1]] != 1:
agents.add_new(pos, 33.0, [.0, .0, .9], 0)
def key_callback(window, key, scancode, action, mods):
global global_intensity
if key == glfw.KEY_KP_ADD and action == glfw.RELEASE:
global_intensity += 10
if global_intensity > 100:
global_intensity = 100
if key == glfw.KEY_KP_SUBTRACT and action == glfw.RELEASE:
global_intensity -= 10
if global_intensity < 0:
global_intensity = 0
if key == glfw.KEY_KP_ADD and action == glfw.RELEASE:
print("Wcisnalem!")
if key == glfw.KEY_SPACE and action == glfw.PRESS:
global simulation_running
simulation_running = not (simulation_running and True)
glfw.set_mouse_button_callback(window, mouse_button_callback)
glfw.set_key_callback(window, key_callback)
old_step_time = glfw.get_time()
previous_time = glfw.get_time()
frame_count = 0
while not glfw.window_should_close(window):
current_time = glfw.get_time()
frame_count += 1
if simulation_running:
agents.step()
if current_time - previous_time >= 1.0:
title = "Crowd Simulation ( " + str(frame_count) + " FPS | Number Of Agents: " + str(
len(agents.agent_list)) + " )" + " intensity: " + str(global_intensity)
glfw.set_window_title(window, title)
frame_count = 0
previous_time = current_time
glfw.poll_events()
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
w, h = glfw.get_window_size(window)
if w != w_prev or h != h_prev:
w_prev = w
h_prev = h
tile_size[0] = (w - 2 * (offset + 1)) / len(maze[0])
tile_size[1] = (h - 2 * (offset + 1)) / len(maze)
agents.set_client_tile_size(w, h, tile_size)
mazeTexture.reconstruct(w, h, tile_size)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, w, 0, h, -10, 10)
glMatrixMode(GL_MODELVIEW)
mazeTexture.draw()
agents.draw_all()
glfw.swap_buffers(window)
intensity = randint(0, 100)
if intensity < global_intensity:
pos = [randint(50, 99), 98]
which_map = randint(0, 1)
agents.add_new(pos, 33.0, [.0, .0, .9], which_map)
pos = [randint(2, 90), 2]
which_map = randint(2, 3)
agents.add_new(pos, 33.0, [.0, .0, .9], which_map)
mazeTexture.release()
glfw.terminate()