Swapping items

Locked
nexus024
Posts: 148
Joined: Fri Oct 06, 2006 7:26 pm

Swapping items

Post 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.
corncobman
Posts: 304
Joined: Fri Aug 08, 2003 7:00 am

Re: Swapping items

Post 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.
-It is not the fall that kills you. It's the sudden stop at the end. (Douglas Adams)-

[url=http://www.violationentertainment.com/misc/ccm]-An eyeful a day is bloody fantastic!-[/url]
jkoder
Posts: 59
Joined: Tue Jun 17, 2008 9:10 pm

Re: Swapping items

Post 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.
nexus024
Posts: 148
Joined: Fri Oct 06, 2006 7:26 pm

Re: Swapping items

Post 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
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: Swapping items

Post 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.
nexus024
Posts: 148
Joined: Fri Oct 06, 2006 7:26 pm

Re: Swapping items

Post 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:
corncobman
Posts: 304
Joined: Fri Aug 08, 2003 7:00 am

Re: Swapping items

Post 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");
-It is not the fall that kills you. It's the sudden stop at the end. (Douglas Adams)-

[url=http://www.violationentertainment.com/misc/ccm]-An eyeful a day is bloody fantastic!-[/url]
nexus024
Posts: 148
Joined: Fri Oct 06, 2006 7:26 pm

Re: Swapping items

Post 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?
corncobman
Posts: 304
Joined: Fri Aug 08, 2003 7:00 am

Re: Swapping items

Post 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.
-It is not the fall that kills you. It's the sudden stop at the end. (Douglas Adams)-

[url=http://www.violationentertainment.com/misc/ccm]-An eyeful a day is bloody fantastic!-[/url]
nexus024
Posts: 148
Joined: Fri Oct 06, 2006 7:26 pm

Re: Swapping items

Post by nexus024 »

I have already included the header file. It shouldnt matter which one its in as long as its included. right?
corncobman
Posts: 304
Joined: Fri Aug 08, 2003 7:00 am

Re: Swapping items

Post 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.
-It is not the fall that kills you. It's the sudden stop at the end. (Douglas Adams)-

[url=http://www.violationentertainment.com/misc/ccm]-An eyeful a day is bloody fantastic!-[/url]
nexus024
Posts: 148
Joined: Fri Oct 06, 2006 7:26 pm

Re: Swapping items

Post 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;
corncobman
Posts: 304
Joined: Fri Aug 08, 2003 7:00 am

Re: Swapping items

Post by corncobman »

The only thing I can think of to try is

strcpy(ent->item, va("%s", as_swap_item_health_mega.string) );
-It is not the fall that kills you. It's the sudden stop at the end. (Douglas Adams)-

[url=http://www.violationentertainment.com/misc/ccm]-An eyeful a day is bloody fantastic!-[/url]
nexus024
Posts: 148
Joined: Fri Oct 06, 2006 7:26 pm

Re: Swapping items

Post by nexus024 »

same warnings...

passing 1 of '__strcpy_small' from incompatible pointer type
passing 1 of 'strcpy' from incompatible pointer type
corncobman
Posts: 304
Joined: Fri Aug 08, 2003 7:00 am

Re: Swapping items

Post by corncobman »

What are you using to compile?
-It is not the fall that kills you. It's the sudden stop at the end. (Douglas Adams)-

[url=http://www.violationentertainment.com/misc/ccm]-An eyeful a day is bloody fantastic!-[/url]
nexus024
Posts: 148
Joined: Fri Oct 06, 2006 7:26 pm

Re: Swapping items

Post by nexus024 »

gcc make
corncobman
Posts: 304
Joined: Fri Aug 08, 2003 7:00 am

Re: Swapping items

Post 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.
-It is not the fall that kills you. It's the sudden stop at the end. (Douglas Adams)-

[url=http://www.violationentertainment.com/misc/ccm]-An eyeful a day is bloody fantastic!-[/url]
nexus024
Posts: 148
Joined: Fri Oct 06, 2006 7:26 pm

Re: Swapping items

Post 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???
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: Swapping items

Post by ^misantropia^ »

Is ent->item a string? I somehow doubt it.
nexus024
Posts: 148
Joined: Fri Oct 06, 2006 7:26 pm

Re: Swapping items

Post by nexus024 »

That was the problem!
corncobman
Posts: 304
Joined: Fri Aug 08, 2003 7:00 am

Re: Swapping items

Post by corncobman »

Oops! *facepalm*

I knew it was something blatantly obvious that I missed somehow. :smirk:
-It is not the fall that kills you. It's the sudden stop at the end. (Douglas Adams)-

[url=http://www.violationentertainment.com/misc/ccm]-An eyeful a day is bloody fantastic!-[/url]
Locked