The current map played shouldn't appear in MapChooser votes or RTV votes now. It can still be nominated in RTV though.
3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "sm_globals.h"
33 #include <IHandleSys.h>
36 #include "HandleSys.h"
37 #include "CellArray.h"
39 /***********************************
40 * About the double array hack *
41 ***************************
43 Double arrays in Pawn are vectors offset by the current offset. For example:
47 In this array, index 0 contains the offset from the current offset which
48 results in the final vector [2] (at [0][2]). Meaning, to dereference [1][2],
51 address = &array[1] + array[1] + 2 * sizeof(cell)
53 The fact that each offset is from the _current_ position rather than the _base_
54 position is very important. It means that if you to try to swap vector positions,
55 the offsets will no longer match, because their current position has changed. A
56 simple and ingenious way around this is to back up the positions in a separate array,
57 then to overwrite each position in the old array with absolute indices. Pseudo C++ code:
59 cell *array; //assumed to be set to the 2+D array
60 cell *old_offsets = new cell[2];
61 for (int i=0; i<2; i++)
63 old_offsets = array[i];
67 Now, you can swap the array indices with no problem, and do a reverse-lookup to find the original addresses.
68 After sorting/modification is done, you must relocate the new indices. For example, if the two vectors in our
69 demo array were swapped, array[0] would be 1 and array[1] would be 0. This is invalid to the virtual machine.
70 Luckily, this is also simple -- all the information is there.
72 for (int i=0; i<2; i++)
74 //get the # of the vector we want to relocate in
75 cell vector_index = array[i];
76 //get the real address of this vector
77 char *real_address = (char *)array + (vector_index * sizeof(cell)) + old_offsets[vector_index];
78 //calc and store the new distance offset
79 array[i] = real_address - ( (char *)array + (vector_index + sizeof(cell)) )
82 Note that the inner expression can be heavily reduced; it is expanded for readability.
83 **********************************/
91 int sort_ints_asc(const void *int1, const void *int2)
93 return (*(int *)int1) - (*(int *)int2);
96 int sort_ints_desc(const void *int1, const void *int2)
98 return (*(int *)int2) - (*(int *)int1);
101 static cell_t sm_SortIntegers(IPluginContext *pContext, const cell_t *params)
104 cell_t array_size = params[2];
105 cell_t type = params[3];
107 pContext->LocalToPhysAddr(params[1], &array);
109 if (type == Sort_Ascending)
111 qsort(array, array_size, sizeof(cell_t), sort_ints_asc);
113 qsort(array, array_size, sizeof(cell_t), sort_ints_desc);
119 int sort_floats_asc(const void *float1, const void *float2)
121 float r1 = *(float *)float1;
122 float r2 = *(float *)float2;
127 } else if (r2 < r1) {
134 int sort_floats_desc(const void *float1, const void *float2)
136 float r1 = *(float *)float1;
137 float r2 = *(float *)float2;
142 } else if (r2 < r1) {
149 static cell_t sm_SortFloats(IPluginContext *pContext, const cell_t *params)
152 cell_t array_size = params[2];
153 cell_t type = params[3];
155 pContext->LocalToPhysAddr(params[1], &array);
157 if (type == Sort_Ascending)
159 qsort(array, array_size, sizeof(cell_t), sort_floats_asc);
161 qsort(array, array_size, sizeof(cell_t), sort_floats_desc);
167 static cell_t *g_CurStringArray = NULL;
168 static cell_t *g_CurRebaseMap = NULL;
170 int sort_strings_asc(const void *blk1, const void *blk2)
172 cell_t reloc1 = *(cell_t *)blk1;
173 cell_t reloc2 = *(cell_t *)blk2;
175 char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]);
176 char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]);
178 return strcmp(str1, str2);
181 int sort_strings_desc(const void *blk1, const void *blk2)
183 cell_t reloc1 = *(cell_t *)blk1;
184 cell_t reloc2 = *(cell_t *)blk2;
186 char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]);
187 char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]);
189 return strcmp(str2, str1);
192 static cell_t sm_SortStrings(IPluginContext *pContext, const cell_t *params)
195 cell_t array_size = params[2];
196 cell_t type = params[3];
198 pContext->LocalToPhysAddr(params[1], &array);
200 /** HACKHACK - back up the old indices, replace the indices with something easier */
201 cell_t amx_addr, *phys_addr;
203 if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE)
205 pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort");
208 g_CurStringArray = array;
209 g_CurRebaseMap = phys_addr;
211 for (int i=0; i<array_size; i++)
213 phys_addr[i] = array[i];
217 if (type == Sort_Ascending)
219 qsort(array, array_size, sizeof(cell_t), sort_strings_asc);
221 qsort(array, array_size, sizeof(cell_t), sort_strings_desc);
224 /* END HACKHACK - restore what we damaged so Pawn doesn't throw up.
225 * We'll browse through each index of the array and patch up the distance.
227 for (int i=0; i<array_size; i++)
229 /* Compute the final address of the old array and subtract the new location.
230 * This is the fixed up distance.
232 array[i] = ((char *)&array[array[i]] + phys_addr[array[i]]) - (char *)&array[i];
235 pContext->HeapPop(amx_addr);
237 g_CurStringArray = NULL;
238 g_CurRebaseMap = NULL;
245 IPluginFunction *pFunc;
252 sort_info g_SortInfo;
254 int sort1d_amx_custom(const void *elem1, const void *elem2)
256 cell_t c1 = *(cell_t *)elem1;
257 cell_t c2 = *(cell_t *)elem2;
260 IPluginFunction *pf = g_SortInfo.pFunc;
263 pf->PushCell(g_SortInfo.array_addr);
264 pf->PushCell(g_SortInfo.hndl);
265 pf->Execute(&result);
270 static cell_t sm_SortCustom1D(IPluginContext *pContext, const cell_t *params)
273 cell_t array_size = params[2];
275 IPluginFunction *pFunction = pContext->GetFunctionById(params[3]);
278 return pContext->ThrowNativeError("Function %x is not a valid function", params[3]);
281 pContext->LocalToPhysAddr(params[1], &array);
283 sort_info oldinfo = g_SortInfo;
286 g_SortInfo.hndl = params[4];
287 g_SortInfo.array_addr = params[1];
288 g_SortInfo.array_remap = NULL;
289 g_SortInfo.array_base = NULL;
290 g_SortInfo.pFunc = pFunction;
292 qsort(array, array_size, sizeof(cell_t), sort1d_amx_custom);
294 g_SortInfo = oldinfo;
299 int sort2d_amx_custom(const void *elem1, const void *elem2)
301 cell_t c1 = *(cell_t *)elem1;
302 cell_t c2 = *(cell_t *)elem2;
304 cell_t c1_addr = g_SortInfo.array_addr + (c1 * sizeof(cell_t)) + g_SortInfo.array_remap[c1];
305 cell_t c2_addr = g_SortInfo.array_addr + (c2 * sizeof(cell_t)) + g_SortInfo.array_remap[c2];
307 IPluginContext *pContext = g_SortInfo.pFunc->GetParentContext();
309 pContext->LocalToPhysAddr(c1_addr, &c1_r);
310 pContext->LocalToPhysAddr(c2_addr, &c2_r);
313 g_SortInfo.pFunc->PushCell(c1_addr);
314 g_SortInfo.pFunc->PushCell(c2_addr);
315 g_SortInfo.pFunc->PushCell(g_SortInfo.array_addr);
316 g_SortInfo.pFunc->PushCell(g_SortInfo.hndl);
317 g_SortInfo.pFunc->Execute(&result);
322 static cell_t sm_SortCustom2D(IPluginContext *pContext, const cell_t *params)
325 cell_t array_size = params[2];
326 IPluginFunction *pFunction;
328 pContext->LocalToPhysAddr(params[1], &array);
330 if ((pFunction=pContext->GetFunctionById(params[3])) == NULL)
332 return pContext->ThrowNativeError("Function %x is not a valid function", params[3]);
335 /** back up the old indices, replace the indices with something easier */
336 cell_t amx_addr, *phys_addr;
338 if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE)
340 pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort");
344 sort_info oldinfo = g_SortInfo;
346 g_SortInfo.pFunc = pFunction;
347 g_SortInfo.hndl = params[4];
348 g_SortInfo.array_addr = params[1];
350 /** Same process as in strings, back up the old indices for later fixup */
351 g_SortInfo.array_base = array;
352 g_SortInfo.array_remap = phys_addr;
354 for (int i=0; i<array_size; i++)
356 phys_addr[i] = array[i];
360 qsort(array, array_size, sizeof(cell_t), sort2d_amx_custom);
362 /** Fixup process! */
363 for (int i=0; i<array_size; i++)
365 /* Compute the final address of the old array and subtract the new location.
366 * This is the fixed up distance.
368 array[i] = ((char *)&array[array[i]] + phys_addr[array[i]]) - (char *)&array[i];
371 pContext->HeapPop(amx_addr);
373 g_SortInfo = oldinfo;
385 int sort_adtarray_strings_asc(const void *str1, const void *str2)
387 return strcmp((char *) str1, (char *) str2);
390 int sort_adtarray_strings_desc(const void *str1, const void *str2)
392 return strcmp((char *) str2, (char *) str1);
395 static cell_t sm_SortADTArray(IPluginContext *pContext, const cell_t *params)
399 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
401 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&cArray))
404 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
407 cell_t order = params[2];
408 cell_t type = params[3];
409 size_t arraysize = cArray->size();
410 size_t blocksize = cArray->blocksize();
411 cell_t *array = cArray->base();
413 if (type == Sort_Integer)
415 if (order == Sort_Ascending)
417 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_ints_asc);
421 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_ints_desc);
424 else if (type == Sort_Float)
426 if (order == Sort_Ascending)
428 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_floats_asc);
432 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_floats_desc);
435 else if (type == Sort_String)
437 if (order == Sort_Ascending)
439 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_adtarray_strings_asc);
443 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_adtarray_strings_desc);
452 IPluginFunction *pFunc;
459 sort_infoADT g_SortInfoADT;
461 int sort_adtarray_custom(const void *elem1, const void *elem2)
464 IPluginFunction *pf = g_SortInfoADT.pFunc;
465 pf->PushCell(((cell_t) ((cell_t *) elem1 - g_SortInfoADT.array_base)) / g_SortInfoADT.array_bsize);
466 pf->PushCell(((cell_t) ((cell_t *) elem2 - g_SortInfoADT.array_base)) / g_SortInfoADT.array_bsize);
467 pf->PushCell(g_SortInfoADT.array_hndl);
468 pf->PushCell(g_SortInfoADT.hndl);
469 pf->Execute(&result);
474 static cell_t sm_SortADTArrayCustom(IPluginContext *pContext, const cell_t *params)
478 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
480 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&cArray))
483 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
486 IPluginFunction *pFunction = pContext->GetFunctionById(params[2]);
489 return pContext->ThrowNativeError("Function %x is not a valid function", params[2]);
492 size_t arraysize = cArray->size();
493 size_t blocksize = cArray->blocksize();
494 cell_t *array = cArray->base();
496 sort_infoADT oldinfo = g_SortInfoADT;
498 g_SortInfoADT.pFunc = pFunction;
499 g_SortInfoADT.array_base = array;
500 g_SortInfoADT.array_bsize = (cell_t) blocksize;
501 g_SortInfoADT.array_hndl = params[1];
502 g_SortInfoADT.hndl = params[3];
504 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_adtarray_custom);
506 g_SortInfoADT = oldinfo;
511 REGISTER_NATIVES(sortNatives)
513 {"SortIntegers", sm_SortIntegers},
514 {"SortFloats", sm_SortFloats},
515 {"SortStrings", sm_SortStrings},
516 {"SortCustom1D", sm_SortCustom1D},
517 {"SortCustom2D", sm_SortCustom2D},
518 {"SortADTArray", sm_SortADTArray},
519 {"SortADTArrayCustom", sm_SortADTArrayCustom},