forked from VANCOLD/fm_drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FM_COLOUR_API.sma
155 lines (124 loc) · 3.55 KB
/
FM_COLOUR_API.sma
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "feckinmad/fm_global"
#define MAX_COLOURS 128
#define MAX_COLOUR_NAME_LEN 12
new const g_sColourFile[] = "fm_colours.ini"
new g_sColourList[MAX_COLOURS][MAX_COLOUR_NAME_LEN] // List of colours
new g_iColourValues[MAX_COLOURS][3] // The RGB colours
new g_iColourNum // Total number of colours loaded
new const g_sTextColourIndexRange[] = "Colour index out of range (%d)"
new const g_sTextColour[][] =
{
"red",
"green",
"blue"
}
public plugin_precache()
{
ReadColourFile()
}
ReadColourFile()
{
new sFile[128]; fm_BuildAMXFilePath(g_sColourFile, sFile, charsmax(sFile), "amxx_configsdir")
new iFileHandle = fopen(sFile, "rt")
if (!iFileHandle)
{
fm_WarningLog(FM_FOPEN_WARNING, sFile)
return 0
}
new sData[128], iValueCount, sColourValue[4], iColourValue
while (!feof(iFileHandle))
{
fgets(iFileHandle, sData, charsmax(sData))
trim(sData)
if(!sData[0] || sData[0] == ';' || sData[0] == '#' || equal(sData, "//", 2))
continue
if (g_iColourNum >= MAX_COLOURS)
{
fm_WarningLog("Maximum colours reached")
break
}
argbreak(sData, g_sColourList[g_iColourNum], MAX_COLOUR_NAME_LEN - 1, sData, charsmax(sData)) // Store the colour name
iValueCount = sColourValue[0] = 0
while (iValueCount < 3) // Load the R G & B values
{
argbreak(sData, sColourValue, charsmax(sColourValue), sData, charsmax(sData))
if (!sColourValue[0])
{
break
}
iColourValue = str_to_num(sColourValue)
if (iColourValue < -1 || iColourValue > 255)
{
fm_WarningLog("Colour \"%s\" has invalid %s value (%d)", g_sColourList[g_iColourNum], g_sTextColour[iValueCount], iColourValue)
}
g_iColourValues[g_iColourNum][iValueCount++] = iColourValue
}
if (iValueCount < 3)
{
fm_WarningLog("Ignoring colour \"%s\": Missing colour values", g_sColourList[g_iColourNum])
}
else
{
fm_DebugPrintLevel(2, "Loaded colour: \"%s\": { %d, %d, %d }", g_sColourList[g_iColourNum], g_iColourValues[g_iColourNum][0], g_iColourValues[g_iColourNum][1], g_iColourValues[g_iColourNum][2])
g_iColourNum++
}
}
fclose(iFileHandle)
log_amx("Loaded %d colours from \"%s\"", g_iColourNum, sFile)
return 1
}
public plugin_init()
{
fm_RegisterPlugin()
}
public plugin_natives()
{
register_native("fm_GetColourCount", "Native_GetColourCount")
register_native("fm_GetColourIndex", "Native_GetColourIndex")
register_native("fm_GetColourNameByIndex", "Native_GetColourNameByIndex")
register_native("fm_GetColoursByIndex", "Native_GetColoursByIndex")
register_library("fm_colour_api")
}
public Native_GetColourCount()
{
return g_iColourNum
}
public Native_GetColourIndex()
{
new sColour[MAX_COLOUR_NAME_LEN]; get_string(1, sColour, charsmax(sColour))
for (new i = 0; i < g_iColourNum; i++)
{
if (equali(g_sColourList[i], sColour))
{
return i
}
}
return -1
}
public Native_GetColourNameByIndex()
{
new iIdent = get_param(1)
if (iIdent < 0 || iIdent >= g_iColourNum)
{
log_error(AMX_ERR_NATIVE, g_sTextColourIndexRange, iIdent)
return 0
}
set_string(2, g_sColourList[iIdent], MAX_COLOUR_NAME_LEN)
return 1
}
public Native_GetColoursByIndex()
{
new iIdent = get_param(1)
if (iIdent < 0 || iIdent >= g_iColourNum)
{
log_error(AMX_ERR_NATIVE, g_sTextColourIndexRange, iIdent)
return 0
}
set_array(2, g_iColourValues[iIdent], 3)
return 1
}
public fm_ScreenMessage(sBuffer[], iSize)
{
// TODO: Move this to FM_GLOW.amxx
formatex(sBuffer, iSize - 1, "There are %d colours availiable for you to glow. Type \"glow menu\" to see them all. I hear %s is in this season", g_iColourNum, g_sColourList[random(g_iColourNum)])
}