Paste #11632: Untitled Paste

Date: 2014/11/27 19:02:16 UTC-08:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445


# Will test if there is a railroad path from the player's
# location to a fixed, predefined point.
train:
    type: command
    name: train
    description: "Versucht, einen Zug zur aktuellen Position herbeizurufen."
    usage: /train
    script:
        # hub position
        - define departure_loc <l@1363,69,25,world>

        # player position
        - define destination_loc <player.location>

        # invoke A*
        - define path <proc[a_star].context[<li@start/%destination_loc%/0/0/null>|<li@goal/%departure_loc%/0/0/null>]>

        # if A* could find a proper path, output it,
        # otherwise, state failure reason.
        - if <def[path].is[!EQUALS].to[-1]> {
            - narrate "Path is <def[path]>"
        } else {
            - narrate "Es konnte kein gueltiger Pfad gefunden werden!"
        }


# This script implements an A* algorithm for finding the shortest path between
# two points (nodes). This will be used for figuring out the shortest railroad connection
# between a player and a fixed starting point in the enhanced farming world.
#
# Nodes are defined as lists: li@node_id/location/g_value/f_value/parent_node_id
#
# The IDs are continuous integers, starting from 0 and going as high as need be. Start and goal
# nodes have string IDs so one doesn't run into issues when dealing with very long
# paths that would exceed a static goal integer id.
# >>>1363,69,25,world
a_star:
    type: procedure
    definitions: start|goal
    speed: 1t
    script:
        - narrate "start <def[start]>"
        - narrate "goal <def[goal]>"

        # start counting node IDs at 0
        - define node_id_cnt 0

        # Nodes that already have been evaluated
        - define closedset <li@>

        # Nodes waiting to be evaluated.
        - define openset <li@%start%>

        # The path to the goal, if it exists.
        - define came_from <li@>

        # The cost from the start along the best known path.
        - define g_score <el@0>

        # Estimated total cost from start to goal through
        # Heuristic function is the straight Euclidean distance.
        - define f_score <def[g_score].add[<def[start].get_sub_items[2].get[1].distance[<def[goal].get_sub_items[2].get[1]>]>]>
        - narrate "f_score <def[f_score]>"

        # id/loc/g/f/came_from_id
        # Update g and f scores of the start node.
        - define startloc <def[start].get_sub_items[1].get[1]>
        - define start <li@start/%startloc%/%g_score%/%f_score%/null>
        - narrate "Start after update was <def[start]>"

        # Continue algorithm as long as there nodes to expand
        # in the open set
        - while <def[openset].is_empty.is[!EQUALS].to[true]> {

            - narrate "openset was <def[openset]>"

            # get node with lowest f value
            - define current <proc[min_f].context[<def[openset].escaped>]>

            # if the goal was reached, output the path
            # as a list of locations (add failure clause here?)
            - if <def[current].get_sub_items[1].get[1].is[EQUALS].to[goal]> {
                - determine <proc[reconstruct_path].context[<def[came_from].escaped>|<def[goal].escaped>]>
            }

            # remove current node from the open set ...
            - define openset <def[openset].exclude[<def[current]>]>

            # ... and add it to the closed set
            - define closedset <def[closedset].include[<def[current]>]>

            # get valid neighbors of the current node
            - define neighbor_nodes <proc[find_neighbors].context[<def[current].escaped>|%g_score%|%node_id_cnt%|<def[goal].escaped>]>

            # update the id count by adding the number of actually returned neighbors
            # to the initial value
            - define node_id_cnt <def[node_id_cnt].add[<def[neighbor_nodes].size>]>

            # Step through all neighbor nodes of the current node
            - foreach <def[neighbor_nodes]> {

                # search neighbor using their id
                - define neighbor_id <def[value].get_sub_items[1].get[1]>
                - define neighbor_node <def[closedset].filter[starts_with[%neighbor_id%]]>
                - narrate "neighbor_node was <def[neighbor_node]>"

                # then, using the (potentially) found node, try to get
                # its position in the closed set
                - define neighbor_index_closed <def[closedset].find[<def[neighbor_node]>]>

                # if the neighbor has been evaluated before
                - if <def[neighbor_index_closed]> == -1 {
                    # do nothing.
                } else {

                    # calculate a preliminary g value for the neighbor node
                    - define distance <def[current].distance[<def[neighbor_node]>]>
                    - define tentative_g_score <proc[g_score].context[<def[current].escaped>].add[<def[distance]>]>

                    # check if neighbor is not in the open set
                    # CHECK IF DOUBLE USE OF NEIGHBOR_NODE IS OK! <-------- TODO
                    - define neighbor_node <def[openset].filter[starts_with[%neighbor_id%]]>
                    - define neighbor_index_open <def[openset].find[<def[neighbor_node]>]>

                    # if neighbor is not in open set or tentative g score is smaller than
                    # the real g score of the neighbor ...
                    - if <def[neighbor_index_open].is[!EQUALS].to[-1]>||<def[tentative_g_score].is[LESS].than[<proc[g_score].context[<def[neighbor_node].escaped>]>]> {

                        # ... update the neighbor node's
                        # g/f-value and came_from node
                        - define new_id <def[neighbor_node].get_sub_items[1].get[1]>
                        - define new_loc <def[neighbor_node].get_sub_items[2].get[1]>
                        - define new_distance <def[neighbor_node].sub_items_get[2].get[1].distance[<def[goal]>.get_sub_items[2].get[1]]>
                        - define new_g_score <def[tentative_g_score]>
                        - define new_f_score <proc[g_score].context[<def[neighbor_node].escaped>].add[<def[new_distance]>]>
                        - define new_came_from <def[current].get_sub_items[1].get[1]>

                        - define new_neighbor <li@%new_id%/%new_loc%/%new_g_score%/%new_f_score%/%new_came_from%>
                        - narrate "new_neighbor was <def[new_neighbor]>"

                        # replace old node with updated, new node
                        - define neighbor_index <def[neighbor_nodes].find[<def[neighbor_node]>]>
                        - define neighbor_nodes <def[neighbor_nodes].exclude[<def[neighbor_node]>]>
                        - define neighbor_nodes <def[neighbor_nodes].insert[<def[new_neighbor]>].at[<def[neighbor_index]>]>

                        # add update neighbor to open set if
                        # it is not yet in there
                        - if <def[neighbor_nodes].find[<def[new_neighbor]>].is[EQUALS].to[-1]> {
                            - define neighbor_nodes <def[neighbor_nodes].include[<def[new_neighbor]>]>
                        }
                    }
                }
            }

            - wait 1s
        }

        # If we reach the end of the loop without
        # having returned upon encountering the goal node,
        # there was no way from start to goal.
        - determine -1

# This helper script reconstructs the path the algorithm
# has taken, using the "came_from" field in the nodes' definition.
reconstruct_path:
    type: procedure
    definitions: came_from|current
    speed: 1t
    script:
        - define came_from <def[came_from].unescaped>
        - define current <def[current].unescaped>

        # path from start to goal
        - define total_path <li@%current%>

        # while current node is in the the came_from list
        - while <def[came_from].find[<def[current]>].is[!EQUALS].to[-1]> {

            # find previous node by its id saved in the came_from field
            - define current <def[came_from].filter[starts_with[<def[current].get_sub_items[5].get[1]>]]>

            # path begins with the last node, so insertions
            # have to be made in reverse order to output the
            # path from start to finish!
            - define total_path <def[total_path].insert[<def[current]>].at[0]>
        }

        # return full path
        - determine <def[total_path]>

# This helper script finds all valid neighbors of a passed node as
# a list, which will contain a maximum of four nodes.
# This means that potentially all non-diagonal blocks from y+1 to y-1
# around the origin block can be considered, since rail tracks can
# only be placed on blocks that have a maximum y difference of 1.
find_neighbors:
    type: procedure
    definitions: node|g_score|id_cnt|goal
    speed: 1t
    script:
        - define node <def[node].unescaped>
        - narrate "node was <def[node]>"

        - define goal <def[goal].unescaped>

        # center location whose neighbors shall
        # be found by this procedure
        - define loc <def[node].get_sub_items[2].get[1].as_location>
        #- narrate "origin was <def[loc]>"

        # output list of valid neighbors
        - define valid_neighbors <li@>

        # Start from top (y+1)
        # Direction: z-1
        - define x <def[loc].x>
        - define y <def[loc].y.add[1]>
        - define z <def[loc].z.sub[1]>
        - define world <def[loc].world.name>

        - define n1 <l@%x%,%y%,%z%,%world%>

        # Direction: z+1
        - define x <def[loc].x>
        - define y <def[loc].y.add[1]>
        - define z <def[loc].z.add[1]>

        - define n2 <l@%x%,%y%,%z%,%world%>

        # Direction: x-1
        - define x <def[loc].x.sub[1]>
        - define y <def[loc].y.add[1]>
        - define z <def[loc].z>

        - define n3 <l@%x%,%y%,%z%,%world%>

        # Direction: x+1
        - define x <def[loc].x.add[1]>
        - define y <def[loc].y.add[1]>
        - define z <def[loc].z>

        - define n4 <l@%x%,%y%,%z%,%world%>

        # Neighboring blocks to be checked
        - define checklist <li@%n1%|%n2%|%n3%|%n4%>
        #- narrate "checklist <def[checklist]>"

        - foreach <def[checklist]> {

            # add information to neighbor node
            - define neighbor_loc <proc[check_rail_placeable].context[<def[value]>]>
            - define neighbor_distance <def[neighbor_loc].distance[<def[current].get_sub_items[2].get[1]>]> 
            - define neighbor_g_score <%g_score%.add[%neighbor_distance%]>
            - define neighbor_f_score <def[neighbor_loc].distance[<def[goal].get_sub_items[1].get[2]>]>            
            - define neighbor_parent <def[node].get_sub_items[1].get[1]>

            - define neighbor <li@%id_cnt%/%neighbor_loc%/%neighbor_g_score%/%neighbor_f_score%/%neighbor_parent%>
            - narrate "checklist neighbor was <def[neighbor]>" 

            # if there was a valid neighbor, add it to the output list
            # and calculate its g and f values
            - if <def[neighbor]> != -1 {
                - narrate "Adding neighbor <def[neighbor]>"
                - define valid_neighbors <def[valid_neighbors].include[<def[neighbor]>]>
            }
        }

        # return list of valid neighbors
        - determine <def[valid_neighbors]>

# This helper script checks if a rail can be placed on the passed block or the two
# below it, and returns a valid block location if there was any.
# Careful: The block BELOW the rail has to be compatible, so when the rail is put
# into place, it has to be placed above the location since it is an entity!
#
# Strategy: For each direction, check the blocks
# top-down. Because the maximum y difference is 1,
# the first valid (= rail-compatible, i.e. not water)
# block denies access to the blocks below it, so these
# do not have to be checked unless the blocks above them
# are air.
check_rail_placeable:
    type: procedure
    definitions: loc
    speed: 1t
    script:
        # loop ending condition
        - define status continue
        #- narrate "status was <def[status]>"

        # retain origin location by using
        # a new variable
        - define inloc <def[loc]>
        #- narrate "inloc was <def[inloc]>"

        # dummy output location for the case
        # that no block matches
        - define outloc -1


        # start at top block and go down a maximum
        # of two blocks from the origin loc
        - while <def[status].is[EQUALS].to[continue]> && <def[loc].y.sub[<def[inloc].y>].is[LESS].than[3]> {
            #- narrate "This is iteration %loop_index%"

            # The material of the block the rail should be placed on must
            # be solid (i.e. not water, air...)
            - if <def[inloc].material.is_solid> == true {

                # Block is solid, but must not be transparent (chests, glass..)
                - if <def[inloc].material.is_transparent> == false {

                    # Block is solid and non-transparent, check if air above
                    - if <def[inloc].above.material> == <m@air> {

                        # Rail is placeable!
                        #- narrate "solid, non-transparent, space above, ACCEPT"
                        - define status abort
                        - define outloc <def[inloc]>

                    # Block is solid and non-transparent,
                    # but there is no free space above
                    } else {

                        # this blocks all potential neighbors below, abort
                        #- narrate "solid, non-transparent, no space above, ABORT"
                        - define status abort
                    }

                # Block is solid, but transparent
                } else {

                    # this blocks all potential neighbors below, abort
                    #- narrate "solid, transparent, ABORT"
                    - define status abort
                }

            # Block is not solid
            } else {

                # if the block is air, the block below might be suitable
                - if <def[inloc].material> == <m@air> {

                    # continue with block below
                    #- narrate "non-solid, air, CONTINUE LOWER"
                    - define status continue

                    # location data
                    - define in_x <def[inloc].x>
                    - define in_y <def[inloc].y.sub[1]>
                    - define in_z <def[inloc].z>
                    - define in_world <def[inloc].world.name>

                    - define inloc <l@%in_x%,%in_y%,%in_z%,%in_world%>
                    #- narrate "inloc is now <def[inloc]>"

                # else, there's water or lava, which means that the
                # block below is either solid or also water/lava
                } else {

                    # hence abort search
                    #- narrate "non-solid, non-air, ABORT"
                    - define status abort
                }
            }
            #- narrate "status was <def[status]> and diff was <def[loc].y.sub[<def[inloc].y>]>"    
        }

        # finally return the valid neighbor block or -1, if there was none
        #- narrate "returning..."
        - determine <def[outloc]>

# This helper script returns the g value (cost from start) of a node.
g_score:
    type: procedure
    definitions: node
    speed: 1t
    script:
        - define node <def[node].unescaped>
        #- narrate "node was <def[node]>"

        - define g_val <def[node].get_sub_items[3].get[1]>
        #- narrate "g_score was <def[g_val]>"

        - determine <def[g_val]>


# This helper script returns the f value (Estimated total cost until goal) of a node.
f_score:
    type: procedure
    definitions: node
    speed: 1t
    script:
        - define node <def[node].unescaped]>
        #- narrate "node was <def[node]>"

        - define f_val <def[node].get_sub_items[4].get[1]>
        #- narrate "f_score was <def[f_val]>"

        - determine <def[f_val]>


# This helper script fetches the came_from value from a passed node.
# The came_from value is not an entire node, but instead just the id
# of the node, which can then be used to look up the node in the closed
# set.
came_from:
    type: procedure
    definitions: node
    speed: 1t
    script:
        - define node <def[node].unescaped]>
        - define came_from <def[node].get_sub_items[5].get[1]>

        - determine <def[came_from]>



# This helper script finds the node with the lowest f value
# in the open set and returns it.
# Usage: <proc[min_f].context[<li@id/loc/g/f/came_from|li@id2...>]>
min_f:
    type: procedure
    definitions: openset
    speed: 1t
    script:
        - define openset <def[openset].unescaped]>
        #- narrate "openset <def[openset]>"

        # set dummy value 
        - define min_f 999999

        # iterate through all nodes in the open set
        # and find the one with the lowest f value.
        - foreach <def[openset]> {
            #- narrate "foreach value was <def[value]>"
            - define current_f <proc[f_score].context[<def[value]>]>
            - if  <def[current_f]> < <def[min_f]> {
                - define min_f <def[current_f]>
                - define min_node <def[value]>
            }
        }

        - narrate "min_f was <def[min_f]>, min_node was <def[min_node]>"
        - determine <def[min_node]>