-
Notifications
You must be signed in to change notification settings - Fork 0
/
pfi.py
executable file
·174 lines (136 loc) · 5.11 KB
/
pfi.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
import curses
import random
from curses import wrapper
import numpy as np
import time
import random
import sys
import argparse
def set_color(x, y, fuel, temp):
""" Temperature based color setting """
if fuel[x][y] <= 33:
return 0
if temp[x][y] > 12:
# How frequently we flicker
if random.randrange(8) == 0:
return 1
else:
return 3
if temp[x][y] > 8:
return 3
if temp[x][y] > 4:
return 1
if temp[x][y] > 0:
return 2
return 0
class ddd(object):
""" Needs a better name """
def __init__(self):
self.screen = curses.initscr()
self.width = self.screen.getmaxyx()[1]
self.height = self.screen.getmaxyx()[0]
self.fuel = np.full((self.width, self.height), 32)
self.temp = np.full((self.width, self.height), 0)
curses.curs_set(0)
curses.start_color()
curses.use_default_colors()
curses.init_pair(1, curses.COLOR_YELLOW, -1)
curses.init_pair(2, curses.COLOR_MAGENTA, -1)
curses.init_pair(3, curses.COLOR_RED, -1)
curses.init_pair(4, curses.COLOR_BLUE, -1)
self.screen.clear
def add_hot_spot(self):
x = random.randrange(self.width)
y = random.randrange(self.height)
self.temp[x][y] = 5
def add_fuel(self, filename):
""" Read in a file and build the fuel matrix based on the file contents
"""
with open(filename) as f:
content = f.readlines()
content = [x.strip() for x in content]
y = 0
for line in content:
x_max = min(self.width - 1, len(line))
for x in range(x_max):
self.fuel[x][y] = int(ord(line[x]))
y += 1
if y >= self.height - 1:
break
def show_screen(self):
for x in range(0, self.width - 1):
for y in range(0, self.height - 1):
color = set_color(x, y, self.fuel, self.temp)
# Print the character
my_chr = chr(self.fuel[x][y])
self.screen.addstr(y, x, my_chr,
curses.color_pair(color) | curses.A_BOLD)
# Print the left most digit of the temperature.
# screen.addstr(y, x, str(temp[x][y])[0],
# curses.color_pair(color) | curses.A_BOLD )
self.screen.refresh()
self.screen.timeout(30)
def key_press(self):
""" Check to see if a curses keypress was detected """
self.screen.nodelay(True)
return self.screen.getch()
def burn_step(self):
""" Run every locaiton through a burn step, update
the surrounding spots with the new temp. """
change = np.full((self.width, self.height), 0)
for x in range(0, self.width - 1):
for y in range(0, self.height - 1):
# How fast we go through the fuel
if random.randrange(2) == 0:
self.fire_check_point(x, y, change)
self.temp = np.maximum(change, self.temp)
def fire_check_point(self, x, y, change):
if self.fuel[x][y] > 32:
if self.temp[x][y] > 2:
self.temp[x][y] += 1
self.fuel[x][y] = self.fuel[x][y] - 1
else:
self.temp[x][y] = max(0, self.temp[x][y] - 1)
# Empty cells drain heat faster than full
else:
self.temp[x][y] = int(self.temp[x][y] / 2)
self.temp[x][y] = min(200, self.temp[x][y])
if self.temp[x][y] > 5:
# How hot we are defines how much heat we spread
# If we have no fuel, then we are just passing heat
# from other cells, so we further reduce heat spread
if self.fuel[x][y] > 32:
heat_spread = int(self.temp[x][y] * 0.90)
else:
heat_spread = int(self.temp[x][y] * 0.75)
# We can't expect a cell next to us to get hotter than
# we are currently.
if x > 0 and x < self.width - 1:
change[x - 1][y] = min(200, max(change[x - 1][y], heat_spread))
if x < (self.width - 1) and x >= 0:
change[x + 1][y] = min(200, max(change[x + 1][y], heat_spread))
if y > 0 and y < self.height:
change[x][y - 1] = min(200, max(change[x][y - 1], heat_spread))
if y < (self.height - 1) and y >= 0:
change[x][y + 1] = min(200, max(change[x][y + 1], heat_spread))
def fire(stdscr, filename):
""" Main loop for display."""
my_dis = ddd()
my_dis.add_fuel(filename)
my_dis.show_screen()
while True:
command = my_dis.key_press()
if command == ord('q'):
break
if command == ord('h'):
my_dis.add_hot_spot()
my_dis.burn_step()
my_dis.show_screen()
# if temp.any() == 0:
# break
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('file', help='Filename to \
display and decay')
args = parser.parse_args()
wrapper(fire, args.file)