Page 1 of 1

Swapping items

Posted: Fri Jul 25, 2008 7:06 pm
by nexus024
Could someone point me in the right direction for swapping items with other items. For example, I would like my mod to have the ability to replace the mega health with a weapon or some other item such as red armor etc.

Re: Swapping items

Posted: Sat Jul 26, 2008 4:43 pm
by corncobman
You can swap the classname of the item with the classname of another item in g_spawn.c in the G_CallSpawn() function.

Say you want to replace all the railguns on the map to BFGs

if(Q_stricmp(ent->classname, "weapon_railgun") == 0)
strcpy(ent->classname, "weapon_bfg");

as long as it's before this bit

// check item spawn functions
for ( item=bg_itemlist+1 ; item->classname ; item++ )

it should work.

Re: Swapping items

Posted: Sat Jul 26, 2008 9:05 pm
by jkoder
corncobman wrote:You can swap the classname of the item with the classname of another item in g_spawn.c in the G_CallSpawn() function.

Say you want to replace all the railguns on the map to BFGs

if(Q_stricmp(ent->classname, "weapon_railgun") == 0)
strcpy(ent->classname, "weapon_bfg");

as long as it's before this bit

// check item spawn functions
for ( item=bg_itemlist+1 ; item->classname ; item++ )

it should work.
But I think that just spawns different weapons rather than lets you choose in real time which power-up you really want. You could implement this as he wants but it would be nice to have some UI code that lets you toggle between holdables just like you can with weapons.

Nexus. What can you actually do? Can you do modeling or something graphical? I would be willing to help you code as long as you did something for me. I am in (what I think is) the opposite situation to you, I want to code but also want to avoid learning blender and GTKRadiant etc.

Re: Swapping items

Posted: Sun Jul 27, 2008 2:31 am
by nexus024
I am not great with C but I am learning. What corncobman wrote did work but as you said its just for weapons. I'm assuming that doing this similar thing for items isn't as easy?

Also, I would like to make this configurable and not just static within the function. For instance, I would like to have a command similar to this:

modname_swap_weapon_railgun "weapon_bfg" // swaps railgun with bfg on the fly

Re: Swapping items

Posted: Sun Jul 27, 2008 9:08 am
by ^misantropia^
Doing entity swapping during game play, when the level and its assets have already been loaded, is somewhat of a hassle.

The client won't load assets of entities that aren't part of the map. So when you mix in a new entity, you'll need to load its assets manually. Grep the cgame source for the CG_Register*() functions.

Re: Swapping items

Posted: Sun Jul 27, 2008 11:56 am
by nexus024
Well, when I said on the fly I didn't mean during gameplay. I would like to set it during the change to a new map so each map has its own different settings.

In my mod I have the ability to exec a config file for each map within my rotation script. All that I need to do is figure out how to code this in to allow the swapping of items and weapons.

EDIT: I was able to successfully swap the mega with railgun with corncob's suggestion so items CAN also be swapped. Now I just have to figure out a way to make this configurable from my mod's .c file. Any suggestions from you C guru's? :sly:

EDIT: I have run into a problem with this scheme. I am needing to replace the spawn point with another item / weapon not the entire class. What is happening for instance, on ctf4 I would like to move the railgun to the mid where mega spawns. Then make the mega spawn on both of the platforms where the railgun once was.

Code: Select all

if(Q_stricmp(ent->classname, "item_health_mega") == 0) {
     strcpy(ent->item, "weapon_railgun");
}
if(Q_stricmp(ent->classname, "weapon_railgun") == 0) {
     strcpy(ent->item, "item_health_mega");
}
Using this scheme above it puts the railgun where mega was no problem. However, once the second if is executed it replaces all of the railguns with mega's. :question:

Re: Swapping items

Posted: Sun Jul 27, 2008 4:52 pm
by corncobman
In my mod, what I did was to use cvars, for example something like

if(Q_stricmp(ent->classname, "bfg") == 0) {
strcpy(ent->item, replacebfg.string);
}

Define replacebfg in g_main.c, somewhere in the cvar table list and add an entry for it in g_local.h

you can then put something like

set replacebfg "weapon_railgun"

in a cfg file or set it ingame.


As for your problem, put an else before the second if.

Code: Select all

if(Q_stricmp(ent->classname, "item_health_mega") == 0) {
     strcpy(ent->item, "weapon_railgun");
}
else if(Q_stricmp(ent->classname, "weapon_railgun") == 0) {
     strcpy(ent->item, "item_health_mega");

Re: Swapping items

Posted: Sun Jul 27, 2008 7:04 pm
by nexus024
Ok, I think I did everything you suggested, however I am getting a couple warnings when compiling.

g_spawn.c

Code: Select all

if(Q_stricmp(ent->classname, "item_health_mega") == 0) {
     strcpy(ent->item, as_swap_item_health_mega.string );
}
q3as.c

Code: Select all

{ &as_swap_item_health_mega, "as_swap_item_health_mega", "item_health_mega", 0, 0, qfalse },
q3as.h

Code: Select all

extern vmCvar_t as_swap_item_health_mega;
Errors:
g_spawn.c: warning: passing arg 1 of `__strcpy_small' from incompatible pointer type

It does compile however it isn't changing the item to what I set the cvar to. Any idea what I'm doing wrong?

Re: Swapping items

Posted: Sun Jul 27, 2008 8:15 pm
by corncobman
Is there a particular reason you are putting it in q3as.c and q3as.h as opposed to g_main.c and g_local.h?

You could try putting

#include "q3as.h"

at the top of the g_spawn.c file.

Re: Swapping items

Posted: Sun Jul 27, 2008 8:23 pm
by nexus024
I have already included the header file. It shouldnt matter which one its in as long as its included. right?

Re: Swapping items

Posted: Sun Jul 27, 2008 8:29 pm
by corncobman
yes. Also make sure you have actually defined as_swap_item_health_mega, probably in q3as.c or something.

vmCvar_t as_swap_item_health_mega;

The bit in the header file is to let other files see it.

Re: Swapping items

Posted: Sun Jul 27, 2008 8:37 pm
by nexus024
Yes, I forgot to mention it but I do have this added at the end of the cvar list in q3as.c

vmCvar_t as_swap_item_health_mega;

Re: Swapping items

Posted: Sun Jul 27, 2008 8:54 pm
by corncobman
The only thing I can think of to try is

strcpy(ent->item, va("%s", as_swap_item_health_mega.string) );

Re: Swapping items

Posted: Sun Jul 27, 2008 9:36 pm
by nexus024
same warnings...

passing 1 of '__strcpy_small' from incompatible pointer type
passing 1 of 'strcpy' from incompatible pointer type

Re: Swapping items

Posted: Sun Jul 27, 2008 10:32 pm
by corncobman
What are you using to compile?

Re: Swapping items

Posted: Mon Jul 28, 2008 1:31 am
by nexus024
gcc make

Re: Swapping items

Posted: Mon Jul 28, 2008 1:41 am
by corncobman
Dunno, I don't get that warning when I compile and it works fine for me, but I am using a different compile method.

Re: Swapping items

Posted: Mon Jul 28, 2008 1:59 am
by nexus024
I know it has to be something small im doing wrong... anyone else ever seen this error and/or know what it means???

Re: Swapping items

Posted: Mon Jul 28, 2008 7:07 am
by ^misantropia^
Is ent->item a string? I somehow doubt it.

Re: Swapping items

Posted: Mon Jul 28, 2008 11:32 am
by nexus024
That was the problem!

Re: Swapping items

Posted: Mon Jul 28, 2008 12:16 pm
by corncobman
Oops! *facepalm*

I knew it was something blatantly obvious that I missed somehow. :smirk: