Skip to content

Commit 0cb10a4

Browse files
committed
core: add native mapm_add_custom_item
1 parent 55cbdaa commit 0cb10a4

3 files changed

Lines changed: 128 additions & 1 deletion

File tree

cstrike/addons/amxmodx/scripting/include/map_manager.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ native mapm_add_vote_to_item(item, value);
191191
*/
192192
native mapm_set_displayed_name(item, name[]);
193193

194+
/**
195+
* Adds a custom item that will always be displayed with the vote.
196+
*
197+
* @note The handler function should be prototyped as:
198+
* public <function>(id, MCustomItem:item)
199+
* id - client index who used the custom item
200+
* item - custom item index
201+
*
202+
* @param name Displayed name
203+
* @param handler Handler function, which will be called after selecting a custom item in the voting
204+
* @param add_blank Adds blank line before custom item
205+
* @param add_number Adds number in vote menu, if false handler will be ignored
206+
*
207+
* @return Custom item index
208+
* Invalid_Custom_Item - If can't register handler
209+
*/
210+
native MCustomItem:mapm_add_custom_item(name[], handler[], bool:add_blank = false, bool:add_number = true);
211+
194212
/**
195213
* Returns if vote started.
196214
*

cstrike/addons/amxmodx/scripting/include/map_manager_consts.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,7 @@ enum {
102102
NOMINATION_ALLOWED,
103103
NOMINATION_BLOCKED
104104
};
105+
106+
enum MCustomItem {
107+
Invalid_Custom_Item = -1
108+
};

cstrike/addons/amxmodx/scripting/map_manager_core.sma

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#endif
88

99
#define PLUGIN "Map Manager: Core"
10-
#define VERSION "3.1.5"
10+
#define VERSION "3.2.0"
1111
#define AUTHOR "Mistrick"
1212

1313
#pragma semicolon 1
@@ -92,6 +92,20 @@ new g_sDisplayedItemName[MAX_VOTELIST_SIZE + 1][MAPNAME_LENGTH * 2];
9292

9393
new g_iPlayersNum;
9494

95+
enum _:CustomItemStruct {
96+
CI_Name[64],
97+
CI_Handler,
98+
bool:CI_AddBlank,
99+
bool:CI_AddNumber
100+
}
101+
102+
new Array:g_aCustomItems;
103+
new g_sCustomItemsMenu[MAX_VOTELIST_SIZE][128];
104+
new g_iCustomItemsKeys;
105+
new g_iCustomItemsHandlers[MAX_VOTELIST_SIZE];
106+
new g_iCustomItemsIndex[MAX_VOTELIST_SIZE];
107+
new bool:g_bCustomItemSkipNum[MAX_VOTELIST_SIZE];
108+
95109
public plugin_init()
96110
{
97111
register_plugin(PLUGIN, VERSION + VERSION_HASH, AUTHOR);
@@ -134,6 +148,8 @@ public plugin_natives()
134148
g_aMapsList = ArrayCreate(MapStruct, 1);
135149
get_mapname(g_sCurMap, charsmax(g_sCurMap));
136150

151+
g_aCustomItems = ArrayCreate(CustomItemStruct, 1);
152+
137153
register_native("mapm_load_maplist", "native_load_maplist");
138154
register_native("mapm_load_maplist_to_array", "native_load_maplist_to_array");
139155
register_native("mapm_block_load_maplist", "native_block_load_maplist");
@@ -152,6 +168,7 @@ public plugin_natives()
152168
register_native("mapm_get_vote_type", "native_get_vote_type");
153169
register_native("mapm_add_vote_to_item", "native_add_vote_to_item");
154170
register_native("mapm_set_displayed_name", "native_set_displayed_name");
171+
register_native("mapm_add_custom_item", "native_add_custom_item");
155172
register_native("is_vote_started", "native_is_vote_started");
156173
register_native("is_vote_finished", "native_is_vote_finished");
157174
}
@@ -358,6 +375,32 @@ public native_set_displayed_name(plugin, params)
358375

359376
return 0;
360377
}
378+
public native_add_custom_item(plugin, params)
379+
{
380+
enum {
381+
arg_name = 1,
382+
arg_handler,
383+
arg_add_blank,
384+
arg_add_number
385+
}
386+
387+
new custom_item[CustomItemStruct];
388+
new handler[32];
389+
get_string(arg_name, custom_item[CI_Name], charsmax(custom_item[CI_Name]));
390+
get_string(arg_handler, handler, charsmax(handler));
391+
custom_item[CI_AddBlank] = bool:get_param(arg_add_blank);
392+
custom_item[CI_AddNumber] = bool:get_param(arg_add_number);
393+
394+
if(custom_item[CI_AddNumber]) {
395+
custom_item[CI_Handler] = CreateOneForward(plugin, handler, FP_CELL, FP_CELL);
396+
397+
if(custom_item[CI_Handler] == -1) {
398+
return -1;
399+
}
400+
}
401+
402+
return ArrayPushArray(g_aCustomItems, custom_item);
403+
}
361404
public native_is_vote_started(plugin, params)
362405
{
363406
return g_bVoteStarted;
@@ -513,6 +556,14 @@ prepare_vote(type)
513556
}
514557
}
515558

559+
// custom items
560+
new c_items_count = custom_items_builder();
561+
new max_custom_items = MAX_VOTELIST_SIZE - (g_iVoteItems + g_bCanExtend);
562+
563+
if(c_items_count >= max_custom_items) {
564+
log_amx("Check your settings. You have more custom items than can add to vote.");
565+
}
566+
516567
if(get_num(RANDOM_NUMS)) {
517568
arrayset(g_iRandomNums, -1, sizeof(g_iRandomNums));
518569
for(new i; i < g_iVoteItems + g_bCanExtend; i++) {
@@ -544,6 +595,39 @@ prepare_vote(type)
544595

545596
return 1;
546597
}
598+
custom_items_builder()
599+
{
600+
new len = 0, item = 0;
601+
new custom_item[CustomItemStruct];
602+
new ci_size = ArraySize(g_aCustomItems);
603+
g_iCustomItemsKeys = 0;
604+
arrayset(g_bCustomItemSkipNum, false, sizeof(g_bCustomItemSkipNum));
605+
606+
for(new i; i < ci_size; i++) {
607+
ArrayGetArray(g_aCustomItems, i, custom_item);
608+
609+
len = 0;
610+
611+
if(custom_item[CI_AddBlank]) {
612+
len += formatex(g_sCustomItemsMenu[i][len], charsmax(g_sCustomItemsMenu[]) - len, "^n");
613+
}
614+
615+
if(custom_item[CI_AddNumber]) {
616+
len += formatex(g_sCustomItemsMenu[i][len], charsmax(g_sCustomItemsMenu[]) - len, "\r%%d. ");
617+
g_iCustomItemsKeys |= (1 << item);
618+
g_iCustomItemsHandlers[item] = custom_item[CI_Handler];
619+
g_iCustomItemsIndex[item] = i;
620+
621+
item++;
622+
} else {
623+
g_bCustomItemSkipNum[i] = true;
624+
}
625+
626+
len += formatex(g_sCustomItemsMenu[i][len], charsmax(g_sCustomItemsMenu[]) - len, "\w%s^n", custom_item[CI_Name]);
627+
}
628+
629+
return item;
630+
}
547631
is_map_allowed(map[], type, index)
548632
{
549633
new ret;
@@ -648,6 +732,15 @@ public show_votemenu(id)
648732
len += formatex(menu[len], charsmax(menu) - len, "^n");
649733
}
650734

735+
// custom items
736+
for(new i, skip, ci_size = ArraySize(g_aCustomItems); i < ci_size; i++) {
737+
if(g_bCustomItemSkipNum[i]) {
738+
skip++;
739+
}
740+
len += formatex(menu[len], charsmax(menu) - len, g_sCustomItemsMenu[i], item + i + 1 - skip);
741+
}
742+
keys |= g_iCustomItemsKeys << item;
743+
651744
len += formatex(menu[len], charsmax(menu) - len, "^n\d%L \r%d\d %L", id, "MAPM_MENU_LEFT", g_iTimer, id, "MAPM_SECONDS");
652745

653746
if(!keys) keys = (1 << 9);
@@ -666,6 +759,18 @@ public show_votemenu(id)
666759
}
667760
public votemenu_handler(id, key)
668761
{
762+
// custom items
763+
if(key >= g_iVoteItems + g_bCanExtend) {
764+
new item = key - (g_iVoteItems + g_bCanExtend);
765+
new ci_handler = g_iCustomItemsHandlers[item];
766+
767+
new ret;
768+
ExecuteForward(ci_handler, ret, id, g_iCustomItemsIndex[item]);
769+
770+
show_votemenu(id);
771+
return PLUGIN_HANDLED;
772+
}
773+
669774
if(g_iVoted[id] != NOT_VOTED) {
670775
show_votemenu(id);
671776
return PLUGIN_HANDLED;

0 commit comments

Comments
 (0)