summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/shapes/regionlist.js
blob: 663f3fd0aa13ea5eb71c952885b4dce69a1fd803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// This algorithm takes the polylines from ShapeList as input and produces
// a set of Rooms which can be used by the existing V1 mover and editor.

// The algorithm assumes that
// 1) all angles are orthogonal
// 2) all polylines are closed

var RegionList = (function(){

  var RegionList = {}
  var regions = RegionList.regions

  RegionList.build = function(){
  
    // first, get the segments sorted right to left & top to bottom
    var segments = RegionList.getSortedSegments()

    var rooms = []
    var seen_rooms = {}
    var open_segments = []
    
    var segment, open_segment, x_segment, y_segments, overlapped, seen_segments

    // first pass: generate rooms from the vertical segments only
    for (var i = 0; i < segments.length; i++) {
      segment = segments[i]
      if (! segment.isVertical()) {
        continue
      }

      // check all the "open segments" we know about, i.e. rooms where we've only found
      // the right wall.
      overlapped = false
      for (var j = 0; j < open_segments.length; j++) {
        open_segment = open_segments[j]
        
        // if these two segments overlap each other, then there is a room between them.
        if (segment.y.overlaps(open_segment.y)) {
          overlapped = true
          open_segments.splice(j--, 1)

          // the X part of the room will be the span between these two vertical segments'
          // X components.  the Y part of the room is the "split" or subdivision between
          // the two horizontal vectors.
          
          // the split function is non-commutative,
          // so we need to call A split B and B split A,
          // then dedupe the segments we got back..
          x_segment = new vec2( open_segment.x.a, segment.x.b )
          y_segments = open_segment.y.split(segment.y, 0, 0)
          
          seen_segments = {}

          // check each of the splits.. if the two segments overlap, then we definitely
          // have a room here.
          y_segments.forEach(function(seg){
            seen_segments[ seg[0]+"" ] = true
            var room = new Rect( x_segment, seg[0] )

            if (seen_rooms[ room+"" ]) return
            seen_rooms[ room+"" ] = true
            
            room.sides = 0
            
            rooms.push(room)
            var new_seg = new Rect( segment.x, seg[0] )
            open_segments.unshift(new_seg)
            j++
          })

          // splitting the other way..
          y_segments = segment.y.split(open_segment.y, 0, 0)
          y_segments.forEach(function(seg){
            if (seen_segments[ seg[0]+"" ]) return;
            var new_seg = new Rect( segment.x, seg[0] )
            open_segments.unshift(new_seg)
            j++
          })
        }
      }
      
      // if we have overlap, then re-sort the open segments Y-wise
      // and (again) dedupe..
      if (overlapped) {
        open_segments = open_segments.sort(function(a,b){
          if (a.y.a < b.y.a) { return -1 }
          if (a.y.a == b.y.a) { return 0 }
          if (a.y.a > b.y.a) { return 1 }
        })
        
        if (open_segments.length < 2) { continue }
        
        for (var k = 1; k < open_segments.length; k++) {
          if (open_segments[k-1].y.containsVec(open_segments[k].y)) {
            open_segments.splice(k--, 1)
          }
          else if (open_segments[k-1].y.overlaps(open_segments[k].y)) {
            open_segments[k].y = open_segments[k].y.clone()
            open_segments[k].y.a = open_segments[k-1].y.b
          }
        }
      }
      // if we don't have any overlap, then this is a new open segment.
      else {
        open_segments.push(segment)
      }
    }
    
    var splits, splitter
    
    // second pass: now that we have a bunch of rooms, assign sides to all of them.
    // sides are used in the "mover" script to do bounds checking.
    for (var i = 0; i < segments.length; i++) {
      segment = segments[i]
      var horizontal = segment.isHorizontal(), vertical = segment.isVertical()
      for (var r = 0; r < rooms.length; r++){
        room = rooms[r]

        // vertical segments determine the left and right edges of the room, fairly simply.
        if (vertical) {
          if (segment.y.containsVec(room.y)) {
            if (segment.x.a == room.x.a) {
              room.sides |= LEFT
            }
            if (segment.x.a == room.x.b) {
              room.sides |= RIGHT
            }
          }
        }
        
        // horizontal segments determine the front and back edges of the room.
        if (horizontal) {
          if (segment.x.containsVec(room.x)) {
            if (segment.y.a == room.y.a) {
              room.sides |= FRONT
            }
            if (segment.y.a == room.y.b) {
              room.sides |= BACK
            }
          }
          
          // however, since we did not split on horizontal segments, our rooms may
          // only have partial overlap with these segments, in which case we need to
          // split the rooms apart.
          else if ((segment.y.a == room.y.a || segment.y.a == room.y.b) && room.x.overlaps(segment.x)) {
            
            // split the room across the segment. preserve whether or not we know the
            // room borders a segment on the left or right.
            splits = room.x.split(segment.x, room.sides & LEFT, room.sides & RIGHT)
            rooms.splice(r--, 1)
            for (var k = 0; k < splits.length; k++) {
              splitter = splits[k]
              var new_room = new Rect( splitter[0], room.y )
              new_room.sides = splitter[1] | ( room.sides & FRONT_BACK )
              if (segment.x.overlaps( splitter[0] )) {
                if (segment.y.a == new_room.y.a) {
                  new_room.sides |= FRONT
                }
                if (segment.y.a == new_room.y.b) {
                  new_room.sides |= BACK
                }
              }
              rooms.unshift(new_room)
              r++
            }
          }

        }
      }
    }

    return { rooms: rooms }
  }
  
  
  // Gets a list of polylines from the ShapeList and sorts the segments.
  RegionList.getSortedSegments = function(){
    // get a list of all segments from these polylines
    var segments = shapes.getAllSegments()

    // re-orient them so they're either facing up or right and make them into rects
    segments = segments.map(function(segment){
      // vertical
      if (segment[0].a == segment[1].a) {
        if (segment[0].b > segment[1].b) {
          segment.push(segment.shift())
        }
      }
      // horizontal
      else if (segment[0].b == segment[1].b) {
        if (segment[0].a > segment[1].a) {
          segment.push(segment.shift())
        }
      }
      return new Rect( segment[0].a, segment[0].b, segment[1].a, segment[1].b )
    })
    
    return sort.rects_by_position(segments)
  }

  return RegionList
  
})()