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.
 
 
 

47 lines
1.4 KiB

  1. module.exports = generator => {
  2. const { rooms, leafConstraints, endConstraints, templates } = generator;
  3. const leafRooms = rooms.filter(r => !r.connections.length);
  4. //Ensure that we have enough leaf rooms
  5. const { minCount: minLeafRooms, maxCount: maxLeafRooms } = leafConstraints;
  6. const leafRoomCount = leafRooms.length;
  7. if (leafRoomCount < minLeafRooms || leafRoomCount > maxLeafRooms)
  8. return false;
  9. //Ensure that the end room exists
  10. const endRoom = rooms.find(r => r.template.properties.end);
  11. if (!endRoom)
  12. return false;
  13. //Ensure that the end room is the correct distance
  14. const { minDistance: minEndDistance, maxDistance: maxEndDistance } = endConstraints;
  15. const endDistance = endRoom.distance;
  16. if (endDistance < minEndDistance || endDistance > maxEndDistance)
  17. return false;
  18. //Ensure that leaf rooms are correct distances
  19. const { minDistance: minLeafDistance, maxDistance: maxLeafDistance } = leafConstraints;
  20. const leafRoomsDistanceOk = !leafRooms.some(({ distance: roomDistance }) => {
  21. return (roomDistance < minLeafDistance || roomDistance > maxLeafDistance);
  22. });
  23. if (!leafRoomsDistanceOk)
  24. return false;
  25. //Ensure that enough minOccur templates have been included
  26. const minOccurOk = templates.every(t => {
  27. const minOccur = ~~t.properties.minOccur || 0;
  28. const occurs = rooms.filter(r => r.template.typeId === t.typeId).length;
  29. return occurs >= minOccur;
  30. });
  31. if (!minOccurOk)
  32. return false;
  33. return true;
  34. };