<commit_before>package fly.systems;

import amaze.Maze;
import edge.*;
import fly.components.*;

class MazeCollision implements ISystem {
  static inline var E = 0.00001;
  var cellSize : Float;
  public function new(cellSize : Float) {
    this.cellSize = cellSize;
  }

  public function update(p : Position, d : Direction, v : Velocity, maze : Maze) {
    var dx   = p.x + d.dx * v.value,
        dy   = p.y + d.dy * v.value,
        dcol = Math.floor(dx / cellSize),
        drow = Math.floor(dy / cellSize),
        col  = Math.floor(p.x / cellSize),
        row  = Math.floor(p.y / cellSize);

    if(dcol == col && drow == row) // no change in cell, nothing to do
      return;
    var cell = maze.cells[row][col];
    if(dcol == col) {
      if(drow < row && !cell.top || drow > row && !cell.bottom)
        d.angle = -d.angle;
    } else if(drow == row) {
      if(dcol < col && !cell.left || dcol > col && !cell.right)
        d.angle = -d.angle + Math.PI;
    }
  }
}<commit_msg>fixed collision corner case (can be simplified)<commit_after>package fly.systems;

import amaze.Maze;
import edge.*;
import fly.components.*;
using thx.core.Floats;

class MazeCollision implements ISystem {
  static inline var E = 0.00001;
  var cellSize : Float;
  public function new(cellSize : Float) {
    this.cellSize = cellSize;
  }

  public function update(p : Position, d : Direction, v : Velocity, maze : Maze) {
    var dx   = p.x + d.dx * v.value,
        dy   = p.y + d.dy * v.value,
        dcol = Math.floor(dx / cellSize),
        drow = Math.floor(dy / cellSize),
        col  = Math.floor(p.x / cellSize),
        row  = Math.floor(p.y / cellSize);

    if(dcol == col && drow == row) // no change in cell, nothing to do
      return;
    var cell = maze.cells[row][col];
    if(dcol == col) {
      if(drow < row && !cell.top || drow > row && !cell.bottom)
        d.angle = -d.angle;
    } else if(drow == row) {
      if(dcol < col && !cell.left || dcol > col && !cell.right)
        d.angle = -d.angle + Math.PI;
    } else if(dcol < col && drow < row) {
      if(pos(col * cellSize, row * cellSize, p.x, p.y, dx, dy) > 0) {
        if(!cell.top) {
          if(!cell.left) {
            d.angle += Math.PI;
          } else {
            d.angle = -d.angle;
          }
        } else if(null != maze.cells[row-1][col] && !maze.cells[row-1][col].left) {
          d.angle = -d.angle + Math.PI;
        }
      } else {
        if(!cell.left) {
          if(!cell.top) {
            d.angle += Math.PI;
          } else {
            d.angle = -d.angle + Math.PI;
          }
        } else if(null != maze.cells[row][col-1] && !maze.cells[row][col-1].top) {
          d.angle = -d.angle;
        }
      }
    } else if(dcol > col && drow > row) {
      if(pos(col * cellSize, row * cellSize, p.x, p.y, dx, dy) > 0) {
        if(!cell.bottom) {
          if(!cell.right) {
            d.angle += Math.PI;
          } else {
            d.angle = -d.angle;
          }
        } else if(null != maze.cells[row+1][col] && !maze.cells[row+1][col].right) {
          d.angle = -d.angle + Math.PI;
        }
      } else {
        if(!cell.right) {
          if(!cell.bottom) {
            d.angle += Math.PI;
          } else {
            d.angle = -d.angle + Math.PI;
          }
        } else if(null != maze.cells[row][col+1] && !maze.cells[row][col+1].bottom) {
          d.angle = -d.angle;
        }
      }
    } else if(dcol < col && drow > row) {
      if(pos(col * cellSize, row * cellSize, p.x, p.y, dx, dy) > 0) {
        if(!cell.bottom) {
          if(!cell.left) {
            d.angle += Math.PI;
          } else {
            d.angle = -d.angle;
          }
        } else if(null != maze.cells[row+1][col] && !maze.cells[row+1][col].left) {
          d.angle = -d.angle + Math.PI;
        }
      } else {
        if(!cell.left) {
          if(!cell.bottom) {
            d.angle += Math.PI;
          } else {
            d.angle = -d.angle + Math.PI;
          }
        } else if(null != maze.cells[row][col-1] && !maze.cells[row][col-1].bottom) {
          d.angle = -d.angle;
        }
      }
    } else if(dcol > col && drow < row) {
      if(pos(col * cellSize, row * cellSize, p.x, p.y, dx, dy) > 0) {
        if(!cell.top) {
          if(!cell.right) {
            d.angle += Math.PI;
          } else {
            d.angle = -d.angle;
          }
        } else if(null != maze.cells[row-1][col] && !maze.cells[row-1][col].right) {
          d.angle = -d.angle + Math.PI;
        }
      } else {
        if(!cell.right) {
          if(!cell.top) {
            d.angle += Math.PI;
          } else {
            d.angle = -d.angle + Math.PI;
          }
        } else if(null != maze.cells[row][col+1] && !maze.cells[row][col+1].top) {
          d.angle = -d.angle;
        }
      }
    }
  }

  function pos(x : Float, y : Float, ax : Float, ay : Float, bx : Float, by : Float)
    return ((bx - ax) * (y - ay) - (by - ay) * (x - ax)).sign();
}
{'fraction_non_alphanumeric': 0.17421812349639135, 'fraction_numerical': 0.006415396952686447, 'mean_word_length': 1.594383775351014, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '6866172', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1799, 'n_words': 390}