Skip to content

Instantly share code, notes, and snippets.

@Tarmean
Created March 28, 2018 20:57
Show Gist options
  • Select an option

  • Save Tarmean/9610d79e54b982ffacd266344403e782 to your computer and use it in GitHub Desktop.

Select an option

Save Tarmean/9610d79e54b982ffacd266344403e782 to your computer and use it in GitHub Desktop.
int getweaponname(lua_State* L) {
int** userdata = (int**)lua_touserdata(L, 1);
int idx = luaL_checkint(L, 2);
luaL_argcheck(L, userdata != NULL, 1, "`pawn' expected");
int* pawn = (int*)userdata[0][2];
int* weapon_arr_start = (int*)pawn[1];
int* weapon_arr_end = (int*)pawn[2];
int* weapon_addr = weapon_arr_start + 2 * idx;
if (weapon_addr < weapon_arr_start || weapon_addr >= weapon_arr_end) {
return luaL_error(L, "weapon index %d out of bounds", idx);
}
int* weapon = (int*)*weapon_addr;
int kind = weapon[5];
if (kind == 15) {
char* name = (char*)weapon;
lua_pushstring(L, name);
return 1;
}
else if (kind == 31) {
char* name = *(char**)weapon;
lua_pushstring(L, name);
return 1;
}
else {
return luaL_error(L, "weapon kind thingy %d unknown!", kind);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment