Skip to content
View in the app

A better way to browse. Learn more.

Learn more

cue-profiles/addons/amxmodx/scripting/olds_cueprofile.sma

olds_cueprofile.sma
347 lines · 9.5 KB · Pawn (AMX Mod X)
Raw
/*
 * Cue Profiles (olds.ro workshop card AMX-083)
 *
 * One setting per player for how olds.ro plugins give their cues: as text,
 * as sound, or both. A player types /cues in chat to cycle text -> sound ->
 * both. The choice is kept in nvault by Steam ID, so it is back on the next
 * visit.
 *
 * This plugin is the shared cue interface. Plugins that want to honour the
 * setting include olds_cues.inc, register each cue once with
 * olds_cue_register(name, sound) and give it with olds_cue_play(id, cue,
 * text). The setting only applies to cues registered this way:
 *   - text only:  the text is shown, the sound is not played;
 *   - sound only: the sound is played; a cue without a registered sound
 *                 still shows its text, so nothing is lost;
 *   - both:       text and sound.
 * A cue given without text (no text equivalent) always plays its sound.
 *
 * Chat command (chat and team chat, any letter case):
 *   /cues    cycles your setting: text -> sound -> both
 *
 * Cvars (in configs/plugins/olds_cueprofile.cfg, written with the defaults on
 * the first start if the file is missing):
 *   olds_cueprofile_default 2   Setting for players without a saved choice:
 *                               0 = text, 1 = sound, 2 = both.
 *
 * A player whose Steam ID is not known (a bot, a LAN or pending ID) keeps the
 * choice for the current visit only. If the vault file is damaged (it is then
 * left untouched) or cannot be opened, every choice is kept for the visit and
 * one line is written to the AMX Mod X log when the map starts.
 * Texts are in data/lang/olds_cueprofile.txt.
 *
 * Requires AMX Mod X 1.10 with the nvault module (ships with AMX Mod X).
 * No ReAPI. Written and tested on AMX Mod X 1.10.0.5490, ReHLDS 3.15.0.896
 * and ReGameDLL_CS 5.30.0.814.
 *
 * 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 <nvault>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Cue Profiles";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const LIBRARY_NAME[] = "olds_cues";
new const VAULT_NAME[] = "olds_cueprofile";

// First 4 bytes of every nVault file ("nVLT").
const VAULT_MAGIC = 0x6E564C54;

// Cues that can be registered by all plugins together.
const MAX_CUES = 64;

const CUE_NAME_LENGTH = 32;
const CUE_SOUND_LENGTH = 64;

// Longest cue text a plugin can give.
const CUE_TEXT_LENGTH = 191;

enum
{
	MODE_TEXT,
	MODE_SOUND,
	MODE_BOTH,
	MODE_COUNT
};

new const MODE_KEYS[MODE_COUNT][] = { "CUES_MODE_TEXT", "CUES_MODE_SOUND", "CUES_MODE_BOTH" };

new g_defaultMode;   // olds_cueprofile_default

new g_vault = INVALID_HANDLE;

new g_cueCount;
new g_cueName[MAX_CUES][CUE_NAME_LENGTH];
new g_cueSound[MAX_CUES][CUE_SOUND_LENGTH];

new g_mode[MAX_PLAYERS + 1];

// The player's Steam ID can be used as a vault key.
new bool:g_canSave[MAX_PLAYERS + 1];
new g_authid[MAX_PLAYERS + 1][MAX_AUTHID_LENGTH];

public plugin_natives()
{
	register_library(LIBRARY_NAME);

	register_native("olds_cue_register", "native_cue_register");
	register_native("olds_cue_play", "native_cue_play");
	register_native("olds_cue_get_mode", "native_cue_get_mode");
}

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
	register_dictionary("olds_cueprofile.txt");

	bind_pcvar_num(create_cvar("olds_cueprofile_default", "2", FCVAR_NONE,
		"Cue setting for players without a saved choice: 0 = text, 1 = sound, 2 = both",
		true, 0.0, true, float(MODE_COUNT - 1)), g_defaultMode);

	AutoExecConfig(true, "olds_cueprofile");

	register_clcmd("say", "cmd_say");
	register_clcmd("say_team", "cmd_say");

	// nVault would quietly start over on a damaged file and overwrite it, so
	// the file is checked first and left alone when it is not a vault. In
	// both failure cases one line is written and choices are kept for the
	// visit only.
	if (!vault_file_is_valid())
	{
		log_amx("Cue Profiles: data/vault/%s.vault is damaged and is left as it is; choices are kept for the current visit only.", VAULT_NAME);
		return;
	}

	g_vault = nvault_open(VAULT_NAME);

	if (g_vault == INVALID_HANDLE)
		log_amx("Cue Profiles: the vault ^"%s^" could not be opened; choices are kept for the current visit only.", VAULT_NAME);
}

public plugin_end()
{
	if (g_vault != INVALID_HANDLE)
		nvault_close(g_vault);
}

public client_connect(id)
{
	// A slot can be reused: start from the default, never from the last player.
	g_mode[id] = g_defaultMode;
	g_canSave[id] = false;
	g_authid[id][0] = EOS;
}

public client_authorized(id, const authid[])
{
	if (is_user_bot(id) || is_user_hltv(id) || !is_real_steamid(authid))
		return;

	copy(g_authid[id], charsmax(g_authid[]), authid);
	g_canSave[id] = true;

	if (g_vault == INVALID_HANDLE)
		return;

	new value[4], timestamp;

	if (nvault_lookup(g_vault, authid, value, charsmax(value), timestamp))
	{
		new mode = str_to_num(value);

		if (0 <= mode < MODE_COUNT)
			g_mode[id] = mode;
	}
}

public client_disconnected(id)
{
	g_canSave[id] = false;
	g_authid[id][0] = EOS;
}

public cmd_say(id)
{
	new text[16];

	// Chat sends the whole line as one argument; a line that fills the buffer
	// is longer than the command.
	if (read_argc() != 2 || read_argv(1, text, charsmax(text)) >= charsmax(text))
		return PLUGIN_CONTINUE;

	trim(text);

	if (!equali(text, "/cues"))
		return PLUGIN_CONTINUE;

	if (is_user_connected(id))
		cycle_mode(id);

	return PLUGIN_HANDLED;
}

cycle_mode(id)
{
	g_mode[id] = (g_mode[id] + 1) % MODE_COUNT;

	new bool:saved = false;

	if (g_canSave[id] && g_vault != INVALID_HANDLE)
	{
		new value[4];
		num_to_str(g_mode[id], value, charsmax(value));
		nvault_set(g_vault, g_authid[id], value);
		saved = true;
	}

	client_print_color(id, print_team_default, "%L", id, MODE_KEYS[g_mode[id]]);

	if (!saved)
		client_print_color(id, print_team_default, "%L", id, "CUES_SESSION_ONLY");
}

/**
 * native olds_cue_register(const name[], const sound[] = "");
 * Returns the cue id (0 or more), or -1 when the name is empty or the table
 * is full. Registering the same name again returns the same id.
 */
public native_cue_register(plugin, params)
{
	new name[CUE_NAME_LENGTH], sound[CUE_SOUND_LENGTH];
	get_string(1, name, charsmax(name));
	get_string(2, sound, charsmax(sound));
	trim(name);
	trim(sound);

	if (!name[0])
		return -1;

	for (new cue = 0; cue < g_cueCount; cue++)
	{
		if (equal(g_cueName[cue], name))
			return cue;
	}

	if (g_cueCount >= MAX_CUES)
	{
		log_amx("Cue Profiles: no room for cue ^"%s^" (%d cues already registered).", name, MAX_CUES);
		return -1;
	}

	// The sound is played with "spk", so it must not be able to end the
	// command or add another one.
	if (contain(sound, "^"") != -1 || contain(sound, ";") != -1)
		sound[0] = EOS;

	new cue = g_cueCount++;
	copy(g_cueName[cue], charsmax(g_cueName[]), name);
	copy(g_cueSound[cue], charsmax(g_cueSound[]), sound);

	return cue;
}

/**
 * native olds_cue_play(id, cue, const text[] = "", any:...);
 * Gives a registered cue to one player in the way they chose. text is the
 * cue's text equivalent (format rules apply). Returns 1 if the player got
 * text or sound, 0 if nothing was given (bad player or cue, nothing to give).
 */
public native_cue_play(plugin, params)
{
	new id = get_param(1);
	new cue = get_param(2);

	if (!(1 <= id <= MaxClients) || !is_user_connected(id) || !(0 <= cue < g_cueCount))
		return 0;

	new text[CUE_TEXT_LENGTH + 1];

	if (params >= 3)
		vdformat(text, charsmax(text), 3, 4);

	new bool:hasText = text[0] != EOS;
	new bool:hasSound = g_cueSound[cue][0] != EOS;
	new mode = g_mode[id];

	// The setting applies only when the cue has both forms; otherwise the
	// form it has is given, so the cue is never lost.
	new bool:showText = hasText && (mode != MODE_SOUND || !hasSound);
	new bool:playSound = hasSound && (mode != MODE_TEXT || !hasText);

	if (showText)
		client_print(id, print_chat, "%s", text);

	if (playSound)
		client_cmd(id, "spk ^"%s^"", g_cueSound[cue]);

	return (showText || playSound) ? 1 : 0;
}

/**
 * native olds_cue_get_mode(id);
 * Returns the player's setting: 0 = text, 1 = sound, 2 = both, or -1 for a
 * bad player index.
 */
public native_cue_get_mode(plugin, params)
{
	new id = get_param(1);

	if (!(1 <= id <= MaxClients) || !is_user_connected(id))
		return -1;

	return g_mode[id];
}

/**
 * True when there is no vault file yet (nVault creates it), or when the file
 * starts with the nVault magic number.
 */
bool:vault_file_is_valid()
{
	new path[128];
	get_localinfo("amxx_datadir", path, charsmax(path));
	format(path, charsmax(path), "%s/vault/%s.vault", path, VAULT_NAME);

	if (!file_exists(path) || file_size(path) == 0)
		return true;

	new file = fopen(path, "rb");

	if (!file)
		return false;

	new magic;
	new read = fread(file, magic, BLOCK_INT);
	fclose(file);

	return read > 0 && magic == VAULT_MAGIC;
}

/**
 * A Steam ID that stays the same between visits: not a LAN, pending or bot
 * placeholder.
 */
bool:is_real_steamid(const authid[])
{
	if (!equal(authid, "STEAM_", 6))
		return false;

	return !equal(authid, "STEAM_ID_LAN") && !equal(authid, "STEAM_ID_PENDING");
}

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.