Added sm_cancelvote to the base commands, as well as appropriate translations.
3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
33 #include "sm_globals.h"
34 #include <IHandleSys.h>
38 /***********************************
39 * About the double array hack *
40 ***************************
42 Double arrays in Pawn are vectors offset by the current offset. For example:
46 In this array, index 0 contains the offset from the current offset which
47 results in the final vector [2] (at [0][2]). Meaning, to dereference [1][2],
50 address = &array[1] + array[1] + 2 * sizeof(cell)
52 The fact that each offset is from the _current_ position rather than the _base_
53 position is very important. It means that if you to try to swap vector positions,
54 the offsets will no longer match, because their current position has changed. A
55 simple and ingenious way around this is to back up the positions in a separate array,
56 then to overwrite each position in the old array with absolute indices. Pseudo C++ code:
58 cell *array; //assumed to be set to the 2+D array
59 cell *old_offsets = new cell[2];
60 for (int i=0; i<2; i++)
62 old_offsets = array[i];
66 Now, you can swap the array indices with no problem, and do a reverse-lookup to find the original addresses.
67 After sorting/modification is done, you must relocate the new indices. For example, if the two vectors in our
68 demo array were swapped, array[0] would be 1 and array[1] would be 0. This is invalid to the virtual machine.
69 Luckily, this is also simple -- all the information is there.
71 for (int i=0; i<2; i++)
73 //get the # of the vector we want to relocate in
74 cell vector_index = array[i];
75 //get the real address of this vector
76 char *real_address = (char *)array + (vector_index * sizeof(cell)) + old_offsets[vector_index];
77 //calc and store the new distance offset
78 array[i] = real_address - ( (char *)array + (vector_index + sizeof(cell)) )
81 Note that the inner expression can be heavily reduced; it is expanded for readability.
82 **********************************/
90 int sort_ints_asc(const void *int1, const void *int2)
92 return (*(int *)int1) - (*(int *)int2);
95 int sort_ints_desc(const void *int1, const void *int2)
97 return (*(int *)int2) - (*(int *)int1);
100 static cell_t sm_SortIntegers(IPluginContext *pContext, const cell_t *params)
103 cell_t array_size = params[2];
104 cell_t type = params[3];
106 pContext->LocalToPhysAddr(params[1], &array);
108 if (type == Sort_Ascending)
110 qsort(array, array_size, sizeof(cell_t), sort_ints_asc);
112 qsort(array, array_size, sizeof(cell_t), sort_ints_desc);
118 int sort_floats_asc(const void *float1, const void *float2)
120 float r1 = *(float *)float1;
121 float r2 = *(float *)float2;
126 } else if (r2 < r1) {
133 int sort_floats_desc(const void *float1, const void *float2)
135 float r1 = *(float *)float1;
136 float r2 = *(float *)float2;
141 } else if (r2 < r1) {
148 static cell_t sm_SortFloats(IPluginContext *pContext, const cell_t *params)
151 cell_t array_size = params[2];
152 cell_t type = params[3];
154 pContext->LocalToPhysAddr(params[1], &array);
156 if (type == Sort_Ascending)
158 qsort(array, array_size, sizeof(cell_t), sort_floats_asc);
160 qsort(array, array_size, sizeof(cell_t), sort_floats_desc);
166 static cell_t *g_CurStringArray = NULL;
167 static cell_t *g_CurRebaseMap = NULL;
169 int sort_strings_asc(const void *blk1, const void *blk2)
171 cell_t reloc1 = *(cell_t *)blk1;
172 cell_t reloc2 = *(cell_t *)blk2;
174 char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]);
175 char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]);
177 return strcmp(str1, str2);
180 int sort_strings_desc(const void *blk1, const void *blk2)
182 cell_t reloc1 = *(cell_t *)blk1;
183 cell_t reloc2 = *(cell_t *)blk2;
185 char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]);
186 char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]);
188 return strcmp(str2, str1);
191 static cell_t sm_SortStrings(IPluginContext *pContext, const cell_t *params)
194 cell_t array_size = params[2];
195 cell_t type = params[3];
197 pContext->LocalToPhysAddr(params[1], &array);
199 /** HACKHACK - back up the old indices, replace the indices with something easier */
200 cell_t amx_addr, *phys_addr;
202 if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE)
204 pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort");
207 g_CurStringArray = array;
208 g_CurRebaseMap = phys_addr;
210 for (int i=0; i<array_size; i++)
212 phys_addr[i] = array[i];
216 if (type == Sort_Ascending)
218 qsort(array, array_size, sizeof(cell_t), sort_strings_asc);
220 qsort(array, array_size, sizeof(cell_t), sort_strings_desc);
223 /* END HACKHACK - restore what we damaged so Pawn doesn't throw up.
224 * We'll browse through each index of the array and patch up the distance.
226 for (int i=0; i<array_size; i++)
228 /* Compute the final address of the old array and subtract the new location.
229 * This is the fixed up distance.
231 array[i] = ((char *)&array[array[i]] + phys_addr[array[i]]) - (char *)&array[i];
234 pContext->HeapPop(amx_addr);
236 g_CurStringArray = NULL;
237 g_CurRebaseMap = NULL;
244 IPluginFunction *pFunc;
251 sort_info g_SortInfo;
253 int sort1d_amx_custom(const void *elem1, const void *elem2)
255 cell_t c1 = *(cell_t *)elem1;
256 cell_t c2 = *(cell_t *)elem2;
259 IPluginFunction *pf = g_SortInfo.pFunc;
262 pf->PushCell(g_SortInfo.array_addr);
263 pf->PushCell(g_SortInfo.hndl);
264 pf->Execute(&result);
269 static cell_t sm_SortCustom1D(IPluginContext *pContext, const cell_t *params)
272 cell_t array_size = params[2];
274 IPluginFunction *pFunction = pContext->GetFunctionById(params[3]);
277 return pContext->ThrowNativeError("Function %x is not a valid function", params[3]);
280 pContext->LocalToPhysAddr(params[1], &array);
282 sort_info oldinfo = g_SortInfo;
285 g_SortInfo.hndl = params[4];
286 g_SortInfo.array_addr = params[1];
287 g_SortInfo.array_remap = NULL;
288 g_SortInfo.array_base = NULL;
289 g_SortInfo.pFunc = pFunction;
291 qsort(array, array_size, sizeof(cell_t), sort1d_amx_custom);
293 g_SortInfo = oldinfo;
298 int sort2d_amx_custom(const void *elem1, const void *elem2)
300 cell_t c1 = *(cell_t *)elem1;
301 cell_t c2 = *(cell_t *)elem2;
303 cell_t c1_addr = g_SortInfo.array_addr + (c1 * sizeof(cell_t)) + g_SortInfo.array_remap[c1];
304 cell_t c2_addr = g_SortInfo.array_addr + (c2 * sizeof(cell_t)) + g_SortInfo.array_remap[c2];
306 IPluginContext *pContext = g_SortInfo.pFunc->GetParentContext();
308 pContext->LocalToPhysAddr(c1_addr, &c1_r);
309 pContext->LocalToPhysAddr(c2_addr, &c2_r);
312 g_SortInfo.pFunc->PushCell(c1_addr);
313 g_SortInfo.pFunc->PushCell(c2_addr);
314 g_SortInfo.pFunc->PushCell(g_SortInfo.array_addr);
315 g_SortInfo.pFunc->PushCell(g_SortInfo.hndl);
316 g_SortInfo.pFunc->Execute(&result);
321 static cell_t sm_SortCustom2D(IPluginContext *pContext, const cell_t *params)
324 cell_t array_size = params[2];
325 IPluginFunction *pFunction;
327 pContext->LocalToPhysAddr(params[1], &array);
329 if ((pFunction=pContext->GetFunctionById(params[3])) == NULL)
331 return pContext->ThrowNativeError("Function %x is not a valid function", params[3]);
334 /** back up the old indices, replace the indices with something easier */
335 cell_t amx_addr, *phys_addr;
337 if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE)
339 pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort");
343 sort_info oldinfo = g_SortInfo;
345 g_SortInfo.pFunc = pFunction;
346 g_SortInfo.hndl = params[4];
347 g_SortInfo.array_addr = params[1];
349 /** Same process as in strings, back up the old indices for later fixup */
350 g_SortInfo.array_base = array;
351 g_SortInfo.array_remap = phys_addr;
353 for (int i=0; i<array_size; i++)
355 phys_addr[i] = array[i];
359 qsort(array, array_size, sizeof(cell_t), sort2d_amx_custom);
361 /** Fixup process! */
362 for (int i=0; i<array_size; i++)
364 /* Compute the final address of the old array and subtract the new location.
365 * This is the fixed up distance.
367 array[i] = ((char *)&array[array[i]] + phys_addr[array[i]]) - (char *)&array[i];
370 pContext->HeapPop(amx_addr);
372 g_SortInfo = oldinfo;
377 REGISTER_NATIVES(sortNatives)
379 {"SortIntegers", sm_SortIntegers},
380 {"SortFloats", sm_SortFloats},
381 {"SortStrings", sm_SortStrings},
382 {"SortCustom1D", sm_SortCustom1D},
383 {"SortCustom2D", sm_SortCustom2D},