+ int i;
+ beam_t *b;
+ vec3_t dist, org;
+ float d;
+ entity_t *ent;
+ float yaw, pitch;
+ float forward;
+
+ for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
+ {
+ if (!b->model || b->endtime < cl.time)
+ continue;
+
+ // if coming from the player, update the start position
+ //if (b->entity == cl.viewentity)
+ // VectorCopy (cl_entities[cl.viewentity].render.origin, b->start);
+ if (cl_beams_relative.integer && b->entity && cl_entities[b->entity].state_current.active && b->relativestartvalid)
+ {
+ entity_state_t *p = &cl_entities[b->entity].state_previous;
+ //entity_state_t *c = &cl_entities[b->entity].state_current;
+ entity_render_t *r = &cl_entities[b->entity].render;
+ matrix4x4_t matrix, imatrix;
+ if (b->relativestartvalid == 2)
+ {
+ // not really valid yet, we need to get the orientation now
+ // (ParseBeam flagged this because it is received before
+ // entities are received, by now they have been received)
+ // note: because players create lightning in their think
+ // function (which occurs before movement), they actually
+ // have some lag in it's location, so compare to the
+ // previous player state, not the latest
+ if (b->entity == cl.viewentity)
+ Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
+ else
+ Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, p->angles[0], p->angles[1], p->angles[2], 1);
+ Matrix4x4_Invert_Simple(&imatrix, &matrix);
+ Matrix4x4_Transform(&imatrix, b->start, b->relativestart);
+ Matrix4x4_Transform(&imatrix, b->end, b->relativeend);
+ b->relativestartvalid = 1;
+ }
+ else
+ {
+ if (b->entity == cl.viewentity)
+ Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
+ else
+ Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, r->angles[0], r->angles[1], r->angles[2], 1);
+ Matrix4x4_Transform(&matrix, b->relativestart, b->start);
+ Matrix4x4_Transform(&matrix, b->relativeend, b->end);
+ }
+ }
+
+ if (b->lightning)
+ {
+ if (cl_beams_lightatend.integer)
+ CL_AllocDlight (NULL, b->end, 200, 0.3, 0.7, 1, 0, 0);
+ if (cl_beams_polygons.integer)
+ continue;
+ }
+
+ // calculate pitch and yaw
+ VectorSubtract (b->end, b->start, dist);
+
+ if (dist[1] == 0 && dist[0] == 0)
+ {
+ yaw = 0;
+ if (dist[2] > 0)
+ pitch = 90;
+ else
+ pitch = 270;
+ }
+ else
+ {
+ yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
+ if (yaw < 0)
+ yaw += 360;
+
+ forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
+ pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
+ if (pitch < 0)
+ pitch += 360;
+ }
+
+ // add new entities for the lightning
+ VectorCopy (b->start, org);
+ d = VectorNormalizeLength(dist);
+ while (d > 0)
+ {
+ ent = CL_NewTempEntity ();
+ if (!ent)
+ return;
+ //VectorCopy (org, ent->render.origin);
+ ent->render.model = b->model;
+ ent->render.effects = EF_FULLBRIGHT;
+ //ent->render.angles[0] = pitch;
+ //ent->render.angles[1] = yaw;
+ //ent->render.angles[2] = rand()%360;
+ Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, org[0], org[1], org[2], -pitch, yaw, lhrandom(0, 360), 1);
+ Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
+ CL_BoundingBoxForEntity(&ent->render);
+ VectorMA(org, 30, dist, org);
+ d -= 30;
+ }
+ }