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
|
{
id: "pathfinding_experiment",
always_active: true,
functions: "def distance(u1,v1,u2,v2) sqrt(abs(u2 - u1)^2 + abs(v2 - v1)^2);
def is_line_solid(object_type obj,u1,v1,u2,v2)
sum(flatten([map( range( num_tests),'foo', solid(obj.level,u1 + segment_length_x*index, v1 + segment_length_y*index, max(5,segment_length_x), max(5,segment_length_y),'debug'))
where segment_length_x = (u2 - u1)/num_tests where segment_length_y = (v2 - v1)/num_tests] where num_tests = distance(u1,v1,u2,v2) / 40));
def line_midpoint(u1,v1,u2,v2)
[u1 + (u2 - u1)/2, v1 + (v2 - v1)/2];
def is_midpoint_solid(object_type obj, u1,v1,u2,v2)
solid(obj.level,midp[0]-20,midp[1]-20,40,40,'debug')
where midp = line_midpoint(u1,v1,u2,v2);
def shift_midpoint(object_type obj,u,v, count)
if( solid(obj.level,new_x-20,new_y-20,40,40,'debug') and count < 40,shift_midpoint(obj, new_x, new_y, -(2*count)) ,[new_x,new_y,if(count > 40, add(obj.vars.midpoint_search_failures,1))])
where new_x = debug_fn('count',u) + (1d2-4)*debug_fn(count,count)
where new_y = v + (1d2-4)*count;
def generate_midpoint(object_type obj,u1,v1,u2,v2)
if( is_midpoint_solid(obj, u1,v1,u2,v2), shift_midpoint(obj,midp[0],midp[1], 1), [midp[0],midp[1]])
where midp = line_midpoint(u1,v1,u2,v2);
def push_pts(object_type obj,the_x,the_y)
[add(obj.vars.Xpoints, [the_x]),
add(obj.vars.Ypoints, [the_y])];
def split_line(object_type obj,u1,v1,u2,v2, count)
if(is_line_solid(obj,u1,v1,u2,v2),
if(count < 20,
[if(is_line_solid(obj,u1,v1,newMidp[0],newMidp[1]), split_line(obj,u1,v1,newMidp[0],newMidp[1], count+1), push_pts(obj,newMidp[0],newMidp[1])),
if(is_line_solid(obj,newMidp[0],newMidp[1],u2,v2), split_line(obj,newMidp[0],newMidp[1],u2,v2,count+1), push_pts(obj,u2,v2) )],
add(obj.vars.pathfinding_failures,1)),
push_pts(obj,u2,v2))
where newMidp = generate_midpoint(obj,u1,v1,u2,v2);
1",
#
# loose intent; we take a source and destination, and treat it as a line. We can test this (or any other) line to see if it's got any obstructions. If there are, we split it in two. For each step of splitting the line, we first check if the midpoint itself is obstructed; if it is, we move that midpoint tangentially to the line it's on until it's unobstructed (how, precisely, remains to be hacked out). If the mipoint is not obstructed, then we repeat the algorithm for both of the new lines.
properties: {
set_target: "def(u1,v1,u2,v2) [set(vars.x_1, u1),set(vars.x_1, u1),set(vars.x_1, u1),set(vars.x_1, u1)]",
},
vars: {
Xpoints: [],
Ypoints: [],
midpoint_search_failures: 0,
pathfinding_failures: 0,
},
on_communicate_path: "[set(parent.vars.path_x, vars.Xpoints),set(parent.vars.path_y, vars.Ypoints)]",
#on_process="debug(distance(vars.x_1,vars.y_1,vars.x_2,vars.y_2)/5)"
#on_process="debug(is_line_solid(me,vars.x_1,vars.y_1,vars.x_2,vars.y_2))"
#on_process="if(cycle%2=1,debug(is_line_solid(me,vars.x_1,vars.y_1,mid_x,mid_y)), debug(is_line_solid(me,mid_x,mid_y,vars.x_2,vars.y_2))) where mid_x = line_midpoint(vars.x_1,vars.y_1,vars.x_2,vars.y_2)[0] where mid_y = line_midpoint(vars.x_1,vars.y_1,vars.x_2,vars.y_2)[1]"
#on_process="debug(is_midpoint_solid(me,vars.x_1,vars.y_1,vars.x_2,vars.y_2))"
#on_process="debug(generate_midpoint(me,vars.x_1,vars.y_1,vars.x_2,vars.y_2))"
#on_create="split_line(me,vars.x_1,vars.y_1,vars.x_2,vars.y_2,0)"
on_build_path: "split_line(me,vars.x_1,vars.y_1,vars.x_2,vars.y_2,0)",
on_create: "fire_event('build_path')",
#on_process="if(cycle=2,[debug([vars.Xpoints,vars.Ypoints]),debug('midpoint_failures = '+vars.midpoint_search_failures),debug('pathfinding_failures = '+vars.pathfinding_failures)])"
timer_frequency: 1,
on_timer: "map(range(vars.Xpoints.size),'foo', if(cycle/10>index,spawn('pathfinding_experiment.dot', vars.Xpoints[index], vars.Ypoints[index],1)))",
on_end_anim: "animation('normal')",
zorder: 50,
#editor_info: {
# category: "experimental",
# var: [
# {
# name: "x_1",
# type: "x",
# value: "midpoint_x",
# },
# {
# name: "y_1",
# type: "y",
# value: "midpoint_y",
# },
# {
# name: "x_2",
# type: "x",
# value: "midpoint_x+1",
# },
# {
# name: "y_2",
# type: "y",
# value: "midpoint_y+1",
# },
# ],
#},
animation: {
id: "normal",
image: "effects/particles.png",
x: 210,
y: 73,
w: 1,
h: 1,
#w=28
#h=28
frames: 1,
duration: 1000,
},
object_type: {
id: "dot",
zorder: 100,
timer_frequency: 100,
on_timer: "die()",
animation: {
id: "normal",
image: "enemies/shooting-plant.png",
rect: [90,2,100,12],
frames: 1,
duration: 250,
},
},
}
|