├── .gitignore ├── version.mak ├── src ├── .gitignore ├── .lldbinit ├── bbox.h ├── planes.h ├── Makefile.mac ├── Makefile.win ├── Makefile.lin ├── drawdebug.c ├── planes.c ├── groundtraffic.h ├── draw.c └── routes.c ├── GroundTraffic ├── .gitignore └── ReadMe.txt ├── Makefile ├── README.md ├── LICENSE.md ├── lgpl-2.1.txt └── ReadMe.html /.gitignore: -------------------------------------------------------------------------------- 1 | *.bak 2 | *.zip 3 | 4 | .DS_Store 5 | Thumbs.db 6 | -------------------------------------------------------------------------------- /version.mak: -------------------------------------------------------------------------------- 1 | PROJECT=GroundTraffic 2 | VER=152 3 | VERSION=1.52 4 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | *.bak 2 | *.d 3 | *.o 4 | *.dep 5 | *.obj 6 | *.pdb 7 | *.xpl 8 | -------------------------------------------------------------------------------- /GroundTraffic/.gitignore: -------------------------------------------------------------------------------- 1 | # Placeholder to create directory for output 2 | *.xpl 3 | *.exp 4 | *.ilk 5 | *.lib 6 | *.idb 7 | *.pdb 8 | -------------------------------------------------------------------------------- /src/.lldbinit: -------------------------------------------------------------------------------- 1 | target create "~/Desktop/X-Plane 10/X-Plane.app/Contents/MacOS/X-Plane" 2 | settings set target.run-args -- --no_crash_reporter 3 | -------------------------------------------------------------------------------- /GroundTraffic/ReadMe.txt: -------------------------------------------------------------------------------- 1 | GroundTraffic plugin 2 | ==================== 3 | 4 | [↯] No user serviceable parts inside [!] 5 | 6 | Version 1.52 7 | Copyright Jonathan Harris 2013-2016 8 | http://marginal.org.uk/x-planescenery/ 9 | Licensed under the GNU Library General Public License v2.1. 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | include version.mak 2 | 3 | TARGET=$(PROJECT)_$(VER).zip 4 | 5 | FILES=ReadMe.html $(PROJECT)/ReadMe.txt $(PROJECT)/lin.xpl $(PROJECT)/mac.xpl $(PROJECT)/win.xpl $(PROJECT)/64/lin.xpl $(PROJECT)/64/win.xpl 6 | 7 | all: $(TARGET) 8 | 9 | clean: 10 | rm $(TARGET) 11 | 12 | $(TARGET): $(FILES) 13 | touch $^ 14 | chmod +x $(PROJECT)/*.xpl $(PROJECT)/64/*.xpl 15 | rm -f $(TARGET) 16 | zip -MM -o $(TARGET) $+ 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GroundTraffic kit for X-Plane® 2 | ==== 3 | 4 | This kit allows [X-Plane](x-plane.com) scenery designers to add animated ground vehicle traffic to airport scenery packages. 5 | 6 | The repo contains assets intended for scenery designers, plus the source code to the X-Plane plugin that performs the animations. Scenery designer oriented documention is contained in the file [ReadMe.html](http://htmlpreview.github.io/?https://raw.githubusercontent.com/Marginal/GroundTraffic/master/ReadMe.html). 7 | 8 | Building the plugin 9 | ---- 10 | The plugin is built from the `src` directory. 11 | 12 | Mac 32 & 64 bit fat binary: 13 | 14 | make -f Makefile.mac 15 | 16 | Linux 32 & 64 bit binaries: 17 | 18 | make -f Makefile.lin 19 | 20 | Windows 32 or 64 bit binary: 21 | 22 | vcvarsall [target] 23 | nmake -f Makefile.win 24 | 25 | -------------------------------------------------------------------------------- /src/bbox.h: -------------------------------------------------------------------------------- 1 | /* 2 | * GroundTraffic 3 | * 4 | * (c) Jonathan Harris 2013 5 | * 6 | * Licensed under GNU LGPL v2.1. 7 | */ 8 | 9 | #ifndef _BBOX_H_ 10 | #define _BBOX_H_ 11 | 12 | typedef struct 13 | { 14 | float minlat, maxlat, minlon, maxlon; 15 | } bbox_t; 16 | 17 | static inline void bbox_init(bbox_t *bbox) 18 | { 19 | bbox->minlat = 90; 20 | bbox->maxlat = -90; 21 | bbox->minlon = 180; 22 | bbox->maxlon = -180; 23 | } 24 | 25 | static inline void bbox_add(bbox_t *bbox, float lat, float lon) 26 | { 27 | if (lat < bbox->minlat) bbox->minlat = lat; 28 | if (lat > bbox->maxlat) bbox->maxlat = lat; 29 | if (lon < bbox->minlon) bbox->minlon = lon; 30 | if (lon > bbox->maxlon) bbox->maxlon = lon; 31 | } 32 | 33 | static inline int bbox_intersect(bbox_t *a, bbox_t *b) 34 | { 35 | return ((a->minlat <= b->maxlat) && (a->maxlat > b->minlat) && 36 | (a->minlon <= b->maxlon) && (a->maxlon > b->minlon)); 37 | } 38 | 39 | #endif /* _BBOX_H_ */ 40 | -------------------------------------------------------------------------------- /src/planes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * GroundTraffic 3 | * 4 | * (c) Jonathan Harris 2013 5 | * 6 | * Licensed under GNU LGPL v2.1. 7 | */ 8 | 9 | #ifdef _MSC_VER 10 | typedef __int32 int32_t; 11 | #else 12 | # include 13 | #endif 14 | 15 | #include "groundtraffic.h" 16 | 17 | #define MAX_PLANES 20 /* Seems to be a hard-coded limit in X-Plane, but maybe could increase in future */ 18 | #define MAX_ACF_NAME 256 19 | #define MAX_ACF_PATH 512 20 | 21 | typedef struct 22 | { 23 | XPLMDataRef x, y, z, vx, vz, hdg, gear; 24 | } plane_ref_t; 25 | 26 | typedef struct 27 | { 28 | point_t p, v; /* Position [m], speed [m/s] */ 29 | float hdg; /* [degrees] */ 30 | } plane_pos_t; 31 | 32 | typedef struct 33 | { 34 | char name[MAX_ACF_NAME]; 35 | float length, semiwidth, refheight, cgz; /* dimensions [m] */ 36 | } plane_acf_t; 37 | 38 | 39 | /* prototypes */ 40 | int setup_plane_refs(); 41 | void reset_planes(); 42 | int count_planes(); 43 | plane_acf_t *get_plane_info(int planeno); 44 | int get_plane_pos(plane_pos_t *pos, int planeno); 45 | point_t *get_plane_footprint(int planeno, float time); 46 | -------------------------------------------------------------------------------- /src/Makefile.mac: -------------------------------------------------------------------------------- 1 | # -*-Makefile-*- 2 | 3 | include ../version.mak 4 | 5 | XPSDK=../../XPSDK213 6 | 7 | CC=cc 8 | #BUILD=-g -DDEBUG 9 | #LDFLAGS=-bundle 10 | BUILD=-O3 -DNDEBUG 11 | LDFLAGS=-bundle -Wl,-x 12 | DEFINES=-DAPL=1 -DIBM=0 -DLIN=0 -DVERSION=$(VERSION) 13 | INC=-F$(XPSDK)/Libraries/Mac -I$(XPSDK)/CHeaders/XPLM 14 | CFLAGS=-arch i386 -arch x86_64 -march=core2 -ffast-math -pipe -Wall -Winline -Wno-missing-braces -fvisibility=hidden -mmacosx-version-min=10.6 $(BUILD) $(DEFINES) $(INC) 15 | 16 | VPATH= 17 | SRC=groundtraffic.c draw.c routes.c planes.c drawdebug.c 18 | LIBS=-framework XPLM -framework OpenGL 19 | TARGETDIR=../$(PROJECT) 20 | INSTALLDIR=~/Desktop/X-Plane\ 10/Custom\ Scenery/KSEA\ Demo\ GroundTraffic/plugins/$(PROJECT) 21 | 22 | ############################################################################ 23 | 24 | BUILDDIR=$(shell uname) 25 | OBJS=$(addprefix $(BUILDDIR)/, $(addsuffix .o, $(basename $(notdir $(SRC))))) 26 | TARGET=$(TARGETDIR)/mac.xpl 27 | 28 | RM=rm -f 29 | CP=cp -p 30 | MD=mkdir -p 31 | 32 | .PHONY: all clean install 33 | 34 | all: $(TARGET) 35 | 36 | install: $(TARGET) | $(INSTALLDIR) 37 | $(CP) $(TARGET) $(INSTALLDIR)/ 38 | 39 | $(TARGET): $(OBJS) | $(TARGETDIR) 40 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $+ $(LIBS) 41 | 42 | $(OBJS): | $(BUILDDIR) 43 | 44 | $(BUILDDIR): 45 | $(MD) $(BUILDDIR) 46 | 47 | $(BUILDDIR)/%.o: %.c 48 | $(CC) $(CFLAGS) -c -o $@ $< 49 | @$(CC) $(BUILD) $(DEFINES) $(INC) -MM $< | sed -e 's|$*.o|$@|' > $(@:.o=.d) 50 | 51 | $(TARGETDIR): 52 | $(MD) $(TARGETDIR) 53 | 54 | $(INSTALLDIR): 55 | $(MD) $(INSTALLDIR) 56 | 57 | clean: 58 | $(RM) *~ *.bak $(OBJS) $(OBJS:.o=.d) $(TARGET) 59 | 60 | # pull in dependency info 61 | -include $(OBJS:.o=.d) 62 | -------------------------------------------------------------------------------- /src/Makefile.win: -------------------------------------------------------------------------------- 1 | # -*-Makefile-*- in NMAKE format 2 | 3 | !include ..\version.mak 4 | 5 | XPSDK=..\..\XPSDK213 6 | 7 | CC=cl 8 | #BUILD=-Zi -DDEBUG 9 | BUILD=-O2 -DNDEBUG 10 | DEFINES=-DIBM=1 -DAPL=0 -DLIN=0 -DVERSION=$(VERSION) 11 | INC=-I$(XPSDK)\CHeaders\XPLM -I$(XPSDK)/CHeaders/Widgets 12 | CFLAGS=-nologo -fp:fast $(BUILD) $(DEFINES) $(INC) 13 | LDFLAGS=-LD 14 | 15 | SRC=.\groundtraffic.c .\draw.c .\routes.c .\planes.c .\drawdebug.c 16 | LIBS=$(XPSDK)\Libraries\Win\XPLM$(ARCHXP).lib $(XPSDK)\Libraries\Win\XPWidgets$(ARCHXP).lib GlU32.Lib OpenGL32.Lib 17 | TARGETDIR=..\$(PROJECT) 18 | INSTALLDIR=X:\Desktop\X-Plane 10\Custom Scenery\KSEA Demo GroundTraffic\plugins\$(PROJECT) 19 | 20 | ############################################################################ 21 | 22 | OBJS=$(SRC:.c=.obj) 23 | # Work out which target we're set up for by looking for a program (ml64.exe) that only exists in the path for one target 24 | !if [ml64 >nul 2>&1] == 0 25 | CPU=x64 26 | ARCHDIR=64 27 | ARCHXP=_64 28 | BUILDDIR=Win64 29 | OBJS=$(OBJS:.\=Win64\) 30 | !else 31 | CFLAGS=$(CFLAGS) -arch:SSE2 32 | CPU=x86 33 | ARCHDIR= 34 | ARCHXP= 35 | BUILDDIR=Win32 36 | OBJS=$(OBJS:.\=Win32\) 37 | !endif 38 | TARGET=$(TARGETDIR)\$(ARCHDIR)\win.xpl 39 | 40 | RM=del /q 41 | CP=copy /y 42 | MD=mkdir 43 | 44 | all: $(TARGET) 45 | 46 | install: $(TARGET) 47 | -@if not exist "$(INSTALLDIR)\$(ARCHDIR)" $(MD) "$(INSTALLDIR)\$(ARCHDIR)" 48 | $(CP) $(TARGET) "$(INSTALLDIR)\$(ARCHDIR)" 49 | 50 | $(TARGET): $(OBJS) 51 | -@if not exist "$(TARGETDIR)\$(ARCHDIR)" $(MD) "$(TARGETDIR)\$(ARCHDIR)" 52 | $(CC) $(CFLAGS) $(LDFLAGS) -Fe$@ $(OBJS) $(LIBS) 53 | 54 | .c{$(BUILDDIR)}.obj: 55 | $(CC) $(CFLAGS) -c -Fo$@ -Fd$* $< 56 | @echo $@: $< \> $*.dep 57 | @for /f "usebackq tokens=4*" %i in (`"$(CC) $(CFLAGS) /Zs /Z7 /w /showIncludes $< 2>nul: | findstr Note:"`) do @echo. "%i %j" \>> $*.dep 58 | @echo.>> $*.dep 59 | 60 | clean: 61 | -$(RM) *~ *.bak $(OBJS:.obj=.*) $(BUILDDIR)\Makefile.dep $(TARGET:.xpl=.*) 2>nul: 62 | 63 | # pull in dependency info 64 | !if [$(MD) $(BUILDDIR) 2>nul: & type $(OBJS:.obj=.dep) > $(BUILDDIR)\Makefile.dep 2>nul:] 65 | !endif 66 | !include $(BUILDDIR)\Makefile.dep 67 | -------------------------------------------------------------------------------- /src/Makefile.lin: -------------------------------------------------------------------------------- 1 | # -*-Makefile-*- 2 | # Setup for building on Ubuntu x86_64 with multilib support, plus: 3 | # sudo ln -s mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so 4 | # sudo ln -s libGLU.so.1 /usr/lib/i386-linux-gnu/libGLU.so 5 | 6 | include ../version.mak 7 | 8 | XPSDK=../../XPSDK213 9 | 10 | CC=gcc 11 | #BUILD=-g -DDEBUG 12 | #LDFLAGS= 13 | BUILD=-O3 -s -DNDEBUG 14 | LDFLAGS=-Wl,-x 15 | DEFINES=-DAPL=0 -DIBM=0 -DLIN=1 -DVERSION=$(VERSION) 16 | INC=-I$(XPSDK)/CHeaders/XPLM -I$(XPSDK)/CHeaders/Widgets 17 | CFLAGS=-march=core2 -ffast-math -pipe -Wall -Wdouble-promotion -Winline -Wno-missing-braces -static-libgcc -shared -fPIC -fvisibility=hidden $(BUILD) $(DEFINES) $(INC) 18 | 19 | VPATH= 20 | SRC=groundtraffic.c draw.c routes.c planes.c drawdebug.c 21 | LIBS=-lGLU -lGL 22 | TARGETDIR=../$(PROJECT) 23 | INSTALLDIR=~/Desktop/X-Plane\ 10/Custom\ Scenery/KSEA\ Demo\ GroundTraffic/plugins/$(PROJECT) 24 | 25 | ############################################################################ 26 | 27 | BUILD_32=$(shell uname)32 28 | OBJS_32=$(addprefix $(BUILD_32)/, $(addsuffix .o, $(basename $(notdir $(SRC))))) 29 | TARGET_32=$(TARGETDIR)/lin.xpl 30 | INSTALL_32=$(INSTALLDIR) 31 | BUILD_64=$(shell uname)64 32 | OBJS_64=$(addprefix $(BUILD_64)/, $(addsuffix .o, $(basename $(notdir $(SRC))))) 33 | TARGET_64=$(TARGETDIR)/64/lin.xpl 34 | INSTALL_64=$(INSTALLDIR)/64 35 | TARGET=$(TARGET_32) $(TARGET_64) 36 | 37 | RM=rm -f 38 | CP=cp -p 39 | MD=mkdir -p 40 | 41 | .PHONY: all clean install 42 | 43 | all: $(TARGET_32) $(TARGET_64) 44 | 45 | install: $(TARGET_32) $(TARGET_64) | $(INSTALL_32) $(INSTALL_64) 46 | $(CP) $(TARGET_32) $(INSTALL_32)/ 47 | $(CP) $(TARGET_64) $(INSTALL_64)/ 48 | 49 | $(TARGET_32): $(OBJS_32) | $(TARGETDIR) 50 | $(CC) $(CFLAGS) $(LDFLAGS) -m32 -o $@ $+ $(LIBS) 51 | 52 | $(TARGET_64): $(OBJS_64) | $(TARGETDIR)/64 53 | $(CC) $(CFLAGS) $(LDFLAGS) -m64 -o $@ $+ $(LIBS) 54 | 55 | $(OBJS_32): | $(BUILD_32) 56 | 57 | $(OBJS_64): | $(BUILD_64) 58 | 59 | $(BUILD_32): 60 | $(MD) $(BUILD_32) 61 | 62 | $(BUILD_64): 63 | $(MD) $(BUILD_64) 64 | 65 | $(BUILD_32)/%.o: %.c 66 | $(CC) $(CFLAGS) -m32 -c -o $@ $< 67 | @$(CC) $(CFLAGS) -m32 -MM $< | sed -e 's|$*.o|$@|' > $(@:.o=.d) 68 | 69 | $(BUILD_64)/%.o: %.c 70 | $(CC) $(CFLAGS) -m64 -c -o $@ $< 71 | @$(CC) $(CFLAGS) -m64 -MM $< | sed -e 's|$*.o|$@|' > $(@:.o=.d) 72 | 73 | $(TARGETDIR): 74 | $(MD) $(TARGETDIR) 75 | 76 | $(TARGETDIR)/64: 77 | $(MD) $(TARGETDIR)/64 78 | 79 | $(INSTALL_32): 80 | $(MD) $(INSTALL_32) 81 | 82 | $(INSTALL_64): 83 | $(MD) $(INSTALL_64) 84 | 85 | clean: 86 | $(RM) *~ *.bak $(OBJS_32) $(OBJS_32:.o=.d) $(OBJS_64) $(OBJS_64:.o=.d) $(TARGET_32) $(TARGET_64) 87 | 88 | # pull in dependency info 89 | -include $(OBJS_32:.o=.d) $(OBJS_64:.o=.d) 90 | -------------------------------------------------------------------------------- /src/drawdebug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * GroundTraffic 3 | * 4 | * (c) Jonathan Harris 2014 5 | * 6 | * Licensed under GNU LGPL v2.1. 7 | */ 8 | 9 | #include "groundtraffic.h" 10 | 11 | #define CIRCLEDIV 72 12 | 13 | 14 | /* Draw route paths in 3d drawing phase */ 15 | void drawdebug3d(int drawnodes, GLint view[4]) 16 | { 17 | GLdouble model[16], proj[16]; 18 | int i; 19 | route_t *route; 20 | 21 | /* This is slow! */ 22 | glGetDoublev(GL_MODELVIEW_MATRIX, model); 23 | glGetDoublev(GL_PROJECTION_MATRIX, proj); 24 | 25 | for(route=airport.routes; route; route=route->next) 26 | if (!route->parent) 27 | { 28 | GLdouble winX, winY, winZ; 29 | 30 | gluProject(route->drawinfo->x, route->drawinfo->y, route->drawinfo->z, model, proj, view, &winX, &winY, &winZ); 31 | if (winZ<=1 && winX>=0 && winX<(view[0]+view[2]) && winY>=0 && winY<(view[1]+view[3])) /* on screen and not behind us */ 32 | { 33 | route->drawX = winX; 34 | route->drawY = winY; 35 | } 36 | else 37 | route->drawX = route->drawY = 0; 38 | 39 | glColor3fv(&route->drawcolor.r); 40 | glBegin((route->highway || route->path[route->pathlen-1].flags.reverse) ? GL_LINE_STRIP : GL_LINE_LOOP); 41 | for (i=0; ipathlen; i++) 42 | { 43 | path_t *node = route->path + i; 44 | 45 | glVertex3fv(&node->p.x); 46 | 47 | if (drawnodes) 48 | { 49 | gluProject(node->p.x, node->p.y, node->p.z, model, proj, view, &winX, &winY, &winZ); 50 | if (winZ<=1 && winX>=0 && winX<(view[0]+view[2]) && winY>=0 && winY<(view[1]+view[3])) /* on screen and not behind us */ 51 | { 52 | node->drawX = winX; 53 | node->drawY = winY; 54 | } 55 | else 56 | node->drawX = node->drawY = 0; 57 | } 58 | else 59 | node->drawX = node->drawY = 0; 60 | } 61 | glEnd(); 62 | } 63 | } 64 | 65 | 66 | /* Draw route labels in 2d drawing phase - uses screen co-ordinates calculated in 3d phase */ 67 | void drawdebug2d() 68 | { 69 | float waycolor[] = { 1, 1, 1 }, routecolor[] = { 0.5f, 1, 1 }; 70 | int i; 71 | route_t *route; 72 | 73 | for (route=airport.routes; route; route=route->next) 74 | if (!route->parent) 75 | for (i=0; ipathlen; i++) 76 | { 77 | path_t *node = route->path + i; 78 | if (node->drawX && node->drawY) 79 | { 80 | // XPLMDrawTranslucentDarkBox(node->drawX-font_width, node->drawY+font_semiheight-2, node->drawX+(strlen(labeltbl+5*i)-1)*font_width+1, node->drawY-font_semiheight-2); 81 | XPLMDrawString(waycolor, node->drawX-font_width, node->drawY-font_semiheight, labeltbl+5*i, NULL, xplmFont_Basic); 82 | } 83 | } 84 | 85 | for (route=airport.routes; route; route=route->next) 86 | if (!route->parent) 87 | if (route->drawX && route->drawY) 88 | { 89 | char buf[32]; 90 | int off; 91 | 92 | sprintf(buf, "%d", route->lineno); 93 | off = strlen(buf); 94 | 95 | /* Test state flags in same order as drawcallback */ 96 | if (route->state.waiting) 97 | sprintf(buf+off, " %d\xE2\x96\xAA" "At", route->last_node); /* BLACK SMALL SQUARE */ 98 | else if (route->state.dataref) 99 | sprintf(buf+off, " %d\xE2\x96\xAA" "When", route->last_node); 100 | else if (route->state.paused) 101 | sprintf(buf+off, " %d\xE2\x96\xAA" "Pause", route->last_node); 102 | else if (route->state.collision == (collision_t*) -1) 103 | sprintf(buf+off, " %d\xE2\xA8\xAF" "aircraft", route->last_node); /* VECTOR OR CROSS PRODUCT */ 104 | else if (route->state.collision) 105 | sprintf(buf+off, " %d\xE2\xA8\xAF" "%d", route->last_node, route->state.collision->route->lineno); 106 | else 107 | sprintf(buf+off, " %d\xE2\x9E\xA1" "%d", route->last_node, route->next_node); /* BLACK RIGHTWARDS ARROW */ 108 | XPLMDrawTranslucentDarkBox(route->drawX-off*font_width, route->drawY+3*font_semiheight, route->drawX+(strlen(buf)-2-off)*font_width+1, route->drawY+font_semiheight); 109 | XPLMDrawString(routecolor, route->drawX-off*font_width, route->drawY+font_semiheight+2, buf, NULL, xplmFont_Basic); 110 | XPLMDrawString(&(route->drawcolor.r), route->drawX, route->drawY+font_semiheight+3, "\xE2\x96\xAE", NULL, xplmFont_Basic); /* BLACK VERTICAL RECTANGLE */ 111 | } 112 | } 113 | 114 | 115 | int drawmap3d(XPLMDrawingPhase inPhase, int inIsBefore, void *inRefcon) 116 | { 117 | int i; 118 | 119 | if (!airport.drawroutes || airport.state == noconfig || !intilerange(airport.tower) || airport.tower.alt == (double) INVALID_ALT) 120 | return 1; 121 | 122 | XPLMSetGraphicsState(0, 0, 0, 0, 1, 0, 0); 123 | glLineWidth(1); 124 | 125 | /* draw activation range */ 126 | glColor3f(0, 0, 0); 127 | glBegin(GL_LINE_LOOP); 128 | for (i=0; ix = XPLMFindDataRef(name))) return 0; 33 | strcpy(c, "local_y"); if (!(plane_ref->y = XPLMFindDataRef(name))) return 0; 34 | strcpy(c, "local_z"); if (!(plane_ref->z = XPLMFindDataRef(name))) return 0; 35 | strcpy(c, "local_vx"); if (!(plane_ref->vx = XPLMFindDataRef(name))) return 0; 36 | strcpy(c, "local_vz"); if (!(plane_ref->vz = XPLMFindDataRef(name))) return 0; 37 | strcpy(c, "psi"); if (!(plane_ref->hdg = XPLMFindDataRef(name))) return 0; 38 | if (!(plane_ref->gear= XPLMFindDataRef("sim/aircraft/parts/acf_gear_deploy"))) return 0; 39 | 40 | /* AI aircraft */ 41 | for (i=1; ix = XPLMFindDataRef(name))) return 0; 47 | strcpy(c, "y"); if (!(plane_ref->y = XPLMFindDataRef(name))) return 0; 48 | strcpy(c, "z"); if (!(plane_ref->z = XPLMFindDataRef(name))) return 0; 49 | strcpy(c, "v_x"); if (!(plane_ref->vx = XPLMFindDataRef(name))) return 0; 50 | strcpy(c, "v_z"); if (!(plane_ref->vz = XPLMFindDataRef(name))) return 0; 51 | strcpy(c, "psi"); if (!(plane_ref->hdg = XPLMFindDataRef(name))) return 0; 52 | strcpy(c, "gear_deploy"); if (!(plane_ref->gear= XPLMFindDataRef(name))) return 0; 53 | } 54 | 55 | return -1; 56 | } 57 | 58 | void reset_planes() 59 | { 60 | plane_count = 0; 61 | } 62 | 63 | int count_planes() 64 | { 65 | int i; 66 | XPLMPluginID controller; 67 | 68 | if (plane_count) return plane_count; 69 | 70 | XPLMCountAircraft(&plane_count, &i, &controller); /* Use total, cos active may increase later */ 71 | assert (plane_count > 0 && plane_count <= MAX_PLANES); 72 | if (plane_count > MAX_PLANES) plane_count = MAX_PLANES; 73 | 74 | for (i=0; iname, path); 83 | for (o=0; oname, plane_info[o].name)) 85 | { 86 | memcpy(info, plane_info+o, sizeof(plane_acf_t)); 87 | break; 88 | } 89 | if (olength = 40; 93 | info->cgz = 18; 94 | info->semiwidth = 18; 95 | info->refheight = 3.5; 96 | 97 | if (!(h = fopen(path, "r"))) continue; 98 | o = fgetc(h); 99 | if (o=='I' || o=='A') 100 | read_v10_plane(h, o, info); 101 | else if (o=='i' || o=='a') 102 | read_old_plane(h, o, info); 103 | fclose(h); 104 | } 105 | 106 | return plane_count; 107 | } 108 | 109 | 110 | static void read_v10_plane(FILE *h, int platform, plane_acf_t *info) 111 | { 112 | char line[256], *c1, *c2; 113 | int eol1; 114 | int version; 115 | 116 | if (!fgets(line, sizeof(line), h)) return; 117 | if (!fgets(line, sizeof(line), h)) return; 118 | c1=strtok(line, sep); 119 | c2=strtok(NULL, sep); 120 | if (!c1 || !sscanf(c1, "%d%n", &version, &eol1) || c1[eol1] || 121 | strcmp(c2, "version") || 122 | version < 1004) 123 | return; /* doesn't look like an ACF file */ 124 | 125 | while (fgets(line, sizeof(line), h)) 126 | { 127 | /* Assume for speed that fields are single-space separated */ 128 | if (!strncmp(line, "P acf/_size_x ", sizeof("P acf/_size_x ")-1)) 129 | { 130 | if (!sscanf(line+sizeof("P acf/_size_x ")-1, "%f", &info->semiwidth)) return; 131 | info->semiwidth *= 0.3048f; 132 | } 133 | else if (!strncmp(line, "P acf/_size_z ", sizeof("P acf/_size_z ")-1)) 134 | { 135 | if (!sscanf(line+sizeof("P acf/_size_z ")-1, "%f", &info->length)) return; 136 | info->length *= 0.3048f; 137 | } 138 | else if (!strncmp(line, "P acf/_h_eqlbm ", sizeof("P acf/_h_eqlbm ")-1)) 139 | { 140 | if (!sscanf(line+sizeof("P acf/_h_eqlbm ")-1, "%f", &info->refheight)) return; 141 | info->refheight *= 0.3048f; 142 | } 143 | else if (!strncmp(line, "P acf/_cgZ ", sizeof("P acf/_cgZ ")-1)) 144 | { 145 | if (!sscanf(line+sizeof("P acf/_cgZ ")-1, "%f", &info->cgz)) return; 146 | info->cgz *= 0.3048f; 147 | } 148 | } 149 | } 150 | 151 | static size_t freadswap(void *ptr, size_t size, size_t nitems, FILE *stream) 152 | { 153 | unsigned char *c = ptr; 154 | int i, b; 155 | 156 | for (i=0; i=0; b--) 158 | if (!fread(c + b, 1, 1, stream)) return i; 159 | 160 | return nitems; 161 | } 162 | 163 | static void read_old_plane(FILE *h, int platform, plane_acf_t *info) 164 | { 165 | int version = 0; 166 | size_t (*readfn)(void *, size_t, size_t, FILE *) = platform=='i' ? fread : freadswap; 167 | 168 | assert(sizeof(int) == 4); /* Code below assumes this - could use int32_t */ 169 | readfn(&version, 4, 1, h); 170 | 171 | /* WB_cgZ */ 172 | if (version>=700 && version<=740) 173 | { 174 | if (fseek(h, 0x98a45, SEEK_SET)) return; 175 | } 176 | else if ((version>=800 && version<=941) || version==8000) 177 | { 178 | if (fseek(h, 0x21489, SEEK_SET)) return; 179 | } 180 | else /* unknown version */ 181 | { 182 | return; 183 | } 184 | if (!readfn(&info->cgz, 4, 1, h)) return; 185 | info->cgz *= 0.3048f; 186 | 187 | /* AUTO_size_x, AUTO_size_z */ 188 | if (version<=740) 189 | { 190 | if (fseek(h, 0x9bc2d, SEEK_SET)) return; 191 | } 192 | else 193 | { 194 | if (fseek(h, 0x21711, SEEK_SET)) return; 195 | } 196 | if (!readfn(&info->semiwidth, 4, 1, h)) return; 197 | info->semiwidth *= 0.3048f; 198 | if (!readfn(&info->length, 4, 1, h)) return; 199 | info->length *= 0.3048f; 200 | 201 | /* AUTO_h_eqlbm */ 202 | if (version<=740) 203 | { 204 | if (fseek(h, 0x9bc3d, SEEK_SET)) return; 205 | } 206 | else 207 | { 208 | if (fseek(h, 0x2171d, SEEK_SET)) return; 209 | } 210 | if (!readfn(&info->refheight, 4, 1, h)) return; 211 | info->refheight *= 0.3048f; 212 | } 213 | 214 | 215 | plane_acf_t *get_plane_info(int planeno) 216 | { 217 | return plane_info + planeno; 218 | } 219 | 220 | 221 | int get_plane_pos(plane_pos_t *pos, int planeno) 222 | { 223 | plane_ref_t *plane_ref = plane_refs + planeno; 224 | float gear; 225 | 226 | assert(planeno < plane_count); 227 | if (!XPLMGetDatavf(plane_ref->gear, &gear, 0, 1) || gear!=1) return 0; /* Not interested in airborne planes */ 228 | 229 | pos->p.x = XPLMGetDataf(plane_ref->x); 230 | pos->p.y = XPLMGetDataf(plane_ref->y); 231 | pos->p.z = XPLMGetDataf(plane_ref->z); 232 | if (!(pos->p.x || pos->p.z)) return 0; /* No position data ??? */ 233 | 234 | pos->v.x = XPLMGetDataf(plane_ref->vx); 235 | pos->v.y = 0; /* Don't care about vertical speed */ 236 | pos->v.z = XPLMGetDataf(plane_ref->vz); 237 | pos->hdg = XPLMGetDataf(plane_ref->hdg); 238 | 239 | return -1; 240 | } 241 | 242 | 243 | /* Get a plane's ground footprint. 244 | * Returns NULL if the plane is airborne. Otherwise returns pointer to a statically allocated 245 | * array of 4 points, contents of which will be overwritten on next call. */ 246 | point_t *get_plane_footprint(int planeno, float time) 247 | { 248 | static point_t p[4]; /* footprint rectangle */ 249 | 250 | plane_pos_t pos; 251 | plane_acf_t *info = plane_info + planeno; 252 | float gndy, h, cosh, sinh; 253 | point_t proj, tail, semi; 254 | 255 | if (!get_plane_pos(&pos, planeno)) return NULL; 256 | 257 | gndy = pos.p.y - info->refheight; 258 | h = D2R(pos.hdg); 259 | cosh = cosf(h); 260 | sinh = sinf(h); 261 | if (pos.v.x || pos.v.z) 262 | { 263 | /* Add space in front of plane */ 264 | proj.x = pos.p.x + sinh * 2 * info->cgz + time * pos.v.x; 265 | proj.z = pos.p.z - cosh * 2 * info->cgz + time * pos.v.z; 266 | } 267 | else 268 | { 269 | /* unless plane is *completely* static (i.e. brake on) */ 270 | proj.x = pos.p.x + sinh * info->cgz; 271 | proj.z = pos.p.z - cosh * info->cgz; 272 | } 273 | tail.x = pos.p.x - sinh * (info->length - info->cgz); 274 | tail.z = pos.p.z + cosh * (info->length - info->cgz); 275 | semi.x = cosh * info->semiwidth; 276 | semi.z = sinh * info->semiwidth; 277 | 278 | p[0].x = proj.x - semi.x; p[0].y = gndy; p[0].z = proj.z - semi.z; 279 | p[1].x = proj.x + semi.x; p[1].y = gndy; p[1].z = proj.z + semi.z; 280 | p[2].x = tail.x + semi.x; p[2].y = gndy; p[2].z = tail.z + semi.z; 281 | p[3].x = tail.x - semi.x; p[3].y = gndy; p[3].z = tail.z - semi.z; 282 | 283 | return p; 284 | } 285 | -------------------------------------------------------------------------------- /src/groundtraffic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * GroundTraffic 3 | * 4 | * (c) Jonathan Harris 2013-2014 5 | * 6 | * Licensed under GNU LGPL v2.1. 7 | */ 8 | 9 | #ifndef _GROUNDTRAFFIC_H_ 10 | #define _GROUNDTRAFFIC_H_ 11 | 12 | #ifdef _MSC_VER 13 | # define _USE_MATH_DEFINES 14 | # define _CRT_SECURE_NO_DEPRECATE 15 | # define inline __forceinline 16 | #endif 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #ifdef _MSC_VER 31 | # define PATH_MAX MAX_PATH 32 | # define snprintf _snprintf 33 | # define hypotf _hypotf 34 | # define strcasecmp(s1, s2) _stricmp(s1, s2) 35 | # define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n) 36 | #endif 37 | 38 | #if IBM /* http://msdn.microsoft.com/en-us/library/windows/desktop/ms686355%28v=vs.85%29.aspx */ 39 | # define WIN32_LEAN_AND_MEAN 40 | # include 41 | #else 42 | # include 43 | # include 44 | # include 45 | # include 46 | # if APL /* https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html */ 47 | # include 48 | # define MemoryBarrier OSMemoryBarrier 49 | # elif LIN /* http://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Atomic-Builtins.html */ 50 | # define MemoryBarrier __sync_synchronize 51 | # endif 52 | #endif 53 | 54 | #if APL 55 | # include 56 | # include 57 | #else 58 | # include 59 | # include 60 | #endif 61 | 62 | #define XPLM210 /* Requires X-Plane 10.0 or later */ 63 | #define XPLM200 64 | #include "XPLMDataAccess.h" 65 | #include "XPLMDisplay.h" 66 | #include "XPLMGraphics.h" 67 | #include "XPLMPlanes.h" 68 | #include "XPLMPlugin.h" 69 | #include "XPLMProcessing.h" 70 | #include "XPLMScenery.h" 71 | #include "XPLMUtilities.h" 72 | 73 | #include "bbox.h" 74 | 75 | /* Version of assert that suppresses "variable ... set but not used" if the variable only exists for the purpose of the asserted expression */ 76 | #ifdef NDEBUG 77 | # undef assert 78 | # define assert(expr) ((void)(expr)) 79 | #elif !IBM 80 | # include 81 | # undef assert 82 | # define assert(expr) { if (!(expr)) raise(SIGTRAP); }; 83 | #endif 84 | 85 | /* constants */ 86 | #define MAX_NAME 256 /* Arbitrary limit on object name lengths */ 87 | #define TILE_RANGE 1 /* How many tiles away from plane's tile to consider getting out of bed for */ 88 | #define ACTIVE_POLL 16 /* Poll to see if we've come into range every n frames */ 89 | #define ACTIVE_DISTANCE 5000.f /* Distance [m] from tower location at which to actually get out of bed */ 90 | #define ACTIVE_WATER 20000.f /* As above when "water" flag is set (you can see a long way on water) */ 91 | #define ACTIVE_HYSTERESIS (ACTIVE_DISTANCE*0.05f) 92 | #define MAX_RADIUS 4000.f /* Arbitrary limit on size of routes' bounding box */ 93 | #define RADIUS 6378145.f /* from sim/physics/earth_radius_m [m] */ 94 | #define DEFAULT_DRAWLOD 2.f /* Equivalent to an object 3m high */ 95 | #define DEFAULT_LOD 2.25f /* Equivalent to "medium" world detail distance */ 96 | #define DEFAULT_DRAWCARS 3.f /* Equivalent to "Chicago Suburbs" world detail distance */ 97 | #define PROBE_ALT_FIRST -100 /* Arbitrary depth below tower for probe of first waypoint */ 98 | #define PROBE_ALT_NEXT -25 /* Arbitrary depth below previous waypoint */ 99 | #define PROBE_INTERVAL 0.5f /* How often to probe ahead for altitude [s] */ 100 | #define PROBE_GRADIENT 0.25f /* Max gradient that vehicles will follow = 1:4 */ 101 | #define TURN_TIME 2.f /* Time [s] to execute a turn at a waypoint */ 102 | #define AT_INTERVAL 60.f /* How often [s] to poll for At times */ 103 | #define WHEN_INTERVAL 1.f /* How often [s] to poll for When DataRef values */ 104 | #define COLLISION_INTERVAL 2.f /* How long [s] to poll for crossing route path to become free. Also minimum spacing on overlapping segments */ 105 | #define COLLISION_TIMEOUT ((int) 60/COLLISION_INTERVAL) /* How many times to poll before giving up to break deadlock */ 106 | #define COLLISION_ALT 3.f /* Objects won't collide if their altitude differs by more than this [m] */ 107 | #define RESET_TIME 15.f /* If we're deactivated for longer than this then reset route timings */ 108 | #define MAX_VAR 10 /* How many var datarefs */ 109 | #define HIGHWAY_VARIANCE 0.25f /* How much to vary spacing of objects on a highway */ 110 | 111 | /* Options */ 112 | #undef DO_BENCHMARK 113 | #undef DO_MARKERS 114 | 115 | /* Published DataRefs */ 116 | #define REF_BASE "marginal/groundtraffic/" 117 | #define REF_VAR REF_BASE "var" 118 | #define REF_DISTANCE REF_BASE "distance" 119 | #define REF_SPEED REF_BASE "speed" 120 | #define REF_STEER REF_BASE "steer" 121 | #define REF_NODE_LAST REF_BASE "waypoint/last" 122 | #define REF_NODE_LAST_DISTANCE REF_BASE "waypoint/last/distance" 123 | #define REF_NODE_NEXT REF_BASE "waypoint/next" 124 | #define REF_NODE_NEXT_DISTANCE REF_BASE "waypoint/next/distance" 125 | #define REF_LOD REF_BASE "lod" 126 | #define REF_RANGE REF_BASE "range" 127 | #define REF_DRAWTIME REF_BASE "drawtime" 128 | 129 | typedef enum 130 | { 131 | distance=0, speed, steer, node_last, node_last_distance, node_next, node_next_distance, 132 | #ifdef DEBUG 133 | lod, range, 134 | #endif 135 | #ifdef DO_BENCHMARK 136 | drawtime, 137 | #endif 138 | dataref_count 139 | } dataref_t; 140 | 141 | /* Geolocation */ 142 | typedef struct 143 | { 144 | float lat, lon, alt; /* drawing routines use float, so no point storing higher precision */ 145 | } loc_t; 146 | 147 | #define INVALID_ALT FLT_MAX 148 | typedef struct 149 | { 150 | double lat, lon, alt; /* but XPLMWorldToLocal uses double, so prevent type conversions */ 151 | } dloc_t; 152 | 153 | /* OpenGL coordinate */ 154 | typedef struct 155 | { 156 | float x, y, z; 157 | } point_t; 158 | 159 | typedef struct 160 | { 161 | double x, y, z; 162 | } dpoint_t; 163 | 164 | /* Days in same order as tm_wday in struct tm, such that 2**tm_wday==DAY_X */ 165 | #define DAY_SUN 1 166 | #define DAY_MON 2 167 | #define DAY_TUE 4 168 | #define DAY_WED 8 169 | #define DAY_THU 16 170 | #define DAY_FRI 32 171 | #define DAY_SAT 64 172 | #define DAY_ALL (DAY_SUN|DAY_MON|DAY_TUE|DAY_WED|DAY_THU|DAY_FRI|DAY_SAT) 173 | #define MAX_ATTIMES 24 /* Number of times allowed in an At command */ 174 | #define INVALID_AT -1 175 | 176 | 177 | /* User-defined published DataRef or per-route var[n] */ 178 | typedef enum { rising, falling } slope_t; 179 | typedef enum { linear, sine } curve_t; 180 | typedef struct userref_t 181 | { 182 | char *name; /* NULL for per-route var[n] datarefs */ 183 | XPLMDataRef ref; 184 | float duration; 185 | float start1, start2; 186 | slope_t slope; 187 | curve_t curve; 188 | struct userref_t *next; /* NULL for per-route var[n] datarefs */ 189 | } userref_t; 190 | 191 | /* Set command */ 192 | typedef struct setcmd_t 193 | { 194 | userref_t *userref; 195 | float duration; 196 | struct { 197 | int set1 : 1; /* set command */ 198 | int set2 : 1; /* pause ... set command */ 199 | slope_t slope : 1; 200 | curve_t curve : 1; 201 | } flags; 202 | struct setcmd_t *next; /* Next setcmd at a waypoint */ 203 | } setcmd_t; 204 | 205 | 206 | /* DataRef referenced in When or And command */ 207 | #define xplmType_Mine -1 208 | typedef struct extref_t 209 | { 210 | char *name; 211 | XPLMDataRef ref; /* ID, or pointer if type == xplmType_Mine */ 212 | XPLMDataTypeID type; 213 | struct extref_t *next; 214 | } extref_t; 215 | 216 | 217 | /* When & And command */ 218 | typedef struct whenref_t 219 | { 220 | extref_t *extref; 221 | int idx; 222 | float from, to; 223 | struct whenref_t *next; /* Next whenref at a waypoint */ 224 | } whenref_t; 225 | 226 | 227 | /* Route path - locations or commands */ 228 | struct collision_t; 229 | typedef struct 230 | { 231 | loc_t waypoint; /* World */ 232 | point_t p; /* Local OpenGL co-ordinates */ 233 | point_t p1, p3; /* Bezier points for turn */ 234 | int pausetime; 235 | short attime[MAX_ATTIMES]; /* minutes past midnight */ 236 | unsigned char atdays; 237 | struct { 238 | int reverse : 1; /* Reverse whole route */ 239 | int backup : 1; /* Just reverse to next node */ 240 | } flags; 241 | struct collision_t *collisions; /* Collisions with other routes */ 242 | setcmd_t *setcmds; 243 | whenref_t *whenrefs; 244 | int drawX, drawY; /* For labeling nodes */ 245 | } path_t; 246 | 247 | typedef struct 248 | { 249 | GLfloat r, g, b; 250 | } glColor3f_t; 251 | 252 | typedef struct 253 | { 254 | char *name; 255 | char *physical_name; 256 | XPLMObjectRef objref; 257 | float drawlod; /* Multiply by lod_factor to get draw distance */ 258 | float lag; /* time lag. [m] in train defn, [s] in route */ 259 | float offset; /* offset applied after rotation before drawing. [m] */ 260 | float heading; /* rotation applied before drawing */ 261 | } objdef_t; 262 | 263 | /* A route from routes.txt */ 264 | struct collision_t; 265 | struct highway_t; 266 | typedef struct route_t 267 | { 268 | int lineno; /* Source line in GroundTraffic.txt */ 269 | objdef_t object; 270 | path_t *path; 271 | int pathlen; 272 | bbox_t bbox; /* Bounding box of path */ 273 | struct 274 | { 275 | int frozen : 1; /* Child whose parent is waiting */ 276 | int paused : 1; /* Waiting for pause duration */ 277 | int waiting : 1; /* Waiting for At time */ 278 | int dataref : 1; /* Waiting for DataRef value */ 279 | int forwardsb : 1; /* Waypoint before backing up */ 280 | int backingup : 1; 281 | int forwardsa : 1; /* Waypoint after backing up */ 282 | int hasdataref: 1; /* Does the object on this route have DataRef callbacks? */ 283 | struct collision_t *collision; /* Waiting for this collision to resolve */ 284 | } state; 285 | int direction; /* Traversing path 1=forwards, -1=reverse */ 286 | int last_node, next_node; /* The last and next waypoints visited on the path */ 287 | float last_time, next_time; /* Time we left last_node, expected time to hit the next node */ 288 | float freeze_time; /* For children: Time when parent started pause */ 289 | float speed; /* [m/s] */ 290 | float last_distance; /* Cumulative distance travelled from first to last_node [m] */ 291 | float next_distance; /* Distance from last_node to next_node [m] */ 292 | float distance; /* Cumulative distance travelled from first node [m] */ 293 | float next_heading; /* Heading from last_node to next_node [m] */ 294 | float steer; /* Approximate steer angle (degrees) while turning */ 295 | glColor3f_t drawcolor; /* debug path color */ 296 | int drawX, drawY; /* debug label position */ 297 | XPLMDrawInfo_t *drawinfo; /* Where to draw - current OpenGL co-ordinates */ 298 | float last_probe, next_probe; /* Time of last altitude probe and when we should probe again */ 299 | float last_y, next_y; /* OpenGL co-ordinates at last and next probe points */ 300 | int deadlocked; /* Counter used to break collision deadlock */ 301 | float highway_offset; /* For highway children: Starting offset from start of route */ 302 | struct highway_t *highway; /* Is a highway */ 303 | userref_t (*varrefs)[MAX_VAR]; /* Per-route var dataref */ 304 | struct route_t *parent; /* Points to head of a train */ 305 | struct route_t *next; 306 | } route_t; 307 | 308 | 309 | /* A train of interconnected objects */ 310 | #define MAX_TRAIN 16 311 | typedef struct train_t 312 | { 313 | char *name; 314 | objdef_t objects[MAX_TRAIN]; 315 | struct train_t *next; 316 | } train_t; 317 | 318 | 319 | /* A highway */ 320 | #define MAX_HIGHWAY 16 321 | typedef struct highway_t 322 | { 323 | objdef_t objects[MAX_HIGHWAY]; 324 | objdef_t *expanded; /* Physical objects */ 325 | int obj_count; /* Physical object count */ 326 | float spacing; 327 | struct highway_t *next; 328 | } highway_t; 329 | 330 | 331 | /* Collision between routes */ 332 | typedef struct collision_t 333 | { 334 | route_t *route; /* Other route */ 335 | int node; /* Other node (assuming forwards direction) */ 336 | struct collision_t *next; 337 | } collision_t; 338 | 339 | 340 | /* airport info from routes.txt */ 341 | typedef struct 342 | { 343 | enum { noconfig=0, inactive, activating, active } state; 344 | int case_folding; /* Whether our package is on a case-sensitive file system (i.e. Linux) */ 345 | int done_first_activation; /* Whether we've calculated collisions and expanded highways */ 346 | int new_airport; /* Whether we've moved to a new airport, so activation should be immediate */ 347 | dloc_t tower; 348 | dpoint_t p; /* Remember OpenGL location of tower to detect scenery shift */ 349 | int drawroutes; 350 | int reflections; 351 | float active_distance; 352 | route_t *routes; 353 | route_t *firstroute; 354 | train_t *trains; 355 | userref_t *userrefs; 356 | extref_t *extrefs; 357 | XPLMDrawInfo_t *drawinfo; /* consolidated XPLMDrawInfo_t array for all routes/objects so they can be batched */ 358 | } airport_t; 359 | 360 | 361 | /* Worker thread */ 362 | /* Align to cache-line - http://software.intel.com/en-us/articles/avoiding-and-identifying-false-sharing-among-threads */ 363 | #if IBM 364 | typedef __declspec(align(64)) struct 365 | #else 366 | typedef struct __attribute__((aligned(64))) 367 | #endif 368 | { 369 | #if IBM 370 | HANDLE thread; 371 | #else 372 | pthread_t thread; 373 | #endif 374 | int die_please; 375 | int finished; 376 | } worker_t; 377 | 378 | 379 | /* prototypes */ 380 | int activate(airport_t *airport); 381 | void deactivate(airport_t *airport); 382 | void proberoutes(airport_t *airport); 383 | void maproutes(airport_t *airport); 384 | float userrefcallback(XPLMDataRef inRefcon); 385 | 386 | int xplog(char *msg); 387 | int readconfig(char *pkgpath, airport_t *airport); 388 | void clearconfig(airport_t *airport); 389 | 390 | void labelcallback(XPLMWindowID inWindowID, void *inRefcon); 391 | int drawcallback(XPLMDrawingPhase inPhase, int inIsBefore, void *inRefcon); 392 | 393 | void drawdebug3d(int drawnodes, GLint view[4]); 394 | void drawdebug2d(); 395 | int drawmap3d(XPLMDrawingPhase inPhase, int inIsBefore, void *inRefcon); 396 | int drawmap2d(XPLMDrawingPhase inPhase, int inIsBefore, void *inRefcon); 397 | 398 | 399 | /* Globals */ 400 | extern char *pkgpath; 401 | extern XPLMDataRef ref_plane_lat, ref_plane_lon, ref_view_x, ref_view_y, ref_view_z, ref_rentype, ref_night, ref_monotonic, ref_doy, ref_tod, ref_LOD; 402 | extern XPLMDataRef ref_datarefs[dataref_count], ref_varref; 403 | extern XPLMProbeRef ref_probe; 404 | extern float lod_bias; 405 | extern airport_t airport; 406 | extern route_t *drawroute; /* Global so can be accessed in dataref callback */ 407 | extern int year; /* Current year (in GMT tz) */ 408 | #ifdef DO_BENCHMARK 409 | extern int drawcumul; 410 | extern int drawframes; 411 | #endif 412 | 413 | extern float last_frame; /* Global so can be reset while disabled */ 414 | extern float lod_factor; 415 | extern char *labeltbl; 416 | extern int font_width, font_semiheight; 417 | 418 | 419 | /* inlines */ 420 | 421 | /* naive UTF8-aware strlen */ 422 | static inline int utf8_strlen(const char *s) 423 | { 424 | int len = 0; 425 | while (*s) 426 | if (((*s++) & 0xC0) != 0x80) len++; /* Not a continuation byte */ 427 | return len; 428 | } 429 | 430 | 431 | static inline int intilerange(dloc_t loc) 432 | { 433 | double tile_lat, tile_lon; 434 | tile_lat = floor(XPLMGetDatad(ref_plane_lat)); 435 | tile_lon = floor(XPLMGetDatad(ref_plane_lon)); 436 | return ((fabs(tile_lat - floor(loc.lat)) <= TILE_RANGE) && (fabs(tile_lon - floor(loc.lon)) <= TILE_RANGE)); 437 | } 438 | 439 | 440 | static inline int indrawrange(float xdist, float ydist, float zdist, float range) 441 | { 442 | assert (airport.tower.alt != (double) INVALID_ALT); /* If altitude is invalid then arguments to this function will be too */ 443 | return (xdist*xdist + ydist*ydist + zdist*zdist <= range*range); 444 | } 445 | 446 | static inline float R2D(float r) 447 | { 448 | return r * ((float) (180*M_1_PI)); 449 | } 450 | 451 | static inline float D2R(float d) 452 | { 453 | return d * ((float) (M_PI/180)); 454 | } 455 | 456 | 457 | /* Operations on point_t */ 458 | 459 | static inline float angleto(point_t *from, point_t *to) 460 | { 461 | return atan2f(to->x-from->x, to->z-from->z); 462 | } 463 | 464 | /* 2D is point inside polygon? */ 465 | static inline int inside(point_t *p, point_t *poly, int npoints) 466 | { 467 | /* http://paulbourke.net/geometry/polygonmesh/ "Determining if a point lies on the interior of a polygon" */ 468 | int i, j, c=0; 469 | for (i=0, j=npoints-1; iz) && (p->z < poly[j].z)) || ((poly[j].z <= p->z) && (p->z < poly[i].z))) && 471 | (p->x < (poly[j].x - poly[i].x) * (p->z - poly[i].z) / (poly[j].z - poly[i].z) + poly[i].x)) 472 | c = !c; 473 | return c; 474 | } 475 | 476 | /* 2D does line p0->p1 intersect p2->p3 */ 477 | static inline int intersect(point_t *p0, point_t *p1, point_t *p2, point_t *p3) 478 | { 479 | /* http://stackoverflow.com/a/1968345 */ 480 | float s, t, d, s1_x, s1_z, s2_x, s2_z; 481 | 482 | s1_x = p1->x - p0->x; s1_z = p1->z - p0->z; 483 | s2_x = p3->x - p2->x; s2_z = p3->z - p2->z; 484 | d = -s2_x * s1_z + s1_x * s2_z; 485 | if (d==0) return 0; /* Precisely parallel or collinear - ignore in either case */ 486 | 487 | s = (-s1_z * (p0->x - p2->x) + s1_x * (p0->z - p2->z)) / d; 488 | t = ( s2_x * (p0->z - p2->z) - s2_z * (p0->x - p2->x)) / d; 489 | 490 | /* use strict comparison because only interested in significant intersections */ 491 | return s > 0 && s < 1 && t > 0 && t < 1; 492 | } 493 | 494 | 495 | /* Operations on loc_t */ 496 | 497 | /* 2D does line p0->p1 intersect p2->p3 */ 498 | static inline int loc_intersect(loc_t *p0, loc_t *p1, loc_t *p2, loc_t *p3) 499 | { 500 | /* http://stackoverflow.com/a/1968345 */ 501 | float s, t, d, s1_x, s1_y, s2_x, s2_y; 502 | 503 | s1_x = p1->lon - p0->lon; s1_y = p1->lat - p0->lat; 504 | s2_x = p3->lon - p2->lon; s2_y = p3->lat - p2->lat; 505 | d = (-s2_x * s1_y + s1_x * s2_y); 506 | if (d==0) return 0; /* Precisely parallel or collinear - ignore in either case */ 507 | 508 | s = (-s1_y * (p0->lon - p2->lon) + s1_x * (p0->lat - p2->lat)) / d; 509 | t = ( s2_x * (p0->lat - p2->lat) - s2_y * (p0->lon - p2->lon)) / d; 510 | 511 | /* use strict comparison because path segments don't count as colliding if they just share a starting node */ 512 | return s > 0 && s < 1 && t > 0 && t < 1; 513 | } 514 | 515 | 516 | /* quick and dirty and not very accurate gettimeofday implementation ignoring timezone */ 517 | #if defined(_MSC_VER) && defined(DO_BENCHMARK) 518 | # include /* for timeval */ 519 | static inline int gettimeofday(struct timeval *tp, void *tzp) 520 | { 521 | LARGE_INTEGER frequency; // ticks per second 522 | LARGE_INTEGER counter; 523 | 524 | QueryPerformanceFrequency(&frequency); 525 | QueryPerformanceCounter(&counter); 526 | counter.QuadPart = (counter.QuadPart * 1000000) / frequency.QuadPart; /* In microseconds */ 527 | tp->tv_sec = counter.QuadPart / 1000000; 528 | tp->tv_usec = counter.QuadPart - tp->tv_sec * 1000000; 529 | return 0; 530 | } 531 | #endif 532 | 533 | 534 | /* Operations on worker_t */ 535 | 536 | static inline int worker_start(worker_t *worker, void *(*start_routine)(void *)) 537 | { 538 | worker->die_please = worker->finished = 0; 539 | MemoryBarrier(); 540 | #if IBM 541 | if (!(worker->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) start_routine, NULL, 0, NULL))) 542 | #else 543 | if (pthread_create(&worker->thread, NULL, start_routine, NULL)) 544 | #endif 545 | { 546 | return xplog("Internal error: Can't create worker thread"); 547 | } 548 | return -1; 549 | } 550 | 551 | /* Wait for worker to stop */ 552 | static inline void worker_wait(worker_t *worker) 553 | { 554 | if (worker->thread) 555 | { 556 | #if IBM 557 | WaitForSingleObject(worker->thread, INFINITE); 558 | CloseHandle(worker->thread); 559 | #else 560 | pthread_join(worker->thread, NULL); 561 | #endif 562 | worker->thread = 0; 563 | } 564 | } 565 | 566 | /* Signal worker and wait for it to stop */ 567 | static inline void worker_stop(worker_t *worker) 568 | { 569 | if (worker->thread) 570 | { 571 | worker->die_please = -1; 572 | MemoryBarrier(); 573 | worker_wait(worker); 574 | } 575 | } 576 | 577 | /* Check whether worker is finished */ 578 | static inline int worker_is_finished(worker_t *worker) 579 | { 580 | if (worker->thread) 581 | { 582 | MemoryBarrier(); 583 | if (worker->finished) 584 | { 585 | worker_wait(worker); 586 | return -1; 587 | } 588 | else 589 | return 0; 590 | } 591 | else 592 | return -1; 593 | } 594 | 595 | /* Called from worker thread to check for early termination */ 596 | #define worker_check_stop(worker) { MemoryBarrier(); if ((*(worker)).die_please) return NULL; } 597 | 598 | /* Called from worker thread to indicate completion */ 599 | #define worker_has_finished(worker) { MemoryBarrier(); (*(worker)).finished = -1; } 600 | 601 | 602 | #endif /* _GROUNDTRAFFIC_H_ */ 603 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | (This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.) 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | {description} 474 | Copyright (C) {year} {fullname} 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 489 | USA 490 | 491 | Also add information on how to contact you by electronic and paper mail. 492 | 493 | You should also get your employer (if you work as a programmer) or your 494 | school, if any, to sign a "copyright disclaimer" for the library, if 495 | necessary. Here is a sample; alter the names: 496 | 497 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 498 | library `Frob' (a library for tweaking knobs) written by James Random 499 | Hacker. 500 | 501 | {signature of Ty Coon}, 1 April 1990 502 | Ty Coon, President of Vice 503 | 504 | That's all there is to it! 505 | -------------------------------------------------------------------------------- /lgpl-2.1.txt: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | -------------------------------------------------------------------------------- /ReadMe.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | GroundTraffic 10 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 |
37 | 38 |

GroundTraffic kit for X-Plane®

39 | 40 |

Overview

41 | 42 |

This kit enables X-Plane scenery designers to add animated ground vehicle traffic to airport scenery packages. It requires X-Plane 10.0 or later.

43 | 44 |

How it works

45 |

This kit supplies a plugin that you can distribute with your scenery package. The plugin reads instructions from a text file that you create (and which you should also distribute with your scenery package) and animates X-Plane scenery objects according to those instructions. The objects to be animated can come from X-Plane's built-in library, from an add-on object library (e.g. OpenSceneryX), or from your scenery package.

46 | 47 |

Installation

48 | 49 |
    50 |
  • Create a folder named plugins within your scenery package folder.
  • 51 |
  • Copy the GroundTraffic folder from this kit into the plugins folder.
  • 52 |
  • Don't copy this ReadMe.html file into your scenery package - this file is intended for you as a scenery designer and would likely confuse users of your scenery package.
  • 53 |
54 | 55 |

Animation instructions

56 | 57 |

Use Notepad, TextEdit, or any other text editor to create a plain text file named GroundTraffic.txt in your scenery package folder. Save this blank file with an “ANSI”, “Western” or “UTF-8” encoding. (If using TextEdit, you may have to first choose Format → Make Plain Text to see those choices).

58 |

Add one or more “Routes” and/or “Highways” to this file and, optionally, a “Water” statement, “Debug” statement, and/or comments.

59 | 60 |

Water

61 |

The Water statement tells the plugin that some or all of your routes are on water. In practice this causes the plugin to activate sooner as the user approaches the airport (since you can see things at sea from a large distance) and, when “water reflection detail” is set to “medium” or above under X-Plane's Rendering Options, to draw objects with reflections. There is a performance cost to both of these activities, so don't use this statement unless you need to.

62 |

The Water statement just consists of the word “water” and should be preceded by a blank line:

63 |
 64 | 
 65 | water
 66 | 
67 | 68 |

Debug

69 |

The Debug statement tells the plugin to display and label routes; each waypoint is labelled with its sequence number, and each animated object is labelled with its line number from GroundTraffic.txt, its last waypoint and, if waiting, the reason why it is waiting. This is useful for checking that your routes' waypoints are at the locations that you intended and for investigating traffic jams.

70 |

The Debug statement just consists of the word “debug” and should be preceded by a blank line:

71 |
 72 | 
 73 | debug
 74 | 
75 | 76 |

Comment

77 |

Lines starting with “#” are treated as comments and are ignored. For example:

78 |
# This is a comment
79 | 80 |

Route

81 |

A Route statement starts a new animation. It should be of the form “route speed offset heading object”, where:

82 |
83 |
speed
84 |
The speed of the animated object, in km/h.
85 |
offset
86 |
Offset in metres to shift the object forwards before animation.
87 |
heading
88 |
Rotation in degrees to be applied to the object before animation.
89 |
object
90 |
The name of the animated object. This can be the name of a library object from X-Plane's built-in library or from an add-on library, the name of an object in your scenery package, or the name of a “Train”. Note that library object and train names are case-sensitive.
91 |
92 |

For the animation to look correct objects should be modelled such that they are pointing North and are roughly centered at their origin (since the plugin rotates them about their origin).

93 |
    94 |
  • If using your own objects, design them so they are pointing North (i.e. along the positive green axis in SketchUp or Blender), sitting at or just slightly below ground level to allow for sloped terrain, and centered at or a little forward of their axes' origin. Use 0 for the “offset” and “heading” values in this statement.
  • 95 |
  • Objects in X-Plane's built-in library tend to be centered at their origin but are not all pointing North; supply a “heading” value to rotate such objects.
  • 96 |
  • Objects in the OpenSceneryX library all point North but are not centered at their origin; supply an “offset” value of at least half the length of the object to correct for this.
  • 97 |
  • For trains of objects, supply desired “offset” and “heading” values in the train Car statement; the values supplied here are not used.
  • 98 |
99 |

For example, a route using an object from the scenery package, travelling at 30km/h (~18mph):

100 |
101 | 
102 | route	30	0	0	objects/airport_fuel_truck.obj
103 | ...
104 |

A route using an object from X-Plane 10's built-in library, travelling at 24km/h (~15mph), to be rotated 180°:

105 |
106 | 
107 | route	24	0	180	lib/airport/Ramp_Equipment/Luggage_Truck.obj
108 | ...
109 |

A route using an object from the OpenSceneryX library, travelling at 20km/h (~12mph), shifted forwards 2.5m so that it rotates about its center:

110 |
111 | 
112 | route	20	2.5	0	opensceneryx/objects/airport/vehicles/loaders/6.obj
113 | ...
114 |

The Route statement should be preceded by a blank line and should be followed by a sequence of “waypoints” and “commands”. The Route ends with a blank line.

115 | 116 |

Waypoint

117 |

Animated objects move from one waypoint to the next at the speed specified in the Route statement. When the object passes the last waypoint it proceeds towards the first waypoint, forming a circular route (unless you specify a “reverse” command). Objects will wait at a waypoint if moving on would risk colliding with another object or getting in the way of an aircraft; refer to the guidance below if you plan to have routes that overlap with each other or with aircraft taxi paths.

118 |

Waypoints should be of the form “lat lon”. For example:

119 |
...
120 | 47.4484450 -122.3001950
121 | ...
122 |

You can use OverlayEditor or WED to map out your route, typing the the waypoint locations into GroundTraffic.txt as you go. You may also want to consider placing a “line” (e.g. one of opensceneryx/lines/airport/zone_outlines/...) in OverlayEditor or WED to record your route; this will make it easier if you want to adjust your route in the future. But remember to remove or hide these lines before publishing your scenery package.

123 |

Alternatively, you can use online mapping tools such as this to map out your route.

124 |

You can use the Debug statement to visualise your routes in X-Plane.

125 | 126 |

Pause command

127 |

The Pause command instructs the animated object to wait at the preceding waypoint for a while before moving on. Optionally, you can cause the value of a DataRef to change while the animated object is waiting, and change back before it proceeds.

128 |

The Pause command should be of the form “pause time set name slope curve duration”, where:

129 |
130 |
time
131 |
Time to wait, in seconds.
132 |
set name slope curve duration (optional)
133 |
As for the “Set” command.
134 |
135 |

For example, waiting at a waypoint for 30 minutes:

136 |
...
137 | 47.4629025 -122.3020905
138 | pause 1800
139 | ...
140 |

Waiting at a waypoint for one minute while setting the value of the DataRef marginal/groundtraffic/var[0] such that it rises from 0 to 1 over five seconds, stays at 1, then starts going back to 0 five seconds before the object proceeds:

141 |
...
142 | 47.4380074 -122.3040742
143 | pause 60 set var[0] rise linear 5
144 | ...
145 | 
146 | 147 |

At command

148 |

The At command instructs the animated object to wait at the preceeding waypoint until a particular time of day. It should be of the form “at HH:MM on days”, where:

149 |
150 |
HH:MM
151 |
Times of day at which to proceed with the animation. Up to 24 times can be specified, in local time, and using a 24 hour clock.
152 |
on days (optional)
153 |
Days on which to proceed with the animation. Specified in English.
154 |
155 |

For example, an animation that waits until 8am:

156 |
...
157 | 47.4559849 -122.3028592
158 | at 08:00
159 | ...
160 |

An animation that waits until 8am or 8pm on a weekday:

161 |
...
162 | 47.4559849 -122.3028592
163 | at 08:00 20:00 on Mon Tue Wed Thu Fri
164 | ...
165 | 166 |

Backup command

167 |

The Backup command instructs the animated object to reverse from the preceding waypoint as far as the next waypoint, and thereafter move forward as normal. If the preceding waypoint also has a Pause command the object points forwards while waiting, and then backs-up after waiting.

168 |

The Backup command just consists of the word “backup”.

169 |

For example backing-up into a parking spot:

170 |
...
171 | 47.4348468 -122.3034628
172 | backup
173 | 47.4349236 -122.3034600
174 | pause 60
175 | 47.4348468 -122.3034628
176 | ...
177 | 
178 |

Backing-up after a pause:

179 |
...
180 | 47.4379349 -122.3040074
181 | 47.4380074 -122.3040742
182 | pause 60
183 | backup
184 | 47.4379349 -122.3040074
185 | ...
186 | 
187 |

For best results the waypoints before and after backing-up should be at the same location, as in the above examples, or at least in a straight line. 188 | 189 |

Reverse command

190 |

Normally, when the animated object passes the last waypoint it proceeds towards the first waypoint, forming a circular route. The Reverse command instead instructs the object to change direction at the last waypoint and re-trace its steps.

191 |

The Reverse command just consists of the word “reverse” and must be at the end of the Route. For example:

192 |
...
193 | reverse
194 | 
195 | 
196 |

Note: You can't use the Reverse command in a route that contains Backup commands.

197 | 198 |

Set command

199 |

The Set command causes the value of a DataRef to change when the animated object arrives at the preceding waypoint. It should be of the form “set name slope curve duration”, where:

200 |
201 |
name
202 |
var[0] ... var[9] or the name of a custom DataRef.
203 |
slope
204 |
rise or fall - whether the value the DataRef rises from 0.0 to 1.0, or falls from 1.0 to 0.0.
205 |
curve
206 |
linear or sine - whether the value the DataRef changes linearly or like  ∫ over the duration.
207 |
duration
208 |
The time in seconds that the DataRef takes to change value. This can be 0 if you want the DataRef to change value instantly.
209 |
210 |

If you specify a name of the form var[n] then this command controls the “array” DataRef marginal/groundtraffic/var[n], which can be used to drive animations in a 3D modelling application that supports X-Plane animations. Each object will see values for this DataRef that are specific to the route that the object is following; so it is safe to re-use this DataRef across different objects and routes. For example, within one GroundTraffic.txt file, you could use marginal/groundtraffic/var[0] to control an animated belt in one or more beltloader objects, and also use marginal/groundtraffic/var[0] to control a refueling animation in one or more fuel truck objects. However, because the values returned for this DataRef are specific to a route, you can't usefully use it in a static object that is placed conventionally within an X-Plane scenery package.

211 |

Alternatively you can specify the name of a custom DataRef of your choice, but which should be unique to this scenery package. This DataRef can be used to animate objects that are used in a route and/or static objects that are placed conventionally within an X-Plane scenery package. Because all objects (either static or animated by this plugin) will see the same value for your custom DataRef it doesn't make sense to set the same custom DataRef in more than one route.

212 |

For example, setting the value of the DataRef marginal/groundtraffic/var[1] such that it rises from 0 to 1 over five seconds on hitting a waypoint:

213 |
...
214 | 47.4380074 -122.3040742
215 | set var[1] rise linear 5
216 | ...
217 | 
218 |

Setting the value of a custom DataRef such that it falls from 1 to 0 over two seconds on hitting a waypoint:

219 |
...
220 | 47.4348578 -122.3034624
221 | set my/custom/dataref fall sine 2
222 | ...
223 | 
224 |

The “Pause ... set” command also supports changing the value of a custom DataRef when the animated object arrives at a waypoint. The “Pause ... set” command restores the DataRef to its previous value before the object moves on from the waypoint; this command does not.

225 | 226 |

When command

227 |

The When command instructs the animated object to wait at the preceeding waypoint until a DataRef is within a specified range. It should be of the form “when name from to”, where:

228 |
229 |
name
230 |
The name of a DataRef published by the sim, the name of a custom DataRef published by the “Set” or “Pause ... set” commands in this plugin, or the name of a custom DataRef published by another plugin. To access an array DataRef, append the array index within square brackets “[n]” to the name.
231 |
from to
232 |
Range of values for the DataRef for which the animation proceeds.
233 |
234 |

For example, an animation that only takes place in daylight:

235 |
...
236 | 47.4559849 -122.3028592
237 | when sim/graphics/scenery/percent_lights_on 0 0.2
238 | ...
239 |

The When command can be used in conjunction with the Set command to synchronise the motions of objects on different routes. For example, to trigger an object on route B to move when the object on route A reaches a waypoint:

240 |
# route A
241 | ...
242 | # when we get to this waypoint signal the object on route B
243 | 47.4484450 -122.3001950
244 | set my/scenerypackage/signalB rise linear 0
245 | ...
246 | # reset the signal for next time around
247 | 47.4471804 -122.3023423
248 | set my/scenerypackage/signalB fall linear 0
249 | ...
250 | 
251 | # route B
252 | ...
253 | # wait here until object on route A signals to us
254 | 47.4559849 -122.3028592
255 | when my/scenerypackage/signalB 1 1
256 | ...
257 | 
258 | 
259 | 260 |

And command

261 |

The And command can be used to add additional conditions to a When command. It should be of the form “and name from to”, where:

262 |
263 |
name from to
264 |
As for the “When” command.
265 |
266 |

For example, an animation that only takes place at night and in the rain:

267 |
...
268 | 47.4559849 -122.3028592
269 | when sim/graphics/scenery/percent_lights_on 0.5 1.0
270 | and  sim/weather/rain_percent 0.1 1.0
271 | ...
272 |

You can have as many And commands at a waypoint as you like.

273 | 274 |

Train

275 |

Instead of animating a single object along a Route, you can animate a number of objects to follow each other in succession like train cars/coaches follow their locomotive.

276 |

A Train statement starts a description of a train of connected objects. It should be of the form “train name”, where:

277 |
278 |
name
279 |
The name of your train. Use this name in a Route statement to animate this train. To avoid any ambiguity don't name your train with the same name as a library object, an object in your scenery package, or another train.
280 |
281 |

For example a suitable name for a train consisting of a tug and four luggage carts:

282 |
283 | 
284 | train	my/scenerypackage/luggage_train_4
285 | ...
286 |

The Train statement should be preceded by a blank line and should be followed by a list of the “Car” statements. The Train description ends with a blank line.

287 | 288 |

Train Car

289 |

A train Car statement should be of the form “lag offset heading object”, where:

290 |
291 |
lag
292 |
Distance in metres that the object should lag behind the lead object in the train.
293 |
offset
294 |
Offset in metres to be applied to the object before animation.
295 |
heading
296 |
Rotation in degrees to be applied to the object before animation.
297 |
object
298 |
The name of the animated object. This can be the name of a library object from X-Plane's built-in library or from an add-on library, or the name of an object in your scenery package. It should not be the name of this or of another Train.
299 |
300 |

To determine how far an object should lag behind the the lead object you can open/import the objects into a 3D modelling application and measure how far (in metres) you have to move the object behind the lead object for it to appear to “link up” correctly. If either object needs an offset to be centered on its axes then adjust for this before measuring. Repeat the process for further objects.

301 |

For example a train consisting of two objects, both of which need to be rotated 180°, the second of which is 3.36m behind the first:

302 |
303 | 
304 | train	my/scenerypackage/luggage_train_2
305 | 0	0	180	lib/airport/Ramp_Equipment/Luggage_Truck.obj
306 | 3.36	0	180	lib/airport/Ramp_Equipment/Luggage_Cart.obj
307 | 
308 | 
309 |

The plugin animates train Car objects as if they are connected to each other so don't leave huge gaps between Cars. In particular, if you want to send two or more separate objects down the same route don't (ab)use the Train statement; just duplicate the route (and optionally change the order of the waypoints in the copy/copies to space out the objects) or use a Highway.

310 | 311 |

Highway

312 |

Instead of animating a single object or train of objects, you can create a “highway” filled with a constant flow of one-way traffic. Highways are similar to Routes, except that a Highway description cannot contain any commands and traffic on a Highway does not stop to avoid collisions with other Highways or Routes.

313 |

A Highway statement starts a description of a traffic flow. It should be of the form “highway speed spacing”, where:

314 |
315 |
speed
316 |
The speed of the animated objects, in km/h.
317 |
spacing
318 |
Average distance in metres between objects when “number of cars” is at the maximum setting under X-Plane's Rendering Options. Lower settings for “number of cars” will result in a larger distance between objects.
319 | 320 |
321 |

For example a highway with traffic moving at 65km/h (~40mph) and objects every 50 metres:

322 |
323 | 
324 | highway	65	50
325 | ...
326 |

The Highway statement should be preceded by a blank line and should be followed by a list of highway “Car” statements, followed by a sequence of “waypoints”. The Highway description ends with a blank line.

327 | 328 |

Highway Car

329 |

A highway Car statement should be of the form “offset heading object”, where:

330 |
331 |
offset
332 |
Offset in metres to be applied to the object before animation.
333 |
heading
334 |
Rotation in degrees to be applied to the object before animation.
335 |
object
336 |
The name of the animated object. This can be the name of a library object from X-Plane's built-in library or from an add-on library, or the name of an object in your scenery package. It should not be the name of a Train. Note that library object names are case-sensitive.
337 |
338 |

For example a mix of traffic consisting of some cars, some more cars plus some trucks, and some Ford Transit vans shifted forwards 2.5m so that they rotate about their center:

339 |
340 | 
341 | highway	65	20
342 | 0	0	lib/cars/car.obj
343 | 0	0	lib/cars/car_or_truck.obj
344 | 2.5	0	opensceneryx/objects/vehicles/commercial/vans.obj
345 | ...
346 | 
347 |

The plugin fills your highway with a random mix of the objects listed in the Car statements, so the order of the Car statements is not significant.

348 | 349 |

Highway Waypoint

350 |

Animated objects move from one waypoint to the next at the speed specified in the Highway statement. When an object passes the last waypoint it disappears.

351 |

Highway Waypoints should be of the form “lat lon”. For example:

352 |
...
353 | 47.4484450 -122.3001950
354 | ...
355 | 356 |

Animation DataRefs

357 | 358 |

The plugin publishes the following DataRefs that you can use to create animated objects in a 3D modelling application that supports X-Plane animations:

359 |
360 |
marginal/groundtraffic/distance
361 |
Distance travelled from the first waypoint, in metres. Resets to zero every time the object passes the first waypoint. See below for how to use this to animate rotating wheels.
362 |
marginal/groundtraffic/speed
363 |
Speed of the object, in metres/second. This returns zero if the object is stationary for any reason, up to the speed that you specified in the Route statement (converted to [m/s]) if it is moving, or negative if it is backing-up (which is useful for displaying reversing lights).
364 |
marginal/groundtraffic/steer
365 |
Turn rate of the object, in degrees. This returns zero if the object is moving in a straight line, and between zero and half the turn angle if the object is negotiating a turn; e.g. if the object is negotiating a right-angled turn, this will return up to ±45. This is useful for animating articulated vehicles / semi-trailer trucks.
366 |
marginal/groundtraffic/var[0] ... var[9]
367 |
DataRef whose value is controlled by the “Set” and “Pause ... set” commands.
368 |
marginal/groundtraffic/waypoint/last
369 |
The last waypoint in the Route (counting from zero) visited by the object.
370 |
marginal/groundtraffic/waypoint/last/distance
371 |
The distance from the last waypoint, in metres. This returns zero if the object is stationary at a waypoint.
372 |
marginal/groundtraffic/waypoint/next
373 |
The next waypoint in the Route (counting from zero) to be visited by the object. This is one greater than the last waypoint unless the object is reversing its route or is circling back to the first waypoint.
374 |
marginal/groundtraffic/waypoint/next/distance
375 |
The distance to the next waypoint, in metres.
376 |
377 |

These DataRefs return values specific to the route that the object is following; so they are only useful in animating objects on routes controlled by this plugin, not in static objects that are placed conventionally within an X-Plane scenery package. (You can use the “Set” and/or “Pause ... set” commands with a custom DataRef name to publish DataRefs for use in static objects). You can examine the values of these DataRefs using DataRefEditor or Data Ref Tool, but they will only display the values for the first route listed in GroundTraffic.txt.

378 |

The objects in a Train or Highway will receive different values for some of these DataRefs (e.g. the value returned by marginal/groundtraffic/distance takes into account the “lag” specified in the object's Train Car statement). But all objects in the train share the same marginal/groundtraffic/var[n] values.

379 | 380 |

Animating rotating wheels

381 |

The speed of rotation of a wheel depends on its size; a smaller wheel needs to rotate more quickly to cover a given distance than a larger wheel. So in a 3D modelling application measure the diameter of the wheel (in metres) and multiple this value by π to obtain the wheel's circumference.

382 |

Set up an animation for the wheel containing two keyframes. Rotate the wheel 90° forwards around its axle between the two keyframes. Specify the X-Plane animation values as follows:

383 |
    384 |
  • DataRef: marginal/groundtraffic/distance
  • 385 |
  • Keyframe#0: DataRef value = 0.
  • 386 |
  • Keyframe#1: DataRef value = one quarter of the circumference of the wheel [m].
  • 387 |
  • Loop DataRef value = the circumference of the wheel [m].
  • 388 |
389 |

For example, for a wheel 1.155m in diameter:

390 |

    391 |
  • DataRef: marginal/groundtraffic/distance
  • 392 |
  • Keyframe#0: DataRef value = 0
  • 393 |
  • Keyframe#1: DataRef value = 1.155 × π ÷ 4 = 1.155 × 0.7854 = 0.907
  • 394 |
  • Loop DataRef value = 1.155 × π = 1.155 × 3.1416 = 3.628
  • 395 |
396 | 397 |

Collision avoidance

398 |

Objects will try to avoid crashing into each other by waiting at a waypoint if an object on another route is crossing ahead. So where routes cross don't leave a large distance between the waypoints on either side of the crossing point otherwise an object might have to wait for a long time for the crossing object to clear:

399 |
400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | routes cross 415 | 416 |
417 |

Similarly, objects will try to avoid getting in the way of the user's and of AI aircraft. So where a route crosses a taxi or apron path don't leave a large distance between the waypoints on either side of the crossing point.

418 |

Where two or more routes share exactly the same waypoint co-ordinates for all or part of their length this is treated as a shared path rather than as a “crossing”; objects will follow each other along the shared path without waiting for other objects to clear:

419 |
420 | 421 | 422 | 423 | 424 | 425 | shared path 426 | 427 |
428 | 429 |

Bridges & “Hard” surfaces

430 | 431 |

The animated objects will always go over “Hard” surfaces in static objects that are placed conventionally within an X-Plane scenery package. At your choice, animated objects can be made to go over or under the bridges that are part of X-Plane's road and rail networks, and over or under “Hard Deck” surfaces in static objects.

432 |
433 |
Going over X-Plane's bridges:
434 |
Place the first waypoint in the route at ground-level. Then add waypoints such that your route follows the path of the bridge. Since objects travel in a straight line between waypoints, if the bridge is curved you will need to add sufficient waypoints to ensure that the object doesn't fall off the bridge between waypoints. Note: If you turn on Debug the waypoints are shown at ground-level, but the object will follow the bridge above unless you let it fall off.
435 |
Going over static objects:
436 |
In a 3D modelling application mark the faces of your object that represent the road surface as “Hard” if the object represents something solid (e.g. an embankment) or as “Hard Deck” if the object represents something under which traffic can cross (e.g. a bridge, overpass or elevated deck). If using “Hard Deck” note that the maximum gradient that objects will follow is 1:4 or 25%. Once you've placed the static object in your scenery package (e.g. using OverlayEditor or WED) then place your waypoints as for X-Plane's bridges.
437 |
Going under X-Plane's bridges:
438 |
Animated objects will cross under X-Plane's bridges provided that there is sufficient clearance.
439 |
Going under static objects:
440 |
Animated objects will ignore static objects (i.e. cross under them or plough right through them) unless the static object contains “Hard” and/or “Hard Deck” surfaces. Objects will cross under “Hard Deck” surfaces provided that there is sufficient clearance.
441 |
442 |

Animated objects will ignore any “Hard” and “Hard Deck” surfaces in other objects that are animated by this or by another plugin.

443 | 444 |

Troubleshooting

445 | 446 |

You can check that the plugin has been loaded and is operating correctly by opening the file Log.txt in the X-Plane folder. You should see:

447 |
Fetching plugins for Custom Scenery/my scenery package/plugins
448 | Loaded: Custom Scenery/my scenery package/plugins/GroundTraffic/mac.xpl.
449 |

If the plugin is operating correctly it produces no other output in the log. However if the plugin has a problem reading your GroundTraffic.txt file you will see one or more entries starting with “GroundTraffic:” in the log and the plugin will not perform any animation. For example:

450 |
GroundTraffic: Empty route at line 15
451 |

The plugin examines your GroundTraffic.txt file when you make a selection from the Location → Select Global Airport dialog; if you have edited the file then the plugin will re-read it and re-start the animations. Or you can force the plugin to re-read the file and re-start the animations by disabling and re-enabling it in the Plugin → Plugin Admin → Enable/Disable dialog.

452 | 453 |

Acknowledgements

454 | 455 |

“X-Plane” is a registered trademark of Laminar Research.

456 | 457 |

License

458 | 459 |

This kit is licensed under the GNU LGPL v2.1 license. In short, you can distribute the plugin provided by this kit in free or commerical scenery packages providing that, in the documentation that accompanies your scenery package, you:

460 | 464 |

If you modify the plugin provided by this kit you must publish the source code to your changes as specified in sections 2 and 3 of the LGPL v2.1 license.

465 |

The author would appreciate a courtesy copy of any commercial scenery that you make using this kit, but you are under no obligation.

466 | 467 |
468 | 469 | 470 | 471 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | -------------------------------------------------------------------------------- /src/draw.c: -------------------------------------------------------------------------------- 1 | /* 2 | * GroundTraffic 3 | * 4 | * (c) Jonathan Harris 2013-2014 5 | * 6 | * Licensed under GNU LGPL v2.1. 7 | */ 8 | 9 | #include "groundtraffic.h" 10 | #include "planes.h" 11 | 12 | /* Globals */ 13 | route_t *drawroute = NULL; /* Global so can be accessed in DataRef callback */ 14 | float last_frame=0; /* last time we recalculated */ 15 | static int is_night=0; /* was night last time we recalculated? */ 16 | float lod_factor; /* screen_width / lod_bias at time of last draw */ 17 | int font_width, font_semiheight; 18 | char *labeltbl = NULL; 19 | #ifdef DO_BENCHMARK 20 | int drawcumul = 0; /* clock time taken drawing [us] */ 21 | int drawframes = 0; /* over cumulative number of frames */ 22 | #endif 23 | 24 | /* In this file */ 25 | static void bez(XPLMDrawInfo_t *drawinfo, point_t *p1, point_t *p2, point_t *p3, float mu); 26 | 27 | 28 | static collision_t* iscollision(route_t *route, int tryno) 29 | { 30 | path_t *last_node = route->path + route->last_node; 31 | path_t *next_node = route->path + route->next_node; 32 | collision_t *c = tryno ? (route->direction>0 ? last_node->collisions : next_node->collisions) : NULL; 33 | int planeno; 34 | float t = route->next_distance / route->speed; /* time to next waypoint */; 35 | 36 | if (route->highway) return NULL; /* Highways aren't subject to collisions */ 37 | 38 | /* Route collisions */ 39 | while (c) 40 | { 41 | path_t *c_end_node; 42 | 43 | /* Avoid immediate deadlock if we're just enabled/activated */ 44 | if (c->route->last_node == c->route->next_node) 45 | { 46 | c = c->next; 47 | continue; 48 | } 49 | 50 | c_end_node = c->route->path + (c->node+1 >= c->route->pathlen ? 0 : c->node+1); /* Node at end of colliding segment */ 51 | if(route->direction>0 && c->route->direction>0 && next_node->waypoint.lat == c_end_node->waypoint.lat && next_node->waypoint.lon == c_end_node->waypoint.lon) 52 | { 53 | /* Co-located end nodes */ 54 | 55 | /* Have to wait if he's sitting on the co-located end node */ 56 | if (c->route->last_node == c->node+1 && 57 | (c->route->state.dataref || c->route->state.waiting || c->route->state.collision || 58 | (c->route->state.paused && route->next_time + t <= c->route->next_time + COLLISION_INTERVAL))) /* Our next_time hasn't yet been updated yet so ~= now. His next_time is the time he will unpause. */ 59 | { 60 | route->deadlocked = COLLISION_TIMEOUT; /* Wait potentially forever */ 61 | return c; 62 | } 63 | 64 | /* Have to wait if he will wait when he reaches the co-located end node, or if we'll get there too early */ 65 | /* We don't check whether he *might* wait for a collision - gets too complicated */ 66 | if (c->route->last_node == c->node && /* On colliding segment */ 67 | !(c->route->state.dataref || c->route->state.waiting || c->route->state.collision || c->route->state.paused) && /* He's not waiting at previous node */ 68 | (c_end_node->whenrefs || c_end_node->attime[0] != INVALID_AT || 69 | route->next_time + t <= c->route->next_time + COLLISION_INTERVAL + c_end_node->pausetime)) /* Our route->next_time hasn't yet been updated yet so ~= now */ 70 | { 71 | route->deadlocked = COLLISION_TIMEOUT; /* Wait potentially forever */ 72 | return c; 73 | } 74 | } 75 | else 76 | { 77 | /* Paths cross */ 78 | 79 | if ((c->route->direction>0 ? c->route->last_node : c->route->next_node) == c->node && /* On colliding segment */ 80 | /* No point waiting for a route that is itself waiting for any reason. He'll re-check on exit from wait. */ 81 | !(c->route->state.dataref||c->route->state.waiting||c->route->state.collision||c->route->state.paused) && 82 | /* At similar altitude? */ 83 | fabsf(c->route->drawinfo->y - route->drawinfo->y) <= COLLISION_ALT) 84 | { 85 | route->deadlocked = tryno; /* Collision */ 86 | return c; 87 | } 88 | } 89 | c = c->next; 90 | } 91 | 92 | /* Plane collisions */ 93 | for (planeno=0; planenop, p, 4)) return 0; 102 | 103 | for (i=0, j=3; i<4; j=i++) 104 | if (intersect(&last_node->p, &next_node->p, p+i, p+j)) 105 | return (collision_t*) -1; /* Next edge intersects this plane's footprint */ 106 | } 107 | 108 | return NULL; 109 | } 110 | 111 | 112 | /* For drawing route nodes. Relies on the fact that the OpenGL view is not clipped to our window */ 113 | void labelcallback(XPLMWindowID inWindowID, void *inRefcon) 114 | { 115 | if (airport.drawroutes) /* labelwin is not destroyed immediately on deactivation */ 116 | drawdebug2d(); 117 | } 118 | 119 | 120 | /* Actually do the drawing. Uses global drawroute so DataRef callbacks have access to the route being drawn. 121 | * Tries to batch concurrent routes that use the same XPLMObjectRef (note: not textual name since one name 122 | * might map to multiple library objects). Route linked list was sorted in XPLMObjectRef order during activate(). 123 | * We can't batch if the object uses per-route DataRefs since we wouldn't know which route/object the accessor 124 | * callback was called for. 125 | * Note we don't know that an object uses per-route DataRefs until we draw it for the first time when the 126 | * accessor callback will set route->state.hasdataref. 127 | * If some objects are in range but others not then we issue one XPLMDrawObjects() call that spans all those 128 | * in range, since this seems to be cheaper than multiple calls even if more drawing results. */ 129 | static void drawroutes() 130 | { 131 | float view_x, view_y, view_z; 132 | 133 | view_x=XPLMGetDataf(ref_view_x); 134 | view_y=XPLMGetDataf(ref_view_y); 135 | view_z=XPLMGetDataf(ref_view_z); 136 | 137 | drawroute=airport.routes; 138 | while (drawroute) 139 | { 140 | if (drawroute->state.hasdataref) /* Objects that use a per-route DataRef can't be batched */ 141 | { 142 | /* Have to check draw range every frame since "now" isn't updated while sim paused */ 143 | if (indrawrange(drawroute->drawinfo->x-view_x, drawroute->drawinfo->y-view_y, drawroute->drawinfo->z-view_z, drawroute->object.drawlod * lod_factor)) 144 | XPLMDrawObjects(drawroute->object.objref, 1, drawroute->drawinfo, is_night, 1); 145 | 146 | if (drawroute->next && drawroute->object.objref == drawroute->next->object.objref) 147 | drawroute->next->state.hasdataref = -1; /* propagate flag to all routes using this objref */ 148 | 149 | drawroute=drawroute->next; 150 | } 151 | else 152 | { 153 | route_t *route, *first = 0, *last = 0; 154 | 155 | for (route=drawroute; route && route->object.objref==drawroute->object.objref; route=route->next) 156 | /* Have to check draw range every frame since "now" isn't updated while sim paused */ 157 | if (indrawrange(route->drawinfo->x-view_x, route->drawinfo->y-view_y, route->drawinfo->z-view_z, route->object.drawlod * lod_factor)) 158 | { 159 | if (!first) first = route; 160 | last = route; 161 | } 162 | 163 | if (first) 164 | XPLMDrawObjects(drawroute->object.objref, 1 + last->drawinfo - first->drawinfo, first->drawinfo, is_night, 1); 165 | 166 | drawroute=route; 167 | } 168 | } 169 | } 170 | 171 | 172 | /* Main update and draw loop */ 173 | int drawcallback(XPLMDrawingPhase inPhase, int inIsBefore, void *inRefcon) 174 | { 175 | double airport_x, airport_y, airport_z; 176 | float now; 177 | route_t *route; 178 | int tod=-1; 179 | unsigned int dow=0; 180 | XPLMProbeInfo_t probeinfo; 181 | #ifdef DO_BENCHMARK 182 | struct timeval t1, t2; 183 | gettimeofday(&t1, NULL); /* start */ 184 | #endif 185 | 186 | assert (airport.state == active); 187 | 188 | XPLMWorldToLocal(airport.tower.lat, airport.tower.lon, airport.tower.alt, &airport_x, &airport_y, &airport_z); 189 | if (airport.p.x != airport_x || airport.p.y != airport_y || airport.p.z != airport_z) 190 | { 191 | /* OpenGL projection has shifted */ 192 | airport.p.x=airport_x; airport.p.y=airport_y; airport.p.z=airport_z; 193 | maproutes(&airport); 194 | } 195 | 196 | if (!XPLMGetDatai(ref_rentype)) 197 | { 198 | int width; 199 | XPLMGetScreenSize(&width, NULL); 200 | lod_factor = (float) width / lod_bias; /* Screen size can change while paused, so need to recalculate once per frame */ 201 | #ifdef DO_BENCHMARK 202 | drawframes += 1; 203 | #endif 204 | 205 | /* draw route paths */ 206 | if (airport.drawroutes) 207 | { 208 | #ifdef DEBUG 209 | int planeno; 210 | #endif 211 | GLint view[4] = { 0 }; 212 | 213 | XPLMSetGraphicsState(0, 0, 0, 0, 1, 0, 0); 214 | glLineWidth(1.5); 215 | 216 | XPLMGetScreenSize(view+2, view+3); /* Real viewport reported by GL_VIEWPORT will be larger than physical screen if FSAA enabled */ 217 | drawdebug3d(-1, view); 218 | 219 | #ifdef DEBUG 220 | /* Draw AI plane positions */ 221 | glColor4f(0,0,0,0.25f); 222 | glBegin(GL_QUADS); 223 | for (planeno=0; planenonext) 259 | { 260 | path_t *last_node, *next_node; 261 | float progress; 262 | float route_now = now - route->object.lag; /* Train objects are drawn in the past */ 263 | 264 | if (route_now >= route->next_time && !route->state.frozen) 265 | { 266 | setcmd_t *setcmd = NULL; 267 | 268 | if (route->state.waiting) 269 | { 270 | /* We don't get notified when time-of-day changes in the sim, so poll once a minute */ 271 | int i; 272 | if (!dow) 273 | { 274 | /* Get current day-of-week. FIXME: This is in user's timezone, not the airport's. */ 275 | struct tm tm = { 0, 0, 12, XPLMGetDatai(ref_doy)+1, 0, year }; 276 | dow = (mktime(&tm) == -1) ? DAY_SUN : 1 << tm.tm_wday; 277 | } 278 | if (tod < 0) tod = (int) (XPLMGetDataf(ref_tod)/60); 279 | for (i=0; ipath[route->last_node].attime[i] == INVALID_AT) 282 | break; 283 | else if ((route->path[route->last_node].attime[i] == tod) && 284 | (route->path[route->last_node].atdays & dow)) 285 | { 286 | route->state.waiting = 0; 287 | route->state.collision = iscollision(route, COLLISION_TIMEOUT); /* Re-check for collision */ 288 | break; 289 | } 290 | } 291 | /* last and next were calculated when we originally hit this waypoint */ 292 | } 293 | else if (route->state.dataref) 294 | { 295 | whenref_t *whenref = route->path[route->last_node].whenrefs; 296 | 297 | while (whenref) 298 | { 299 | float val; 300 | extref_t *extref = whenref->extref; 301 | 302 | if (extref->type == xplmType_Mine) 303 | { 304 | val = userrefcallback(extref->ref); 305 | } 306 | else if (whenref->idx < 0) 307 | { 308 | /* Not an array */ 309 | if (extref->type & xplmType_Float) 310 | val = XPLMGetDataf(extref->ref); 311 | else if (extref->type & xplmType_Double) 312 | val = XPLMGetDatad(extref->ref); 313 | else if (extref->type & xplmType_Int) 314 | val = XPLMGetDatai(extref->ref); 315 | else 316 | val = 0; /* Lookup failed or otherwise unusable */ 317 | } 318 | else if (extref->type & xplmType_FloatArray) 319 | { 320 | XPLMGetDatavf(extref->ref, &val, whenref->idx, 1); 321 | } 322 | else if (extref->type & xplmType_IntArray) 323 | { 324 | int ival; 325 | XPLMGetDatavi(extref->ref, &ival, whenref->idx, 1); 326 | val = ival; 327 | } 328 | else 329 | { 330 | val = 0; /* Lookup failed or otherwise unusable */ 331 | } 332 | 333 | if ((val >= whenref->from) && (val <= whenref->to)) 334 | whenref = whenref->next; 335 | else 336 | break; /* fail */ 337 | } 338 | 339 | if (!whenref) 340 | { 341 | /* All passed */ 342 | route->state.dataref = 0; 343 | route->state.collision = iscollision(route, COLLISION_TIMEOUT); /* Re-check for collision */ 344 | /* last and next were calculated when we originally hit this waypoint */ 345 | } 346 | } 347 | else if (route->state.paused) 348 | { 349 | route->state.paused = 0; 350 | route->state.collision = iscollision(route, COLLISION_TIMEOUT); /* Re-check for collision */ 351 | /* last and next were calculated when we originally hit this waypoint */ 352 | } 353 | else if (route->state.collision) 354 | { 355 | route->state.collision = iscollision(route, route->deadlocked-1); /* Break deadlock on timeout */ 356 | /* last and next were calculated when we originally hit this waypoint */ 357 | } 358 | else /* next waypoint */ 359 | { 360 | #ifdef DO_BENCHMARK 361 | if (route == airport.firstroute) 362 | { 363 | drawcumul = 0; 364 | drawframes= XPLMGetDatai(ref_rentype) ? 0 : 1; 365 | } 366 | #endif 367 | route->last_node = route->next_node; 368 | route->next_node += route->direction; 369 | if (!route->last_node || (route->highway && route->next_node >= route->pathlen)) 370 | route->last_distance = 0; /* reset distance travelled to prevent growing stupidly large */ 371 | else if (route->state.backingup) 372 | route->last_distance -= route->next_distance; 373 | else 374 | route->last_distance += route->next_distance; 375 | route->distance = route->last_distance; 376 | 377 | if (route->highway && !route->next_time) 378 | { 379 | /* reset highway route */ 380 | int i; 381 | float path_cumul = 0; 382 | 383 | route->distance = route->highway_offset; 384 | route->last_distance = 0; 385 | for (i=1; ipathlen; i++) 386 | { 387 | path_t *node = route->path+i, *prev = route->path+i-1; 388 | path_cumul += hypotf(node->p.x - prev->p.x, node->p.z - prev->p.z); 389 | if (path_cumul >= route->highway_offset) 390 | { 391 | route->next_time = now - (route->highway_offset - route->last_distance) / route->speed; 392 | route->last_node = i-1; 393 | route->next_node = i; 394 | break; 395 | } 396 | else 397 | { 398 | route->last_distance = path_cumul; 399 | } 400 | } 401 | } 402 | else if (route->path[route->last_node].flags.reverse) 403 | { 404 | route->direction = -1; 405 | route->next_node = route->pathlen-2; 406 | } 407 | else if (route->next_node >= route->pathlen) 408 | { 409 | /* At end of route */ 410 | if (route->highway) 411 | { 412 | route->last_node = 0; /* jump back to start */ 413 | route->next_node = 1; 414 | route->next_y = INVALID_ALT; /* Discontinuity so reset */ 415 | } 416 | else 417 | { 418 | route->next_node = 0; /* head on to start */ 419 | } 420 | } 421 | else if (route->next_node < 0) 422 | { 423 | /* Back at start of route - start again */ 424 | route->direction = 1; 425 | route->next_node = 1; 426 | } 427 | last_node = route->path + route->last_node; 428 | next_node = route->path + route->next_node; 429 | 430 | /* Assume distances are too small to care about earth curvature so just calculate using OpenGL coords */ 431 | route->next_heading = R2D(atan2f(next_node->p.x - last_node->p.x, last_node->p.z - next_node->p.z)); 432 | route->next_distance = sqrtf((next_node->p.x - last_node->p.x) * (next_node->p.x - last_node->p.x) + 433 | (next_node->p.z - last_node->p.z) * (next_node->p.z - last_node->p.z)); 434 | 435 | if (!route->parent) 436 | { 437 | if (last_node->whenrefs) 438 | route->state.dataref = 1; 439 | if (last_node->attime[0] != INVALID_AT) 440 | route->state.waiting = 1; 441 | if (last_node->pausetime) 442 | route->state.paused = 1; 443 | setcmd = last_node->setcmds; 444 | if (last_node->flags.backup) 445 | { 446 | if (last_node->pausetime) /* A */ 447 | { 448 | /* Backing up after pause */ 449 | route->state.backingup = 1; 450 | route->state.forwardsa = 1; 451 | } 452 | else /* Y */ 453 | { 454 | /* Backing up before pause */ 455 | route->state.forwardsb = 1; 456 | } 457 | } 458 | else 459 | { 460 | if (!route->state.forwardsa) /* !Q */ 461 | { 462 | route->state.backingup = 0; 463 | route->state.forwardsb = 0; 464 | } 465 | if (!route->state.backingup && !route->state.forwardsb) /* !B */ 466 | { 467 | route->state.forwardsa = 0; 468 | } 469 | } 470 | route->state.collision = iscollision(route, COLLISION_TIMEOUT); 471 | } 472 | } 473 | 474 | last_node = route->path + route->last_node; 475 | next_node = route->path + route->next_node; 476 | 477 | /* Maintain speed/progress unless there's been a large gap in draw callbacks because we were deactivated / disabled */ 478 | if (route->highway || (route->last_time && route_now - route->next_time < RESET_TIME)) 479 | route->last_time = route->next_time; 480 | else 481 | { 482 | route->last_time = now; /* reset */ 483 | route_now = route->last_time - route->object.lag; 484 | } 485 | 486 | if (route->state.waiting) 487 | route->next_time = route->last_time + AT_INTERVAL; 488 | else if (route->state.dataref) 489 | route->next_time = route->last_time + WHEN_INTERVAL; 490 | else if (route->state.paused) 491 | route->next_time = route->last_time + last_node->pausetime; 492 | else if (route->state.collision) 493 | route->next_time = route->last_time + COLLISION_INTERVAL; 494 | else if (route->state.forwardsa && !last_node->flags.backup) /* B */ 495 | { 496 | route->next_distance += route->speed * TURN_TIME; /* Allow for extra turning distance */ 497 | route->next_time = route->last_time + route->next_distance / route->speed; 498 | } 499 | else if (route->state.forwardsb && last_node->flags.backup) /* Y */ 500 | { 501 | route->last_time += TURN_TIME; /* Allow for extra turning distance */ 502 | route->next_time = route->last_time + route->next_distance / route->speed; 503 | } 504 | else 505 | route->next_time = route->last_time + route->next_distance / route->speed; 506 | 507 | /* Set DataRefs. Need to do this after calculating last_time so use hacky flag */ 508 | while (setcmd) 509 | { 510 | userref_t *userref = setcmd->userref; 511 | 512 | userref->duration = setcmd->duration; 513 | userref->slope = setcmd->flags.slope; 514 | userref->curve = setcmd->flags.curve; 515 | if (setcmd->flags.set2) 516 | { 517 | userref->start1 = route->last_time; 518 | userref->start2 = route->last_time + last_node->pausetime - userref->duration; 519 | } 520 | else if (setcmd->flags.set1) 521 | { 522 | userref->start1 = route->last_time; 523 | userref->start2 = 0; 524 | } 525 | setcmd = setcmd->next; 526 | } 527 | 528 | /* Force re-probe since we've changed direction */ 529 | route->next_probe = route_now; 530 | 531 | } // (route_now >= route->next_time && !route->state.frozen) 532 | 533 | /* Parent controls state of children */ 534 | if (route->parent && !route->highway) 535 | { 536 | if ((route->parent->last_time == now) || (route->path[route->pathlen-1].flags.reverse && (!route->parent->last_node || route->parent->last_node==route->pathlen-1))) 537 | { 538 | /* Parent was reset or at end of a reversible route - line up back in time from it */ 539 | route->direction = route->parent->direction; 540 | route->last_node = route->parent->last_node; 541 | route->next_node = route->parent->next_node; 542 | route->last_distance = route->parent->last_distance; 543 | route->next_distance = route->parent->next_distance; 544 | route->distance = route->parent->distance - route->object.lag * route->speed; /* Negative at first node */ 545 | route->next_heading = route->parent->next_heading; 546 | route->last_time = route->parent->last_time; 547 | route->next_time = route->last_time + route->next_distance / route->speed; 548 | route->state.frozen = 0; 549 | } 550 | 551 | if (route->parent->state.paused||route->parent->state.waiting||route->parent->state.dataref||route->parent->state.collision) 552 | { 553 | /* Parent is paused */ 554 | if (!route->state.frozen) 555 | { 556 | route->freeze_time = route->parent->last_time; /* Save time parent started pause */ 557 | route->state.frozen = 1; 558 | } 559 | route_now = route->freeze_time - route->object.lag; 560 | } 561 | else if (route->state.frozen && !(route->parent->state.paused||route->parent->state.waiting||route->parent->state.dataref||route->parent->state.collision)) 562 | { 563 | /* Parent has just unpaused - maintain spacing */ 564 | route->last_time += (route->parent->last_time - route->freeze_time); 565 | route->next_time += (route->parent->last_time - route->freeze_time); 566 | route->state.frozen = 0; 567 | } 568 | } 569 | 570 | /* Calculate drawing position */ 571 | last_node = route->path + route->last_node; 572 | next_node = route->path + route->next_node; 573 | 574 | if (route->next_y == INVALID_ALT) 575 | { 576 | /* Just loaded, or OpenGL projection has shifted while we are active */ 577 | route->next_y = last_node->p.y; /* Unfortunately this will cause the object to fall off any bridge */ 578 | route->next_probe = route_now; /* Force probe ahead */ 579 | } 580 | 581 | if (!(route->state.paused||route->state.waiting||route->state.dataref||route->state.collision)) 582 | { 583 | float probe_interval; 584 | 585 | if (route->state.backingup && route->state.forwardsa && !last_node->flags.backup && route_now-route->last_time >= TURN_TIME/2) /* C */ 586 | { 587 | /* Reached mirror of p3. Fixup things so we're backwards in time on otherwise normal path */ 588 | route->state.backingup = 0; 589 | route->last_time += TURN_TIME; 590 | route->next_distance -= route->speed * TURN_TIME; 591 | } 592 | if (route->state.forwardsb && !route->state.backingup && route->last_time - route_now <= TURN_TIME/2) /* Z */ 593 | { 594 | /* Reached mirror of p1. */ 595 | route->state.backingup = 1; 596 | } 597 | else if (!(route->state.forwardsb || route->state.forwardsa) && now < route->last_time) 598 | { 599 | /* We must be in replay, and we've gone back in time beyond the last decision. Just show at the last node. */ 600 | route_now = route->last_time - route->object.lag; /* Train objects are drawn in the past */ 601 | } 602 | 603 | if (route_now >= route->next_probe) 604 | { 605 | /* Probe up to PROBE_INTERVAL into the future */ 606 | route->last_y = route->next_y; 607 | route->last_probe = route_now; 608 | if (route_now + (PROBE_INTERVAL * 1.25f) >= route->next_time) 609 | { 610 | route->next_probe = route->next_time; 611 | probe_interval = route->next_probe - route->last_probe; 612 | XPLMProbeTerrainXYZ(ref_probe, next_node->p.x, route->last_y + route->speed * probe_interval * PROBE_GRADIENT, next_node->p.z, &probeinfo); 613 | } 614 | else 615 | { 616 | route->next_probe = route_now + PROBE_INTERVAL; 617 | probe_interval = route->next_probe - route->last_probe; 618 | progress = (route->next_probe - route->last_time) / (route->next_time - route->last_time); 619 | XPLMProbeTerrainXYZ(ref_probe, last_node->p.x + progress * (next_node->p.x - last_node->p.x), route->last_y + route->speed * PROBE_INTERVAL * PROBE_GRADIENT, last_node->p.z + progress * (next_node->p.z - last_node->p.z), &probeinfo); 620 | } 621 | route->next_y = probeinfo.locationY; 622 | } 623 | else 624 | { 625 | probe_interval = route->next_probe - route->last_probe; 626 | } 627 | 628 | progress = (route_now - route->last_time) / (route->next_time - route->last_time); 629 | route->drawinfo->y = route->next_y + (route->last_y - route->next_y) * (route->next_probe - route_now) / probe_interval; 630 | if (!route->object.heading) 631 | route->drawinfo->pitch = R2D(sinf((route->next_y - route->last_y) / (probe_interval * route->speed))); 632 | else if (route->object.heading == 180) 633 | route->drawinfo->pitch = R2D(sinf((route->last_y - route->next_y) / (probe_interval * route->speed))); 634 | if (route->state.backingup) 635 | route->distance = route->last_distance - progress * route->next_distance; 636 | else 637 | route->distance = route->last_distance + progress * route->next_distance; 638 | route->steer = 0; 639 | } 640 | else 641 | { 642 | /* Paused: Fake up times for drawing code below */ 643 | progress = - (route->object.lag * route->speed) / route->next_distance; 644 | route_now = route->last_time - route->object.lag; 645 | route->drawinfo->y = route->next_y; 646 | route->drawinfo->pitch = 0; /* Since we're not probing */ 647 | } 648 | 649 | #ifdef DO_MARKERS 650 | { 651 | /* Show markers - which are only visible if shadows turned off! */ 652 | path_t *node = progress < 0.5f ? last_node : next_node; 653 | XPLMSetGraphicsState(0, 0, 0, 0, 0, 0, 0); 654 | glLineWidth(3); 655 | glColor3f(1,0,0); 656 | glBegin(GL_LINE_STRIP); 657 | glVertex3f(node->p1.x, node->p.y, node->p1.z); 658 | glVertex3f(node->p1.x, node->p.y+10, node->p1.z); 659 | glEnd(); 660 | glColor3f(0,1,0); 661 | glBegin(GL_LINE_STRIP); 662 | glVertex3f(node->p.x, node->p.y, node->p.z); 663 | glVertex3f(node->p.x, node->p.y+10, node->p.z); 664 | glEnd(); 665 | glColor3f(0,0,1); 666 | glBegin(GL_LINE_STRIP); 667 | glVertex3f(node->p3.x, node->p.y, node->p3.z); 668 | glVertex3f(node->p3.x, node->p.y+10, node->p3.z); 669 | glEnd(); 670 | } 671 | #endif 672 | 673 | /* Finally do the drawing */ 674 | if (route->state.backingup) 675 | { 676 | point_t pr; /* Mirror of p1/p3 */ 677 | 678 | if (progress >= 0.5f) 679 | { 680 | /* Approaching a waypoint while backing up */ 681 | if (next_node->flags.backup || route->next_time - route_now >= TURN_TIME/2 || !(next_node->p1.x || next_node->p1.z)) 682 | { 683 | /* No bezier points, or not in range, or approaching backup node */ 684 | route->drawinfo->x = last_node->p.x + progress * (next_node->p.x - last_node->p.x); 685 | route->drawinfo->z = last_node->p.z + progress * (next_node->p.z - last_node->p.z); 686 | route->drawinfo->heading = route->next_heading; 687 | } 688 | else 689 | { 690 | assert(route->state.forwardsa); 691 | pr.x = next_node->p.x + next_node->p.x - next_node->p3.x; 692 | pr.z = next_node->p.z + next_node->p.z - next_node->p3.z; 693 | if (route->speed * 2 <= route->next_distance) 694 | bez(route->drawinfo, &next_node->p1, &next_node->p, &pr, 0.5f + (route_now - route->next_time)/TURN_TIME); 695 | else /* Short edge */ 696 | bez(route->drawinfo, &next_node->p1, &next_node->p, &pr, progress - 0.5f); 697 | route->steer = route->next_heading - route->drawinfo->heading; 698 | } 699 | } 700 | else if (route->state.forwardsb && (route_now - route->last_time < TURN_TIME/2) && (last_node->p1.x || last_node->p1.z)) 701 | { 702 | /* Leaving mirrored p1 waypoint while backing up */ 703 | pr.x = last_node->p.x + last_node->p.x - last_node->p1.x; 704 | pr.z = last_node->p.z + last_node->p.z - last_node->p1.z; 705 | if (progress <0 || route->speed * 2 <= route->next_distance) 706 | bez(route->drawinfo, &pr, &last_node->p, &last_node->p3, 0.5f + (route_now - route->last_time)/TURN_TIME); 707 | else /* Short edge */ 708 | bez(route->drawinfo, &pr, &last_node->p, &last_node->p3, progress + 0.5f); 709 | if (progress < 0) 710 | route->steer = 180 - route->drawinfo->heading + R2D(atan2f(pr.x - last_node->p.x, last_node->p.z - pr.z)); /* Don't have a route->last_heading */ 711 | else 712 | route->steer = route->drawinfo->heading - route->next_heading; 713 | } 714 | else if (route->state.forwardsa && (route_now - route->last_time < TURN_TIME/2) && (last_node->p3.x || last_node->p3.z)) 715 | { 716 | /* Leaving a waypoint while backing up */ 717 | pr.x = last_node->p.x + last_node->p.x - last_node->p3.x; 718 | pr.z = last_node->p.z + last_node->p.z - last_node->p3.z; 719 | if (route->speed * 2 <= route->next_distance) 720 | bez(route->drawinfo, &last_node->p1, &last_node->p, &pr, 0.5f + (route_now - route->last_time)/TURN_TIME); 721 | else /* Short edge */ 722 | bez(route->drawinfo, &last_node->p1, &last_node->p, &pr, progress + 0.5f); 723 | route->steer = 180 - route->next_heading + route->drawinfo->heading; 724 | } 725 | else 726 | { 727 | route->drawinfo->x = last_node->p.x + progress * (next_node->p.x - last_node->p.x); 728 | route->drawinfo->z = last_node->p.z + progress * (next_node->p.z - last_node->p.z); 729 | route->drawinfo->heading = route->next_heading; 730 | } 731 | route->drawinfo->heading -= 180; 732 | route->drawinfo->pitch = -route->drawinfo->pitch; 733 | } /* (route->state.backingup) */ 734 | 735 | else if (route->state.forwardsb && (last_node->p1.x || last_node->p1.z)) 736 | { 737 | /* Backing up to pause, keep going to mirror of p1 */ 738 | progress = 2 - (route->last_time - route_now) / (TURN_TIME/2); 739 | route->drawinfo->x = last_node->p.x + progress * (last_node->p.x - last_node->p1.x); 740 | route->drawinfo->z = last_node->p.z + progress * (last_node->p.z - last_node->p1.z); 741 | route->drawinfo->heading -= route->object.heading; /* Keep last heading */ 742 | } 743 | else if (progress >= 0.5f) 744 | { 745 | /* Approaching a waypoint */ 746 | if (next_node->flags.backup || (route->next_time - route_now >= TURN_TIME/2) || !(next_node->p1.x || next_node->p1.z)) 747 | { 748 | /* No bezier points, or not in range, or approaching backup node */ 749 | route->drawinfo->x = last_node->p.x + progress * (next_node->p.x - last_node->p.x); 750 | route->drawinfo->z = last_node->p.z + progress * (next_node->p.z - last_node->p.z); 751 | route->drawinfo->heading = route->next_heading; 752 | } 753 | else if (route->direction > 0) 754 | { 755 | if (route->speed * 2 <= route->next_distance) 756 | bez(route->drawinfo, &next_node->p1, &next_node->p, &next_node->p3, 0.5f + (route_now - route->next_time)/TURN_TIME); 757 | else /* Short edge */ 758 | bez(route->drawinfo, &next_node->p1, &next_node->p, &next_node->p3, progress - 0.5f); 759 | route->steer = route->drawinfo->heading - route->next_heading; 760 | } 761 | else 762 | { 763 | if (route->speed * 2 <= route->next_distance) 764 | bez(route->drawinfo, &next_node->p3, &next_node->p, &next_node->p1, 0.5f + (route_now - route->next_time)/TURN_TIME); 765 | else /* Short edge */ 766 | bez(route->drawinfo, &next_node->p3, &next_node->p, &next_node->p1, progress - 0.5f); 767 | route->steer = route->drawinfo->heading - route->next_heading; 768 | } 769 | } 770 | else if (route->state.forwardsa && progress<0 && (last_node->p3.x || last_node->p3.z)) 771 | { 772 | /* Leaving mirror of p3. Special handling to deal with short paths. */ 773 | progress = (route->last_time - route_now) / (TURN_TIME/2); 774 | route->drawinfo->x = last_node->p.x + progress * (last_node->p.x - last_node->p3.x); 775 | route->drawinfo->z = last_node->p.z + progress * (last_node->p.z - last_node->p3.z); 776 | route->drawinfo->heading = route->next_heading; 777 | } 778 | else if (!route->state.forwardsa && (route_now - route->last_time < TURN_TIME/2) && (last_node->p3.x || last_node->p3.z)) 779 | { 780 | /* Leaving a waypoint (may be from a negative direction if a paused child) */ 781 | if (route->direction > 0) 782 | { 783 | if ((progress < 0) || (route->speed * 2 <= route->next_distance)) 784 | bez(route->drawinfo, &last_node->p1, &last_node->p, &last_node->p3, 0.5f + (route_now - route->last_time)/TURN_TIME); 785 | else /* Short edge */ 786 | bez(route->drawinfo, &last_node->p1, &last_node->p, &last_node->p3, progress + 0.5f); 787 | } 788 | else 789 | { 790 | if ((progress < 0) || (route->speed * 2 <= route->next_distance)) 791 | bez(route->drawinfo, &last_node->p3, &last_node->p, &last_node->p1, 0.5f + (route_now - route->last_time)/TURN_TIME); 792 | else /* Short edge */ 793 | bez(route->drawinfo, &last_node->p3, &last_node->p, &last_node->p1, progress + 0.5f); 794 | } 795 | route->steer = route->next_heading - route->drawinfo->heading; 796 | } 797 | else 798 | { 799 | route->drawinfo->x = last_node->p.x + progress * (next_node->p.x - last_node->p.x); 800 | route->drawinfo->z = last_node->p.z + progress * (next_node->p.z - last_node->p.z); 801 | route->drawinfo->heading = route->next_heading; 802 | } 803 | if (route->object.offset) 804 | { 805 | float h = D2R(route->drawinfo->heading); 806 | route->drawinfo->x += sinf(h) * route->object.offset; 807 | route->drawinfo->z -= cosf(h) * route->object.offset; 808 | } 809 | if (route->steer) 810 | route->steer = fmodf(route->steer + 540, 360) - 180; /* to range -180..180 */ 811 | route->drawinfo->heading += route->object.heading; 812 | } 813 | 814 | drawroutes(); 815 | 816 | #ifdef DO_BENCHMARK 817 | gettimeofday(&t2, NULL); /* stop */ 818 | drawcumul += (t2.tv_sec-t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec; 819 | #endif 820 | return 1; 821 | } 822 | 823 | 824 | static void bez(XPLMDrawInfo_t *drawinfo, point_t *p1, point_t *p2, point_t *p3, float mu) 825 | { 826 | float mum1, mum12, mu2; 827 | float tx, tz; 828 | 829 | // assert (mu>=0 && mu<=1); // Trains may go negative at start or in replay 830 | 831 | mu2 = mu * mu; 832 | mum1 = 1 - mu; 833 | mum12 = mum1 * mum1; 834 | drawinfo->x = p1->x * mum12 + 2 * p2->x * mum1 * mu + p3->x * mu2; 835 | drawinfo->z = p1->z * mum12 + 2 * p2->z * mum1 * mu + p3->z * mu2; 836 | 837 | tx = 2 * mum1 * (p2->x - p1->x) + 2 * mu * (p3->x - p2->x); 838 | tz =-2 * mum1 * (p2->z - p1->z) - 2 * mu * (p3->z - p2->z); 839 | drawinfo->heading = R2D(atan2f(tx, tz)); 840 | } 841 | -------------------------------------------------------------------------------- /src/routes.c: -------------------------------------------------------------------------------- 1 | /* 2 | * GroundTraffic 3 | * 4 | * (c) Jonathan Harris 2013 5 | * 6 | * Licensed under GNU LGPL v2.1. 7 | */ 8 | 9 | #include "groundtraffic.h" 10 | #include "bbox.h" 11 | 12 | #define N(c) (c?c:"") 13 | 14 | /* Globals */ 15 | static time_t mtime=-1; /* control file modification time */ 16 | static const char sep[]=" \t\r\n"; 17 | 18 | /* In this file */ 19 | static setcmd_t *readsetcmd(airport_t *airport, route_t *currentroute, path_t *node, char *buffer, int lineno); 20 | static route_t *expandtrain(airport_t *airport, route_t *currentroute); 21 | 22 | const glColor3f_t colors[16] = { { 0.0, 1.0, 0.0 }, // lime (match DRE color) 23 | { 1.0, 0.0, 0.0 }, // red 24 | { 1.0, 1.0, 0.0 }, // yellow 25 | { 0.0, 0.0, 1.0 }, // blue 26 | { 0.0, 1.0, 1.0 }, // aqua 27 | { 1.0, 0.0, 1.0 }, // fuchsia 28 | { 1.0,0.65, 1.0 }, // orange 29 | { 0.5, 0.5, 0.5 }, // gray 30 | { 0.5, 0.0, 0.0 }, // maroon 31 | { 0.5, 0.5, 0.0 }, // olive 32 | { 0.0, 0.5, 0.0 }, // green 33 | { 0.0, 0.5, 0.5 }, // teal 34 | { 0.0, 0.0, 0.5 }, // navy 35 | { 0.5, 0.0, 0.5 }, // purple 36 | {0.75,0.75,0.75 }, // silver 37 | { 0.0, 0.0, 0.0 }, // black 38 | }; 39 | 40 | void clearconfig(airport_t *airport) 41 | { 42 | route_t *route; 43 | train_t *train; 44 | userref_t *userref; 45 | extref_t *extref; 46 | 47 | deactivate(airport); 48 | 49 | airport->tower.lat=airport->tower.lon=0; 50 | airport->tower.alt = (double) INVALID_ALT; 51 | airport->state = noconfig; 52 | airport->done_first_activation = 0; 53 | airport->new_airport = -1; /* Reloaded config causes synchronous load */ 54 | airport->drawroutes = 0; 55 | airport->reflections = 0; 56 | airport->active_distance = ACTIVE_DISTANCE; 57 | 58 | route = airport->routes; 59 | while (route) 60 | { 61 | route_t *nextroute = route->next; 62 | 63 | if (!route->parent) /* Paths and highways are shared with parent */ 64 | { 65 | int i; 66 | for (i=0; ipathlen; i++) 67 | { 68 | collision_t *collision = route->path[i].collisions; 69 | setcmd_t *setcmd = route->path[i].setcmds; 70 | whenref_t *whenref = route->path[i].whenrefs; 71 | 72 | while (collision) 73 | { 74 | collision_t *next = collision->next; 75 | free (collision); 76 | collision = next; 77 | } 78 | while (setcmd) 79 | { 80 | setcmd_t *next = setcmd->next; 81 | free (setcmd); 82 | setcmd = next; 83 | } 84 | while (whenref) 85 | { 86 | whenref_t *next = whenref->next; 87 | free (whenref); 88 | whenref = next; 89 | } 90 | } 91 | free(route->path); 92 | free(route->varrefs); 93 | if (route->highway) 94 | for (i=0; ihighway->objects[i++].name)); 95 | free(route->highway); 96 | } 97 | free(route->object.name); 98 | free(route->object.physical_name); 99 | free(route); 100 | route = nextroute; 101 | } 102 | airport->routes = airport->firstroute = NULL; 103 | 104 | train = airport->trains; 105 | while (train) 106 | { 107 | int i; 108 | train_t *next = train->next; 109 | free(train->name); 110 | for (i=0; iobjects[i++].name)); 111 | free(train); 112 | train = next; 113 | } 114 | airport->trains = NULL; 115 | 116 | userref = airport->userrefs; 117 | while (userref) 118 | { 119 | userref_t *next = userref->next; 120 | if (userref->ref) 121 | XPLMUnregisterDataAccessor(userref->ref); 122 | free(userref->name); 123 | free(userref); 124 | userref = next; 125 | } 126 | airport->userrefs = NULL; 127 | 128 | extref = airport->extrefs; 129 | while (extref) 130 | { 131 | extref_t *next = extref->next; 132 | free(extref->name); 133 | free(extref); 134 | extref = next; 135 | } 136 | airport->extrefs = NULL; 137 | 138 | free(airport->drawinfo); 139 | airport->drawinfo = NULL; 140 | 141 | free(labeltbl); 142 | labeltbl = NULL; 143 | mtime=-1; /* Don't cache */ 144 | } 145 | 146 | /* Convenience function */ 147 | static int failconfig(FILE *h, airport_t *airport, char *buffer, const char *format, ...) 148 | { 149 | va_list ap; 150 | 151 | va_start(ap, format); 152 | vsprintf(buffer, format, ap); 153 | va_end(ap); 154 | xplog(buffer); 155 | clearconfig(airport); 156 | fclose(h); 157 | return 1; 158 | } 159 | 160 | /* 161 | * Read our config file 162 | * Return: 0=config hasn't changed, !0=config has changed and airport->state is updated 163 | */ 164 | int readconfig(char *pkgpath, airport_t *airport) 165 | { 166 | struct stat info; 167 | char buffer[MAX_NAME+128], line[MAX_NAME+64]; 168 | FILE *h; 169 | int lineno=0, count=0, water=0, maxpathlen=0, doneprologue=0; 170 | route_t *currentroute=NULL; 171 | train_t *currenttrain=NULL; 172 | userref_t *userref; 173 | bbox_t bounds; 174 | #ifdef DO_BENCHMARK 175 | struct timeval t1, t2; 176 | gettimeofday(&t1, NULL); /* start */ 177 | #endif 178 | 179 | #if APL || LIN /* Might be a case sensitive file system */ 180 | ino_t lower=0, upper=0; 181 | 182 | strcpy(buffer, pkgpath); 183 | strcat(buffer, "/GROUNDTRAFFIC.TXT"); 184 | if (!stat(buffer, &info)) 185 | upper = info.st_ino; 186 | strcpy(buffer + strlen(pkgpath), "/groundtraffic.txt"); 187 | if (!stat(buffer, &info)) 188 | lower = info.st_ino; 189 | 190 | if (lower && upper==lower) 191 | { 192 | airport->case_folding = -1; 193 | } 194 | else 195 | { 196 | /* Case-sensitive filesystem or file does not exist */ 197 | DIR *dir; 198 | struct dirent *ent; 199 | 200 | if (!(dir=opendir(pkgpath))) 201 | { 202 | clearconfig(airport); 203 | xplog("Can't find my scenery folder"); 204 | return 1; 205 | } 206 | *buffer = '\0'; 207 | while ((ent=readdir(dir))) 208 | if (!strcasecmp(ent->d_name, "groundtraffic.txt")) 209 | { 210 | /* Go with first found if multiple files exist */ 211 | strcpy(buffer, pkgpath); 212 | strcat(buffer, "/"); 213 | strcat(buffer, ent->d_name); 214 | break; 215 | } 216 | closedir(dir); 217 | if (!*buffer) 218 | { 219 | clearconfig(airport); 220 | sprintf(buffer, "Can't find groundtraffic.txt in %s", pkgpath); 221 | xplog(buffer); 222 | return 1; 223 | } 224 | airport->case_folding = 0; 225 | } 226 | #else /* Assume Windows uses a case folding file system */ 227 | airport->case_folding = -1; 228 | strcpy(buffer, pkgpath); 229 | strcat(buffer, "/groundtraffic.txt"); 230 | #endif 231 | 232 | if (stat(buffer, &info)) 233 | { 234 | clearconfig(airport); 235 | sprintf(buffer, "Can't find groundtraffic.txt in %s", pkgpath); 236 | xplog(buffer); 237 | return 1; 238 | } 239 | if (info.st_mtime==mtime) return 0; /* File hasn't changed */ 240 | clearconfig(airport); /* File has changed - free old config */ 241 | bbox_init(&bounds); 242 | 243 | if (!(h=fopen(buffer, "r"))) 244 | { 245 | sprintf(buffer, "Can't open %s/groundtraffic.txt", pkgpath); 246 | xplog(buffer); 247 | return 1; 248 | } 249 | while (fgets(line, sizeof(line), h)) 250 | { 251 | char *c1, *c2=NULL, *c3; 252 | int eol1, eol2, eol3; 253 | if (!lineno && !strncmp(line, "\xef\xbb\xbf", 3)) /* skip UTF-8 BOM */ 254 | c1=strtok(line+3, sep); 255 | else 256 | c1=strtok(line, sep); 257 | lineno++; 258 | 259 | if (!c1) /* Blank line = end of route or train */ 260 | { 261 | if (currentroute && !currentroute->pathlen) 262 | return failconfig(h, airport, buffer, currentroute->highway ? "Empty highway at line %d" : "Empty route at line %d", lineno); 263 | currentroute = NULL; 264 | if (currenttrain && !currenttrain->objects[0].name) 265 | return failconfig(h, airport, buffer, "Empty train at line %d", lineno); 266 | currenttrain = NULL; 267 | continue; 268 | } 269 | else if (*c1=='#') /* Skip comment lines */ 270 | { 271 | continue; 272 | } 273 | else if (currentroute && currentroute->highway) /* Existing highway */ 274 | { 275 | highway_t *highway = currentroute->highway; 276 | int n; /* Object count */ 277 | char *c4; 278 | 279 | c2=strtok(NULL, sep); 280 | for (c3 = c2+strlen(c2)+1; isspace(*c3); c3++); /* ltrim */ 281 | for (c4 = c3+strlen(c3)-1; c4>=c3 && isspace(*c4); *(c4--) = '\0'); /* rtrim */ 282 | if (!*c3) 283 | { 284 | /* Waypoint */ 285 | if (!highway->objects[0].name) /* Expect at least one car */ 286 | return failconfig(h, airport, buffer, "Expecting a car \"offset heading object\" at line %d", lineno); 287 | /* Fall through for waypoint */ 288 | } 289 | else 290 | { 291 | /* Car */ 292 | if (currentroute->pathlen) /* Once we've had the first waypoint, we only expect waypoints */ 293 | return failconfig(h, airport, buffer, "Expecting a waypoint \"lat lon\" or a blank line at line %d", lineno); 294 | 295 | for (n=0; nobjects[n].name; n++); 296 | if (n>=MAX_HIGHWAY) 297 | return failconfig(h, airport, buffer, "Exceeded %d objects in a highway at line %d", MAX_HIGHWAY, lineno); 298 | else if (!c1 || !sscanf(c1, "%f%n", &highway->objects[n].offset, &eol1) || c1[eol1] || 299 | !c2 || !sscanf(c2, "%f%n", &highway->objects[n].heading, &eol2) || c2[eol2]) 300 | return failconfig(h, airport, buffer, "Expecting a car \"offset heading\", found \"%s %s\" at line %d", N(c1), N(c2), lineno); 301 | else if (*c3 == '.' || *c3 == '/' || *c3 == '\\') 302 | return failconfig(h, airport, buffer, "Object name cannot start with a \"%c\" at line %d", *c3, lineno); 303 | else if (strlen(c3) >= MAX_NAME) 304 | return failconfig(h, airport, buffer, "Object name exceeds %d characters at line %d", MAX_NAME-1, lineno); 305 | else if (!(highway->objects[n].name = strdup(c3))) 306 | return failconfig(h, airport, buffer, "Out of memory!"); 307 | continue; 308 | } 309 | } 310 | 311 | if (currentroute) /* Existing route */ 312 | { 313 | path_t *node = currentroute->pathlen ? currentroute->path + (currentroute->pathlen - 1) : NULL; 314 | 315 | if (!currentroute->highway && !strcasecmp(c1, "pause")) 316 | { 317 | int pausetime; 318 | if (!node) 319 | return failconfig(h, airport, buffer, "Route can't start with a \"pause\" command at line %d", lineno); 320 | else if (currentroute->pathlen>1 && currentroute->path[currentroute->pathlen-2].flags.backup && currentroute->path[currentroute->pathlen-2].pausetime) 321 | return failconfig(h, airport, buffer, "Can't pause both before and after a \"backup\" command at line %d", lineno); 322 | 323 | c1=strtok(NULL, sep); 324 | if (!c1 || !sscanf(c1, "%d%n", &pausetime, &eol1) || c1[eol1]) 325 | return failconfig(h, airport, buffer, "Expecting a pause time, found \"%s\" at line %d", N(c1), lineno); 326 | else if (pausetime <= 0 || pausetime >= 86400) 327 | return failconfig(h, airport, buffer, "Pause time should be between 1 and 86399 seconds at line %d", lineno); 328 | node->pausetime += pausetime; /* Multiple pauses stack */ 329 | 330 | if ((c1=strtok(NULL, sep))) 331 | { 332 | setcmd_t *setcmd; 333 | 334 | if (strcasecmp(c1, "set")) 335 | return failconfig(h, airport, buffer, "Expecting \"set\" or nothing, found \"%s\" at line %d", c1, lineno); 336 | else if ((setcmd = readsetcmd(airport, currentroute, node, buffer, lineno))) 337 | setcmd->flags.set2=1; 338 | else 339 | { 340 | fclose(h); 341 | clearconfig(airport); 342 | xplog(buffer); 343 | return 1; 344 | } 345 | } 346 | } 347 | else if (!currentroute->highway && !strcasecmp(c1, "at")) 348 | { 349 | int hour, minute, i=0; 350 | char daynames[7][10] = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" }; 351 | int dayvals[7] = { DAY_SUN, DAY_MON, DAY_TUE, DAY_WED, DAY_THU, DAY_FRI, DAY_SAT }; 352 | 353 | if (!node) 354 | return failconfig(h, airport, buffer, "Route can't start with an \"at\" command at line %d", lineno); 355 | else if (node->attime[0] != INVALID_AT) 356 | return failconfig(h, airport, buffer, "Waypoint can't have more than one \"at\" command at line %d", lineno); 357 | while ((c1=strtok(NULL, sep))) 358 | { 359 | if (!strcasecmp(c1, "on")) 360 | break; 361 | else if (i>=MAX_ATTIMES) 362 | return failconfig(h, airport, buffer, "Exceeded %d times-of-day at line %d", MAX_ATTIMES, lineno); 363 | else if (sscanf(c1, "%d:%d%n", &hour, &minute, &eol1)!=2 || c1[eol1] || hour<0 || hour>23 || minute<0 || minute>59) 364 | return failconfig(h, airport, buffer, "Expecting a time-of-day \"HH:MM\" or \"on\", found \"%s\" at line %d", c1, lineno); 365 | node->attime[i++] = hour*60+minute; 366 | } 367 | if (iattime[i] = INVALID_AT; /* Terminate */ 368 | 369 | while ((c1=strtok(NULL, sep))) 370 | { 371 | for (i=0; i<7; i++) 372 | if (!strncasecmp(c1, daynames[i], strlen(c1))) 373 | { 374 | node->atdays |= dayvals[i]; 375 | break; 376 | } 377 | if (i>=7) 378 | return failconfig(h, airport, buffer, "Expecting a day name, found \"%s\" at line %d", c1, lineno); 379 | } 380 | if (!node->atdays) node->atdays = DAY_ALL; 381 | } 382 | else if (!currentroute->highway && (!strcasecmp(c1, "when") || !strcasecmp(c1, "and"))) 383 | { 384 | whenref_t *whenref; 385 | extref_t *extref; 386 | 387 | if (!strcasecmp(c1, "when")) 388 | { 389 | if (!node) 390 | return failconfig(h, airport, buffer, "Route can't start with a \"when\" command at line %d", lineno); 391 | else if (node->whenrefs) 392 | return failconfig(h, airport, buffer, "Waypoint can't have more than one \"when\" command, consider using an \"and\" command at line %d", lineno); 393 | } 394 | else // "and" 395 | { 396 | if (!node) 397 | return failconfig(h, airport, buffer, "Route can't start with an \"and\" command at line %d", lineno); 398 | else if (!node->whenrefs) 399 | return failconfig(h, airport, buffer, "Waypoint can't have an \"and\" command without a preceding \"when\" command at line %d", lineno); 400 | } 401 | 402 | if (!(whenref = calloc(1, sizeof(whenref_t)))) 403 | return failconfig(h, airport, buffer, "Out of memory!"); 404 | whenref->next = node->whenrefs; 405 | node->whenrefs = whenref; 406 | 407 | if (!(c2=strtok(NULL, sep))) 408 | return failconfig(h, airport, buffer, "Expecting a DataRef name at line %d", lineno); 409 | 410 | if (!strncasecmp(c2, "var[", 4) || !strncasecmp(c2, REF_BASE, sizeof(REF_BASE)-1)) 411 | { 412 | c3 = c1; 413 | while ((*c3 = tolower(*c3))) c3++; 414 | return failconfig(h, airport, buffer, "Can't use a per-route DataRef in a \"%s\" command at line %d", c1, lineno); 415 | } 416 | 417 | if ((c3 = strchr(c2, '['))) 418 | { 419 | *(c3++) = '\0'; /* Strip index for lookup */ 420 | if (!sscanf(c3, "%d%n", &whenref->idx, &eol3) || eol3!=strlen(c3)-1 || c3[eol3]!=']') 421 | return failconfig(h, airport, buffer, "Expecting a DataRef index \"[n]\", found \"[%s\" at line %d", N(c3), lineno); 422 | else if (whenref->idx < 0) 423 | return failconfig(h, airport, buffer, "DataRef index cannot be negative at line %d", lineno); 424 | } 425 | else 426 | whenref->idx = -1; 427 | 428 | for (extref = airport->extrefs; extref && strcmp(c2, extref->name); extref=extref->next); 429 | if (!extref) 430 | { 431 | /* new */ 432 | if (!(extref = calloc(1, sizeof(extref_t))) || !(extref->name = strdup(c2))) 433 | return failconfig(h, airport, buffer, "Out of memory!"); 434 | /* Defer lookup to activation, after other plugins have Enabled */ 435 | extref->next = airport->extrefs; 436 | airport->extrefs = extref; 437 | } 438 | node->whenrefs->extref = extref; 439 | 440 | c1=strtok(NULL, sep); 441 | c2=strtok(NULL, sep); 442 | if (!c1 || !sscanf(c1, "%f%n", &whenref->from, &eol1) || c1[eol1] || 443 | !c2 || !sscanf(c2, "%f%n", &whenref->to, &eol2) || c2[eol2]) 444 | return failconfig(h, airport, buffer, "Expecting a range \"from to\", found \"%s %s\" at line %d", N(c1), N(c2), lineno); 445 | if (whenref->from > whenref->to) 446 | { 447 | float foo = whenref->from; 448 | whenref->from = whenref->to; 449 | whenref->to = foo; 450 | } 451 | } 452 | else if (!currentroute->highway && !strcasecmp(c1, "backup")) 453 | { 454 | if (!node) 455 | return failconfig(h, airport, buffer, "Route can't start with a \"backup\" command at line %d", lineno); 456 | else if (currentroute->pathlen>1 && currentroute->path[currentroute->pathlen-2].flags.backup) 457 | return failconfig(h, airport, buffer, "Can't backup from two waypoints in sequence at line %d", lineno); 458 | 459 | node->flags.backup=1; 460 | } 461 | else if (!currentroute->highway && !strcasecmp(c1, "reverse")) 462 | { 463 | int i; 464 | if (!node) 465 | return failconfig(h, airport, buffer, "Empty route at line %d", lineno); 466 | for (i=0; ipathlen; i++) 467 | if (currentroute->path[i].flags.backup) 468 | return failconfig(h, airport, buffer, "Can't use \"backup\" and \"reverse\" in the same route at line %d", lineno); 469 | node->flags.reverse=1; 470 | currentroute=NULL; /* reverse terminates */ 471 | } 472 | else if (!currentroute->highway && !strcasecmp(c1, "set")) 473 | { 474 | setcmd_t *setcmd; 475 | 476 | if (!node) 477 | return failconfig(h, airport, buffer, "Route can't start with a \"set\" command at line %d", lineno); 478 | else if ((setcmd = readsetcmd(airport, currentroute, node, buffer, lineno))) 479 | setcmd->flags.set1=1; 480 | else 481 | { 482 | fclose(h); 483 | clearconfig(airport); 484 | xplog(buffer); 485 | return 1; 486 | } 487 | } 488 | else /* waypoint */ 489 | { 490 | path_t *path, *last; 491 | float slat, slon, aa; 492 | 493 | if (!(path = realloc(currentroute->path, (1+currentroute->pathlen) * sizeof(path_t)))) 494 | return failconfig(h, airport, buffer, "Out of memory!"); 495 | currentroute->path = path; 496 | 497 | node = path + currentroute->pathlen; 498 | last = node - 1; 499 | memset(node, 0, sizeof(path_t)); 500 | node->attime[0] = INVALID_AT; 501 | if (!currentroute->highway) c2=strtok(NULL, sep); /* done above for highways */ 502 | if (!c1 || !sscanf(c1, "%f%n", &node->waypoint.lat, &eol1) || c1[eol1] || 503 | !c2 || !sscanf(c2, "%f%n", &node->waypoint.lon, &eol2) || c2[eol2]) 504 | return failconfig(h, airport, buffer, currentroute->pathlen ? (currentroute->highway ? "Expecting a waypoint \"lat lon\" or a blank line, found \"%s %s\" at line %d" : "Expecting a waypoint \"lat lon\", a command or a blank line, found \"%s %s\" at line %d") : "Expecting a waypoint \"lat lon\", found \"%s %s\" at line %d", N(c1), N(c2), lineno); 505 | else if (currentroute->pathlen && node->waypoint.lat==last->waypoint.lat && node->waypoint.lon==last->waypoint.lon) 506 | { 507 | /* Duplicate nodes screw up cornering and collision avoidance, but KJFK contains loads so we will just skip them for now */ 508 | // return failconfig(h, airport, buffer, "Duplicate waypoint at line %d", lineno); 509 | sprintf(buffer, "Note: Ignoring duplicate waypoint at line %d", lineno); 510 | xplog(buffer); 511 | continue; 512 | } 513 | bbox_add(¤troute->bbox, node->waypoint.lat, node->waypoint.lon); 514 | 515 | /* determine activation radius using Haversine formula. http://mathforum.org/library/drmath/view/51879.html */ 516 | bbox_add(&bounds, node->waypoint.lat, node->waypoint.lon); 517 | airport->tower.lat = (bounds.minlat + bounds.maxlat) / 2; 518 | airport->tower.lon = (bounds.minlon + bounds.maxlon) / 2; 519 | slat = sinf((bounds.maxlat-bounds.minlat) * (float) (M_PI/360)); 520 | slon = sinf((bounds.maxlon-bounds.minlon) * (float) (M_PI/360)); 521 | aa = slat*slat + cosf(bounds.minlat * (float) (M_PI/180)) * cosf(bounds.maxlat * (float) (M_PI/180)) * slon*slon; 522 | if ((airport->active_distance = RADIUS * atan2f(sqrtf(aa), sqrtf(1-aa))) > MAX_RADIUS) 523 | return failconfig(h, airport, buffer, "Waypoint too far away at line %d", lineno); 524 | 525 | if (++(currentroute->pathlen) > maxpathlen) maxpathlen = currentroute->pathlen; 526 | } 527 | if ((c1=strtok(NULL, sep))) 528 | return failconfig(h, airport, buffer, "Extraneous input \"%s\" at line %d", c1, lineno); 529 | } 530 | 531 | else if (currenttrain) /* Existing train */ 532 | { 533 | int n; /* Train length */ 534 | 535 | for (n=0; nobjects[n].name; n++); 536 | if (n>=MAX_TRAIN) 537 | return failconfig(h, airport, buffer, "Exceeded %d objects in a train at line %d", MAX_TRAIN, lineno); 538 | 539 | c2=strtok(NULL, sep); 540 | c3=strtok(NULL, sep); 541 | if (!c1 || !sscanf(c1, "%f%n", ¤ttrain->objects[n].lag, &eol1) || c1[eol1] || 542 | !c2 || !sscanf(c2, "%f%n", ¤ttrain->objects[n].offset, &eol2) || c2[eol2] || 543 | !c3 || !sscanf(c3, "%f%n", ¤ttrain->objects[n].heading, &eol3) || c3[eol3]) 544 | return failconfig(h, airport, buffer, n ? "Expecting a car \"lag offset heading\" or a blank line, found \"%s %s %s\" at line %d" : "Expecting a car \"lag offset heading\", found \"%s %s %s\" at line %d", N(c1), N(c2), N(c3), lineno); 545 | else if (!n && currenttrain->objects[n].lag < 0) 546 | return failconfig(h, airport, buffer, "Train car lag must be greater or equal to 0 at line %d", lineno); 547 | else if (n && currenttrain->objects[n].lag < currenttrain->objects[n-1].lag) 548 | return failconfig(h, airport, buffer, "Train car lag must be greater than previous car's lag at line %d", lineno); 549 | 550 | for (c1 = c3+strlen(c3)+1; isspace(*c1); c1++); /* ltrim */ 551 | for (c2 = c1+strlen(c1)-1; c2>=c1 && isspace(*c2); *(c2--) = '\0'); /* rtrim */ 552 | if (!*c1) 553 | return failconfig(h, airport, buffer, "Expecting an object name at line %d", lineno); 554 | else if (*c1 == '.' || *c1 == '/' || *c1 == '\\') 555 | return failconfig(h, airport, buffer, "Object name cannot start with a \"%c\" at line %d", *c1, lineno); 556 | else if (strlen(c1) >= MAX_NAME) 557 | return failconfig(h, airport, buffer, "Object name exceeds %d characters at line %d", MAX_NAME-1, lineno); 558 | else if (!(currenttrain->objects[n].name = strdup(c1))) 559 | return failconfig(h, airport, buffer, "Out of memory!"); 560 | } 561 | 562 | else if (!strcasecmp(c1, "route")) /* New route */ 563 | { 564 | if (!(currentroute = calloc(1, sizeof(route_t))) || !(currentroute->varrefs = calloc(MAX_VAR, sizeof(userref_t)))) 565 | return failconfig(h, airport, buffer, "Out of memory!"); 566 | 567 | currentroute->next = airport->routes; 568 | airport->routes = currentroute; 569 | if (!airport->firstroute) airport->firstroute = currentroute; /* Save for DRE */ 570 | 571 | /* Initialise the route */ 572 | currentroute->lineno = lineno; 573 | bbox_init(¤troute->bbox); 574 | currentroute->direction = 1; 575 | if (count<16) 576 | { 577 | currentroute->drawcolor = colors[count++]; 578 | } 579 | else 580 | { 581 | int r = rand(); /* Use the lower 15bits, which is all you get on Windows */ 582 | currentroute->drawcolor.r = ((float) (0x001 + (r & 0x001F))) / 0x0020; 583 | currentroute->drawcolor.g = ((float) (0x020 + (r & 0x03E0))) / 0x0400; 584 | currentroute->drawcolor.b = ((float) (0x400 + (r & 0x7C00))) / 0x8000; 585 | } 586 | 587 | c1=strtok(NULL, sep); 588 | c2=strtok(NULL, sep); 589 | c3=strtok(NULL, sep); 590 | if (!c1 || !sscanf(c1, "%f%n", ¤troute->speed, &eol1) || c1[eol1] || 591 | !c2 || !sscanf(c2, "%f%n", ¤troute->object.offset, &eol2) || c2[eol2] || 592 | !c3 || !sscanf(c3, "%f%n", ¤troute->object.heading, &eol3) || c3[eol3]) 593 | return failconfig(h, airport, buffer, "Expecting a route \"speed offset heading\", found \"%s %s %s\" at line %d", N(c1), N(c2), N(c3), lineno); 594 | else if (currentroute->speed <= 0) 595 | return failconfig(h, airport, buffer, "Route speed must be greater than 0 at line %d", lineno); 596 | 597 | for (c1 = c3+strlen(c3)+1; isspace(*c1); c1++); /* ltrim */ 598 | for (c2 = c1+strlen(c1)-1; c2>=c1 && isspace(*c2); *(c2--) = '\0'); /* rtrim */ 599 | if (!*c1) 600 | return failconfig(h, airport, buffer, "Expecting an object name at line %d", lineno); 601 | else if (*c1 == '.' || *c1 == '/' || *c1 == '\\') 602 | return failconfig(h, airport, buffer, "Object name cannot start with a \"%c\" at line %d", *c1, lineno); 603 | else if (strlen(c1) >= MAX_NAME) 604 | return failconfig(h, airport, buffer, "Object name exceeds %d characters at line %d", MAX_NAME-1, lineno); 605 | else if (!(currentroute->object.name = strdup(c1))) 606 | return failconfig(h, airport, buffer, "Out of memory!"); 607 | 608 | currentroute->speed *= (float) (1000.0 / (60*60)); /* convert km/h to m/s */ 609 | } 610 | else if (!strcasecmp(c1, "train")) /* New train */ 611 | { 612 | for (c1 = c1+strlen(c1)+1; isspace(*c1); c1++); /* ltrim */ 613 | for (c2 = c1+strlen(c1)-1; c2>=c1 && isspace(*c2); *(c2--) = '\0'); /* rtrim */ 614 | if (!*c1) 615 | return failconfig(h, airport, buffer, "Expecting a train name at line %d", lineno); 616 | else if (*c1 == '.' || *c1 == '/' || *c1 == '\\') 617 | return failconfig(h, airport, buffer, "Train name cannot start with a \"%c\" at line %d", *c1, lineno); 618 | else if (strlen(c1) >= MAX_NAME) 619 | return failconfig(h, airport, buffer, "Train name exceeds %d characters at line %d", MAX_NAME-1, lineno); 620 | for (currenttrain=airport->trains; currenttrain; currenttrain=currenttrain->next) 621 | if (!strcmp(currenttrain->name, c1)) 622 | return failconfig(h, airport, buffer, "Can't re-define train \"%s\" at line %d", c1, lineno); 623 | 624 | if (!(currenttrain = calloc(1, sizeof(train_t))) || !(currenttrain->name = strdup(c1))) 625 | return failconfig(h, airport, buffer, "Out of memory!"); 626 | 627 | currenttrain->next = airport->trains; 628 | airport->trains = currenttrain; 629 | } 630 | else if (!strcasecmp(c1, "highway")) /* New highway */ 631 | { 632 | highway_t *highway; 633 | 634 | if (!(currentroute = calloc(1, sizeof(route_t))) || !(highway = calloc(1, sizeof(highway_t)))) 635 | return failconfig(h, airport, buffer, "Out of memory!"); 636 | 637 | c1=strtok(NULL, sep); 638 | c2=strtok(NULL, sep); 639 | if (!c1 || !sscanf(c1, "%f%n", ¤troute->speed, &eol1) || c1[eol1] || 640 | !c2 || !sscanf(c2, "%f%n", &highway->spacing, &eol2) || c2[eol2]) 641 | return failconfig(h, airport, buffer, "Expecting a highway \"speed spacing\", found \"%s %s\" at line %d", N(c1), N(c2), lineno); 642 | else if (currentroute->speed <= 0) 643 | return failconfig(h, airport, buffer, "Highway speed must be greater than 0 at line %d", lineno); 644 | else if (highway->spacing <= 0) 645 | return failconfig(h, airport, buffer, "Highway spacing must be greater than 0 at line %d", lineno); 646 | if ((c3=strtok(NULL, sep))) return failconfig(h, airport, buffer, "Extraneous input \"%s\" at line %d", c3, lineno); 647 | 648 | currentroute->next = airport->routes; 649 | airport->routes = currentroute; 650 | if (!airport->firstroute) airport->firstroute = currentroute; /* Save for DRE */ 651 | 652 | /* Initialise the route */ 653 | currentroute->lineno = lineno; 654 | bbox_init(¤troute->bbox); 655 | currentroute->direction = 1; 656 | if (count<16) 657 | { 658 | currentroute->drawcolor = colors[count++]; 659 | } 660 | else 661 | { 662 | int r = rand(); /* Use the lower 15bits, which is all you get on Windows */ 663 | currentroute->drawcolor.r = ((float) (0x001 + (r & 0x001F))) / 0x0020; 664 | currentroute->drawcolor.g = ((float) (0x020 + (r & 0x03E0))) / 0x0400; 665 | currentroute->drawcolor.b = ((float) (0x400 + (r & 0x7C00))) / 0x8000; 666 | } 667 | currentroute->speed *= (float) (1000.0 / (60*60)); /* convert km/h to m/s */ 668 | currentroute->highway = highway; 669 | } 670 | else if (!strcasecmp(c1, "water")) 671 | { 672 | airport->reflections = -1; 673 | water = -1; 674 | if ((c1=strtok(NULL, sep))) return failconfig(h, airport, buffer, "Extraneous input \"%s\" at line %d", c1, lineno); 675 | } 676 | else if (!strcasecmp(c1, "debug")) 677 | { 678 | airport->drawroutes = -1; 679 | if ((c1=strtok(NULL, sep))) return failconfig(h, airport, buffer, "Extraneous input \"%s\" at line %d", c1, lineno); 680 | } 681 | else if (!doneprologue) /* Used to be airport header ICAO lat lon */ 682 | { 683 | /* Silently skip input if in valid old format */ 684 | c2=strtok(NULL, sep); 685 | c3=strtok(NULL, sep); 686 | if (strlen(c1)!=4 || 687 | !c2 || !sscanf(c2, "%lf%n", &airport->tower.lat, &eol2) || c2[eol2] || 688 | !c3 || !sscanf(c3, "%lf%n", &airport->tower.lon, &eol3) || c3[eol3] || 689 | strtok(NULL, sep)) 690 | return failconfig(h, airport, buffer, "Expecting a route or train, found \"%s\" at line %d", c1, lineno); 691 | } 692 | else 693 | { 694 | return failconfig(h, airport, buffer, "Expecting a route or train, found \"%s\" at line %d", c1, lineno); 695 | } 696 | doneprologue = -1; 697 | } 698 | 699 | /* Turn train routes into multiple individual routes */ 700 | currentroute = airport->routes; 701 | while (currentroute) 702 | { 703 | if (!(currentroute = expandtrain(airport, currentroute))) 704 | return failconfig(h, airport, buffer, "Out of memory!"); 705 | currentroute = currentroute->next; 706 | } 707 | 708 | /* Register user's DataRefs. 709 | * Have to do this early rather than during activate() because objects in DSF are loaded while we're still inactive */ 710 | userref = airport->userrefs; 711 | while (userref) 712 | { 713 | if (XPLMFindDataRef(userref->name)) 714 | return failconfig(h, airport, buffer, "Another plugin has already registered custom DataRef \"%s\"", userref->name); 715 | userref->ref = XPLMRegisterDataAccessor(userref->name, xplmType_Float, 0, 716 | NULL, NULL, userrefcallback, NULL, NULL, NULL, 717 | NULL, NULL, NULL, NULL, NULL, NULL, userref, NULL); 718 | userref = userref->next; 719 | } 720 | 721 | if (!airport->routes) 722 | return failconfig(h, airport, buffer, "No routes defined!"); 723 | 724 | /* Finishing up */ 725 | #ifdef DEBUG 726 | sprintf(buffer, "Tower=%.9lf,%.9lf r=%d", airport->tower.lat, airport->tower.lon, (int) airport->active_distance); 727 | xplog(buffer); 728 | #endif 729 | airport->state = inactive; 730 | airport->active_distance += (water ? ACTIVE_WATER : ACTIVE_DISTANCE); 731 | 732 | if (airport->drawroutes) 733 | { 734 | /* build route label lookup table for speed */ 735 | int i; 736 | 737 | if (!(labeltbl = malloc(maxpathlen*5))) 738 | return failconfig(h, airport, buffer, "Out of memory!"); 739 | for (i=0; i= MAX_NAME) 767 | { 768 | sprintf(buffer, "DataRef name exceeds %d characters at line %d", MAX_NAME-1, lineno); 769 | return 0; 770 | } 771 | 772 | if ((!strncasecmp(c1, "var[", 4) || !strncasecmp(c1, REF_VAR "[", sizeof(REF_VAR "[")-1)) && c1[strlen(c1)-1]==']') 773 | { 774 | /* Standard DataRef = route-specific */ 775 | int i; 776 | c1 = strchr(c1, '[') + 1; 777 | if (!sscanf(c1, "%d%n", &i, &eol1) || eol1!=strlen(c1)-1) 778 | { 779 | sprintf(buffer, "Expecting DataRef name \"var[n]\", found \"%s\" at line %d", N(c1), lineno); 780 | return 0; 781 | } 782 | else if (i<0 || i>=MAX_VAR) 783 | { 784 | sprintf(buffer, "var DataRef index outside the range 0 to %d at line %d", MAX_VAR-1, lineno); 785 | return 0; 786 | } 787 | userref = *currentroute->varrefs + i; 788 | } 789 | else 790 | { 791 | /* User DataRef = global */ 792 | for(userref = airport->userrefs; userref && strcmp(c1, userref->name); userref=userref->next); 793 | if (!userref) 794 | { 795 | /* new */ 796 | if (!strncasecmp(c1, "sim/", 4)) 797 | { 798 | sprintf(buffer, "Custom DataRef name can't start with \"sim/\" at line %d", lineno); 799 | return 0; 800 | } 801 | else if (!strncasecmp(c1, "marginal/", 9)) 802 | { 803 | sprintf(buffer, "Custom DataRef name can't start with \"marginal/\", invent your own name! at line %d", lineno); 804 | return 0; 805 | } 806 | else if (!(userref = calloc(1, sizeof(userref_t))) || !(userref->name = malloc(strlen(c1)+1))) 807 | { 808 | strcpy(buffer, "Out of memory!"); 809 | return 0; 810 | } 811 | strcpy(userref->name, c1); 812 | userref->next = airport->userrefs; 813 | airport->userrefs = userref; 814 | } 815 | } 816 | 817 | if (!(setcmd = calloc(1, sizeof(setcmd_t)))) 818 | { 819 | strcpy(buffer, "Out of memory!"); 820 | return 0; 821 | } 822 | setcmd->next = node->setcmds; 823 | node->setcmds = setcmd; 824 | 825 | setcmd->userref = userref; 826 | c1=strtok(NULL, sep); 827 | if (c1 && !strcasecmp(c1, "rise")) 828 | setcmd->flags.slope = rising; 829 | else if (c1 && !strcasecmp(c1, "fall")) 830 | setcmd->flags.slope = falling; 831 | else 832 | { 833 | sprintf(buffer, "Expecting a slope \"rise\" or \"fall\", found \"%s\" at line %d", N(c1), lineno); 834 | return 0; 835 | } 836 | 837 | c1=strtok(NULL, sep); 838 | if (c1 && !strcasecmp(c1, "linear")) 839 | setcmd->flags.curve = linear; 840 | else if (c1 && !strcasecmp(c1, "sine")) 841 | setcmd->flags.curve = sine; 842 | else 843 | { 844 | sprintf(buffer, "Expecting a curve \"linear\" or \"sine\", found \"%s\" at line %d", N(c1), lineno); 845 | return 0; 846 | } 847 | 848 | c1=strtok(NULL, sep); 849 | if (!c1 || !sscanf(c1, "%f%n", &setcmd->duration, &eol1) || c1[eol1]) 850 | { 851 | sprintf(buffer, "Expecting a duration, found \"%s\" at line %d", N(c1), lineno); 852 | return 0; 853 | } 854 | 855 | return setcmd; 856 | } 857 | 858 | 859 | /* Check if this route names a train; if so replicate into multiple routes, and return pointer to last */ 860 | static route_t *expandtrain(airport_t *airport, route_t *currentroute) 861 | { 862 | int i; 863 | train_t *train = airport->trains; 864 | route_t *route = currentroute; 865 | 866 | assert (currentroute); 867 | if (!currentroute) return NULL; 868 | if (currentroute->highway) return currentroute; /* Highways don't have an object name */ 869 | 870 | while (train) 871 | { 872 | if (!strcmp(currentroute->object.name, train->name)) break; 873 | train = train->next; 874 | } 875 | if (!train) return currentroute; 876 | 877 | /* It's a train */ 878 | free(route->object.name); 879 | for (i=0; iobjects[i].name) break; 882 | if (i) 883 | { 884 | /* Duplicate original route */ 885 | route_t *newroute; 886 | if (!(newroute=malloc(sizeof(route_t)))) return NULL; /* OOM */ 887 | memcpy(newroute, currentroute, sizeof(route_t)); 888 | newroute->next = route->next; 889 | route->next = newroute; 890 | route = newroute; 891 | route->parent = currentroute; 892 | } 893 | /* Assign carriage to its route */ 894 | if (!(route->object.name = strdup(train->objects[i].name))) return NULL; /* OOM */ 895 | route->object.lag = train->objects[i].lag / route->speed; /* Convert distance to time lag */ 896 | route->object.offset = train->objects[i].offset; 897 | route->object.heading = train->objects[i].heading; 898 | route->next_time = -route->object.lag; /* Force recalc on first draw */ 899 | } 900 | 901 | return route; 902 | } 903 | --------------------------------------------------------------------------------