-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtick.py
1167 lines (905 loc) · 42.4 KB
/
tick.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
"""Provides the `tick` function, which does all of the game's updates.
At regular intervals, the AI of all entities are processed, gravity is applied,
new entities are spawned, other entities are removed, collisions occur, etc.
"""
from entity import Entity
from player import Player
from client import ClientState
from server import ServerState, Window
from dimension import Dimension
from util import BlockPos, roundHalfUp, ChunkPos
import util
import world
from world import World, isSolid, Furnace
import time
import math
import config
from math import cos, sin
import copy
import random
import network
import resources
from inventory import Stack, Slot
import inventory
import typing
from typing import Optional, List, Tuple
from quarry.types.uuid import UUID
from quarry.types.chat import Message
def sendPlayerDigging(app, action: network.DiggingAction, location: BlockPos, face: int):
if hasattr(app, 'server'):
server: ServerState = app.server
if action == network.DiggingAction.START_DIGGING:
server.breakingBlock = 0.0
server.breakingBlockPos = location
elif action == network.DiggingAction.CANCEL_DIGGING:
server.breakingBlock = 0.0
elif action == network.DiggingAction.FINISH_DIGGING:
server.breakingBlock = 1000.0
elif action == network.DiggingAction.DROP_ITEM:
# TODO: Spawn the item
player = server.getLocalPlayer()
slot = player.inventory[player.hotbarIdx]
if not slot.stack.isEmpty() and not slot.stack.isInfinite():
slot.stack.amount -= 1
else:
print(f"Ignoring other action {action}")
else:
network.c2sQueue.put(network.PlayerDiggingC2S(action, location, face))
def sendPlayerLook(app, yaw: float, pitch: float, onGround: bool):
if hasattr(app, 'server'):
player = app.server.getLocalPlayer()
player.headYaw = yaw
player.headPitch = pitch
player.onGround = onGround
else:
network.c2sQueue.put(network.PlayerLookC2S(yaw, pitch, onGround))
def sendPlayerPosition(app, x, y, z, onGround):
if hasattr(app, 'server'):
player = app.server.getLocalPlayer()
player.pos = [x, y, z]
player.onGround = onGround
else:
network.c2sQueue.put(network.PlayerPositionC2S(x, y, z, onGround))
def getSlotsInWindow(server: ServerState, windowId: int) -> Tuple[Stack, List[Slot]]:
if windowId == 0:
player = server.getLocalPlayer()
if player.entityId not in server.craftSlots:
server.craftSlots[player.entityId] = [Slot(canInput=False)] + [Slot() for _ in range(4)]
# TODO: Armor slots
baseSlots = server.craftSlots[player.entityId] + [Slot() for _ in range(4)]
else:
window = server.openWindows[windowId]
player = None
for p in server.players:
if p.entityId == window.playerId:
player = p
break
if player is None:
raise Exception(f'Window click from nonexistent player {window.playerId}')
if window.kind == 'furnace':
(chunk, localPos) = server.getLocalDimension().world.getChunk(window.pos)
furnace: world.Furnace = chunk.tileEntities[localPos]
baseSlots = [furnace.inputSlot, furnace.fuelSlot, furnace.outputSlot]
elif window.kind == 'crafting':
if player.entityId not in server.craftSlots:
server.craftSlots[player.entityId] = [Slot(canInput=False)] + [Slot() for _ in range(9)]
baseSlots = server.craftSlots[player.entityId]
else:
raise Exception(f'Unknown window kind {window.kind}')
slots = baseSlots + player.inventory[9:36] + player.inventory[0:9]
if player.entityId not in server.heldItems:
server.heldItems[player.entityId] = Stack('', 0)
return (server.heldItems[player.entityId], slots)
def sendClickWindow(app, windowId: int, slotIdx: int, button: int, actionNum: int, mode: int, item, count):
print(f'Sending window ID {windowId} and slot {slotIdx}, action {actionNum}')
if hasattr(app, 'server'):
server: ServerState = app.server
heldItem, slots = getSlotsInWindow(server, windowId)
if windowId == 0 or server.openWindows[windowId].kind == 'crafting':
prevOutput = copy.deepcopy(slots[0].stack)
else:
prevOutput = None
if mode == 0:
if button == 0:
isRight = False
elif button == 1:
isRight = True
else:
raise Exception(f'Invalid button {button} in mode 0')
inventory.onSlotClicked(heldItem, app, isRight, slots[slotIdx])
else:
raise Exception(f'Invalid mode {mode} and button {button}')
if windowId == 0:
assert(prevOutput is not None)
craftingGuiPostClick(slots, False, app, slotIdx, prevOutput)
elif server.openWindows[windowId].kind == 'crafting':
assert(prevOutput is not None)
craftingGuiPostClick(slots, True, app, slotIdx, prevOutput)
else:
network.c2sQueue.put(network.ClickWindowC2S(windowId, slotIdx, button, actionNum, mode, item, count))
def craftingGuiPostClick(slots: List[Slot], is3x3: bool, app, slotIdx, prevOutput: Stack):
if is3x3:
totalCraftSlots = 9+1
else:
totalCraftSlots = 4+1
if slotIdx == 0 and prevOutput != slots[0].stack:
# Something was crafted
for slot in slots[1:totalCraftSlots]:
if slot.stack.amount > 0:
slot.stack.amount -= 1
def toid(s): return None if s.isEmpty() else s.item
rowLen = round(math.sqrt(totalCraftSlots - 1))
c = []
for rowIdx in range(rowLen):
row = []
for colIdx in range(rowLen):
row.append(toid(slots[1 + rowIdx * rowLen + colIdx].stack))
c.append(row)
slots[0].stack = Stack('', 0)
for r in app.recipes:
if r.isCraftedBy(c):
slots[0].stack = copy.copy(r.outputs)
break
def sendCloseWindow(app, windowId: int):
if hasattr(app, 'server'):
server: ServerState = app.server
player = server.getLocalPlayer()
if windowId != 0:
server.openWindows.pop(windowId)
if player.entityId in server.craftSlots:
craftSlots = server.craftSlots.pop(player.entityId)
for craftSlot in craftSlots[1:]:
player.pickUpItem(app, craftSlot.stack)
else:
network.c2sQueue.put(network.CloseWindowC2S(windowId))
def sendUseItem(app, hand: int):
if hasattr(app, 'server'):
server: ServerState = app.server
player: Player = server.getLocalPlayer()
wld = server.getLocalDimension().world
heldSlot = player.inventory[player.hotbarIdx]
if heldSlot.stack.isEmpty():
return
# FIXME:
cameraPos = (player.pos[0], player.pos[1] + player.height, player.pos[2])
if heldSlot.stack.item == 'bucket':
block = wld.lookedAtBlock(player.reach, cameraPos,
player.headPitch, player.headYaw, useFluids=True)
if block is not None:
(pos, _) = block
blockId = wld.getBlock(pos)
blockState = wld.getBlockState(pos)
if blockId in ('water', 'flowing_water') and blockState['level'] == '0':
wld.setBlock((app.textures, app.cube, app.textureIndices), pos, 'air', {})
if not player.creative:
heldSlot.stack.item = 'water_bucket'
elif blockId in ('lava', 'flowing_lava') and blockState['level'] == '0':
wld.setBlock((app.textures, app.cube, app.textureIndices), pos, 'air', {})
if not player.creative:
heldSlot.stack.item = 'lava_bucket'
elif heldSlot.stack.item == 'water_bucket' or heldSlot.stack.item == 'lava_bucket':
block = wld.lookedAtBlock(player.reach, cameraPos,
player.headPitch, player.headYaw, useFluids=False)
if block is not None:
(pos, face) = block
faceIdx = ['left', 'right', 'back', 'front', 'bottom', 'top'].index(face) * 2
pos2 = world.adjacentBlockPos(pos, faceIdx)
if heldSlot.stack.item == 'water_bucket':
blockId = 'flowing_water'
else:
blockId = 'flowing_lava'
wld.setBlock((app.textures, app.cube, app.textureIndices), pos2, blockId, { 'level': '0' })
if not player.creative:
heldSlot.stack.item = 'bucket'
elif heldSlot.stack.item == 'flint_and_steel':
block = wld.lookedAtBlock(player.reach, cameraPos,
player.headPitch, player.headYaw)
if block is not None:
(pos, face) = block
faceIdx = ['left', 'right', 'back', 'front', 'bottom', 'top'].index(face) * 2
pos2 = world.adjacentBlockPos(pos, faceIdx)
portals = findPortalFrame(server, server.getLocalDimension(), pos2)
if portals is not None:
portals, axis = portals
instData = (app.textures, app.cube, app.textureIndices)
for p in portals:
server.getLocalDimension().world.setBlock(instData, p, 'nether_portal', { 'axis': axis }, doBlockUpdates=False)
else:
network.c2sQueue.put(network.UseItemC2S(hand))
def sendTeleportConfirm(app, teleportId: int):
if hasattr(app, 'server'):
pass
else:
network.c2sQueue.put(network.TeleportConfirmC2S(teleportId))
def sendPlayerMovement(app, onGround: bool):
if hasattr(app, 'server'):
player = app.server.getLocalPlayer()
player.onGround = True
else:
network.c2sQueue.put(network.PlayerMovementC2S(onGround))
def syncWindowSlots(server: ServerState, windowId: int):
if windowId == 0:
# TODO:
raise Exception()
else:
for slotIdx, slot in enumerate(getSlotsInWindow(server, windowId)[1]):
if not slot.stack.isEmpty():
itemId = util.REGISTRY.encode('minecraft:item', 'minecraft:' + slot.stack.item)
network.s2cQueue.put(network.SetSlotS2C(windowId, slotIdx, itemId, slot.stack.amount))
def sendPlayerPlacement(app, hand: int, location: BlockPos, face: int, cx: float, cy: float, cz: float, insideBlock: bool):
if hasattr(app, 'server'):
server: ServerState = app.server
player: Player = server.getLocalPlayer()
blockId = server.getLocalDimension().world.getBlock(location)
if blockId == 'crafting_table':
windowId = server.getWindowId()
kind = util.REGISTRY.encode('minecraft:menu', 'minecraft:crafting')
title = Message.from_string('Crafting Table')
server.openWindows[windowId] = Window(player.entityId, location, 'crafting')
network.s2cQueue.put(network.OpenWindowS2C(windowId, kind, title))
syncWindowSlots(server, windowId)
elif blockId == 'furnace':
windowId = server.getWindowId()
kind = util.REGISTRY.encode('minecraft:menu', 'minecraft:furnace')
title = Message.from_string('Furnace')
server.openWindows[windowId] = Window(player.entityId, location, 'furnace')
network.s2cQueue.put(network.OpenWindowS2C(windowId, kind, title))
syncWindowSlots(server, windowId)
elif not player.creative and blockId == 'air':
stack = player.inventory[player.hotbarIdx].stack
if not stack.isInfinite():
stack.amount -= 1
else:
network.c2sQueue.put(network.PlayerPlacementC2S(hand, location, face, cx, cy, cz, insideBlock))
def sendChatMessage(app, text: str):
if hasattr(app, 'server'):
if text.startswith('/'):
text = text.removeprefix('/')
parts = text.split()
server: ServerState = app.server
dim = server.getLocalDimension()
wld, entities = dim.world, dim.entities
print(f"COMMAND {text}")
if parts[0] == 'pathfind':
player: Player = server.getLocalPlayer()
target = player.getBlockPos()
for ent in entities:
ent.updatePath(wld, target)
elif parts[0] == 'give':
itemId = parts[1]
if len(parts) == 3:
amount = int(parts[2])
else:
amount = 1
server.getLocalPlayer().pickUpItem(app, Stack(itemId, amount))
elif parts[0] == 'time':
if parts[1] == 'set':
if parts[2] == 'day':
server.time = 1000
elif parts[2] == 'night':
server.time = 13000
elif parts[2] == 'midnight':
server.time = 18000
else:
server.time = int(parts[2])
elif parts[1] == 'add':
server.time += int(parts[2])
elif parts[0] == 'gamemode':
if parts[1] == 'creative':
server.getLocalPlayer().creative = True
elif parts[1] == 'survival':
server.getLocalPlayer().creative = False
elif parts[0] == 'summon':
player = server.getLocalPlayer()
ent = Entity(app, server.getEntityId(), parts[1],
player.pos[0]+0.5, player.pos[1]+0.5, player.pos[2]+0.5)
app.entities.append(ent)
elif parts[0] == 'explode':
power = int(parts[1])
player = server.getLocalPlayer()
pos = world.nearestBlockPos(player.pos[0], player.pos[1], player.pos[2])
wld.explodeAt(pos, power, (app.textures, app.cube, app.textureIndices))
elif parts[0] == 'dimension':
player = server.getLocalPlayer()
if player.dimension == 'minecraft:overworld':
player.dimension = 'minecraft:the_nether'
elif player.dimension == 'minecraft:the_nether':
player.dimension = 'minecraft:overworld'
else:
raise Exception(player.dimension)
import quarry.types.nbt as quarrynbt
player.portalCooldown = 80
# TODO:
network.s2cQueue.put(network.RespawnS2C(
quarrynbt.TagCompound({}), player.dimension,
0, 0, None, False, False, True
))
elif parts[0] == 'chunkstates':
for dim in server.dimensions:
print(f'== DIMENSION {dim} ==')
for pos, chunk in dim.world.chunks.items():
print(f'{pos} - {chunk.worldgenStage}')
elif parts[0] == 'killall':
toRemove = []
idx = 0
dim = server.getLocalDimension()
while idx < len(dim.entities):
if dim.entities[idx].kind.name != 'player':
toRemove.append(dim.entities[idx].entityId)
dim.entities.pop(idx)
else:
idx += 1
network.s2cQueue.put(network.DestroyEntitiesS2C(toRemove))
elif parts[0] == 'tp':
# TODO:
'''
player = server.getLocalPlayer()
player.pos[0] = float(parts[1])
player.pos[1] = float(parts[2])
player.pos[2] = float(parts[3])
'''
else:
network.c2sQueue.put(network.ChatMessageC2S(text))
def sendClientStatus(app, status: int):
# TODO: This isn't *really* a player joining packet, buuuut...
if hasattr(app, 'server'):
server: ServerState = app.server
player = server.getLocalPlayer()
if player.health <= 0.0:
player.health = 20.0
network.s2cQueue.put(network.UpdateHealthS2C(player.health, 20, 5.0))
network.s2cQueue.put(network.PlayerPositionAndLookS2C(
0.0, 72.0, 0.0, 0.0, 0.0,
False, False, False, True, True, server.getTeleportId()
))
else:
network.s2cQueue.put(network.PlayerPositionAndLookS2C(
player.pos[0], player.pos[1], player.pos[2], 0.0, 0.0,
False, False, False, True, True, server.getTeleportId()
))
'''
for ent in server.entities:
# TODO:
uuid = UUID.random()
kind = util.REGISTRY.encode('minecraft:entity_type', entity.)
network.s2cQueue.put(network.SpawnMobS2C(
ent.entityId, uuid,
))
'''
else:
network.c2sQueue.put(network.ClientStatusC2S(status))
def sendHeldItemChange(app, newSlot: int):
if hasattr(app, 'server'):
player = app.server.getLocalPlayer()
player.hotbarIdx = newSlot
else:
network.c2sQueue.put(network.HeldItemChangeC2S(newSlot))
def sendInteractEntity(app, entityId, kind, *, x=None, y=None, z=None, hand=None, sneaking):
if hasattr(app, 'server'):
server: ServerState = app.server
player = server.getLocalPlayer()
entities = server.getLocalDimension().entities
for ent in entities:
if ent.entityId == entityId:
if kind == network.InteractKind.ATTACK:
knockX = ent.pos[0] - player.pos[0]
knockZ = ent.pos[2] - player.pos[2]
mag = math.sqrt(knockX**2 + knockZ**2)
knockX /= mag
knockZ /= mag
slot = player.inventory[player.hotbarIdx]
if slot.isEmpty():
dmg = 1.0
else:
dmg = 1.0 + resources.getAttackDamage(app, slot.stack.item)
ent.hit(app, dmg, (knockX, knockZ))
break
else:
# TODO:
pass
else:
network.c2sQueue.put(network.InteractEntityC2S(
entityId, kind, x=x, y=y, z=z, hand=hand, sneaking=sneaking
))
def updateBlockBreaking(app, server: ServerState):
pos = server.breakingBlockPos
if server.breakingBlock == 0.0:
return
wld = server.getLocalDimension().world
blockId = wld.getBlock(pos)
blockState = wld.getBlockState(pos)
if blockId == 'air':
print(f'Invalid mining at {server.breakingBlockPos}')
return
# HACK:
player = server.getLocalPlayer()
toolStack = player.inventory[player.hotbarIdx].stack
if toolStack.isEmpty():
tool = ''
else:
tool = toolStack.item
hardness = resources.getHardnessAgainst(blockId, tool)
# TODO: Sound effect packets
if server.breakingBlock >= hardness:
if blockId == 'oak_log':
blockState['axis'] = 'y'
mcBlockId = util.REGISTRY.encode_block({ 'name': 'minecraft:' + blockId } | blockState)
network.s2cQueue.put(network.AckPlayerDiggingS2C(
pos,
mcBlockId,
network.DiggingAction.FINISH_DIGGING,
True
))
droppedItem = resources.getBlockDrop(app, blockId, tool)
resources.getDigSound(app, blockId).play()
wld.setBlock((app.textures, app.cube, app.textureIndices), pos, 'air')
server.breakingBlock = 0.0
if droppedItem is not None:
stack = Stack(droppedItem, 1)
entityId = server.getEntityId()
xVel = ((random.random() - 0.5) * 0.1)
yVel = ((random.random() - 0.5) * 0.1)
zVel = ((random.random() - 0.5) * 0.1)
# TODO: UUID
network.s2cQueue.put(network.SpawnEntityS2C(entityId, None, 37,
float(pos.x), float(pos.y), float(pos.z), 0.0, 0.0, 1,
int(xVel * 8000), int(yVel * 8000), int(zVel * 8000)))
itemId = util.REGISTRY.encode('minecraft:item', 'minecraft:' + stack.item)
network.s2cQueue.put(network.EntityMetadataS2C(
entityId, { (6, 7): { 'item': itemId, 'count': stack.amount } }
))
ent = Entity(app, entityId, 'item', float(pos.x), float(pos.y), float(pos.z))
ent.extra.stack = stack
ent.velocity = [xVel, yVel, zVel]
server.getLocalDimension().entities.append(ent)
def findPortalFrame(server: ServerState, dim: Dimension, pos: BlockPos) -> Optional[Tuple[List[BlockPos], str]]:
bottomPos1 = None
topPos1 = None
for i in range(1, 4):
p = BlockPos(pos.x, pos.y - i, pos.z)
if dim.world.getBlock(p) == 'obsidian':
bottomPos1 = p
break
if bottomPos1 is None:
return None
for i in range(1, 4):
p = BlockPos(pos.x, pos.y + i, pos.z)
if dim.world.getBlock(p) == 'obsidian':
topPos1 = p
break
if topPos1 is None:
return None
if topPos1.y - bottomPos1.y != 4:
return None
for dx, dz in ((-1, 0), (1, 0), (0, -1), (0, 1)):
bottomPos2 = BlockPos(bottomPos1.x + dx, bottomPos1.y, bottomPos1.z + dz)
topPos2 = BlockPos(topPos1.x + dx, topPos1.y, topPos1.z + dz)
# Need 2 blocks on top, 2 blocks on bottom
if dim.world.getBlock(bottomPos2) != 'obsidian':
continue
if dim.world.getBlock(topPos2) != 'obsidian':
continue
answer = []
ok = True
for i in range(3):
backSidePos = BlockPos(bottomPos1.x - dx, bottomPos1.y + 1 + i, bottomPos1.z - dz)
backMidPos = BlockPos(bottomPos1.x, bottomPos1.y + 1 + i, bottomPos1.z)
frontMidPos = BlockPos(bottomPos2.x, bottomPos2.y + 1 + i, bottomPos2.z)
frontSidePos = BlockPos(bottomPos2.x + dx, bottomPos2.y + 1 + i, bottomPos2.z + dz)
if (dim.world.getBlock(backSidePos) != 'obsidian'
or dim.world.getBlock(frontSidePos) != 'obsidian'
or dim.world.getBlock(backMidPos) != 'air'
or dim.world.getBlock(frontMidPos) != 'air'):
ok = False
break
answer.append(backMidPos)
answer.append(frontMidPos)
if ok:
return (answer, 'x' if dx != 0 else 'z')
return None
def getDestination(app, world: World, searchPos: BlockPos, maxHeight: int) -> BlockPos:
existing = findPortalNear(world, searchPos, maxHeight)
if existing is not None:
print(f'Found existing portal at {existing}')
return existing
spot = findSpaceForPortal(world, searchPos, maxHeight)
if spot is not None:
print(f'Found space for portal at {spot}')
createPortalAt(app, world, spot, clearNearby=False)
return BlockPos(spot.x, spot.y + 1, spot.z)
forcedPos = BlockPos(searchPos.x, maxHeight - 20, searchPos.z)
createPortalAt(app, world, forcedPos, clearNearby=True)
return BlockPos(forcedPos.x, forcedPos.y + 1, forcedPos.z)
def findPortalNear(world: World, blockPos: BlockPos, maxHeight: int) -> Optional[BlockPos]:
for totalDist in range(32):
for xDist in range(totalDist):
zDist = totalDist - xDist
for y in range(maxHeight):
if world.getBlock(BlockPos(blockPos.x + xDist, y, blockPos.z + zDist)) == 'nether_portal':
return BlockPos(blockPos.x + xDist, y, blockPos.z + zDist)
if world.getBlock(BlockPos(blockPos.x + xDist, y, blockPos.z - zDist)) == 'nether_portal':
return BlockPos(blockPos.x + xDist, y, blockPos.z + zDist)
if world.getBlock(BlockPos(blockPos.x - xDist, y, blockPos.z + zDist)) == 'nether_portal':
return BlockPos(blockPos.x + xDist, y, blockPos.z + zDist)
if world.getBlock(BlockPos(blockPos.x - xDist, y, blockPos.z - zDist)) == 'nether_portal':
return BlockPos(blockPos.x + xDist, y, blockPos.z + zDist)
return None
def createPortalAt(app, world: World, blockPos: BlockPos, clearNearby: bool):
instData = (app.textures, app.cube, app.textureIndices)
for dx in (-1, 0, 1, 2):
world.setBlock(instData, BlockPos(blockPos.x + dx, blockPos.y, blockPos.z), 'obsidian', {})
world.setBlock(instData, BlockPos(blockPos.x + dx, blockPos.y + 4, blockPos.z), 'obsidian', {})
for dy in (1, 2, 3):
world.setBlock(instData, BlockPos(blockPos.x - 1, blockPos.y + dy, blockPos.z), 'obsidian', {})
world.setBlock(instData, BlockPos(blockPos.x + 2, blockPos.y + dy, blockPos.z), 'obsidian', {})
for dx in (0, 1):
for dy in (1, 2, 3):
world.setBlock(instData, BlockPos(blockPos.x + dx, blockPos.y + dy, blockPos.z), 'nether_portal', { 'axis': 'x' }, doBlockUpdates=False)
if clearNearby:
for dz in (-1, 1):
for dx in (-1, 0, 1, 2):
for dy in (0, 1, 2, 3):
if dx in (0, 1) and dy == 0:
blockId = 'obsidian'
blockState = {}
else:
blockId = 'air'
blockState = {}
world.setBlock(instData, BlockPos(blockPos.x + dx, blockPos.y + dy, blockPos.z + dz), blockId, blockState)
def findSpaceForPortal(world: World, blockPos: BlockPos, maxHeight: int) -> Optional[BlockPos]:
def isValidPos(pos: BlockPos):
for dy in range(0, 4):
for dx in range(-1, 4):
blockId = world.getBlock(BlockPos(pos.x + dx, pos.y + dy, pos.z))
if dy == 0:
if not isSolid(blockId):
return False
else:
if blockId != 'air':
return False
return True
for totalDist in range(16):
for xDist in range(totalDist):
zDist = totalDist - xDist
for y in range(10, maxHeight - 4):
if isValidPos(BlockPos(blockPos.x + xDist, y, blockPos.z + zDist)):
return BlockPos(blockPos.x + xDist, y, blockPos.z + zDist)
if isValidPos(BlockPos(blockPos.x + xDist, y, blockPos.z - zDist)):
return BlockPos(blockPos.x + xDist, y, blockPos.z - zDist)
if isValidPos(BlockPos(blockPos.x - xDist, y, blockPos.z + zDist)):
return BlockPos(blockPos.x - xDist, y, blockPos.z + zDist)
if isValidPos(BlockPos(blockPos.x - xDist, y, blockPos.z - zDist)):
return BlockPos(blockPos.x - xDist, y, blockPos.z - zDist)
return None
def updatePlayerPos(client: ClientState):
player: Player = client.player
playerChunkPos = world.toChunkLocal(player.getBlockPos())[0]
playerChunkPos = ChunkPos(playerChunkPos.x, 0, playerChunkPos.z)
# W makes the player go forward, S makes them go backwards,
# and pressing both makes them stop!
z = float(client.w) - float(client.s)
# Likewise for side to side movement
x = float(client.d) - float(client.a)
ticks = min((time.perf_counter() - client.lastFrameTime) / 0.05, 2)
if playerChunkPos in client.world.chunks and client.world.chunks[playerChunkPos].isTicking:
if x != 0.0 or z != 0.0:
mag = math.sqrt(x*x + z*z)
x /= mag
z /= mag
newX = math.cos(client.cameraYaw) * x - math.sin(client.cameraYaw) * z
newZ = math.sin(client.cameraYaw) * x + math.cos(client.cameraYaw) * z
x, z = newX, newZ
x *= player.walkSpeed
z *= player.walkSpeed
#player.tick(app, app.world, app.entities, 0.0, 0.0)
if player.flying:
if client.space:
player.velocity[1] = 0.2
elif client.shift:
player.velocity[1] = -0.2
else:
player.velocity[1] = 0.0
collideY(client.world, player, ticks, useGravity=False)
if player.onGround:
player.velocity[0] = x
player.velocity[2] = z
else:
player.velocity[0] += x / 10.0
player.velocity[2] += z / 10.0
player.velocity[0] *= 0.9
player.velocity[2] *= 0.9
collideXZ(client.world, player, ticks)
client.cameraPos = copy.copy(player.pos)
client.cameraPos[1] += player.height
def clientTick(client: ClientState, instData):
startTime = time.time()
client.time += 1
if not client.local:
chunkPos, _ = world.toChunkLocal(client.player.getBlockPos())
client.world.tickets[chunkPos] = 1
client.world.loadUnloadChunks(instData)
client.world.addChunkDetails(instData, needUrgent=False)
client.world.flushLightChanges()
player: Player = client.player
if not player.onGround:
player.velocity[1] -= GRAVITY
playerChunkPos = world.toChunkLocal(player.getBlockPos())[0]
playerChunkPos = ChunkPos(playerChunkPos.x, 0, playerChunkPos.z)
for entity in client.entities:
entChunkPos = world.toChunkLocal(entity.getBlockPos())[0]
entChunkPos = ChunkPos(entChunkPos.x, 0, entChunkPos.z)
if entChunkPos not in client.world.chunks or not client.world.chunks[entChunkPos].isTicking:
continue
entity.clientTick()
#collide(client, entity)
endTime = time.time()
client.tickTimes[client.tickTimeIdx] = (endTime - startTime)
client.tickTimeIdx += 1
client.tickTimeIdx %= len(client.tickTimes)
client.lastTickTime = endTime
def serverTick(app, server: ServerState):
startTime = time.time()
server.time += 1
instData = (app.textures, app.cube, app.textureIndices)
for i, dim in enumerate(server.dimensions):
player = server.getLocalPlayer()
chunkPos, _ = world.toChunkLocal(player.getBlockPos())
if i == 0 and player.dimension == 'minecraft:overworld':
dim.world.addTicket(chunkPos, 1)
elif i == 1 and player.dimension == 'minecraft:the_nether':
dim.world.addTicket(chunkPos, 1)
dim.world.loadUnloadChunks((app.textures, app.cube, app.textureIndices))
dim.world.addChunkDetails(instData)
dim.world.tickChunks(app)
updateBlockBreaking(app, server)
for dim in server.dimensions:
doMobSpawning(app, server, dim)
doMobDespawning(app, server, dim)
# Ticking is done in stages so that collision detection works as expected:
# First we update the player's Y position and resolve Y collisions,
# then we update the player's X position and resolve X collisions,
# and finally update the player's Z position and resolve Z collisions.
# TODO: USE MOVE PACKETS
# W makes the player go forward, S makes them go backwards,
# and pressing both makes them stop!
z = float(app.client.w) - float(app.client.s)
# Likewise for side to side movement
x = float(app.client.d) - float(app.client.a)
for player in server.players:
playerChunkPos = world.toChunkLocal(player.getBlockPos())[0]
playerChunkPos = ChunkPos(playerChunkPos.x, 0, playerChunkPos.z)
dim = server.getDimensionOf(player)
player.tick(app, dim.world, dim.entities, 0.0, 0.0)
if dim.world.getBlock(player.getBlockPos()) == 'nether_portal':
if player.portalCooldown == 0:
import quarry.types.nbt as quarrynbt
if player.dimension == 'minecraft:overworld':
player.dimension = 'minecraft:the_nether'
elif player.dimension == 'minecraft:the_nether':
player.dimension = 'minecraft:overworld'
else:
raise Exception(player.dimension)
player.portalCooldown = 80
destDim = server.getDimension(player.dimension)
destDim.world.addTicket(world.toChunkLocal(player.getBlockPos())[0], 300)
destDim.world.loadUnloadChunks(instData)
destDim.world.addChunkDetails(instData)
dest = getDestination(app, destDim.world, player.getBlockPos(), destDim.world.dimTy.logicalHeight)
# TODO:
network.s2cQueue.put(network.RespawnS2C(
quarrynbt.TagCompound({}), player.dimension,
0, 0, None, False, False, True
))
network.s2cQueue.put(network.PlayerPositionAndLookS2C(
dest.x, dest.y + 1, dest.z, 0.0, 0.0, False, False, False, False, False, server.getTeleportId()
))
else:
player.portalCooldown = 80
'''
collideY(app, player)
if player.onGround:
player.velocity[0] = x
player.velocity[2] = z
else:
player.velocity[0] += x / 10.0
player.velocity[2] += z / 10.0
collideXZ(app, player)
'''
# FIXME: types???
entities = server.getLocalDimension().entities + server.players #type:ignore
# FIXME:
player = server.getLocalPlayer()
for dim in server.dimensions:
for entity in dim.entities:
entChunkPos = world.toChunkLocal(entity.getBlockPos())[0]
entChunkPos = ChunkPos(entChunkPos.x, 0, entChunkPos.z)
if entChunkPos not in dim.world.chunks or not dim.world.chunks[entChunkPos].isTicking:
continue
againstWall = collide(dim.world, entity)
if againstWall and entity.onGround:
entity.velocity[1] = 0.40
entity.tick(app, dim.world, entities, player.pos[0], player.pos[2])
if entity.kind.name == 'item':
dx = (player.pos[0] - entity.pos[0])**2
dy = (player.pos[1] - entity.pos[1])**2
dz = (player.pos[2] - entity.pos[2])**2
if math.sqrt(dx + dy + dz) < 2.0 and entity.extra.pickupDelay == 0:
player.pickUpItem(app, entity.extra.stack)
entity.health = 0.0
if entity.pos[1] < -64.0:
entity.hit(app, 10.0, (0.0, 0.0))
if player.pos[1] < -64.0:
player.hit(app, 10.0, (0.0, 0.0))
network.s2cQueue.put(network.TimeUpdateS2C(0, server.time))
for dim in server.dimensions:
# HACK:
for ent1, ent2 in zip(dim.entities, app.client.entities):
ent1.variables = copy.copy(ent2.variables)
network.s2cQueue.put(network.UpdateHealthS2C(server.getLocalPlayer().health, 20, 5.0))
for windowId, openWindow in server.openWindows.items():
if windowId != 0 and openWindow.kind == 'furnace':
chunk, localPos = server.getLocalDimension().world.getChunk(openWindow.pos)
furnace = typing.cast(Furnace, chunk.tileEntities[localPos])
network.s2cQueue.put(network.WindowPropertyS2C(
windowId, 0, furnace.fuelLeft
))
network.s2cQueue.put(network.WindowPropertyS2C(
windowId, 1, 1600
))
network.s2cQueue.put(network.WindowPropertyS2C(
windowId, 2, furnace.progress
))
network.s2cQueue.put(network.WindowPropertyS2C(
windowId, 3, 200
))
for slotIdx, slot in enumerate((furnace.inputSlot, furnace.fuelSlot, furnace.outputSlot, )):
# FIXME: EMPTY SLOTS??
if slot.stack.isEmpty():
itemId = 'stone'
else:
itemId = slot.stack.item
itemId = util.REGISTRY.encode('minecraft:item', 'minecraft:' + itemId)
network.s2cQueue.put(network.SetSlotS2C(
windowId, slotIdx, itemId, slot.stack.amount
))
app.client.entities = copy.deepcopy(server.getLocalDimension().entities)
app.client.player.inventory = copy.deepcopy(server.getLocalPlayer().inventory)
app.client.player.creative = server.getLocalPlayer().creative
endTime = time.time()
server.tickTimes[server.tickTimeIdx] = (endTime - startTime)
server.tickTimeIdx += 1
server.tickTimeIdx %= len(server.tickTimes)
app.client.serverTickTimes = server.tickTimes
app.client.serverTickTimeIdx = server.tickTimeIdx
def syncClient(app):
# TODO: Copy
client: ClientState = app.client
client.world = app.world
client.entities = app.entities
client.player = app.mode.player
client.time = app.time
#client.tickTimes = app.tickTimes