using System.Collections.Generic; namespace Harmony.VPHeadlightPatches { public class Headlights { public float durability; public int quality; public int side; public Headlights() { } public Headlights(float _durability, int _quality, int _side) { durability = _durability; quality = _quality; side = _side; } } [HarmonyPatch(typeof(VPHeadlight), "Update")] public class UpdatePatch { public static bool Prefix(VPHeadlight __instance, float _dt) { if (__instance.IsBroken()) { __instance.SetOn(false); return false; } int numLights = 0; float durability = 0f; int side = 0; float quality = 0; int index = 0; List headlights = new List(); var entityVehicle = __instance.vehicle.entity as EntityVehicleRebirth; if (entityVehicle == null) return false; foreach (ItemValue item in entityVehicle.itemValues) { if (item.type == ItemValue.None.type || (item.MaxUseTimes - item.UseTimes <= 0) || item.ItemClass == null) { index++; continue; } //Log.Out("entityVehicle.itemValues[" + index + "]: " + entityVehicle.itemValues[index].ItemClass.Name); if (item.ItemClass.GetItemName() == "resourceHeadlight") { durability += item.PercentUsesLeft; quality += item.Quality; numLights++; side = index % 2; headlights.Add(new Headlights() { quality = item.Quality, durability = item.PercentUsesLeft, side = side }); } index++; } if (numLights == 0) { __instance.SetOn(false); return false; } if (__instance.lights != null && entityVehicle.itemValues != null && entityVehicle.itemValues.Length >= __instance.lights.Count) { //Log.Out("VPHeadlightPatches-Update __instance.lights.Count: " + __instance.lights.Count); for (int i = 0; i < __instance.lights.Count; i++) { Light light = __instance.lights[i]; //Log.Out("VPHeadlightPatches-Update light.name[" + i + "]: " + light.name); ItemValue item = entityVehicle.itemValues[i]; float brightness = 0; float range = 0; if (__instance.lights.Count == 1) { quality = headlights[i].quality; float num = 0.2f + (0.3f * __instance.vehicle.EffectLightIntensity * headlights[i].durability); float num2 = 0.5f * quality / 6; range = 35f + 0.5f * (50f * num); if (numLights == 1) { num *= 0.85f; } brightness = 0.25f + num + num2; } else { side = -1; if (light.name.ToLower().Contains("left")) { side = 0; } else if (light.name.ToLower().Contains("right")) { side = 1; } //Log.Out("VPHeadlightPatches-Update side: " + side); for (int j = 0; j < headlights.Count; j++) { //Log.Out("VPHeadlightPatches-Update headlights[" + j + "].quality: " + headlights[j].quality); //Log.Out("VPHeadlightPatches-Update headlights[" + j + "].durability: " + headlights[j].durability); //Log.Out("VPHeadlightPatches-Update headlights[" + j + "].side: " + headlights[j].side); if (headlights[j].side == side || (side == -1 && headlights.Count > 0)) { quality = headlights[j].quality; float num = 0.2f + (0.3f * __instance.vehicle.EffectLightIntensity * headlights[j].durability); float num2 = 0.5f * quality / 6; range = 35f + 0.5f * (50f * num); if (numLights == 1) { num *= 0.85f; } brightness = 0.25f + num + num2; } } } light.intensity = brightness; light.range = range; } } return false; } } }