Could someone write a plugin for the radius of the flashlight and the color of the flashlight beam?
+ Random color option
I found a SP script maybe it helps to write it in sm.
It is about the game HL2: DM
Greets Painkiller
Code:
# ../flashlight/flashlight.py
# Python
from colorsys import hsv_to_rgb
# Source.Python
from colors import Color
from commands import CommandReturn
from commands.client import ClientCommand
from entities.entity import Entity
from entities.helpers import index_from_pointer
from entities.hooks import EntityCondition, EntityPreHook
from listeners import OnNetworkedEntityDeleted
from mathlib import NULL_VECTOR, NULL_QANGLE
from players.entity import Player
# Color of the flashlight.
FLASHLIGHT_COLOR = Color(255, 255, 255)
# Radius of the flashlight.
FLASHLIGHT_RADIUS = 53
# Brightness of the flashlight.
FLASHLIGHT_BRIGHTNESS = 10
# Block the weapon inspect animation?
BLOCK_ANIMATION = True
# Should the color of the flashlight be based on the player's steamid?
COLOR_FROM_STEAMID = True
# List of vibrant colors.
pretty_colors = []
# Edict flag used for making sure an entity gets transmitted to all clients
# no matter what.
FL_EDICT_ALWAYS = 1<<3
is_projected_texture = EntityCondition.equals_entity_classname(
'env_projectedtexture')
def load():
"""Called when the plugin gets loaded."""
# Generate 10 vibrant colors, one for each digit from 0 to 9.
pretty_colors.extend(get_pretty_colors(amount=10))
def unload():
"""Called when the plugin gets unloaded."""
for player in PlayerF.cache.values():
try:
# Try to remove the player's flashlight.
player.torch.remove()
except AttributeError:
continue
@ClientCommand('+lookatweapon')
def inspect_weapon(command, index):
"""Called when a player inspects their weapon."""
PlayerF(index).toggle_torch()
if BLOCK_ANIMATION:
return CommandReturn.BLOCK
@EntityPreHook(is_projected_texture, 'set_transmit')
def set_transmit_pre(stack_data):
index = index_from_pointer(stack_data[0])
try:
# Does this index belong to a Torch instance?
torch = Torch.cache[index]
except KeyError:
return None
torch.state_flags |= FL_EDICT_ALWAYS
@OnNetworkedEntityDeleted
def on_networked_entity_deleted(entity):
"""Called when a networked entity gets deleted."""
if 'env_projectedtexture' in entity.classname:
try:
player = PlayerF.from_inthandle(entity.owner_handle)
except (ValueError, OverflowError):
# ValueError: Not a player.
# OverflowError: Invalid 'owner_handle' (-1).
return
player.torch = None
player.torch_enabled = False
class PlayerF(Player):
"""Extended Player class.
Args:
index (int): A valid player index.
caching (bool): Check for a cached instance?
Attributes:
torch (Entity): Entity instance of the 'env_projectedtexture'.
torch_enabled (bool): Is the torch/flashlight turned on?
"""
def __init__(self, index, caching=True):
"""Initializes the object."""
super().__init__(index, caching)
self.torch = None
self.torch_enabled = False
def toggle_torch(self):
"""Toggles the player's flashlight."""
# Flip the flashlight status.
self.torch_enabled = not self.torch_enabled
torch = self.torch
# Is the player missing a flashlight?
if torch is None:
# Should the player's steamid determine the flashlight color?
if COLOR_FROM_STEAMID:
color = pretty_colors[int(self.steamid[-1])]
# Or not?
else:
color = FLASHLIGHT_COLOR
torch = Torch.create(
color=color,
owner_handle=self.inthandle
)
# Usually, when parenting an entity to the player's viewmodel, the
# entity would only be transmitted to that player. However, if the
# entity that we're parenting has the 'FL_EDICT_ALWAYS' flag, it
# should be transmitted to all connected players.
torch.set_parent(Entity.from_inthandle(
self.get_datamap_property_int('m_hViewModel')), -1)
torch.teleport(NULL_VECTOR, NULL_QANGLE)
# Store the instance.
self.torch = torch
torch.call_input('TurnOn' if self.torch_enabled else 'TurnOff')
self.emit_sound(
sample='items/flashlight1.wav',
attenuation=0.8,
volume=0.3
)
class Torch(Entity):
"""Extended Entity class used for creating a flashlight."""
caching = True
@classmethod
def create(cls, color, owner_handle):
"""Creates a colored flashlight.
Args:
color (Color): Color of the flashlight.
owner_handle (int): Inthandle of the owner.
"""
texture = super().create('env_projectedtexture')
texture.owner_handle = owner_handle
texture.light_color = color
# Radius of the flashlight.
texture.light_fov = FLASHLIGHT_RADIUS
# Should objects/geometry cast shadows when lit by the flashlight?
texture.enable_shadows = False
# Should the flashlight affect static world geometry?
texture.light_world = True
# Speed at which the color changes.
texture.color_transition_time = 100
# Brightness of the flashlight.
texture.brightness_scale = FLASHLIGHT_BRIGHTNESS
# Set a spawn flag:
# 2: Always Update (moving light)
texture.spawn_flags = 2
texture.spawn()
return texture
def remove(self):
"""Turns the flashlight off before removing it."""
self.call_input('TurnOff')
self.delay(0, super().remove)
def get_pretty_colors(amount):
"""Returns a list of vibrant colors.
Args:
amount (int): How many colors should be generated?
Returns:
list of Color: A list containing Color instances.
"""
colors = []
step = 1 / amount
for hue in range(0, amount):
colors.append(
Color(*(int(255 * x) for x in hsv_to_rgb(step * hue, 1, 1))))
return colors