All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.8 KiB

1 year ago
  1. /* theripper93
  2. * Copyright (C) 2021 dnd-randomizer
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * Original License:
  15. * https://github.com/theripper93/dnd-randomizer/blob/master/LICENSE
  16. */
  17. /* WARPGATE CHANGES
  18. * exporting propagator class
  19. * removed test function from original code
  20. */
  21. export class Propagator{
  22. // Find a non-occupied cell in the grid that matches the size of the token given an origin
  23. static getFreePosition(tokenData,origin, collision = true){
  24. const center = canvas.grid.getCenter(origin.x,origin.y)
  25. origin = {x:center[0],y:center[1]};
  26. const positions = Propagator.generatePositions(origin);
  27. for(let position of positions){
  28. if(Propagator.canFit(tokenData, position, positions[0], collision)){
  29. return position;
  30. }
  31. }
  32. }
  33. //generate positions radiantially from the origin
  34. static generatePositions(origin){
  35. let positions = [canvas.grid.getSnappedPosition(origin.x-1,origin.y-1)];
  36. for(let r = canvas.scene.dimensions.size; r < canvas.scene.dimensions.size*10; r+=canvas.scene.dimensions.size){
  37. for(let theta = 0; theta < 2*Math.PI; theta+=Math.PI/(4*r/canvas.scene.dimensions.size)){
  38. const newPos = canvas.grid.getTopLeft(origin.x + r*Math.cos(theta),origin.y + r*Math.sin(theta))
  39. positions.push({x:newPos[0],y:newPos[1]});
  40. }
  41. }
  42. return positions;
  43. }
  44. //check if a position is free
  45. static isFree(position){
  46. for(let token of canvas.tokens.placeables){
  47. const hitBox = new PIXI.Rectangle(token.x,token.y,token.w,token.h);
  48. if(hitBox.contains(position.x,position.y)){
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54. //check if a token can fit in a position
  55. static canFit(tokenData, position, origin, collision){
  56. for(let i = 0; i < tokenData.width; i++){
  57. for(let j = 0; j < tokenData.height; j++){
  58. const x = position.x + j*canvas.scene.dimensions.size;
  59. const y = position.y + i*canvas.scene.dimensions.size;
  60. if(!Propagator.isFree({x,y})){
  61. return false;
  62. }
  63. }
  64. }
  65. const wallCollisions = canvas.walls.checkCollision(
  66. new Ray(origin, {
  67. x:position.x+tokenData.width*canvas.scene.dimensions.size/2,
  68. y:position.y+tokenData.height*canvas.scene.dimensions.size/2
  69. }),{ type: "move" }
  70. )?.length ?? 0;
  71. return !collision || !wallCollisions
  72. }
  73. }