<commit_before>#include "common/lazyworld.h"
#include <cstdio>

void LazyWorld::DoStep() {
    // Collision check against player
}

void LazyWorld::CheckCollision(Entity *entity) {
    PositionedBlock *block;
    
    for (std::list<PositionedBlock*>::iterator it = VisibleBlocks.begin(); it != VisibleBlocks.end(); ++it) {
        block = *it;
        if (block->block->Type == 0) continue;
        
        if (entity->Pos.X + entity->Hitbox.X >= block->pos.X && entity->Pos.X <= block->pos.X + 1
         && entity->Pos.Y + entity->Hitbox.Y >= block->pos.Y && entity->Pos.Y <= block->pos.Y + 1
         && entity->Pos.Z + entity->Hitbox.Z >= block->pos.Z && entity->Pos.Z <= block->pos.Z + 1)
            entity->Collide(*block);
    }
}

PositionedBlock *LazyWorld::CheckAim(Player *player) {
    PositionedBlock *block;
    Ray ray = Ray(player);
    
    PositionedBlock *target = NULL;
    float dist, best = 5.f;
    
    for (std::list<PositionedBlock*>::iterator it = VisibleBlocks.begin(); it != VisibleBlocks.end(); ++it) {
        block = *it; // Blocks[i];
        
        dist = ray.CheckCollision(block);
        if (0.f < dist && dist < best) {
            best = dist;
            target = block;
        }
    }
    
    if (target != NULL)
        target->marked = true;
    
    return target;
}

void LazyWorld::DestroyTarget(Player *player) {
    PositionedBlock *block = CheckAim(player);
    if (!block) return;
    
    DestroyBlock(block);
}

void LazyWorld::DestroyBlock(PositionedBlock *block) {
    Vector3 chunk, pos;
    
    chunk.X = floor(block->pos.X / ChunkSize);
    chunk.Y = floor(block->pos.Y / ChunkSize);
    chunk.Z = floor(block->pos.Z / ChunkSize);
    
    // TODO: handle variable chunk sizes
    PlaceBlock(0, chunk, block->pos - (chunk * 16));
    
    // TODO
    //context->socket->SendBlock(0, chunkIndex, blockIndex);
    sf::Packet Packet;
    Packet << (sf::Uint8) 9 << (sf::Uint8) 0 << chunk << pos;
    Socket.Send(Packet);
}

Block *MakeBlock(char type) {
    if (type == 3)
        return new GrassBlock();
    else
        return new AirBlock();
}

void LazyWorld::PlaceBlock(char type, Vector3 chunkIndex, Vector3 blockIndex) {
    // TODO: check if the block is visible before doing this loop
    // TODO: handle variable chunk sizes
    Vector3 absolute = (chunkIndex * 16) + blockIndex;
    for (std::list<PositionedBlock*>::iterator it = VisibleBlocks.begin(); it != VisibleBlocks.end(); ++it) {
        if ((*it)->pos == absolute) {
            VisibleBlocks.remove(*it);
            break;
        }
    }
    
    std::map<Vector3, Block*> chunk = LoadedChunks[chunkIndex];
    if (chunk.size() == 0) return; // chunk isn't loaded, don't bother
    
    Block *blk = MakeBlock(type);
    chunk[blockIndex] = blk;
    if (type != 0) VisibleBlocks.push_back(new PositionedBlock(blk, blockIndex, 1));
    
    // TODO: handle placing blocks without just counting on a [reliable] glitch!
    
    if (blockIndex.X > 0 && chunk[blockIndex - Vector3(1, 0, 0)]->Type > 0) {
        blk = chunk[blockIndex - Vector3(1, 0, 0)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, blockIndex - Vector3(1, 0, 0), 1));
        blk->faces |= 0x08;
    }
    
    if (blockIndex.Y > 0 && chunk[blockIndex - Vector3(0, 1, 0)]->Type > 0) {
        blk = chunk[blockIndex - Vector3(0, 1, 0)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, blockIndex - Vector3(0, 1, 0), 1));
        blk->faces |= 0x10;
    }
    
    if (blockIndex.Z > 0 && chunk[blockIndex - Vector3(0, 0, 1)]->Type > 0) {
        blk = chunk[blockIndex - Vector3(0, 0, 1)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, blockIndex - Vector3(0, 0, 1), 1));
        blk->faces |= 0x20;
    }
    
    // TODO: handle variable chunk sizes
    if (blockIndex.X < 15 && chunk[blockIndex + Vector3(1, 0, 0)]->Type > 0) {
        blk = chunk[blockIndex + Vector3(1, 0, 0)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, blockIndex + Vector3(1, 0, 0), 1));
        blk->faces |= 0x01;
    }
    
    if (blockIndex.Y < 15 && chunk[blockIndex + Vector3(0, 1, 0)]->Type > 0) {
        blk = chunk[blockIndex + Vector3(0, 1, 0)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, blockIndex + Vector3(0, 1, 0), 1));
        blk->faces |= 0x02;
    }
    
    if (blockIndex.Z < 15 && chunk[blockIndex + Vector3(0, 0, 1)]->Type > 0) {
        blk = chunk[blockIndex + Vector3(0, 0, 1)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, blockIndex + Vector3(0, 0, 1), 1));
        blk->faces |= 0x04;
    }
}

bool contains(std::vector<Vector3> vector, Vector3 value) {
    for (int i = 0; i < vector.size(); i++)
        if (vector[i] == value) return true;
    
    return false;
}
<commit_msg>Fix for block placing<commit_after>#include "common/lazyworld.h"
#include <cstdio>

void LazyWorld::DoStep() {
    // Collision check against player
}

void LazyWorld::CheckCollision(Entity *entity) {
    PositionedBlock *block;
    
    for (std::list<PositionedBlock*>::iterator it = VisibleBlocks.begin(); it != VisibleBlocks.end(); ++it) {
        block = *it;
        if (block->block->Type == 0) continue;
        
        if (entity->Pos.X + entity->Hitbox.X >= block->pos.X && entity->Pos.X <= block->pos.X + 1
         && entity->Pos.Y + entity->Hitbox.Y >= block->pos.Y && entity->Pos.Y <= block->pos.Y + 1
         && entity->Pos.Z + entity->Hitbox.Z >= block->pos.Z && entity->Pos.Z <= block->pos.Z + 1)
            entity->Collide(*block);
    }
}

PositionedBlock *LazyWorld::CheckAim(Player *player) {
    PositionedBlock *block;
    Ray ray = Ray(player);
    
    PositionedBlock *target = NULL;
    float dist, best = 5.f;
    
    for (std::list<PositionedBlock*>::iterator it = VisibleBlocks.begin(); it != VisibleBlocks.end(); ++it) {
        block = *it; // Blocks[i];
        
        dist = ray.CheckCollision(block);
        if (0.f < dist && dist < best) {
            best = dist;
            target = block;
        }
    }
    
    if (target != NULL)
        target->marked = true;
    
    return target;
}

void LazyWorld::DestroyTarget(Player *player) {
    PositionedBlock *block = CheckAim(player);
    if (!block) return;
    
    DestroyBlock(block);
}

void LazyWorld::DestroyBlock(PositionedBlock *block) {
    Vector3 chunk, pos;
    
    chunk.X = floor(block->pos.X / ChunkSize);
    chunk.Y = floor(block->pos.Y / ChunkSize);
    chunk.Z = floor(block->pos.Z / ChunkSize);
    pos = block->pos - (chunk * 16); // TODO: handle variable chunk sizes
    
    PlaceBlock(0, chunk, pos);
    
    // TODO
    //context->socket->SendBlock(0, chunk, pos);
    sf::Packet Packet;
    Packet << (sf::Uint8) 9 << (sf::Uint8) 0 << chunk << pos;
    Socket.Send(Packet);
}

Block *MakeBlock(char type) {
    if (type == 3)
        return new GrassBlock();
    else
        return new AirBlock();
}

void LazyWorld::PlaceBlock(char type, Vector3 chunkIndex, Vector3 blockIndex) {
    // TODO: check if the block is visible before doing this loop
    // TODO: handle variable chunk sizes
    Vector3 absolute = (chunkIndex * 16) + blockIndex;
    for (std::list<PositionedBlock*>::iterator it = VisibleBlocks.begin(); it != VisibleBlocks.end(); ++it) {
        if ((*it)->pos == absolute) {
            VisibleBlocks.remove(*it);
            break;
        }
    }
    
    std::map<Vector3, Block*> chunk = LoadedChunks[chunkIndex];
    if (chunk.size() == 0) return; // chunk isn't loaded, don't bother
    
    Block *blk = MakeBlock(type);
    chunk[blockIndex] = blk;
    if (type != 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute, 1));
    
    // TODO: handle placing blocks without just counting on a [reliable] glitch!
    
    if (blockIndex.X > 0 && chunk[blockIndex - Vector3(1, 0, 0)]->Type > 0) {
        blk = chunk[blockIndex - Vector3(1, 0, 0)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute - Vector3(1, 0, 0), 1));
        blk->faces |= 0x08;
    }
    
    if (blockIndex.Y > 0 && chunk[blockIndex - Vector3(0, 1, 0)]->Type > 0) {
        blk = chunk[blockIndex - Vector3(0, 1, 0)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute - Vector3(0, 1, 0), 1));
        blk->faces |= 0x10;
    }
    
    if (blockIndex.Z > 0 && chunk[blockIndex - Vector3(0, 0, 1)]->Type > 0) {
        blk = chunk[blockIndex - Vector3(0, 0, 1)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute - Vector3(0, 0, 1), 1));
        blk->faces |= 0x20;
    }
    
    // TODO: handle variable chunk sizes
    if (blockIndex.X < 15 && chunk[blockIndex + Vector3(1, 0, 0)]->Type > 0) {
        blk = chunk[blockIndex + Vector3(1, 0, 0)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute + Vector3(1, 0, 0), 1));
        blk->faces |= 0x01;
    }
    
    if (blockIndex.Y < 15 && chunk[blockIndex + Vector3(0, 1, 0)]->Type > 0) {
        blk = chunk[blockIndex + Vector3(0, 1, 0)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute + Vector3(0, 1, 0), 1));
        blk->faces |= 0x02;
    }
    
    if (blockIndex.Z < 15 && chunk[blockIndex + Vector3(0, 0, 1)]->Type > 0) {
        blk = chunk[blockIndex + Vector3(0, 0, 1)];
        if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute + Vector3(0, 0, 1), 1));
        blk->faces |= 0x04;
    }
}

bool contains(std::vector<Vector3> vector, Vector3 value) {
    for (int i = 0; i < vector.size(); i++)
        if (vector[i] == value) return true;
    
    return false;
}

{'fraction_non_alphanumeric': 0.1628597767079791, 'fraction_numerical': 0.029703984431015056, 'mean_word_length': 2.5910261125413756, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3186477', 'n_tokens_mistral': 3501, 'n_tokens_neox': 3258, 'n_words': 811}