-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.py
404 lines (313 loc) · 12.7 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import cProfile, pdb, timeit, os, sys, glob
from time import time, sleep
from math import radians
from random import random
import console as c
from colours import init_colours
from console import DEBUG, log, in_game_log, CLS, SHOW_CUR, HIDE_CUR
from nbinput import NonBlockingInput
from items import items_to_render_objects
from events import process_events
import saves, ui, terrain, player, render, render_interface, server_interface, data
def main():
settings = None
try:
meta, settings, profile, debug, benchmarks, name, port = setup()
while True:
data = ui.main(meta, settings)
if data is None:
break
if data['local']:
# Local Server
server_obj = server_interface.LocalInterface(name, data['save'], port, settings)
else:
# Remote Server
server_obj = server_interface.RemoteInterface(name, data['ip'], data['port'])
if not server_obj.error:
render_interface.setup_render_module(settings)
if profile:
cProfile.runctx('game(server_obj, settings, benchmarks)', globals(), locals(), filename='game.profile')
elif debug:
pdb.run('game(server_obj, settings, benchmarks)', globals(), locals())
else:
game(server_obj, settings, benchmarks)
if server_obj.error:
ui.error(server_obj.error)
finally:
setdown()
def setup():
log('Start\n')
print(CLS)
meta = saves.get_global_meta()
settings = saves.get_settings()
profile = c.getenv_b('PYCRAFT_PROFILE')
benchmarks = c.getenv_b('PYCRAFT_BENCHMARKS')
debug = c.getenv_b('PYCRAFT_PDB')
# For externally connecting GDB
with open('.pid', 'w') as f:
print(os.getpid(), file=f)
name = c.getenv('PYCRAFT_NAME') or settings.get('name') or ui.name(settings)
port = c.getenv('PYCRAFT_PORT') or meta.get('port') or 0
init_colours(settings)
saves.check_map_dir()
print(HIDE_CUR + CLS)
return meta, settings, profile, debug, benchmarks, name, port
def setdown():
print(SHOW_CUR + CLS)
def game(server, settings, benchmarks):
dt = 0 # Tick
df = 0 # Frame
dc = 0 # Cursor
ds = 0 # Selector
dpos = False
dinv = False # Inventory
dcraft = False # Crafting
FPS = 15 # Max
MPS = 15 # Movement
SPS = 5 # Mob spawns
old_bk_objects = None
old_edges = None
redraw_all = True
last_move = time()
last_mob_spawn = time()
inp = None
jump = 0
cursor = 0
crafting = False
crafting_sel = 0
crafting_list = []
inv_sel = 0
cursor_hidden = True
new_blocks = {}
alive = True
events = []
crafting_list, crafting_sel = player.get_crafting(
server.inv,
crafting_list,
crafting_sel
)
# Game loop
with NonBlockingInput() as nbi:
while server.game:
x, y = server.pos
dt = server.dt()
frame_start = time()
## Input
char = True
inp = []
while char:
# Receive input if a key is pressed
char = nbi.char()
if char:
char = str(char).lower()
if char in 'wasdhkjliuoc-=\n ':
inp.append(char)
# Hard pause
if DEBUG and '\n' in inp:
input()
inp.remove('\n')
# Pause game
if ' ' in inp or '\n' in inp:
server.redraw = True
redraw_all = True
if ui.pause(server, settings) == 'exit':
server.logout()
continue
width = settings.get('width')
height = settings.get('height')
# Update player and mobs position / damage
move_period = 1 / MPS
while frame_start >= move_period + last_move and x in server.map_:
dx, dy, jump = player.get_pos_delta_on_input(
inp, server.map_, x, y, jump, settings.get('flight'))
if dx or dy:
dpos = True
x += dx
y += dy
if x in server.map_ and not settings.get('flight'):
# Player falls when no solid block below it and not jumping
jump -= dt
if jump <= 0:
jump = 0
if not terrain.is_solid(server.map_[x][y + 1]):
# Fall
y += 1
dpos = True
if 'h' in inp:
item = render.blocks[server.inv[inv_sel]['block']] if len(server.inv) else {}
server.player_attack(item.get('attack_radius', 5), item.get('attack_damage', 10))
server.update_items()
server.update_mobs()
if server.health <= 0:
alive = False
last_move += move_period
## Update Map
# Finds display boundaries
edges = (x - int(width / 2), x + int(width / 2))
edges_y = (y - int(height / 2), y + int(height / 2))
if edges_y[1] > data.world_gen['height']:
edges_y = (data.world_gen['height'] - height, data.world_gen['height'])
elif edges_y[0] < 0:
edges_y = (0, height)
extended_edges = (edges[0]-render.max_light, edges[1]+render.max_light)
slice_list = terrain.detect_edges(server.map_, extended_edges)
if slice_list:
log('slices to load', slice_list)
chunk_list = terrain.get_chunk_list(slice_list)
server.get_chunks(chunk_list)
server.unload_slices(extended_edges)
# Moving view
if not edges == old_edges or server.view_change:
extended_view = terrain.move_map(server.map_, extended_edges)
old_edges = edges
server.redraw = True
server.view_change = False
# Sun has moved
bk_objects, sky_colour, day = render.bk_objects(server.time, width, edges[0], settings.get('fancy_lights'))
if not bk_objects == old_bk_objects:
old_bk_objects = bk_objects
server.redraw = True
if settings.get('gravity'):
blocks = terrain.apply_gravity(server.map_, extended_edges)
if blocks: server.set_blocks(blocks)
## Crafting
if 'c' in inp:
server.redraw = True
crafting = not crafting and len(crafting_list)
dcraft, dcraftC, dcraftN = False, False, False
if dinv: crafting = False
if crafting:
# Craft if player pressed craft
server.inv, inv_sel, crafting_list, dcraftC = \
player.crafting(inp, server.inv, inv_sel,
crafting_list, crafting_sel)
# Increment/decrement craft no.
crafting_list, dcraftN = \
player.craft_num(inp, server.inv, crafting_list,
crafting_sel)
dcraft = dcraftC or dcraftN
# Update crafting list
crafting_list, crafting_sel = \
player.get_crafting(server.inv, crafting_list,
crafting_sel, dcraftC)
if not len(crafting_list): crafting = False
dc = player.move_cursor(inp)
cursor = (cursor + dc) % 6
ds = player.move_sel(inp)
if crafting:
crafting_sel = ((crafting_sel + ds) % len(crafting_list)
if len(crafting_list) else 0)
else:
inv_sel = ((inv_sel + ds) % len(server.inv)
if len(server.inv) else 0)
if any((dpos, dc, ds, dinv, dcraft)):
server.redraw = True
if dpos:
dpos = False
server.pos = x, y
cursor_hidden = True
if dc:
cursor_hidden = False
## Eating or placing blocks
p_hungry = server.health < player.MAX_PLAYER_HEALTH
new_blocks, server.inv, inv_sel, new_events, dhealth, dinv = \
player.cursor_func(
inp, server.map_, x, y, cursor, inv_sel, server.inv, p_hungry
)
server.add_health(dhealth)
if new_blocks:
server.set_blocks(new_blocks)
## Process events
events += new_events
new_blocks = {}
for i in range(int(dt)):
new_blocks.update(process_events(events, server))
if new_blocks:
server.set_blocks(new_blocks)
# If no block below, kill player
try:
block = server.map_[x][y+1]
except IndexError:
alive = False
# Respawn player if dead
if not alive:
if ui.respawn() == 'exit':
server.logout()
continue
server.redraw = True
redraw_all = True
alive = True
server.respawn()
## Spawning mobs / Generating lighting buffer
lights = render.get_lights(extended_view, bk_objects, x)
spawn_period = 1 / SPS
n_mob_spawn_cycles = int((frame_start - last_mob_spawn) // spawn_period)
last_mob_spawn += spawn_period * n_mob_spawn_cycles
server.spawn_mobs(n_mob_spawn_cycles, bk_objects, sky_colour, day, lights)
## Render
if server.redraw:
server.redraw = False
# TODO: It would be nice to reuse any of the lighting_buffer generated for the mobs which overlaps with the screen
render_interface.create_lighting_buffer(width, height, edges[0], edges_y[0], server.map_, server.slice_heights, bk_objects, sky_colour, day, lights)
entities = {
'player': list(server.current_players.values()),
'zombie': list(server.mobs.values())
}
objects = player.entities_to_render_objects(
entities, x, int(width / 2), edges
)
objects += items_to_render_objects(server.items, x, int(width / 2))
if not cursor_hidden:
cursor_colour = player.cursor_colour(
x, y, cursor, server.map_, server.inv, inv_sel
)
objects.append(player.assemble_cursor(
int(width / 2), y, cursor, cursor_colour
))
render_args = [
server.map_,
server.slice_heights,
edges,
edges_y,
objects,
bk_objects,
sky_colour,
day,
lights,
settings,
redraw_all
]
render_map = lambda: render_interface.render_map(*render_args)
if benchmarks:
timer = timeit.Timer(render_map)
t = timer.timeit(1)
log('Render call time = {}'.format(t), m="benchmarks")
else:
render_map()
redraw_all = False
crafting_grid = render.render_grid(
player.CRAFT_TITLE, crafting, crafting_list,
height, crafting_sel
)
inv_grid = render.render_grid(
player.INV_TITLE, not crafting, server.inv,
height, inv_sel
)
label = (player.label(crafting_list, crafting_sel)
if crafting else
player.label(server.inv, inv_sel))
health = 'Health: {}/{}'.format(round(server.health), player.MAX_PLAYER_HEALTH)
render.render_grids(
[
[inv_grid, crafting_grid],
[[label]],
[[health]]
],
width, height
)
in_game_log('({}, {})'.format(x, y), 0, 0)
d_frame = time() - frame_start
if d_frame < (1/FPS):
sleep((1/FPS) - d_frame)
if __name__ == '__main__':
main()