Page 1 of 1

LUA command creation help

Posted: Sun Jul 08, 2012 12:04 pm
by Durge
Hello devs, I didn't see a lua command that would allow me to change the npc's equipment, so I figured I'd give it a stab even though I have never done C++ before. I was using the SpawnSet command as a guide and saw that I need a few things, the actual command in the luafunctions.cpp, to add it into the luafunctions.h, some slot values pre-set in the commands.cpp and a commands::setspawnequipment entry into the commands.cpp. Hoenstly, I have no idea if this is even remotely close to what should actually happen, but that was my analysis. Carrying on assuming I'm at least partially right, this is what I GUESSED might go in the LuaFunctions.cpp.

NOTE: I have never coded in C++ before and am totally unfamiliar with it, coder rage-mode inc. from devs xD

Code: Select all

int EQ2Emu_lua_SpawnEquip(lua_State* state){
	if(!lua_interface)
		return 0;
	Spawn* spawn = lua_interface->GetSpawn(state);
	string slot = commands.GetSpawnEquipmentID(variable);
	string appid = lua_interface->GetStringValue(state, 2);
	if(slot != 0xFFFFFFFF && appid.length() > 0 && spawn)
		commands.SetSpawnEquipment(0, spawn, slot, appid.c_str());
	return 0;
}
In LuaFunctions.h

Code: Select all

int EQ2Emu_lua_SpawnEquip(lua_State* state);
In Commands.cpp

Code: Select all

spawn_equip_values["1"] = SPAWN_SET_EQUIPMENT_PRIMARY;
spawn_equip_values["2"] = SPAWN_SET_EQUIPMENT_SECONDARY;
spawn_equip_values["4"] = SPAWN_SET_EQUIPMENT_HEAD;
spawn_equip_values["8"] = SPAWN_SET_EQUIPMENT_CHEST;
spawn_equip_values["16"] = SPAWN_SET_EQUIPMENT_SHOULDERS;
spawn_equip_values["32"] = SPAWN_SET_EQUIPMENT_FOREARMS;
spawn_equip_values["64"] = SPAWN_SET_EQUIPMENT_HANDS;
spawn_equip_values["128"] = SPAWN_SET_EQUIPMENT_LEGS;
spawn_equip_values["256"] = SPAWN_SET_EQUIPMENT_FEET;
spawn_equip_values["512"] = SPAWN_SET_EQUIPMENT_LRING;
spawn_equip_values["1024"] = SPAWN_SET_EQUIPMENT_RRING;
spawn_equip_values["2048"] = SPAWN_SET_EQUIPMENT_EAR1;
spawn_equip_values["4096"] = SPAWN_SET_EQUIPMENT_EAR2;
spawn_equip_values["8192"] = SPAWN_SET_EQUIPMENT_NECK;
spawn_equip_values["16384"] = SPAWN_SET_EQUIPMENT_LWRIST;
spawn_equip_values["32768"] = SPAWN_SET_EQUIPMENT_RWRIST;
spawn_equip_values["65536"] = SPAWN_SET_EQUIPMENT_RANGE;
spawn_equip_values["131072"] = SPAWN_SET_EQUIPMENT_AMMO;
spawn_equip_values["262144"] = SPAWN_SET_EQUIPMENT_WAIST;
spawn_equip_values["524288"] = SPAWN_SET_EQUIPMENT_CLOAK;
spawn_equip_values["1048576"] = SPAWN_SET_EQUIPMENT_CHARM1;
spawn_equip_values["2097152"] = SPAWN_SET_EQUIPMENT_CHARM2;
spawn_equip_values["4194304"] = SPAWN_SET_EQUIPMENT_FOOD;
spawn_equip_values["8388608"] = SPAWN_SET_EQUIPMENT_DRINK;
spawn_equip_values["16777216"] = SPAWN_SET_EQUIPMENT_TEXTURES;

Code: Select all

bool Commands::SetSpawnEquipment(Client* client, Spawn* target, int32 slot, int8 appid, bool send_update)
Not sure how to code the actual code of this ;p

Re: LUA command creation help

Posted: Mon Jul 09, 2012 11:32 am
by John Adams
First of all, are you just trying to change the appearance of the NPC? If so, that has nothing to do with "slots". NPCs do not have equipment slots. They have appearances, only. You could use the existing SpawnSet() LUA function to do this. There is no need for another LUA function. Check out Commands.cpp, the SetSpawnCommand() function.

Code: Select all

			case SPAWN_SET_VALUE_CHEST_TYPE:{
				if(target->IsEntity()){
					sprintf(tmp, "%i", ((Entity*)target)->GetChestType());
					((Entity*)target)->SetChestType(val, send_update);
				}
This switch will change the chest "appearance" to whatever value you send via LUA.

Example:

Code: Select all

SpawnSet(NPC, chest_type_id, 12345)
should change the spawn's chest model to 12345.


I highly recommend utilizing the Search function of this forum because quite a few things you have been asking are clearly outlined already. SpawnSet, for instance -
http://eq2emulator.net/phpBB3/viewtopic ... 4389#p4389
http://eq2emulator.net/phpBB3/viewtopic ... 430#p15430


PS: don't be afraid to try code. we all had to start somewhere, and using existing examples is a great way to learn.

Re: LUA command creation help

Posted: Mon Jul 09, 2012 3:38 pm
by John Adams
After taking my own advice and "Searching" a bit, I have determined that I am dead wrong about NPC Equipment not having slots :mrgreen: I was focusing only on general spawn appearance types in the spawn_npcs record, and totally forgot about the npc_appearance_equip table. You are correct in that there are no LUA functions currently to modify the data in these tables. SpawnSet() will not work, because that is just about the Spawn itself. So I suppose a new LUA Function is necessary after all ;)

Not bad for a noob coder, Durge. Now let's talk about how to make this happen.

In npc_appearance_equip, you have these values -
npc_appearance_equip.jpg
You would want to set spawn_id's slot entry first (what slot are you modifying), then the equip_type (appearance_id), and the RGB + Highlight values. So a LUA function call might look like this:

Code: Select all

SetNPCEquip(NPC, slot_id, equip_id, red, blue, green, hred, hblue, hgreen)
As for getting these values from LUA to the Spawn, you would have to be sure all these options are accessible, and I am not sure the link has been made for this yet but I will check further into it this week.

Re: LUA command creation help

Posted: Mon Jul 09, 2012 4:24 pm
by Durge
You doubting me John!! :(, JK :p. Anyway, yeah I wasn't sure how to establish the link between db and LUA, so I just kinda gave it a shot in the dark, I figured since that was literally my first time touching C++ I'd just keep the minimum in, so that's why I didn't add the coloring and such.

Re: LUA command creation help

Posted: Mon Jul 09, 2012 5:11 pm
by Jabantiz
@John
We have /spawn equipment slot type (red) (green) (blue) (hred) (hgreen) (hblue) the main issue with this command is that it does not save the info so on a respawn everything is set back to normal, wich would work for a lua command so looking at that command all you really need to do is call

Code: Select all

((Entity*)spawn)->SetEquipment(slot, type, red, green, blue, h_red, h_green, h_blue);
@Durge
The command class (commands.cpp/.h) is used for in game slash commands, in a lua function we can just do what we need it to do without calling a slash command.

Re: LUA command creation help

Posted: Mon Jul 09, 2012 5:37 pm
by Durge
Ah, I see, that makes sense, thanks.