+void R_Shadow_FreeWorldLight(worldlight_t *light)
+{
+ worldlight_t **lightpointer;
+ for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
+ if (*lightpointer != light)
+ Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
+ *lightpointer = light->next;
+ if (light->cubemapname)
+ Mem_Free(light->cubemapname);
+ if (light->shadowvolume)
+ Mod_ShadowMesh_Free(light->shadowvolume);
+ if (light->surfaces)
+ Mem_Free(light->surfaces);
+ if (light->leafs)
+ Mem_Free(light->leafs);
+ Mem_Free(light);
+}
+
+void R_Shadow_ClearWorldLights(void)
+{
+ while (r_shadow_worldlightchain)
+ R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
+ r_shadow_selectedlight = NULL;
+}
+
+void R_Shadow_SelectLight(worldlight_t *light)
+{
+ if (r_shadow_selectedlight)
+ r_shadow_selectedlight->selected = false;
+ r_shadow_selectedlight = light;
+ if (r_shadow_selectedlight)
+ r_shadow_selectedlight->selected = true;
+}
+
+void R_Shadow_FreeSelectedWorldLight(void)
+{
+ if (r_shadow_selectedlight)
+ {
+ R_Shadow_FreeWorldLight(r_shadow_selectedlight);
+ r_shadow_selectedlight = NULL;
+ }
+}
+
+void R_Shadow_SelectLightInView(void)
+{
+ float bestrating, rating, temp[3], dist;
+ worldlight_t *best, *light;
+ best = NULL;
+ bestrating = 1e30;
+ for (light = r_shadow_worldlightchain;light;light = light->next)
+ {
+ VectorSubtract(light->origin, r_refdef.vieworg, temp);
+ dist = sqrt(DotProduct(temp, temp));
+ if (DotProduct(temp, vpn) >= 0.97 * dist && bestrating > dist && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
+ {
+ bestrating = dist;
+ best = light;
+ }
+ }
+ R_Shadow_SelectLight(best);
+}
+
+void R_Shadow_LoadWorldLights(const char *mapname)
+{
+ int n, a, style;
+ char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
+ float origin[3], radius, color[3];
+ COM_StripExtension(mapname, name);
+ strcat(name, ".rtlights");
+ lightsstring = COM_LoadFile(name, false);
+ if (lightsstring)
+ {
+ s = lightsstring;
+ n = 0;
+ while (*s)
+ {
+ t = s;
+ while (*s && *s != '\n')
+ s++;
+ if (!*s)
+ break;
+ *s = 0;
+ a = sscanf(t, "%f %f %f %f %f %f %f %d %s", &origin[0], &origin[1], &origin[2], &radius, &color[0], &color[1], &color[2], &style, &cubemapname);
+ if (a < 9)
+ cubemapname[0] = 0;
+ *s = '\n';
+ if (a < 8)
+ {
+ Con_Printf("found %d parameters on line %i, should be 8 or 9 parameters (origin[0] origin[1] origin[2] radius color[0] color[1] color[2] style cubemapname)\n", a, n + 1);
+ break;
+ }
+ R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname);
+ s++;
+ n++;
+ }
+ if (*s)
+ Con_Printf("invalid rtlights file \"%s\"\n", name);
+ Mem_Free(lightsstring);
+ }
+}
+
+void R_Shadow_SaveWorldLights(const char *mapname)
+{
+ worldlight_t *light;
+ int bufchars, bufmaxchars;
+ char *buf, *oldbuf;
+ char name[MAX_QPATH];
+ char line[1024];
+ if (!r_shadow_worldlightchain)
+ return;
+ COM_StripExtension(mapname, name);
+ strcat(name, ".rtlights");
+ bufchars = bufmaxchars = 0;
+ buf = NULL;
+ for (light = r_shadow_worldlightchain;light;light = light->next)
+ {
+ sprintf(line, "%g %g %g %g %g %g %g %d %s\n", light->origin[0], light->origin[1], light->origin[2], light->lightradius, light->light[0], light->light[1], light->light[2], light->style, light->cubemapname ? light->cubemapname : "");
+ if (bufchars + strlen(line) > bufmaxchars)
+ {
+ bufmaxchars = bufchars + strlen(line) + 2048;
+ oldbuf = buf;
+ buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
+ if (oldbuf)
+ {
+ if (bufchars)
+ memcpy(buf, oldbuf, bufchars);
+ Mem_Free(oldbuf);
+ }
+ }
+ if (strlen(line))
+ {
+ memcpy(buf + bufchars, line, strlen(line));
+ bufchars += strlen(line);
+ }
+ }
+ if (bufchars)
+ COM_WriteFile(name, buf, bufchars);
+ if (buf)
+ Mem_Free(buf);
+}
+
+void R_Shadow_SetCursorLocationForView(void)
+{
+ vec_t dist, push, frac;
+ vec3_t dest, endpos, normal;
+ VectorMA(r_refdef.vieworg, r_editlights_cursordistance.value, vpn, dest);
+ frac = CL_TraceLine(r_refdef.vieworg, dest, endpos, normal, 0, true, NULL);
+ if (frac < 1)
+ {
+ dist = frac * r_editlights_cursordistance.value;
+ push = r_editlights_cursorpushback.value;
+ if (push > dist)
+ push = dist;
+ push = -push;
+ VectorMA(endpos, push, vpn, endpos);
+ VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
+ }
+ r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
+ r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
+ r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
+}
+
+extern void R_DrawCrosshairSprite(rtexture_t *texture, vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca);
+void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
+{
+ cachepic_t *pic;
+ pic = Draw_CachePic("gfx/crosshair1.tga");
+ if (pic)
+ R_DrawCrosshairSprite(pic->tex, r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 1);
+}
+
+void R_Shadow_DrawCursor(void)
+{
+ R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
+}
+
+void R_Shadow_UpdateLightingMode(void)
+{
+ r_shadow_lightingmode = 0;
+ if (r_shadow_realtime.integer)
+ {
+ if (r_shadow_worldlightchain)
+ r_shadow_lightingmode = 2;
+ else
+ r_shadow_lightingmode = 1;
+ }
+}
+
+void R_Shadow_UpdateWorldLightSelection(void)
+{
+ if (r_editlights.integer)
+ {
+ R_Shadow_SelectLightInView();
+ R_Shadow_SetCursorLocationForView();
+ R_Shadow_DrawCursor();
+ }
+ else
+ R_Shadow_SelectLight(NULL);
+}
+
+void R_Shadow_EditLights_Clear_f(void)
+{
+ R_Shadow_ClearWorldLights();
+}
+
+void R_Shadow_EditLights_Reload_f(void)
+{
+ if (cl.worldmodel)
+ {
+ R_Shadow_ClearWorldLights();
+ R_Shadow_LoadWorldLights(cl.worldmodel->name);
+ }
+}
+
+void R_Shadow_EditLights_Save_f(void)
+{
+ if (cl.worldmodel)
+ R_Shadow_SaveWorldLights(cl.worldmodel->name);
+}
+
+void R_Shadow_EditLights_Spawn_f(void)
+{
+ vec3_t origin, color;
+ vec_t radius;
+ int style;
+ const char *cubemapname;
+ if (!r_editlights.integer)
+ {
+ Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
+ return;
+ }
+ if (Cmd_Argc() <= 7)
+ {
+ radius = 200;
+ color[0] = color[1] = color[2] = 1;
+ style = 0;
+ cubemapname = NULL;
+ if (Cmd_Argc() >= 2)
+ {
+ radius = atof(Cmd_Argv(1));
+ if (Cmd_Argc() >= 3)
+ {
+ color[0] = atof(Cmd_Argv(2));
+ color[1] = color[0];
+ color[2] = color[0];
+ if (Cmd_Argc() >= 5)
+ {
+ color[1] = atof(Cmd_Argv(3));
+ color[2] = atof(Cmd_Argv(4));
+ if (Cmd_Argc() >= 6)
+ {
+ style = atoi(Cmd_Argv(5));
+ if (Cmd_Argc() >= 7)
+ cubemapname = Cmd_Argv(6);
+ }
+ }
+ }
+ }
+ if (cubemapname && !cubemapname[0])
+ cubemapname = NULL;
+ if (radius >= 16 && color[0] >= 0 && color[1] >= 0 && color[2] >= 0 && style >= 0 && style < 256 && (color[0] >= 0.1 || color[1] >= 0.1 || color[2] >= 0.1))
+ {
+ VectorCopy(r_editlights_cursorlocation, origin);
+ R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname);
+ return;
+ }
+ }
+ Con_Printf("usage: r_editlights_spawn radius red green blue [style [cubemap]]\n");
+}
+
+void R_Shadow_EditLights_Edit_f(void)
+{
+ vec3_t origin, color;
+ vec_t radius;
+ int style;
+ const char *cubemapname;
+ if (!r_editlights.integer)
+ {
+ Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
+ return;
+ }
+ if (!r_shadow_selectedlight)
+ {
+ Con_Printf("No selected light.\n");
+ return;
+ }
+ if (Cmd_Argc() <= 7)
+ {
+ radius = 200;
+ color[0] = color[1] = color[2] = 1;
+ style = 0;
+ cubemapname = NULL;
+ if (Cmd_Argc() >= 2)
+ {
+ radius = atof(Cmd_Argv(1));
+ if (Cmd_Argc() >= 3)
+ {
+ color[0] = atof(Cmd_Argv(2));
+ color[1] = color[0];
+ color[2] = color[0];
+ if (Cmd_Argc() >= 5)
+ {
+ color[1] = atof(Cmd_Argv(3));
+ color[2] = atof(Cmd_Argv(4));
+ if (Cmd_Argc() >= 6)
+ {
+ style = atoi(Cmd_Argv(5));
+ if (Cmd_Argc() >= 7)
+ cubemapname = Cmd_Argv(6);
+ }
+ }
+ }
+ }
+ if (cubemapname && !cubemapname[0])
+ cubemapname = NULL;
+ if (radius >= 16 && color[0] >= 0 && color[1] >= 0 && color[2] >= 0 && style >= 0 && style < 256 && (color[0] >= 0.1 || color[1] >= 0.1 || color[2] >= 0.1))
+ {
+ VectorCopy(r_shadow_selectedlight->origin, origin);
+ R_Shadow_FreeWorldLight(r_shadow_selectedlight);
+ r_shadow_selectedlight = NULL;
+ R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname);
+ return;
+ }
+ }
+ Con_Printf("usage: r_editlights_edit radius red green blue [style [cubemap]]\n");
+}
+
+void R_Shadow_EditLights_Remove_f(void)
+{
+ if (!r_editlights.integer)
+ {
+ Con_Printf("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
+ return;
+ }
+ if (!r_shadow_selectedlight)
+ {
+ Con_Printf("No selected light.\n");
+ return;
+ }
+ R_Shadow_FreeSelectedWorldLight();
+}
+
+void R_Shadow_EditLights_Init(void)
+{
+ Cvar_RegisterVariable(&r_editlights);
+ Cvar_RegisterVariable(&r_editlights_cursordistance);
+ Cvar_RegisterVariable(&r_editlights_cursorpushback);
+ Cvar_RegisterVariable(&r_editlights_cursorpushoff);
+ Cvar_RegisterVariable(&r_editlights_cursorgrid);
+ Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
+ Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
+ Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
+ Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
+ Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
+ Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
+}