Skip to content
View in the app

A better way to browse. Learn more.

Learn more

day-and-night-lighting/addons/amxmodx/scripting/olds_daynight.sma

olds_daynight.sma
486 lines · 12.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Day And Night Lighting (olds.ro workshop card LIB-1540)
 *
 * Switches the light of the whole map between day and night: by an admin
 * command, by a players' vote at map start, or every round in turn.
 *
 * Admin command (ADMIN_MAP, flag "f"; also from the server console):
 *   amx_daynight <day|night|toggle>
 *   An admin's choice also cancels a running vote.
 *
 * Server command:
 *   olds_dn_status   prints the mode, the light in use, the vote and how
 *                    many joining players were sent the light
 *
 * Cvars (saved with their descriptions in configs/plugins/olds_daynight.cfg
 * the first time the plugin starts):
 *   olds_dn_mode  0     0 = only the admin command changes the light
 *                       1 = players vote day or night at map start
 *                       2 = day and night take turns, one round each
 *   olds_dn_day   "m"   light style for day
 *   olds_dn_night "c"   light style for night
 * A light style is a string of letters from a (darkest) to z (brightest);
 * m is the normal light. Several letters make the light pulse.
 *
 * The light is the engine's light style 0, set with EngFunc_LightStyle for
 * everyone at once. Players who join later are sent the same style. A new
 * map always starts with its own light.
 *
 * Requires AMX Mod X 1.10 or newer with the fakemeta module, and ReAPI
 * (ReGameDLL_CS) for the round restart hook.
 *
 * Copyright (C) 2026 alexit (olds.ro workshop)
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details: <https://www.gnu.org/licenses/>.
 */

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Day And Night Lighting";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_daynight.txt";

// Light style 0 is the normal light of the whole map.
const MAP_LIGHT_STYLE = 0;

// Engine message that sets one light style on a client (svc_lightstyle).
const MSG_LIGHT_STYLE = 12;

// Longest style string kept (the engine allows 64 letters).
const MAX_STYLE_LENGTH = 32;

// The vote starts this long after the map start and stays open this long.
const Float:VOTE_DELAY = 20.0;
const VOTE_SECONDS = 15;

// A joining player is sent the style again after this delay, once the
// engine has sent its own copy.
const Float:JOIN_RESEND_DELAY = 1.0;

const TASK_VOTE_START = 15401;
const TASK_VOTE_END = 15402;
const TASK_JOIN = 15410;          // + player id

enum
{
	LIGHT_MAP = 0,                // the map's own light, nothing changed
	LIGHT_DAY,
	LIGHT_NIGHT
};

new const LIGHT_NAMES[][] = { "the map's own", "day", "night" };
new const SET_KEYS[][] = { "", "OLDS_DN_SET_DAY", "OLDS_DN_SET_NIGHT" };
new const CHAT_KEYS[][] = { "", "OLDS_DN_CHAT_DAY", "OLDS_DN_CHAT_NIGHT" };
new const ROUND_KEYS[][] = { "", "OLDS_DN_ROUND_DAY", "OLDS_DN_ROUND_NIGHT" };
new const VOTED_KEYS[][] = { "OLDS_DN_VOTED_DAY", "OLDS_DN_VOTED_NIGHT" };

new g_mode;                               // olds_dn_mode
new g_dayCvar[MAX_STYLE_LENGTH];          // olds_dn_day
new g_nightCvar[MAX_STYLE_LENGTH];        // olds_dn_night

new g_light = LIGHT_MAP;
new g_style[MAX_STYLE_LENGTH];            // the style in use
new g_rounds;

new bool:g_voting;
new g_voteMenu = -1;
new g_votes[2];                           // [0] day, [1] night
new bool:g_voted[MAX_PLAYERS + 1];

new g_resent;
new g_lastJoiner[MAX_NAME_LENGTH];

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
	register_dictionary(DICTIONARY);

	register_concmd("amx_daynight", "cmd_daynight", ADMIN_MAP, "OLDS_DN_HELP", -1, true);
	register_srvcmd("olds_dn_status", "cmd_status");

	bind_pcvar_num(create_cvar("olds_dn_mode", "0", FCVAR_NONE,
		"0 = only amx_daynight changes the light, 1 = players vote day or night at map start, 2 = day and night take turns every round",
		true, 0.0, true, 2.0), g_mode);

	bind_pcvar_string(create_cvar("olds_dn_day", "m", FCVAR_NONE,
		"Light style for day: letters a (darkest) to z (brightest), m = normal"),
		g_dayCvar, charsmax(g_dayCvar));

	bind_pcvar_string(create_cvar("olds_dn_night", "c", FCVAR_NONE,
		"Light style for night: letters a (darkest) to z (brightest), m = normal"),
		g_nightCvar, charsmax(g_nightCvar));

	AutoExecConfig(true, "olds_daynight");

	RegisterHookChain(RG_CSGameRules_RestartRound, "fw_restart_round_post", true);
}

/**
 * Once per map, after all configs have run: schedule the vote in mode 1.
 */
public OnConfigsExecuted()
{
	if (g_mode == 1)
		set_task(VOTE_DELAY, "task_start_vote", TASK_VOTE_START);
}

public client_putinserver(id)
{
	if (g_light == LIGHT_MAP)
		return;

	send_style(id);
	set_task(JOIN_RESEND_DELAY, "task_join_resend", TASK_JOIN + id);
}

public client_disconnected(id)
{
	remove_task(TASK_JOIN + id);
	g_voted[id] = false;
}

public task_join_resend(taskId)
{
	new id = taskId - TASK_JOIN;

	if (g_light != LIGHT_MAP && is_user_connected(id))
		send_style(id);
}

/**
 * The engine sends the light styles to a joining player itself; this sends
 * the plugin's style once more so the player surely sees the same light.
 */
send_style(id)
{
	message_begin(MSG_ONE, MSG_LIGHT_STYLE, _, id);
	write_byte(MAP_LIGHT_STYLE);
	write_string(g_style);
	message_end();

	g_resent++;
	get_user_name(id, g_lastJoiner, charsmax(g_lastJoiner));
}

/**
 * Every new round (also "Game Commencing" and sv_restart): mode 2 swaps
 * day and night.
 */
public fw_restart_round_post()
{
	g_rounds++;

	if (g_mode != 2)
		return;

	new light = LIGHT_NIGHT;

	if (g_light != LIGHT_DAY)
		light = LIGHT_DAY;

	apply_light(light);

	client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, ROUND_KEYS[light]);
	log_amx("Round %d: %s (light style ^"%s^")", g_rounds, LIGHT_NAMES[light], g_style);
}

/**
 * amx_daynight <day|night|toggle>
 */
public cmd_daynight(id, level, cid)
{
	if (!admin_allowed(id, level))
		return PLUGIN_HANDLED;

	new arg[16], light;
	read_argv(1, arg, charsmax(arg));

	if (equali(arg, "day"))
	{
		light = LIGHT_DAY;
	}
	else if (equali(arg, "night"))
	{
		light = LIGHT_NIGHT;
	}
	else if (equali(arg, "toggle"))
	{
		light = LIGHT_NIGHT;

		if (g_light == LIGHT_NIGHT)
			light = LIGHT_DAY;
	}
	else
	{
		console_print(id, "%L", id, "OLDS_DN_USAGE");
		return PLUGIN_HANDLED;
	}

	if (g_voting)
	{
		stop_vote();
		client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, "OLDS_DN_VOTE_CANCELLED");
		log_amx("Day or night vote cancelled by the admin command.");
	}

	apply_light(light);

	console_print(id, "%L", id, SET_KEYS[light], g_style);

	new name[MAX_NAME_LENGTH];

	if (id)
		get_user_name(id, name, charsmax(name));
	else
		copy(name, charsmax(name), "Server");

	client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, CHAT_KEYS[light], name);

	new who[96];
	describe_admin(id, who, charsmax(who));
	log_amx("Cmd: %s set the light to %s (light style ^"%s^")", who, LIGHT_NAMES[light], g_style);
	return PLUGIN_HANDLED;
}

/**
 * olds_dn_status
 */
public cmd_status()
{
	if (g_light == LIGHT_MAP)
	{
		server_print("[olds_daynight] mode %d, light: %s, rounds %d",
			g_mode, LIGHT_NAMES[g_light], g_rounds);
	}
	else
	{
		server_print("[olds_daynight] mode %d, light: %s (style ^"%s^"), rounds %d",
			g_mode, LIGHT_NAMES[g_light], g_style, g_rounds);
	}

	if (g_voting)
		server_print("[olds_daynight] vote open: day %d, night %d", g_votes[0], g_votes[1]);
	else
		server_print("[olds_daynight] no vote open (last count: day %d, night %d)", g_votes[0], g_votes[1]);

	server_print("[olds_daynight] style sent to joining players %d time(s), last: %s",
		g_resent, g_lastJoiner);

	return PLUGIN_HANDLED;
}

/**
 * Sets the light for everyone at once.
 */
apply_light(light)
{
	g_light = light;
	pick_style(light);

	engfunc(EngFunc_LightStyle, MAP_LIGHT_STYLE, g_style);
}

/**
 * Copies the style for day or night from its cvar into g_style. A value
 * that is not a light style is replaced by the default and logged.
 */
pick_style(light)
{
	new value[MAX_STYLE_LENGTH];

	if (light == LIGHT_NIGHT)
		copy(value, charsmax(value), g_nightCvar);
	else
		copy(value, charsmax(value), g_dayCvar);

	trim(value);
	strtolower(value);

	if (is_light_style(value))
	{
		copy(g_style, charsmax(g_style), value);
		return;
	}

	if (light == LIGHT_NIGHT)
	{
		copy(g_style, charsmax(g_style), "c");
		log_amx("olds_dn_night ^"%s^" is not a light style (letters a to z only); ^"c^" is used.", value);
	}
	else
	{
		copy(g_style, charsmax(g_style), "m");
		log_amx("olds_dn_day ^"%s^" is not a light style (letters a to z only); ^"m^" is used.", value);
	}
}

bool:is_light_style(const text[])
{
	if (!text[0])
		return false;

	for (new i = 0; text[i]; i++)
	{
		if (text[i] < 'a' || text[i] > 'z')
			return false;
	}

	return true;
}

/**
 * Mode 1: the day or night menu for every player, VOTE_DELAY seconds after
 * the map start.
 */
public task_start_vote()
{
	if (g_mode != 1 || g_voting)
		return;

	remove_task(TASK_VOTE_START);

	g_voting = true;
	g_votes[0] = 0;
	g_votes[1] = 0;
	arrayset(g_voted, false, sizeof(g_voted));

	new text[64];
	formatex(text, charsmax(text), "%L", LANG_SERVER, "OLDS_DN_VOTE_TITLE");
	g_voteMenu = menu_create(text, "vote_handler");

	formatex(text, charsmax(text), "%L", LANG_SERVER, "OLDS_DN_VOTE_DAY");
	menu_additem(g_voteMenu, text);

	formatex(text, charsmax(text), "%L", LANG_SERVER, "OLDS_DN_VOTE_NIGHT");
	menu_additem(g_voteMenu, text);

	menu_setprop(g_voteMenu, MPROP_EXIT, MEXIT_NEVER);

	new players[MAX_PLAYERS], count;
	get_players(players, count, "ch");

	for (new i = 0; i < count; i++)
		menu_display(players[i], g_voteMenu, 0, VOTE_SECONDS);

	client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, "OLDS_DN_VOTE_STARTED", VOTE_SECONDS);
	log_amx("Day or night vote started for %d player(s).", count);

	set_task(float(VOTE_SECONDS), "task_end_vote", TASK_VOTE_END);
}

/**
 * Item 0 = day, 1 = night. One vote per player.
 */
public vote_handler(id, menu, item)
{
	if (!g_voting || item < 0 || item > 1 || id < 1 || id > MaxClients || g_voted[id])
		return PLUGIN_HANDLED;

	g_voted[id] = true;
	g_votes[item]++;

	new name[MAX_NAME_LENGTH];
	get_user_name(id, name, charsmax(name));
	client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, VOTED_KEYS[item], name);

	return PLUGIN_HANDLED;
}

/**
 * The vote time is over: the side with more votes wins; a tie changes
 * nothing.
 */
public task_end_vote()
{
	if (!g_voting)
		return;

	stop_vote();

	new day = g_votes[0];
	new night = g_votes[1];

	if (day == night)
	{
		client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, "OLDS_DN_VOTE_TIE", day, night);
		log_amx("Day or night vote: day %d, night %d, no winner; the light stays %s.",
			day, night, LIGHT_NAMES[g_light]);
		return;
	}

	if (night > day)
	{
		apply_light(LIGHT_NIGHT);
		client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, "OLDS_DN_VOTE_WON_NIGHT", night, day);
	}
	else
	{
		apply_light(LIGHT_DAY);
		client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, "OLDS_DN_VOTE_WON_DAY", day, night);
	}

	log_amx("Day or night vote: day %d, night %d, %s wins (light style ^"%s^")",
		day, night, LIGHT_NAMES[g_light], g_style);
}

/**
 * Closes the vote and its menu; the counts stay for olds_dn_status.
 */
stop_vote()
{
	g_voting = false;
	remove_task(TASK_VOTE_END);

	if (g_voteMenu != -1)
	{
		menu_destroy(g_voteMenu);
		g_voteMenu = -1;
	}
}

/**
 * The same rules as cmd_access: the server console (and the host of a listen
 * server) may always; ADMIN_ALL = everyone; ADMIN_ADMIN = any admin flag.
 * The level can be changed in configs/cmdaccess.ini.
 */
bool:admin_allowed(id, level)
{
	if (id == (is_dedicated_server() ? 0 : 1) || level == ADMIN_ALL)
		return true;

	if ((level == ADMIN_ADMIN && is_user_admin(id)) || (get_user_flags(id) & level))
		return true;

	console_print(id, "%L", id, "OLDS_DN_NO_ACCESS");
	return false;
}

describe_admin(id, out[], maxLength)
{
	if (!id)
	{
		copy(out, maxLength, "Server");
		return;
	}

	new name[MAX_NAME_LENGTH], authid[44];
	get_user_name(id, name, charsmax(name));
	get_user_authid(id, authid, charsmax(authid));
	formatex(out, maxLength, "^"%s<%d><%s><>^"", name, get_user_userid(id), authid);
}

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.