· 6 years ago · May 15, 2019, 10:12 PM
1#include <sqlitei>
2#include <zcmd>
3
4/* -- Map Icons System Defines -- */
5#define MAX_MAP_ICONS (100)
6#define MAX_MAP_ICON_TEXT (100)
7/* ----------------------------- */
8
9/* -- Map Icons System Enums -- */
10enum E_MAP_ICON
11{
12 mapiconDBID,
13
14 Float: mapiconX,
15 Float: mapiconY,
16 Float: mapiconZ,
17
18 mapiconID,
19 mapiconText[MAX_MAP_ICON_TEXT],
20
21 /* -- Temp -- */
22 createMapIcon,
23 Text3D: mapiconLabel,
24 bool: mapiconExists
25 /* ---------- */
26}
27/* ---------------------------- */
28
29/* -- Map Icons System News -- */
30new MapIconData[MAX_MAP_ICONS][E_MAP_ICON];
31
32new DB: MapIconDB;
33
34new DBStatement: LoadMapIcons, DBStatement: RemoveServerMapIcon;
35/* --------------------------- */
36
37/* -- Map Icons System Generals -- */
38UpdateMapIconInfo(id)
39{
40 new label[MAX_MAP_ICON_TEXT];
41
42 MapIconData[id][createMapIcon] = CreateDynamicMapIcon(MapIconData[id][mapiconX], MapIconData[id][mapiconY], MapIconData[id][mapiconZ], MapIconData[id][mapiconID], 0, 0, 0, -1, 150.0);
43
44 format(label, sizeof(label), "%s", MapIconData[id][mapiconText]);
45 MapIconData[id][mapiconLabel] = CreateDynamic3DTextLabel(label, -1, MapIconData[id][mapiconX], MapIconData[id][mapiconY], MapIconData[id][mapiconZ], 20.0);
46}
47
48FindFreeMapIconDBID()
49{
50 for(new i; i < MAX_MAP_ICONS; i++) if(!MapIconData[i][mapiconExists]) return i;
51 return -1;
52}
53
54ResetMapIconLMIP(id)
55{
56 if(IsValidDynamicMapIcon(MapIconData[id][mapiconID])) DestroyDynamicMapIcon(MapIconData[id][mapiconID]);
57 if(IsValidDynamic3DTextLabel(MapIconData[id][mapiconLabel])) DestroyDynamic3DTextLabel(MapIconData[id][mapiconLabel]);
58}
59/* ------------------------------- */
60
61/* -- Map Icons System -- */
62for(new i; i < MAX_MAP_ICONS; i++)
63{
64 MapIconData[i][mapiconID] = -1;
65 MapIconData[i][mapiconLabel] = Text3D: -1;
66}
67
68MapIconDB = db_open("mapicons.db");
69
70db_query(MapIconDB, "CREATE TABLE IF NOT EXISTS mapicons (mapiconDBID INTEGER, mapiconX FLOAT, mapiconY FLOAT, mapiconZ FLOAT, mapiconID INTEGER, mapiconText STRING)");
71
72LoadMapIcons = db_prepare(MapIconDB, "SELECT * FROM mapicons");
73
74RemoveServerMapIcon = db_prepare(MapIconDB, "DELETE FROM mapicons WHERE mapiconDBID=?");
75
76new id, Float: pos[3], mpid, mptext[MAX_MAP_ICON_TEXT];
77
78stmt_bind_result_field(LoadMapIcons, 0, DB::TYPE_INTEGER, id);
79
80stmt_bind_result_field(LoadMapIcons, 1, DB::TYPE_FLOAT, pos[0]);
81stmt_bind_result_field(LoadMapIcons, 2, DB::TYPE_FLOAT, pos[1]);
82stmt_bind_result_field(LoadMapIcons, 3, DB::TYPE_FLOAT, pos[2]);
83
84stmt_bind_result_field(LoadMapIcons, 4, DB::TYPE_INTEGER, mpid);
85
86stmt_bind_result_field(LoadMapIcons, 5, DB::TYPE_STRING, mptext, sizeof(mptext));
87
88if(stmt_execute(LoadMapIcons))
89{
90 while(stmt_fetch_row(LoadMapIcons))
91 {
92 MapIconData[id][mapiconX] = pos[0];
93 MapIconData[id][mapiconY] = pos[1];
94 MapIconData[id][mapiconZ] = pos[2];
95
96 MapIconData[id][mapiconID] = mpid;
97
98 MapIconData[id][mapiconText] = mptext;
99
100 UpdateMapIconInfo(id);
101
102 MapIconData[id][mapiconExists] = true;
103 }
104}
105/* ---------------------- */
106
107/* -- OnGameModeExit -- */
108db_close(MapIconDB);
109/* -------------------- */
110
111CMD:cmapicon(playerid, params[])
112{
113 new id = FindFreeMapIconDBID();
114
115 if(id == -1) return SendClientMessage(playerid, -1, "» Server map icons limit reached.");
116
117 new mpid, text[MAX_MAP_ICON_TEXT];
118
119 if(sscanf(params, "ds", mpid, text)) return SendClientMessage(playerid, -1, "/cmapicon [mapicon id] [text]");
120
121 if((mpid < 0 || mpid > 63)) return SendClientMessage(playerid, -1, "» Invalid mapicon ID.");
122 if((strlen(text) < 1 || strlen(text) > MAX_MAP_ICON_TEXT)) return SendClientMessage(playerid, -1, "» Map icon text must be between 3 and 100 characters.");
123
124 new Float: PX, Float: PY, Float: PZ;
125
126 GetPlayerPos(playerid, PX, PY, PZ);
127
128 MapIconData[id][mapiconX] = PX;
129 MapIconData[id][mapiconY] = PY;
130 MapIconData[id][mapiconZ] = PZ;
131
132 MapIconData[id][mapiconID] = mpid;
133
134 format(MapIconData[id][mapiconText], MAX_MAP_ICON_TEXT, text);
135
136 UpdateMapIconInfo(id);
137
138 MapIconData[id][mapiconExists] = true;
139
140 new Query[500];
141
142 format(Query, sizeof(Query), "INSERT INTO mapicons (mapiconDBID, mapiconX, mapiconY, mapiconZ, mapiconID, mapiconText) VALUES ('%d', '%f', '%f', '%f', '%d', '%s')",
143 id, MapIconData[id][mapiconX], MapIconData[id][mapiconY], MapIconData[id][mapiconZ], mpid, MapIconData[id][mapiconText]);
144
145 db_query(MapIconDB, Query);
146
147 SendClientMessage(playerid, COLOR_YELLOW, "» Map icon created successfully at your position!");
148 return 1;
149}
150
151CMD:rmapicon(playerid, params[])
152{
153 if(isnull(params)) return SendClientMessage(playerid, -1, "/rmapicon [mapicon database ID]");
154
155 new id = strval(params);
156
157 if(!(0 <= id <= MAX_MAP_ICONS - 1)) return SendClientMessage(playerid, -1, "» Invalid mapicon database ID.");
158 if(!MapIconData[id][mapiconExists]) return SendClientMessage(playerid, -1, "» Map icon doesn't exist.");
159
160 ResetMapIconLMIP(id);
161
162 format(MapIconData[id][mapiconText], MAX_MAP_ICON_TEXT, "-");
163
164 MapIconData[id][mapiconExists] = false;
165
166 stmt_bind_value(RemoveServerMapIcon, 0, DB::TYPE_INTEGER, id);
167
168 if(stmt_execute(RemoveServerMapIcon)) SendClientMessage(playerid, COLOR_YELLOW, "» Map icon successfully removed from database.");
169 return 1;
170}
171
172CMD:rallmapicons(playerid)
173{
174 for(new i; i < MAX_MAP_ICONS; i++) if(MapIconData[i][mapiconExists])
175 {
176 ResetMapIconLMIP(i);
177
178 MapIconData[i][mapiconExists] = false;
179
180 stmt_bind_value(RemoveServerMapIcon, 0, DB::TYPE_INTEGER, i);
181 }
182
183 if(stmt_execute(RemoveServerMapIcon)) SendClientMessage(playerid, COLOR_YELLOW, "» All map icons have been successfully removed.");
184 return 1;
185}