1{
2id: "pathfinding_experiment",
3always_active: true,
4functions: "def distance(u1,v1,u2,v2) sqrt(abs(u2 - u1)^2 + abs(v2 - v1)^2);
5
6		def is_line_solid(object_type obj,u1,v1,u2,v2)
7			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'))
8			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));
9
10		def line_midpoint(u1,v1,u2,v2)
11			[u1 + (u2 - u1)/2, v1 + (v2 - v1)/2];
12
13		def is_midpoint_solid(object_type obj, u1,v1,u2,v2)
14			solid(obj.level,midp[0]-20,midp[1]-20,40,40,'debug')
15			where midp = line_midpoint(u1,v1,u2,v2);
16
17
18		def shift_midpoint(object_type obj,u,v, count)
19			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))])
20
21			where new_x = debug_fn('count',u) + (1d2-4)*debug_fn(count,count)
22			where new_y = v + (1d2-4)*count;
23
24		def generate_midpoint(object_type obj,u1,v1,u2,v2)
25			if(	is_midpoint_solid(obj, u1,v1,u2,v2), shift_midpoint(obj,midp[0],midp[1], 1), [midp[0],midp[1]])
26
27			where midp = line_midpoint(u1,v1,u2,v2);
28
29		def push_pts(object_type obj,the_x,the_y)
30			[add(obj.vars.Xpoints, [the_x]),
31			add(obj.vars.Ypoints, [the_y])];
32
33		def split_line(object_type obj,u1,v1,u2,v2, count)
34			if(is_line_solid(obj,u1,v1,u2,v2),
35				if(count < 20,
36					[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])),
37
38
39					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)   )],
40
41					add(obj.vars.pathfinding_failures,1)),
42						push_pts(obj,u2,v2))
43
44			where newMidp = generate_midpoint(obj,u1,v1,u2,v2);
45
46
47		1",
48
49#
50# 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.
51properties: {
52	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)]",
53},
54vars: {
55	Xpoints: [],
56	Ypoints: [],
57	midpoint_search_failures: 0,
58	pathfinding_failures: 0,
59},
60on_communicate_path: "[set(parent.vars.path_x, vars.Xpoints),set(parent.vars.path_y, vars.Ypoints)]",
61
62#on_process="debug(distance(vars.x_1,vars.y_1,vars.x_2,vars.y_2)/5)"
63#on_process="debug(is_line_solid(me,vars.x_1,vars.y_1,vars.x_2,vars.y_2))"
64#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]"
65#on_process="debug(is_midpoint_solid(me,vars.x_1,vars.y_1,vars.x_2,vars.y_2))"
66#on_process="debug(generate_midpoint(me,vars.x_1,vars.y_1,vars.x_2,vars.y_2))"
67#on_create="split_line(me,vars.x_1,vars.y_1,vars.x_2,vars.y_2,0)"
68on_build_path: "split_line(me,vars.x_1,vars.y_1,vars.x_2,vars.y_2,0)",
69on_create: "fire_event('build_path')",
70
71#on_process="if(cycle=2,[debug([vars.Xpoints,vars.Ypoints]),debug('midpoint_failures = '+vars.midpoint_search_failures),debug('pathfinding_failures = '+vars.pathfinding_failures)])"
72timer_frequency: 1,
73on_timer: "map(range(vars.Xpoints.size),'foo', if(cycle/10>index,spawn('pathfinding_experiment.dot', vars.Xpoints[index], vars.Ypoints[index],1)))",
74on_end_anim: "animation('normal')",
75zorder: 50,
76#editor_info: {
77#	category: "experimental",
78#	var: [
79#		{
80#			name: "x_1",
81#			type: "x",
82#			value: "midpoint_x",
83#		},
84#		{
85#			name: "y_1",
86#			type: "y",
87#			value: "midpoint_y",
88#		},
89#		{
90#			name: "x_2",
91#			type: "x",
92#			value: "midpoint_x+1",
93#		},
94#		{
95#			name: "y_2",
96#			type: "y",
97#			value: "midpoint_y+1",
98#		},
99#	],
100#},
101animation: {
102	id: "normal",
103	image: "effects/particles.png",
104	x: 210,
105	y: 73,
106	w: 1,
107	h: 1,
108
109	#w=28
110	#h=28
111	frames: 1,
112	duration: 1000,
113},
114object_type: {
115	id: "dot",
116	zorder: 100,
117	timer_frequency: 100,
118	on_timer: "die()",
119	animation: {
120		id: "normal",
121		image: "enemies/shooting-plant.png",
122		rect: [90,2,100,12],
123		frames: 1,
124		duration: 250,
125	},
126},
127}