Build against MM:S 1.10.
3 * =============================================================================
5 * Copyright (C) 2004-2008 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 "UserMessages.h"
34 #ifndef USE_PROTOBUF_USERMESSAGES
36 #include "sourcemod.h"
39 #include <HalfLife2.h>
40 #include "smn_usermsgs.h"
41 #include "sourcemod.h"
42 #include "logic_bridge.h"
44 static cell_t smn_BfWriteBool(IPluginContext *pCtx, const cell_t *params)
46 Handle_t hndl = static_cast<Handle_t>(params[1]);
52 sec.pIdentity = g_pCoreIdent;
54 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
57 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
60 pBitBuf->WriteOneBit(params[2]);
65 static cell_t smn_BfWriteByte(IPluginContext *pCtx, const cell_t *params)
67 Handle_t hndl = static_cast<Handle_t>(params[1]);
73 sec.pIdentity = g_pCoreIdent;
75 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
78 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
81 pBitBuf->WriteByte(params[2]);
86 static cell_t smn_BfWriteChar(IPluginContext *pCtx, const cell_t *params)
88 Handle_t hndl = static_cast<Handle_t>(params[1]);
94 sec.pIdentity = g_pCoreIdent;
96 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
99 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
102 pBitBuf->WriteChar(params[2]);
107 static cell_t smn_BfWriteShort(IPluginContext *pCtx, const cell_t *params)
109 Handle_t hndl = static_cast<Handle_t>(params[1]);
115 sec.pIdentity = g_pCoreIdent;
117 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
120 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
123 pBitBuf->WriteShort(params[2]);
128 static cell_t smn_BfWriteWord(IPluginContext *pCtx, const cell_t *params)
130 Handle_t hndl = static_cast<Handle_t>(params[1]);
136 sec.pIdentity = g_pCoreIdent;
138 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
141 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
144 pBitBuf->WriteWord(params[2]);
149 static cell_t smn_BfWriteNum(IPluginContext *pCtx, const cell_t *params)
151 Handle_t hndl = static_cast<Handle_t>(params[1]);
157 sec.pIdentity = g_pCoreIdent;
159 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
162 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
165 pBitBuf->WriteLong(static_cast<long>(params[2]));
170 static cell_t smn_BfWriteFloat(IPluginContext *pCtx, const cell_t *params)
172 Handle_t hndl = static_cast<Handle_t>(params[1]);
178 sec.pIdentity = g_pCoreIdent;
180 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
183 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
186 pBitBuf->WriteFloat(sp_ctof(params[2]));
191 static cell_t smn_BfWriteString(IPluginContext *pCtx, const cell_t *params)
193 Handle_t hndl = static_cast<Handle_t>(params[1]);
200 sec.pIdentity = g_pCoreIdent;
202 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
205 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
209 if ((err=pCtx->LocalToString(params[2], &str)) != SP_ERROR_NONE)
211 pCtx->ThrowNativeErrorEx(err, NULL);
215 pBitBuf->WriteString(str);
220 static cell_t smn_BfWriteEntity(IPluginContext *pCtx, const cell_t *params)
222 Handle_t hndl = static_cast<Handle_t>(params[1]);
228 sec.pIdentity = g_pCoreIdent;
230 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
233 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
236 int index = g_HL2.ReferenceToIndex(params[2]);
243 pBitBuf->WriteShort(index);
248 static cell_t smn_BfWriteAngle(IPluginContext *pCtx, const cell_t *params)
250 Handle_t hndl = static_cast<Handle_t>(params[1]);
256 sec.pIdentity = g_pCoreIdent;
258 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
261 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
264 pBitBuf->WriteBitAngle(sp_ctof(params[2]), params[3]);
269 static cell_t smn_BfWriteCoord(IPluginContext *pCtx, const cell_t *params)
271 Handle_t hndl = static_cast<Handle_t>(params[1]);
277 sec.pIdentity = g_pCoreIdent;
279 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
282 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
285 pBitBuf->WriteBitCoord(sp_ctof(params[2]));
290 static cell_t smn_BfWriteVecCoord(IPluginContext *pCtx, const cell_t *params)
292 Handle_t hndl = static_cast<Handle_t>(params[1]);
298 sec.pIdentity = g_pCoreIdent;
300 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
303 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
307 pCtx->LocalToPhysAddr(params[2], &pVec);
308 Vector vec(sp_ctof(pVec[0]), sp_ctof(pVec[1]), sp_ctof(pVec[2]));
309 pBitBuf->WriteBitVec3Coord(vec);
314 static cell_t smn_BfWriteVecNormal(IPluginContext *pCtx, const cell_t *params)
316 Handle_t hndl = static_cast<Handle_t>(params[1]);
322 sec.pIdentity = g_pCoreIdent;
324 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
327 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
331 pCtx->LocalToPhysAddr(params[2], &pVec);
332 Vector vec(sp_ctof(pVec[0]), sp_ctof(pVec[1]), sp_ctof(pVec[2]));
333 pBitBuf->WriteBitVec3Normal(vec);
338 static cell_t smn_BfWriteAngles(IPluginContext *pCtx, const cell_t *params)
340 Handle_t hndl = static_cast<Handle_t>(params[1]);
346 sec.pIdentity = g_pCoreIdent;
348 if ((herr=handlesys->ReadHandle(hndl, g_WrBitBufType, &sec, (void **)&pBitBuf))
351 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
355 pCtx->LocalToPhysAddr(params[2], &pAng);
356 QAngle ang(sp_ctof(pAng[0]), sp_ctof(pAng[1]), sp_ctof(pAng[2]));
357 pBitBuf->WriteBitAngles(ang);
362 static cell_t smn_BfReadBool(IPluginContext *pCtx, const cell_t *params)
364 Handle_t hndl = static_cast<Handle_t>(params[1]);
370 sec.pIdentity = g_pCoreIdent;
372 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
375 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
378 return pBitBuf->ReadOneBit() ? 1 : 0;
381 static cell_t smn_BfReadByte(IPluginContext *pCtx, const cell_t *params)
383 Handle_t hndl = static_cast<Handle_t>(params[1]);
389 sec.pIdentity = g_pCoreIdent;
391 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
394 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
397 return pBitBuf->ReadByte();
400 static cell_t smn_BfReadChar(IPluginContext *pCtx, const cell_t *params)
402 Handle_t hndl = static_cast<Handle_t>(params[1]);
408 sec.pIdentity = g_pCoreIdent;
410 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
413 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
416 return pBitBuf->ReadChar();
419 static cell_t smn_BfReadShort(IPluginContext *pCtx, const cell_t *params)
421 Handle_t hndl = static_cast<Handle_t>(params[1]);
427 sec.pIdentity = g_pCoreIdent;
429 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
432 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
435 return pBitBuf->ReadShort();
438 static cell_t smn_BfReadWord(IPluginContext *pCtx, const cell_t *params)
440 Handle_t hndl = static_cast<Handle_t>(params[1]);
446 sec.pIdentity = g_pCoreIdent;
448 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
451 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
454 return pBitBuf->ReadWord();
457 static cell_t smn_BfReadNum(IPluginContext *pCtx, const cell_t *params)
459 Handle_t hndl = static_cast<Handle_t>(params[1]);
465 sec.pIdentity = g_pCoreIdent;
467 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
470 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
473 return static_cast<cell_t>(pBitBuf->ReadLong());
476 static cell_t smn_BfReadFloat(IPluginContext *pCtx, const cell_t *params)
478 Handle_t hndl = static_cast<Handle_t>(params[1]);
484 sec.pIdentity = g_pCoreIdent;
486 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
489 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
492 return sp_ftoc(pBitBuf->ReadFloat());
495 static cell_t smn_BfReadString(IPluginContext *pCtx, const cell_t *params)
497 Handle_t hndl = static_cast<Handle_t>(params[1]);
505 sec.pIdentity = g_pCoreIdent;
507 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
510 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
513 pCtx->LocalToPhysAddr(params[2], (cell_t **)&buf);
514 pBitBuf->ReadString(buf, params[3], params[4] ? true : false, &numChars);
516 if (pBitBuf->IsOverflowed())
518 return -numChars - 1;
524 static cell_t smn_BfReadEntity(IPluginContext *pCtx, const cell_t *params)
526 Handle_t hndl = static_cast<Handle_t>(params[1]);
532 sec.pIdentity = g_pCoreIdent;
534 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
537 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
540 int ref = g_HL2.IndexToReference(pBitBuf->ReadShort());
542 return g_HL2.ReferenceToBCompatRef(ref);
545 static cell_t smn_BfReadAngle(IPluginContext *pCtx, const cell_t *params)
547 Handle_t hndl = static_cast<Handle_t>(params[1]);
553 sec.pIdentity = g_pCoreIdent;
555 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
558 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
561 return sp_ftoc(pBitBuf->ReadBitAngle(params[2]));
564 static cell_t smn_BfReadCoord(IPluginContext *pCtx, const cell_t *params)
566 Handle_t hndl = static_cast<Handle_t>(params[1]);
572 sec.pIdentity = g_pCoreIdent;
574 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
577 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
580 return sp_ftoc(pBitBuf->ReadBitCoord());
583 static cell_t smn_BfReadVecCoord(IPluginContext *pCtx, const cell_t *params)
585 Handle_t hndl = static_cast<Handle_t>(params[1]);
591 sec.pIdentity = g_pCoreIdent;
593 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
596 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
600 pCtx->LocalToPhysAddr(params[2], &pVec);
603 pBitBuf->ReadBitVec3Coord(vec);
605 pVec[0] = sp_ftoc(vec.x);
606 pVec[1] = sp_ftoc(vec.y);
607 pVec[2] = sp_ftoc(vec.z);
612 static cell_t smn_BfReadVecNormal(IPluginContext *pCtx, const cell_t *params)
614 Handle_t hndl = static_cast<Handle_t>(params[1]);
620 sec.pIdentity = g_pCoreIdent;
622 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
625 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
629 pCtx->LocalToPhysAddr(params[2], &pVec);
632 pBitBuf->ReadBitVec3Normal(vec);
634 pVec[0] = sp_ftoc(vec.x);
635 pVec[1] = sp_ftoc(vec.y);
636 pVec[2] = sp_ftoc(vec.z);
641 static cell_t smn_BfReadAngles(IPluginContext *pCtx, const cell_t *params)
643 Handle_t hndl = static_cast<Handle_t>(params[1]);
649 sec.pIdentity = g_pCoreIdent;
651 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
654 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
658 pCtx->LocalToPhysAddr(params[2], &pAng);
661 pBitBuf->ReadBitAngles(ang);
663 pAng[0] = sp_ftoc(ang.x);
664 pAng[1] = sp_ftoc(ang.y);
665 pAng[2] = sp_ftoc(ang.z);
670 static cell_t smn_BfGetNumBytesLeft(IPluginContext *pCtx, const cell_t *params)
672 Handle_t hndl = static_cast<Handle_t>(params[1]);
678 sec.pIdentity = g_pCoreIdent;
680 if ((herr=handlesys->ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
683 return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
686 return pBitBuf->GetNumBitsLeft() >> 3;
689 REGISTER_NATIVES(bitbufnatives)
691 {"BfWriteBool", smn_BfWriteBool},
692 {"BfWriteByte", smn_BfWriteByte},
693 {"BfWriteChar", smn_BfWriteChar},
694 {"BfWriteShort", smn_BfWriteShort},
695 {"BfWriteWord", smn_BfWriteWord},
696 {"BfWriteNum", smn_BfWriteNum},
697 {"BfWriteFloat", smn_BfWriteFloat},
698 {"BfWriteString", smn_BfWriteString},
699 {"BfWriteEntity", smn_BfWriteEntity},
700 {"BfWriteAngle", smn_BfWriteAngle},
701 {"BfWriteCoord", smn_BfWriteCoord},
702 {"BfWriteVecCoord", smn_BfWriteVecCoord},
703 {"BfWriteVecNormal", smn_BfWriteVecNormal},
704 {"BfWriteAngles", smn_BfWriteAngles},
705 {"BfReadBool", smn_BfReadBool},
706 {"BfReadByte", smn_BfReadByte},
707 {"BfReadChar", smn_BfReadChar},
708 {"BfReadShort", smn_BfReadShort},
709 {"BfReadWord", smn_BfReadWord},
710 {"BfReadNum", smn_BfReadNum},
711 {"BfReadFloat", smn_BfReadFloat},
712 {"BfReadString", smn_BfReadString},
713 {"BfReadEntity", smn_BfReadEntity},
714 {"BfReadAngle", smn_BfReadAngle},
715 {"BfReadCoord", smn_BfReadCoord},
716 {"BfReadVecCoord", smn_BfReadVecCoord},
717 {"BfReadVecNormal", smn_BfReadVecNormal},
718 {"BfReadAngles", smn_BfReadAngles},
719 {"BfGetNumBytesLeft", smn_BfGetNumBytesLeft},