Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code for episode 14 #57

Open
wants to merge 55 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
747d229
ep13: copy code
obiwac Feb 4, 2022
6667647
ep13: entities data file
obiwac Feb 4, 2022
53d9289
ep13: collision sizes for all entities
obiwac Feb 4, 2022
f3cf6a1
ep12: changed the way acceleration from inputs & friction work
obiwac Feb 11, 2022
c59a27c
Merge branch 'master' of ssh://github.com/obiwac/python-minecraft-clo…
obiwac Feb 11, 2022
b61d290
Merge branch 'master' of ssh://github.com/obiwac/python-minecraft-clo…
obiwac Feb 12, 2022
a89044c
Merge branch 'master' of ssh://github.com/obiwac/python-minecraft-clo…
obiwac Mar 12, 2022
23cf38f
ep13: Mobs prototype
obiwac Mar 14, 2022
62aeff0
ep13: `Mob` class for basic AI
obiwac Mar 14, 2022
e269216
ep13: Entity model normal computation and directional lighting
obiwac Mar 14, 2022
808b646
ep13: `GL_STATIC_DRAW` -> `GL_STREAM_DRAW` in preperation for animations
obiwac Mar 16, 2022
3086746
ep13: Straighten out entity models
obiwac Jun 26, 2022
4ac5470
Merge branch 'mobs' of ssh://github.com/obiwac/python-minecraft-clone…
obiwac Jun 26, 2022
b9055de
Merge branch 'master' of ssh://github.com/obiwac/python-minecraft-clo…
obiwac Jun 26, 2022
690a7fd
ep13: Loading mobs from saves
obiwac Jun 26, 2022
07fbd1f
ep13: Add Stephen Curry
obiwac Jun 26, 2022
61c0c14
ep13: Move entity textures to `textures/`
obiwac Jun 26, 2022
2b35693
ep13: Remove redundant `ai` method in `Entity`
obiwac Jun 26, 2022
d3ffc74
ep13: Syntax highlighting for `data/entities.mcpy`
obiwac Jun 26, 2022
cdb989b
ep13: Don't update entities more than 32 blocks away from the player
obiwac Jun 26, 2022
9058563
ep13: Optimization attempts and 2D texture coordinates for entities
obiwac Jul 17, 2022
5f57e91
ep13: Clean up code a little
obiwac Jul 24, 2022
3829326
ep13: Fix rotational interpolation for mobs
obiwac Jul 24, 2022
f07f524
community: Add mobs (segfaults on Python 3.10)
obiwac Jul 24, 2022
3626802
Fix Segfault on community
Jukitsu Jul 25, 2022
030642d
Merge pull request #73 from Jukitsu/mobs
obiwac Jul 25, 2022
996af6d
Fix transparent values
Jukitsu Sep 28, 2022
13b4fad
Light affects mob
Jukitsu Sep 28, 2022
4eadaf9
Fix some _ lying around
Jukitsu Sep 28, 2022
3576816
Revert fix
Jukitsu Sep 28, 2022
3984b90
Frustum culling for entities
Jukitsu Sep 28, 2022
d9f290c
Changed some stuff as requested by our lord and savior
Jukitsu Sep 28, 2022
0f2ef9f
This is what happens when you don't test: you commit bad code (previo…
Jukitsu Sep 28, 2022
80df202
Refactors
Jukitsu Sep 29, 2022
7824b94
Fix entity lighting
Jukitsu Nov 3, 2022
95a91e6
Rudimentary fix in main.py for pyglet 2
Jukitsu Dec 30, 2022
9cabdae
Revert "Rudimentary fix in main.py for pyglet 2"
Jukitsu Dec 30, 2022
5ee0486
Fix format
Jukitsu Dec 31, 2022
85e3a12
Merge pull request #74 from Jukitsu/mobs
obiwac Dec 31, 2022
fe922e1
ep13: Generate mob models on the fly from bones
obiwac Jan 13, 2023
bc79639
Merge branch 'mobs' of ssh://github.com/obiwac/python-minecraft-clone…
obiwac Jan 13, 2023
5f96035
ep13: Animation
obiwac Jan 13, 2023
dffa832
ep13: Arm animation
obiwac Jan 13, 2023
6bb44a6
ep13: Add mob lighting back
obiwac Jan 13, 2023
2f6a80f
ep13: Fix mob lighting looking off
obiwac Jan 13, 2023
54ff451
ep13: Make mobs constantly look at the player
obiwac Jan 14, 2023
adc85ac
ep13: Fix incorrect normal transformations
obiwac Jan 14, 2023
444f486
ep13: Shade the bottom faces of entities darker
obiwac Jan 14, 2023
cac676b
ep13: Adjust entity lighting parameters
obiwac Jan 14, 2023
901fb38
ep13: Make animation not depend on framerate
obiwac Jan 14, 2023
bfb1b10
community: Merge new changes to mobs
obiwac Jan 14, 2023
a4c7c4e
community: Make sprinting behaviour more similar to Minecraft - fixes…
Arta48 Feb 7, 2023
f76033b
merge: Resolve conflicts
obiwac May 15, 2023
eec9efa
13: Set `double_buffer` to true in GL config
obiwac May 15, 2023
a09bf7c
Merge branch 'master' of ssh://github.com/obiwac/python-minecraft-clo…
obiwac May 15, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions community/block_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import models.cube # default model

class Block_type:
# new optional model argument (cube model by default)
def __init__(self, texture_manager, name = "unknown", block_face_textures = {"all": "cobblestone"}, model = models.cube):
self.name = name
self.block_face_textures = block_face_textures
Expand All @@ -12,6 +11,7 @@ def __init__(self, texture_manager, name = "unknown", block_face_textures = {"al
# create members based on model attributes

self.transparent = model.transparent
self.transparency = model.transparency
self.is_cube = model.is_cube
self.glass = model.glass
self.translucent = model.translucent
Expand All @@ -23,7 +23,7 @@ def __init__(self, texture_manager, name = "unknown", block_face_textures = {"al
for _collider in model.colliders:
self.colliders.append(collider.Collider(*_collider))

# replace data contained in numbers.py with model specific data
# get model specific data

self.vertex_positions = model.vertex_positions
self.tex_coords = model.tex_coords # to deprecate
Expand All @@ -32,8 +32,10 @@ def __init__(self, texture_manager, name = "unknown", block_face_textures = {"al

def set_block_face(face, texture):
# make sure we don't add inexistent face

if face > len(self.tex_coords) - 1:
return

self.tex_indices[face] = texture

for face in block_face_textures:
Expand Down
75 changes: 37 additions & 38 deletions community/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pyglet.gl as gl

import subchunk
import subchunk

import options

Expand All @@ -14,27 +14,26 @@
class Chunk:
def __init__(self, world, chunk_position):
self.world = world
self.shader_chunk_offset_location = self.world.shader.find_uniform(b"u_ChunkPosition")


self.modified = False
self.chunk_position = chunk_position

self.position = (
self.chunk_position[0] * CHUNK_WIDTH,
self.chunk_position[1] * CHUNK_HEIGHT,
self.chunk_position[2] * CHUNK_LENGTH)

self.blocks = [[[0 for z in range(CHUNK_LENGTH)]
for y in range(CHUNK_HEIGHT)]
for x in range(CHUNK_WIDTH)]
# Numpy is really slow there
self.lightmap = [[[0 for z in range(CHUNK_LENGTH)]
for y in range(CHUNK_HEIGHT)]
for x in range(CHUNK_WIDTH)]

self.subchunks = {}
self.chunk_update_queue = deque()

for x in range(int(CHUNK_WIDTH / subchunk.SUBCHUNK_WIDTH)):
for y in range(int(CHUNK_HEIGHT / subchunk.SUBCHUNK_HEIGHT)):
for z in range(int(CHUNK_LENGTH / subchunk.SUBCHUNK_LENGTH)):
Expand All @@ -53,48 +52,48 @@ def __init__(self, world, chunk_position):
self.vao = gl.GLuint(0)
gl.glGenVertexArrays(1, self.vao)
gl.glBindVertexArray(self.vao)

self.vbo = gl.GLuint(0)
gl.glGenBuffers(1, self.vbo)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vbo)
gl.glBufferData(gl.GL_ARRAY_BUFFER, ctypes.sizeof(gl.GLfloat * CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_LENGTH * 7), None, gl.GL_DYNAMIC_DRAW)

gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT,
gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT,
gl.GL_FALSE, 7 * ctypes.sizeof(gl.GLfloat), 0)
gl.glEnableVertexAttribArray(0)
gl.glVertexAttribPointer(1, 1, gl.GL_FLOAT,
gl.glVertexAttribPointer(1, 1, gl.GL_FLOAT,
gl.GL_FALSE, 7 * ctypes.sizeof(gl.GLfloat), 3 * ctypes.sizeof(gl.GLfloat))
gl.glEnableVertexAttribArray(1)
gl.glVertexAttribPointer(2, 1, gl.GL_FLOAT,
gl.glVertexAttribPointer(2, 1, gl.GL_FLOAT,
gl.GL_FALSE, 7 * ctypes.sizeof(gl.GLfloat), 4 * ctypes.sizeof(gl.GLfloat))
gl.glEnableVertexAttribArray(2)
gl.glVertexAttribPointer(3, 1, gl.GL_FLOAT,
gl.glVertexAttribPointer(3, 1, gl.GL_FLOAT,
gl.GL_FALSE, 7 * ctypes.sizeof(gl.GLfloat), 5 * ctypes.sizeof(gl.GLfloat))
gl.glEnableVertexAttribArray(3)
gl.glVertexAttribPointer(4, 1, gl.GL_FLOAT,
gl.glVertexAttribPointer(4, 1, gl.GL_FLOAT,
gl.GL_FALSE, 7 * ctypes.sizeof(gl.GLfloat), 6 * ctypes.sizeof(gl.GLfloat))
gl.glEnableVertexAttribArray(4)



gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, world.ibo)

if self.world.options.INDIRECT_RENDERING:
self.indirect_command_buffer = gl.GLuint(0)
gl.glGenBuffers(1, self.indirect_command_buffer)
gl.glBindBuffer(gl.GL_DRAW_INDIRECT_BUFFER, self.indirect_command_buffer)
gl.glBufferData(
gl.GL_DRAW_INDIRECT_BUFFER,
gl.GL_DRAW_INDIRECT_BUFFER,
ctypes.sizeof(gl.GLuint * 10),
None,
gl.GL_DYNAMIC_DRAW
)
)

self.draw_commands = []

self.occlusion_query = gl.GLuint(0)
gl.glGenQueries(1, self.occlusion_query)


def __del__(self):
gl.glDeleteQueries(1, self.occlusion_query)
Expand Down Expand Up @@ -132,20 +131,20 @@ def get_transparency(self, position):

if not block_type:
return 2

return block_type.transparent

def is_opaque_block(self, position):
# get block type and check if it's opaque or not
# air counts as a transparent block, so test for that too

block_type = self.world.block_types[self.get_block_number(position)]

if not block_type:
return False

return not block_type.transparent

def update_subchunk_meshes(self):
self.chunk_update_queue.clear()
for subchunk in self.subchunks.values():
Expand Down Expand Up @@ -193,7 +192,7 @@ def process_chunk_updates(self):

def update_mesh(self):
# combine all the small subchunk meshes into one big chunk mesh

for subchunk in self.subchunks.values():
self.mesh += subchunk.mesh
self.translucent_mesh += subchunk.translucent_mesh
Expand All @@ -208,7 +207,7 @@ def update_mesh(self):

self.mesh = []
self.translucent_mesh = []

def send_mesh_data_to_gpu(self): # pass mesh data to gpu
if not self.mesh_quad_count:
return
Expand All @@ -217,8 +216,8 @@ def send_mesh_data_to_gpu(self): # pass mesh data to gpu

gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vbo)
gl.glBufferData(gl.GL_ARRAY_BUFFER, # Orphaning
ctypes.sizeof(gl.GLfloat * CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_LENGTH * 7),
None,
ctypes.sizeof(gl.GLfloat * CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_LENGTH * 7),
None,
gl.GL_DYNAMIC_DRAW
)
gl.glBufferSubData(
Expand All @@ -236,7 +235,7 @@ def send_mesh_data_to_gpu(self): # pass mesh data to gpu

if not self.world.options.INDIRECT_RENDERING:
return

self.draw_commands = [
# Index Count Instance Count Base Index Base Vertex Base Instance
self.mesh_quad_count * 6, 1, 0, 0, 0, # Opaque mesh commands
Expand All @@ -255,7 +254,7 @@ def draw_direct(self, mode):
if not self.mesh_quad_count:
return
gl.glBindVertexArray(self.vao)
gl.glUniform2i(self.shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])
gl.glUniform2i(self.world.block_shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])
gl.glDrawElements(
mode,
self.mesh_quad_count * 6,
Expand All @@ -269,7 +268,7 @@ def draw_indirect(self, mode):

gl.glBindVertexArray(self.vao)
gl.glBindBuffer(gl.GL_DRAW_INDIRECT_BUFFER, self.indirect_command_buffer)
gl.glUniform2i(self.shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])
gl.glUniform2i(self.world.block_shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])

gl.glDrawElementsIndirect(
mode,
Expand All @@ -282,7 +281,7 @@ def draw_direct_advanced(self, mode):
return

gl.glBindVertexArray(self.vao)
gl.glUniform2i(self.shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])
gl.glUniform2i(self.world.block_shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])

gl.glBeginQuery(gl.GL_ANY_SAMPLES_PASSED, self.occlusion_query)
gl.glDrawElements(
Expand All @@ -293,7 +292,7 @@ def draw_direct_advanced(self, mode):
)
gl.glEndQuery(gl.GL_ANY_SAMPLES_PASSED)


gl.glBeginConditionalRender(self.occlusion_query, gl.GL_QUERY_BY_REGION_WAIT)
gl.glDrawElements(
mode,
Expand All @@ -309,7 +308,7 @@ def draw_indirect_advanced(self, mode):

gl.glBindVertexArray(self.vao)
gl.glBindBuffer(gl.GL_DRAW_INDIRECT_BUFFER, self.indirect_command_buffer)
gl.glUniform2i(self.shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])
gl.glUniform2i(self.world.block_shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])

gl.glBeginQuery(gl.GL_ANY_SAMPLES_PASSED, self.occlusion_query)
gl.glDrawElementsIndirect(
Expand All @@ -319,7 +318,7 @@ def draw_indirect_advanced(self, mode):
)
gl.glEndQuery(gl.GL_ANY_SAMPLES_PASSED)


gl.glBeginConditionalRender(self.occlusion_query, gl.GL_QUERY_BY_REGION_WAIT)
gl.glDrawElementsIndirect(
mode,
Expand All @@ -335,9 +334,9 @@ def draw_indirect_advanced(self, mode):
def draw_translucent_direct(self, mode):
if not self.mesh_quad_count:
return

gl.glBindVertexArray(self.vao)
gl.glUniform2i(self.shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])
gl.glUniform2i(self.world.block_shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])

gl.glDrawElementsBaseVertex(
mode,
Expand All @@ -350,10 +349,10 @@ def draw_translucent_direct(self, mode):
def draw_translucent_indirect(self, mode):
if not self.translucent_quad_count:
return

gl.glBindVertexArray(self.vao)
gl.glBindBuffer(gl.GL_DRAW_INDIRECT_BUFFER, self.indirect_command_buffer)
gl.glUniform2i(self.shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])
gl.glUniform2i(self.world.block_shader_chunk_offset_location, self.chunk_position[0], self.chunk_position[2])

gl.glMemoryBarrier(gl.GL_COMMAND_BARRIER_BIT)

Expand All @@ -364,4 +363,4 @@ def draw_translucent_indirect(self, mode):
)

draw_translucent = draw_translucent_indirect if options.INDIRECT_RENDERING else draw_translucent_direct

10 changes: 10 additions & 0 deletions community/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import player
import chunk
import hit
import mob

from enum import IntEnum

Expand All @@ -21,6 +22,7 @@ class MiscMode(IntEnum):
TELEPORT = 6
TOGGLE_F3 = 7
TOGGLE_AO = 8
SPAWN = 9

class MoveMode(IntEnum):
LEFT = 0
Expand Down Expand Up @@ -54,6 +56,14 @@ def hit_callback(current_block, next_block):
def misc(self, mode):
if mode == self.MiscMode.RANDOM:
self.game.holding = random.randint(1, len(self.game.world.block_types) - 1)

elif mode == self.MiscMode.SPAWN:
# _mob = mob.Mob(self.game.world, random.choice([*self.game.world.entity_types.values()]))
_mob = mob.Mob(self.game.world, random.choice([self.game.world.entity_types["Zombie"], self.game.world.entity_types["Creeper"], self.game.world.entity_types["Skeleton"]]))
self.game.world.entities.append(_mob)

_mob.teleport(self.game.player.position)

elif mode == self.MiscMode.SAVE:
self.game.world.save.save()
elif mode == self.MiscMode.ESCAPE:
Expand Down
7 changes: 7 additions & 0 deletions community/data/entities.mcpy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Player: name "Player", width 0.6, height 1.8, model models.pig, texture pig
Pig: name "Pig", width 0.9, height 0.9, model models.pig, texture pig
Zombie: name "Zombie", width 0.6, height 1.95, model models.zombie, texture zombie
Skeleton: name "Skeleton", width 0.6, height 1.99, model models.skeleton, texture skeleton
Creeper: name "Creeper", width 0.6, height 1.7, model models.creeper, texture creeper
Cow: name "Cow", width 0.9, height 1.3, model models.cow, texture cow
Curry: name "Curry", width 0.9, height 1.8, model models.curry, texture curry
Loading