-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuilderPlug.cs
428 lines (350 loc) · 13.1 KB
/
BuilderPlug.cs
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
#region ================== Copyright (c) 2009 Boris Iwanski
/*
* Copyright (c) 2009 Boris Iwanski
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using CodeImp.DoomBuilder.Windows;
using CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Map;
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.Geometry;
using System.Drawing;
using CodeImp.DoomBuilder.Editing;
using CodeImp.DoomBuilder.Plugins;
using CodeImp.DoomBuilder.Actions;
using CodeImp.DoomBuilder.Types;
using CodeImp.DoomBuilder.Config;
using CodeImp.DoomBuilder.Data;
#endregion
namespace CodeImp.DoomBuilder.LightingMode
{
//
// MANDATORY: The plug!
// This is an important class to the Doom Builder core. Every plugin must
// have exactly 1 class that inherits from Plug. When the plugin is loaded,
// this class is instantiated and used to receive events from the core.
// Make sure the class is public, because only public classes can be seen
// by the core.
//
public class BuilderPlug : Plug
{
// Static instance. We can't use a real static class, because BuilderPlug must
// be instantiated by the core, so we keep a static reference. (this technique
// should be familiar to object-oriented programmers)
private static BuilderPlug me;
// Static property to access the BuilderPlug
public static BuilderPlug Me { get { return me; } }
private BlockMap<BlockEntry> blockmap;
// This event is called when the plugin is initialized
public override void OnInitialize()
{
base.OnInitialize();
// This binds the methods in this class that have the BeginAction
// and EndAction attributes with their actions. Without this, the
// attributes are useless. Note that in classes derived from EditMode
// this is not needed, because they are bound automatically when the
// editing mode is engaged.
General.Actions.BindMethods(this);
// Keep a static reference
me = this;
}
public override void OnMapSaveBegin(SavePurpose purpose)
{
LightingMode.SaveLightingData();
}
// This is called when the plugin is terminated
public override void Dispose()
{
base.Dispose();
// This must be called to remove bound methods for actions.
General.Actions.UnbindMethods(this);
}
public Queue<DrawnVertex> CreateDVL(LightingThing lt)
{
List<Vertex> shadowCastingVertices = new List<Vertex>();
Queue<DrawnVertex> dvl = new Queue<DrawnVertex>();
// Update the blockmap if the map was changed
if (General.Map.IsChanged)
CreateBlockmap();
// clear all vertices found for previous light sources
shadowCastingVertices.Clear();
// look for the vertices in the map that may cast a shadow
// vertices that have a "blocking" line both facing and not
// facing the light source are added to the list
foreach (Vertex v in General.Map.Map.Vertices)
{
int facing = 0;
if (Vector2D.Distance(lt.Position, v.Position) > 1024.0f) continue;
foreach (Linedef ld in v.Linedefs)
{
// only one sided lines are "blocking" right now
// TODO: other lines may be blocking too, for example
// when the height differences between sectors is too big
if (ld.Back == null)
{
if (ld.SideOfLine(lt.Position) <= 0.0f)
{
// with only one sided lines "blocking", only
// one of those lines may face the light source,
// if two are facing, that vertex can't cast
// a shadow.
// This method may not work when other lines
// besides one sided lines can block
facing++;
}
}
}
// only one line connected to the vertex facing the light source?
// then let's check if the LOS from the light source to that vertex
// is not blocked by another line. If it's not blocked finaly add
// the vertex to the shadow casting vertex list
if (facing == 1)
{
bool blocked = false;
// get all blockmap blocks that are between the light and the
// shadow casting vertex
List<BlockEntry> blocks = blockmap.GetLineBlocks(lt.Position, v.Position);
foreach (BlockEntry be in blocks)
{
foreach (Linedef ld in be.Lines)
{
// don't process lines that have the current vertex
// as start or end vertex (they would instantly result
// in an intersection)
if (ld.Start == v || ld.End == v) continue;
// don't process this linedef if it's start or end vertices are on the
// line between the light and the current vertex
if (Line2D.GetSideOfLine(lt.Position, v.Position, ld.Start.Position) == 0.0f) continue;
if (Line2D.GetSideOfLine(lt.Position, v.Position, ld.End.Position) == 0.0f) continue;
// only check one sided lines
// the intersection check is done from between:
// - the line we are looking at (start and end vertex)
// - the line between the light source position (t.Position)
// and the current vertex position (v.Position)
//
// if any intersection was found no other lines have to be checked
if (ld.Back == null && Line2D.GetIntersection(ld.Start.Position, ld.End.Position, lt.Position.x, lt.Position.y, v.Position.x, v.Position.y) == true)
{
// MessageBox.Show("vertex " + v.Index.ToString() + " intersection with line " + ld.Index.ToString());
blocked = true;
break;
}
}
if (blocked == true) break;
}
// only add the vertex if no blocking occured
if (blocked == false) shadowCastingVertices.Add(v);
}
}
// process all shadow casting vertices
foreach (Vertex v in shadowCastingVertices)
{
Vector2D normal;
Vector2D closestIntersection;
DrawnVertex[] dv = new DrawnVertex[2];
// List<DrawnVertex> dvl = new List<DrawnVertex>();
List<Vector2D> intersections = new List<Vector2D>();
// get the unit vector of the line between the shadow casting vertex
// and the light source
normal = Line2D.GetNormal(v.Position.x - lt.Position.x, v.Position.y - lt.Position.y);
// check for intersections between the view line of the light source
// throught the shadow casting vertex against all one sided lines
// in the map. The intersections are stored in a list
foreach (Linedef ld in General.Map.Map.Linedefs)
{
float u_ray = 0;
float u_line = 0;
// don't process lines that have the current vertex
// as start or end vertex (they would instantly result
// in an intersection)
if (ld.Start == v || ld.End == v) continue;
// only check one sided lines
// the intersection check is done from between:
// - the line we are looking at (start and end vertex)
// - the line between the shadow casting vertex and an imaginary
// end point in the far distance
//
// TODO: make Line2D.GetIntersection more readable
if (ld.Back == null && Line2D.GetIntersection(ld.Start.Position, ld.End.Position, v.Position.x, v.Position.y, v.Position.x + normal.x * 10000, v.Position.y + normal.y * 10000, out u_ray, out u_line) == true)
{
// the position is computed by the info returned by Line2D.GetIntersection
intersections.Add(new Vector2D(Line2D.GetCoordinatesAt(ld.Start.Position, ld.End.Position, u_line)));
}
}
// if there are no intersections found continue with the next vertex
if (intersections.Count == 0) continue;
// now the closest intersection has to be found
// the first intersection in the list is set as
// the the closest intersection for now
closestIntersection = intersections[0];
// check out all intersections
foreach (Vector2D vec in intersections)
{
// if the distance between the shadow casting vertex and the
// looked at intersection is shorter than the old closest
// intersection, set the looked at intersection as closest
if (Vector2D.Distance(vec, v.Position) < Vector2D.Distance(closestIntersection, v.Position))
{
closestIntersection = vec;
}
}
// create the vertices we need to draw
// first the vertex at the shadow casting vertex...
dv[0].pos.x = v.Position.x;
dv[0].pos.y = v.Position.y;
dv[0].stitch = true;
dv[0].stitchline = true;
// ... then the vertex at the closest intersection
dv[1].pos.x = closestIntersection.x;
dv[1].pos.y = closestIntersection.y;
dv[1].stitch = true;
dv[1].stitchline = true;
// add the two vertices to the list we want to draw
dvl.Enqueue(dv[0]);
dvl.Enqueue(dv[1]);
}
return dvl;
}
public void CreateBlockmap()
{
// Make blockmap
RectangleF area = MapSet.CreateArea(General.Map.Map.Vertices);
area = MapSet.IncreaseArea(area, General.Map.Map.Things);
if(blockmap != null) blockmap.Dispose();
blockmap = new BlockMap<BlockEntry>(area);
blockmap.AddLinedefsSet(General.Map.Map.Linedefs);
blockmap.AddSectorsSet(General.Map.Map.Sectors);
blockmap.AddThingsSet(General.Map.Map.Things);
}
public bool HasLOS(Linedef ld, Vector3D p)
{
Vector2D v1 = Line2D.GetCoordinatesAt(ld.Start.Position, ld.End.Position, 0.5f);
string bla = "";
if (Line2D.GetSideOfLine(ld.Start.Position, ld.End.Position, p) == 0.0f)
{
return false;
}
foreach (Linedef ldx in General.Map.Map.Linedefs)
{
if (ld == ldx) continue;
/*
if (ld.Start == sd.Line.Start) continue;
if (ld.Start == sd.Line.End) continue;
if (ld.End == sd.Line.Start) continue;
if (ld.End == sd.Line.End) continue;
*/
//if (Line2D.GetSideOfLine(ld.Start.Position, ld.End.Position, p) <= 0.0f)
//{
// continue;
//}
bla += "checking ld " + ldx.Index.ToString() + " @ " + ldx.Start.Position.ToString() + " / " + ldx.End.Position.ToString() + " -vs- " + v1.ToString() + " / " + p.ToString();
if (ldx.Back == null && Line2D.GetIntersection(ldx.Start.Position, ldx.End.Position, v1.x, v1.y, p.x, p.y) == true)
{
bla += " collision\n";
//MessageBox.Show(bla);
// MessageBox.Show(ld.Index.ToString());
return false;
}
bla += " no collision\n";
}
//if (ld.Index == 12)
// MessageBox.Show(bla);
return true;
}
public bool HasLOS(Vector3D p1, Vector3D p2)
{
Line2D line = new Line2D(p1, p2);
// TODO: use blockmap
foreach (Linedef ld in General.Map.Map.Linedefs)
{
if (ld.Back == null && Line2D.GetIntersection(line, ld.Line) == true)
return false;
}
return true;
}
private Vector2D GetIncenter(List<Vector2D> points)
{
Line2D a = new Line2D(points[0], points[1]);
Line2D b = new Line2D(points[1], points[2]);
Line2D c = new Line2D(points[2], points[0]);
Vector2D A = points[2];
Vector2D B = points[0];
Vector2D C = points[1];
float p = a.GetLength() + b.GetLength() + c.GetLength();
float x = (a.GetLength() * A.x + b.GetLength() * B.x + c.GetLength() * C.x) / p;
float y = (a.GetLength() * A.y + b.GetLength() * B.y + c.GetLength() * C.y) / p;
return new Vector2D(x, y);
}
#region ================== Actions
[BeginAction("do_lighting")]
public void DoLighting()
{
Queue<DrawnVertex> dvl = new Queue<DrawnVertex>();
// Make it not crash on drawing
General.Settings.FindDefaultDrawSettings();
// Make the action undo-able
General.Map.UndoRedo.CreateUndo("Create lighting");
// process every light source in the map
foreach (LightingThing lt in LightingMode.lights)
{
Queue<DrawnVertex> dvltmp;
// only process lights that are enabled
if (lt.Enabled == false) continue;
dvltmp = CreateDVL(lt);
while (dvltmp.Count != 0)
{
dvl.Enqueue(dvltmp.Dequeue());
}
}
General.Interface.DisplayStatus(StatusType.Action, "Drawing lighting...");
// draw all lines
while(dvl.Count != 0)
{
Tools.DrawLines(new List<DrawnVertex> { dvl.Dequeue(), dvl.Dequeue() });
if(!General.Map.UDMF) General.Map.Map.SnapAllToAccuracy();
General.Map.Map.Update();
}
// change the light values of the sectors
foreach (LightingThing lt in LightingMode.lights)
{
// right now the light source must be a floor lamp
if (lt.Enabled == false) continue;
foreach (Sector s in General.Map.Map.Sectors)
{
bool lightit = false;
s.Triangles.Triangulate(s);
Vector2D incenter = GetIncenter(new List<Vector2D>() { s.Triangles.Vertices[0], s.Triangles.Vertices[1], s.Triangles.Vertices[2] });
lightit = HasLOS(lt.Position, incenter);
if (lightit == true)
{
s.Brightness += lt.Brightness;
}
}
}
General.Map.IsChanged = true;
General.Map.Map.Update();
General.Map.Data.UpdateUsedTextures();
General.Interface.RedrawDisplay();
General.Interface.DisplayStatus(StatusType.Action, "Created lighting.");
}
#endregion
}
}