-
Notifications
You must be signed in to change notification settings - Fork 0
/
project360.py
1902 lines (1838 loc) · 75.3 KB
/
project360.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import gc
import math
import natsort
import numpy
import os
import psutil
import pickle
import pygame
import synthizer as syn
import sys
import time
from collections import deque
from cytolk import tolk
from glob import glob
from numba import jit
from pdb import Pdb
from random import choice, randint, uniform, shuffle
tolk.load()
wav = '.wav'
soundpath = os.getcwd() + str('/data/sounds/')
class EmptyClass:
def __init__(self):
pass
class Block:
def __init__(self):
pass
def set_to_rock(self):
self.name = "Rock"
self.sounds = [
]
class Floor:
def __init__(self):
pass
def set_to_concrete(self):
self.name = "Concrete"
self.sounds = [
"ft-concrete01",
"ft-concrete02",
"ft-concrete03",
"ft-concrete04",
"ft-concrete05",
]
def set_to_grass(self):
self.name = "Grass"
self.sounds = [
"ft-grass01",
"ft-grass02",
"ft-grass03",
"ft-grass04",
"ft-grass05",
]
def set_to_forest(self):
self.name = "Forest"
self.sounds = [
"ft-forest01",
"ft-forest02",
"ft-forest03",
"ft-forest04",
"ft-forest05",
"ft-forest06",
"ft-forest07",
]
def set_to_rustwood(self):
self.name = "Rust Wood"
self.sounds = [
"ft-rustwood01",
"ft-rustwood02",
"ft-rustwood03",
"ft-rustwood04",
"ft-rustwood05",
"ft-rustwood06",
"ft-rustwood07",
]
def set_to_sand(self):
self.name = "Sand"
self.sounds = [
"ft-sand01",
"ft-sand02",
"ft-sand03",
"ft-sand04",
"ft-sand05",
]
def set_to_water(self):
self.name = "Water"
self.sounds = [
"ft-water01",
"ft-water02",
"ft-water03",
"ft-water04",
"ft-water05",
"ft-water06",
"ft-water07",
"ft-water08",
"ft-water09",
]
def set_to_wood(self):
self.name = "Wood"
self.sounds = [
"ft-wood01",
"ft-wood02",
"ft-wood03",
"ft-wood04",
"ft-wood05",
]
class Item:
def __init__(self):
pass
class SoundEvent:
def __init__(self, position):
self.y = position[0]
self.x = position[1]
self.z = position[2]
self.position = position
self.sounds = []
self.source = None
self.distance_ref = 1
self.distance_max = 10
self.gain = 1
self.loop = 0
self.pitch_bend = 1
self.ratio = 0
self.rolloff = 1
def add_sound(self):
sound = main.get_sound(path=soundpath, filext="/*.wav")
self.sounds += [sound]
def check_complete(self):
if self.source == None: return
position = self.generator.playback_position.value
length = self.buffer.get_length_in_seconds()
if position >= length:
self.clean()
def clean(self):
if self.source:
self.source.remove_generator(self.generator)
self.source.dec_ref()
self.source = None
self.generator.dec_ref()
del(self.generator)
del(self.buffer)
def update(self):
self.sounds = [it for it in self.sounds if it != None]
self.check_complete()
if self.source:
self.generator.looping.value = self.loop
self.generator.pitch_bend.value = self.pitch_bend
self.source.gain.value = self.gain
self.source.position.value = self._position
self.source.distance_ref.value = self.distance_ref
self.source.distance_max.value = self.distance_max
self.source.rolloff.value = self.rolloff
class Player:
def __init__(self):
self.name = ""
self.vision_range = 100
self.running = 0
self.walk_countdown = 500
self.run_countdown_factor = 0.7
self._countdown = 0
self.step_length = 0.0174
self.run_step_factor = 2
self.locations = []
self.sensor_timers = [0, 0, 0,]
self.foot = [
]
self.passable_floor = ["Grass", "Sand"]
self.passable_wall = [None]
self.editor = 0
self.generator = None
self.source = None
def can_pass(self, destination):
if self.editor: return True
floor = None
if destination.floor not in self.passable_floor:
floor = f"Can not pass {destination.floor} floor.."
if floor:
#if floor: tolk.output(floor)
return False
else: return True
def can_walk(self):
if self.ctime > self._countdown: return 1
def clean(self):
if self.source:
self.source.dec_ref()
self.source = None
if self.generator:
self.generator.dec_ref()
self.generator = None
def check_location(self):
locations = main.get_location(self.position)
if locations != self.locations:
for it in locations:
if it not in self.locations: tolk.output(f"{it}")
self.locations = locations
main.add_source3d(
sound="notify2", linger=1, position=self.position, pitch=2)
def get_cdt(self):
degrees = None
if self.degrees > 340 or self.degrees < 20: degrees = 0
elif self.degrees > 70 and self.degrees <= 110: degrees = 1
elif self.degrees > 160 and self.degrees < 200: degrees = 2
elif self.degrees > 250 and self.degrees <= 290: degrees = 3
coordinates = ["North", "East", "South", "West"]
if isinstance(degrees, int): return coordinates[degrees]
else: return None
def say_degrees(self):
degrees = self.get_cdt()
if not hasattr(self, "degrees_name"): self.degrees_name = degrees
if degrees != self.degrees_name:
self.degrees_name = degrees
if isinstance(degrees, str):
tolk.output(f"{self.degrees_name}.",1)
def say_cdt(self):
x = main.fix_decimal(self.position[1], prec=4)
y = main.fix_decimal(self.position[0], prec=4)
tolk.output(f"y {y}, x {x}.",1)
def say_location(self):
locations = main.get_location(self.position)
if locations:
for it in locations:
tolk.output(f"{it}")
def set_editor(self):
self.editor = 1
self.walk_countdown = 0
self.step_length = main.world_step
def set_name(self, name):
self.name = name
def set_degrees(self, degrees):
if degrees < 0: degrees += 360
elif degrees > 360: degrees -= 360
elif degrees == 360: degrees = 0
self.degrees = degrees
def set_position(self, position=[]):
self.position = position
def set_run(self):
if self.running == 0:
self.running = 1
tolk.output(f"Running On.",1)
elif self.running:
self.running = 0
tolk.output(f"Running Off.",1)
def update(self):
self.ctime = main.ctime
class World:
def __init__(self):
self.name = None
self.jumppoints = []
self.locations = []
self.players = []
self.sources = []
def add_player(self, degrees, name, position=[]):
player = Player()
player.set_degrees(degrees)
player.set_name(name)
player.set_position(position)
self.players += [player]
def new_world(self, info=0):
inicio = pygame.time.get_ticks()
tolk.output(f"creating world.", 1)
self.name = main.set_attr(attrtype="str")
if self.name == None: return
self.ext = ".map"
self.width = 512
self.length = 512
self.map = []
for y in range(0, self.length, 1):
self.map.append([])
for x in range(0, self.width, 1):
tile = Tile()
#tile.y, tile.x = y, x
#tile.set_to_sand()
self.map[y].append(tile)
if info:
pworld = pickle.dumps(self)
mem = psutil.Process(os.getpid()).memory_info().rss/1024
print(f"pickle: {sys.getsizeof(pworld)/1024}.")
ctime = pygame.time.get_ticks()
print(f"{mem=:}.")
print(f"{ctime-inicio}.")
tolk.output(f"Done.")
Pdb().set_trace()
def init_tiles(self):
tolk.output(f"Setting basics.")
for y in range(0, len(self.map), 1):
for x in range(0, len(self.map[0]), 1):
tile = self.map[y][x]
tile.y = y
tile.x = x
#if not hasattr(tile, "floors"): tile.floors = []
#if not hasattr(tile, "blocks"): tile.blocks = []
def set_savingmap_settings(self):
self.players = []
for y in self.map:
for it in y:
del(it.y)
del(it.x)
if hasattr(it, "blocks") and not it.blocks: del(it.blocks)
if hasattr(it, "floors") and not it.floors: del(it.floors)
for pl in self.players:
pl.clean()
for src in self.sources:
if src.source:
src.clean()
def update(self):
[src.update() for src in self.sources]
class Tile:
def __init__(self):
pass
#self.floors = []
#self.blocks = []
def hasfloor(self, position):
if not hasattr(self, "floors"): return
y = position[0]
x = position[1]
for fl in self.floors:
#3print(f"{fl.position}, {(y, x)}.")
if fl.position[0] == 100:
#print(f"Custom {fl.position}, {(y, x)}.")
pass
if (fl.position[0] <= y <= fl.position[0] +main.world_step
and fl.position[1] <= x <= fl.position[1] + main.world_step):
return fl
def is_blocked(self, position):
if not hasattr(self, "blocks"): return
y = position[0]
x = position[1]
for bl in self.blocks:
if (bl.position[0] <= y <= bl.position[0] + main.world_step
and bl.position[1] <= x <= bl.position[1] + main.world_step):
return bl
def set_block(self, block_type):
y = self.y
x = self.x
self.blocks = []
if block_type == "None": return
for r in range(int(1/main.world.step_length)):
x = self.x
for i in range(int(1/main.world.step_length)):
block = Block()
if block_type == "Rock": block.set_to_rock()
block.position = (y, x)
self.blocks += [block ]
x = round(x + main.world_step, 2)
if i == 9: y = round(y + main.world_step, 2)
def set_floor(self, floor_type):
y = self.y
x = self.x
self.floors = []
if floor_type == "None": return
steps = 1/main.world_step
steps = int(steps + 1)
for r in range(steps):
x = self.x
for i in range(steps):
floor = Floor()
if floor_type == "Concrete": floor.set_to_concrete()
if floor_type == "Grass": floor.set_to_grass()
if floor_type == "Forest": floor.set_to_forest()
if floor_type == "Sand": floor.set_to_sand()
if floor_type == "Rust Wood": floor.set_to_rustwood()
if floor_type == "Water": floor.set_to_water()
if floor_type == "Wood": floor.set_to_wood()
floor.position = (y, x)
self.floors += [floor]
print(f"{y=:}, {x=:}.")
print(f"{i=:}.")
x = round(x + main.world_step, 2)
if i == steps - 1: y = round(y + main.world_step, 2)
def say_blocked(self, position):
for bl in self.blocks:
if bl.position == position:
tolk.output(f"{bl.name}.")
def say_floor(self):
position = main.player.position[:2]
position = list(position)
position[0] = main.fix_decimal(value=position[0], prec=1)
position[0] = main.fix_decimal(value=position[1], prec=1)
floor = None
for fl in self.floors:
if (position[0], position[1]) == fl.position:
floor = fl.name
if floor: tolk.output(f"{fl.name}.")
elif not floor: tolk.output(f"Empty.")
def update(self):
pass
class Main:
def __init__(self, size=[1024, 768]):
# Pygame initial settings.
pygame.init()
pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption("Project360")
# Synthizer initial settings
syn.initialize()
self.ctx = syn.Context()
self.ctx.default_panner_strategy.value = syn.PannerStrategy.HRTF
self.ctx.default_distance_max.value = 10
# Other initial settings.
self.locations = []
self.macro_mode = 1
self._mouse_timer = 0
self.mouse_timer = 5
self.mouse_max_speed = 3
self.sounds = []
self.sensor = 0
self.world = None
self.world_step = 0.025
def _walk(self):
self.world.init_tiles()
self.world.add_player(
degrees=0, name="player1", position=[104.2, 102.4, 0])
self.player = self.world.players[0]
self.wmap = self.world.map
position = self.player.position
self.pos = self.wmap[int(position[0])][int(position[1])]
tolk.output(f"Explorer.",1)
self.center_mouse()
while True:
pygame.time.Clock().tick(60)
self.update()
self.player.update()
self.get_pressed_keys()
if self.sensor: self.sensor_event()
self.keys_object_movement()
for event in pygame.event.get():
self.mouse_input(event)
if event.type == pygame.KEYDOWN:
self.keys_set_degree(event)
self.keys_global(event)
if event.key == pygame.K_F12:
tolk.output(f"Debug On.")
Pdb().set_trace()
tolk.output(f"Debug Off.")
if event.key == pygame.K_ESCAPE:
exit()
def _edit_map(self):
self.world.init_tiles()
self.wmap = self.world.map
self.world.add_player(0, name="Editor", position=[102.4, 102.4, 0])
self.player = self.world.players[0]
self.player.set_editor()
self.pos = self.wmap[0][0]
self.y, self.x = self.player.position[0], self.player.position[1]
self.xrange = 0
self.yrange = 0
self.tiles = []
self.positions = set([])
self.goto_position(self.player.position[1], self.player.position[0])
tolk.output(f"Editor.",1)
while True:
pygame.time.Clock().tick(60)
self.update()
self.get_pressed_keys()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
self.keys_map_editor(event)
self.keys_global(event)
self.keys_set_degree(event)
if event.key == pygame.K_F12:
tolk.output(f"Debug On.")
Pdb().set_trace()
tolk.output(f"Debug Off.")
if event.key == pygame.K_ESCAPE:
exit()
def test(self):
items = [1 for i in range(10000000)]
for r in range(len(items)-1): items[r]
#for it in items: it
tolk.output(f"Done.")
def add_directsound(self, sound, vol=1):
buffer = syn.Buffer.from_file(f"{soundpath+sound}.wav")
generator = syn.BufferGenerator(self.ctx)
generator.buffer.value = buffer
generator.looping.value = 0
if vol: generator.gain.value = vol
src = syn.DirectSource(self.ctx)
src.add_generator(generator)
def add_item(self):
pass
def add_jumppoint(self):
name = self.set_attr(attrtype="str")
if name == None: return
jumppoint = EmptyClass()
jumppoint.position = self.player.position
jumppoint.name = name
self.world.jumppoints += [jumppoint]
def add_location_event(self):
name = self.set_attr("str")
if name == None: return
location = EmptyClass()
self.world.locations += [location]
location.name = name
location.position = self.player.position
location.locations = list(self.positions)
def add_source3d(
self, sound, gain=None, loop=0, linger=None, position=(), z=0,
ds_ref=None, ds_max=None, pitch=None, rolloff=None, play=1,
info=0):
if info: print(f"New Buffer {sound}.")
buffer = None
if ".wav" not in sound: sound += ".wav"
for sn in self.sounds:
if sn[0] == sound:
buffer = sn[1]
break
if buffer == None:
buffer = syn.Buffer.from_file(f"{soundpath+sound}")
self.sounds += [[sound, buffer]]
generator = syn.BufferGenerator(self.ctx)
generator.buffer.value = buffer
if pitch: generator.pitch_bend.value = pitch
generator.looping.value = loop
source = syn.Source3D(self.ctx, position=position)
if ds_max: source.distance_max.value = ds_max
if ds_ref: source.distance_ref.value = ds_ref
if gain: source.gain.value = gain
if rolloff: source.rolloff.value = rolloff
if linger:
generator.config_delete_behavior(linger=True)
source.config_delete_behavior(linger=True)
if play: source.add_generator(generator)
return buffer, generator, source
def add_sound_event(self):
name = self.set_attr(attrtype="str")
if name == None: return
source = SoundEvent(self.player.position)
source.name = name
self.world.sources += [source]
main.view_sound_events(-1)
def center_mouse(self):
sizes = pygame.display.get_desktop_sizes()
self.screen_size = [sizes[0][0], sizes[0][1]]
pygame.mouse.set_pos(self.screen_size[0]/2, self.screen_size[0]/2)
def fix_decimal(self, value, prec=1):
value = str(value)
dotindex = value.rfind(".")
if dotindex < 0: return float(value)
lng = len(value) - 1
dec_digits = abs(dotindex - lng)
if dec_digits > prec:
value = value[:(dotindex+1)+prec]
return float(value)
def fix_position(self, position, divisor):
if not position: return
while True:
division = round(position / divisor, 4)
if division == int(division): return position
else: position = round(position + 0.001, 3)
def get_distance(self, source, current):
distx = abs(source[0] - current[0])
disty = abs(source[1] - current[1])
#distz = abs(source[2] - current[2])
distance = max([distx, disty])
return distance
def get_i2d(self, lst, obj,timer=0):
if timer:print(f"index starts at {pygame.time.get_ticks()}.")
for y in range(len(lst)):
for x in range(len(lst[y])):
if lst[y][x] == obj:
location = [x, y, 0]
if timer: print(f"ends at {pygame.time.get_ticks()}.")
return location
def get_options(self, items):
items.sort()
say = 1
x = 0
while True:
pygame.time.Clock().tick(60)
if say:
say = 0
tolk.output(f"{items[x]}")
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
x = self.selector(items, x, "up")
say = 1
if event.key == pygame.K_DOWN:
x = self.selector(items, x, "down")
say = 1
if event.key == pygame.K_RETURN:
tolk.silence()
return items[x]
if event.key == pygame.K_ESCAPE:
tolk.silence()
return
def get_pressed_keys(self):
self.key_pressed = pygame.key.get_pressed()
self.alt = self.key_pressed[pygame.K_LALT]
self.ctrl = self.key_pressed[pygame.K_LCTRL]
self.ctrl += self.key_pressed[pygame.K_RCTRL]
self.shift = self.key_pressed[pygame.K_LSHIFT]
self.shift += self.key_pressed[pygame.K_RSHIFT]
def get_sound(self, path=soundpath, filext=".wav", islist=0):
src = None
say = 1
x = 0
if islist == 0:
sounds = glob(os.path.join(path + filext))
sounds = natsort.natsorted(sounds)
else: sounds = path
while True:
pygame.time.Clock().tick(30)
if say:
say = 0
if sounds:
if islist == 0:
sound = sounds[x][sounds[x].find("sounds")+7:]
else: sound = sounds[x]
tolk.output(f"{sound}")
else: tolk.output(f"No sounds.")
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if src == None:
buffer, generator, src = main.add_source3d(
sound, linger=1,
position=self.player.position, ds_ref=1,)
elif src:
src.remove_generator(generator)
src.dec_ref()
generator.dec_ref()
src = None
if event.key == pygame.K_UP:
x = self.selector(sounds, x, go="up")
say = 1
if event.key == pygame.K_DOWN:
x = self.selector(sounds, x, go="down")
say = 1
if event.key == pygame.K_HOME:
x = 0
say = 1
if event.key == pygame.K_END:
x = len(sounds) - 1
say = 1
if event.key == pygame.K_PAGEUP:
x -= 10
if x < 0: x = 0
say = 1
if event.key == pygame.K_PAGEDOWN:
x += 10
if x >= len(sounds): x = len(sounds) - 1
say = 1
if event.key == pygame.K_RETURN:
tolk.silence()
if src:
src.remove_generator(generator)
src.dec_ref()
generator.dec_ref()
if sounds:return sound
else: return None
if event.key == pygame.K_F12:
tolk.output(f"Debug Onn.",1)
Pdb().set_trace()
tolk.output(f"Debug Off.",1)
if event.key == pygame.K_ESCAPE:
tolk.silence()
if src:
src.remove_generator(generator)
src.dec_ref()
generator.dec_ref()
return
def get_radians(self, degrees):
r = math.radians(degrees)
sin = math.sin(r)
cos = math.cos(r)
sin = round(sin, 6)
cos = round(cos, 6)
return [sin, cos]
def get_xrange(self, xrange, dec, extra=0):
x = self.player.position[1]
if extra:
extra = 0
if int(x + xrange) > int(x): extra = 1
if int(x + xrange) < int(x): extra = -1
xrange = xrange + extra
x = int(x)
xrange = int(xrange)
xrange = [x, x + xrange]
xrange = numpy.arange(min(xrange), max(xrange) + dec, dec)
xrange = [round(it,1) for it in xrange]
return xrange
def get_xrange_dec(self, xrange, dec):
x = self.player.position[1]
xrange = [x, x + xrange]
xrange = numpy.arange(
round(min(xrange), 4),
round(max(xrange) + dec, 4),
dec)
xrange = [round(it,4) for it in xrange]
return xrange
def get_yrange(self, yrange, dec, extra=0):
y = self.player.position[0]
if extra:
extra = 0
if int(y + yrange) > int(y): extra = 1
if int(y + yrange) < int(y): extra = -1
yrange = yrange + extra
y = int(y)
yrange = int(yrange)
yrange = [y, y + yrange]
yrange = numpy.arange(min(yrange), max(yrange) + dec, dec)
yrange = [round(it,1) for it in yrange]
return yrange
def get_yrange_dec(self, yrange, dec=0.10000):
y = self.player.position[0]
yrange = [y, y + yrange]
yrange = numpy.arange(
round(min(yrange), 4),
round(max(yrange) + dec, 4),
dec)
yrange = [round(it,4) for it in yrange]
return yrange
def goto_position(self, y=None, x=None):
if not y: y = self.player.position[0]
if not x: x = self.player.position[1]
y = self.fix_position(position=y, divisor=self.world_step)
x = self.fix_position(position=x, divisor=self.world_step)
self.player.position = y, x, 0
self.set_map_move()
def init_source3d(self, info=0):
for it in self.world.sources:
if it.sounds == []: continue
if it.source: continue
distance = self.get_distance(it.position, self.player.position)
if distance < self.ctx.default_distance_max.value:
if info: print(f"Adding sound in {it.position}.")
sound = choice(it.sounds)
it._position = list(it.position)
if hasattr(it, "ratio") == False: it.ratio = 0
if it.ratio:
it._position[0]= uniform(it._position[0]- it.ratio,
it._position[0] + it.ratio)
it._position[1]= uniform(it._position[1]- it.ratio,
it._position[1] + it.ratio)
if info: print(f"name {it.name}, pos {it._position}.")
bf, gen, src = self.add_source3d(
sound, position=it._position, linger=1, play=0)
it.buffer = bf
it.generator = gen
it.source = src
it.sound_name = sound
it.generator.looping.value = it.loop
it.source.distance_ref.value = it.distance_ref
it.source.gain.value = it.gain
it.source.rolloff.value = it.rolloff
it.source.add_generator(it.generator)
def keys_edit_tile(self, event):
if event.key == pygame.K_F3:
tolk.output(f"View.",1)
items = ["Sound Events", "Locations", "Jump Points"]
selected = self.get_options(items)
if selected == "Sound Events": self.view_sound_events()
elif selected == "Locations": self.view_location_events()
elif selected == "Jump Points": self.view_jumppoint()
if event.key == pygame.K_F9:
self.save_map()
if event.key == pygame.K_a and self.ctrl:
tolk.output(f"Add.",1)
items = ["Sound Event", "Location", "Jump Point"]
selected = self.get_options(items)
if selected == "Jump Point": self.add_jumppoint()
elif selected == "Location": self.add_location_event()
if selected == "Sound Event": self.add_sound_event()
if event.key == pygame.K_b:
if not self.positions:
tolk.output(f"No tiles selected.")
return
selected = self.select_block()
if not selected: return
self.set_block(selected)
if event.key == pygame.K_f:
if not self.positions: return
selected = self.select_floor()
if selected == None: return
self.set_floor(selected)
if event.key == pygame.K_i:
self.add_item()
if event.key == pygame.K_m:
if self.macro_mode:
self.macro_mode = 0
tolk.output(f"Macro Off",1)
elif self.macro_mode == 0:
self.macro_mode = 1
tolk.output(f"Macro On",1)
if event.key == pygame.K_n:
name = self.set_attr(attrtype="str")
if name: self.pos.name = name
if event.key == pygame.K_q:
self.pos.say_floor()
if event.key == pygame.K_s:
tolk.output(f"{len(self.tiles)} tiles selected.")
if event.key == pygame.K_t:
if not self.tiles:
tolk.output(f"No tiles selected.")
return
options = ["Block", "Floor"]
selected = self.get_options(options)
if selected == "Block":
selected = self.select_block()
if not selected: return
[it.set_block(selected) for it in self.tiles]
elif selected == "Floor":
selected = self.select_floor()
if not selected: return
[it.set_floor(selected) for it in self.tiles]
if event.key == pygame.K_x and self.shift == 0:
tolk.output(f"{self.player.degrees}>")
if event.key == pygame.K_x and self.shift:
self.player.say_degrees()
if event.key == pygame.K_y and not self.shift:
tolk.output(f"Enter a value for Y.",1)
value = self.set_attr(attrtype="float")
value = self.fix_position(value, self.world_step)
if value: self.yrange = value
elif event.key == pygame.K_y and self.shift:
tolk.output(f"Enter a value for X.",1)
value = self.set_attr(attrtype="float")
value = self.fix_position(value, self.world_step)
if value: self.xrange = value
if event.key == pygame.K_PAGEUP:
self.xrange = round(self.xrange - self.world_step, 3)
if self.ctrl:
for r in range(9):
self.xrange = round(self.xrange - self.world_step, 3)
tolk.output(f"X Range {self.say_range(self.xrange)}.")
if event.key == pygame.K_PAGEDOWN:
self.xrange = round(self.xrange + self.world_step, 3)
if self.ctrl:
for r in range(9):
self.xrange = round(self.xrange + self.world_step, 3)
tolk.output(f"X Range {self.say_range(self.xrange)}.")
if event.key == pygame.K_HOME:
self.yrange = round(self.yrange + self.world_step, 3)
if self.ctrl:
for r in range(9):
self.yrange = round(self.yrange + self.world_step, 3)
tolk.output(f"Y Range {self.say_range(self.yrange)}.")
if event.key == pygame.K_END:
self.yrange = round(self.yrange - self.world_step, 3)
if self.ctrl:
for r in range(9):
self.yrange = round(self.yrange - self.world_step, 3)
tolk.output(f"Y range {self.say_range(self.yrange)}.")
if event.key == pygame.K_DELETE:
self.yrange =0
self.xrange = 0
tolk.output(f"Ranges reseted.")
if event.key == pygame.K_BACKSPACE:
self.positions.clear()
self.tiles = []
tolk.output(f"Cleaned.", 1)
if event.key == pygame.K_SPACE and self.ctrl:
tolk.silence()
if self.macro_mode:
self.select_square(remove=1)
else:
self.select_tile(remove=1)
elif event.key == pygame.K_SPACE:
tolk.silence()
if self.macro_mode:
self.select_square()
else:
self.select_tile()
def keys_global(self, event):
if event.key == pygame.K_e:
if self.sensor == 0:
self.sensor = 1
tolk.output(f"Sensor On.")
elif self.sensor:
self.sensor = 0
tolk.output(f"Sensor Off.")
if event.key == pygame.K_r:
self.player.set_run()
if event.key == pygame.K_v:
self.player.say_location()
if event.key == pygame.K_x and not self.shift:
self.player.say_cdt()
if event.key == pygame.K_x and self.shift:
tolk.output(f"{self.player.degrees}.")
if event.key == pygame.K_z:
tolk.output(f"{self.ctx.orientation.value}.")
if event.key == pygame.K_F11:
mem = psutil.Process(os.getpid()).memory_info().rss/1024
tolk.output(f"{mem}.")
def keys_map_editor(self, event):
self.keys_edit_tile(event)
# Map movement.
if event.key == pygame.K_g:
msg = f"Enter a float value for y."
tolk.output(msg)
y = self.set_attr(attrtype="float")
msg = f"Enter a float value for x."
tolk.output(msg)
x = self.set_attr(attrtype="float")
self.goto_position(y, x)
if event.key == pygame.K_DOWN:
if self.ctrl == 0: self.move_editor(self.player, 180)
elif self.ctrl:
for r in range(10): self.move_editor(self.player, 180)
self.set_map_move()
if event.key == pygame.K_UP:
if self.ctrl == 0: self.move_editor(self.player, 0)
elif self.ctrl:
for r in range(10): self.move_editor(self.player, 0)
self.set_map_move()
if event.key == pygame.K_LEFT:
if self.ctrl == 0: self.move_editor(self.player, 270)
elif self.ctrl:
for r in range(10): self.move_editor(self.player, 270)
self.set_map_move()
if event.key == pygame.K_RIGHT:
if self.ctrl == 0: self.move_editor(self.player, 90)
elif self.ctrl:
for r in range(10): self.move_editor(self.player, 90)
self.set_map_move()
def keys_object_movement(self):
if self.key_pressed[pygame.K_w]:
output = self.Move_object(self.player)
if isinstance(output, str): tolk.output(output)
if self.key_pressed[pygame.K_s]:
self.Move_object(self.player, 1)
def keys_set_degree(self, event):
if self.player.editor:
if event.key == pygame.K_LEFT and self.shift:
degrees = self.player.degrees
degrees -= 45
self.player.set_degrees(degrees)
self.player.say_degrees()
if event.key == pygame.K_RIGHT and self.shift:
degrees = self.player.degrees
degrees += 45
self.player.set_degrees(degrees)
self.player.say_degrees()
if self.player.editor == 0:
if event.key == pygame.K_a:
degrees = self.player.degrees
if self.shift: degrees -= 180
else: degrees -= 45
self.player.set_degrees(degrees)
self.player.say_degrees()
if event.key == pygame.K_d:
degrees = self.player.degrees
if self.shift: degrees += 180
else: degrees += 45
self.player.set_degrees(degrees)
self.player.say_degrees()
def load_map(self, location, filext, saved=0):
x = 0
say = 1
maps = glob(os.path.join(location + filext))
maps = natsort.natsorted(maps)
while True:
pygame.time.Clock().tick(30)
if say:
say = 0
if maps:
file = open(maps[x], "rb")
name = pickle.load(file)