Thrown Item Code?

Posted by Gatewaysysop2 on Fri 06 May 2005 06:20 AM — 30 posts, 104,331 views.

USA #0
Was curious if anyone happens to know of a decent implementation / snippet of thrown item code? As I understand it, the thrown item code in stock SMAUG isn't all there and was never finished so I'm looking for something to provide this functionality.

Basically looking for something that'll let you throw a weapon, either at a person or in a direction. Not sure if another codebase has a finished version of this or if there's a snippet, but if anyone's seen or heard of something useful in this regard, I'd very much appreciate a heads up.

Thanks!
Australia #1
This is one of my many TODO'S the code i have laying about somewhere is what i pulled out of a rom deriv, think it was Ghost MUD that i will eventually get around to porting over to Smaug.

USA #2
Think there's a snippet somewhere.. I'll go look if i've got it... I'm a snippet packrat of sorts :-p

--------EDIT---------
Found it, doing a "dirty" port, it's all kinds of messy so i'll try to fix it up a bit.

----------EDIT--------
Lol said forget it and i'm just rewriting it using existing functions, will still work all the same... just about 100 lines shorter and alot more simple.
*hugs smaug 1.4a and all of its incomplete mystery function greatness*
Amended on Fri 06 May 2005 01:26 PM by Txzeenath
USA #3
here it is, you will need to add in the item types to both mud.h and build.c, also act_obj.c will need to be modified for the do_put code(quiver is a good thing to copy from), just use search and it's obvious where to add them, you may also want to add a new weapontype(skill/gsn) for throwing:

http://www.alumuble.divineright.org/throw.c

Adding the new weapontype will require minimum work.. simply go into skills.c and find ranged_attack
Amended on Fri 06 May 2005 01:52 PM by Txzeenath
USA #4
Will take a look, thanks for the help!
USA #5
Anyone else who needs this code will have to wait.. There was a problem with my server and my entire mud got wiped out.. luckily I had an off-site backup from about a month before.


I'll get this back up when my mud is nice and stable. I may even release some snippets on my site when I finish it.


----------EDIT--------
Code is back up, should be fine to use.
Amended on Sat 07 May 2005 05:44 PM by Txzeenath
#6
Hmm, the link doesn't work for me. I could sure use that piece of code.
Godbless,
Longbow
#7
Does anybody have a copy of this code that I could have? Please :)


Godbless,
Longbow
Canada #8
Sounds pretty simple.
Just copy some code from the ranged_attack function if the arg's true for find_door, then check victim in room for the arg.

Then it's just an obj from char, make a forumla depending (presumably) on weight of the item and the strength of the thrower, and call the damage code, then obj_to_room(vict)...

Should take all of 10 minutes to code, don't see why you need a snippet :/
#9
Well, in all honesty, I don't know C worth beans. I'm in the process of learning, but I thought it'd be helpful to my MUD to get a throw code sooner than later. Not trying to be lazy; I just don't have the ability to do even a simple 10 minute operation in C yet. :)
USA #10
Quote:

Should take all of 10 minutes to code, don't see why you need a snippet :/


Sigh.

Is it really that difficult to grasp that what is easy for some people is not for others?

There's a reason people almost universally include examples when trying to teach others to code, in any language, and it is precisely because pseudo-explanations like yours are meaningless to those without an already good understanding of the subject matter.

IMHO, most of the people reading your post who don't "need a snippet" and can code this "in 10 minutes" would already have been able to do so without our terse explanation. To the rest of them, all they'll get out of it is feeling bad about their skill level, which accomplishes nothing.

For those interested, I may have a copy of the code that was posted here. I'll hunt around for it and post if I find it. In the mean time, I believe either SWR has thrown item code in it somewhere that you might look at.
USA #11
You are right in many ways, Gatewaysysop2, but people should always be aware that what they're trying to do is not at all easy and they shouldn't expect walk-in-the-park solutions. I strongly discourage anybody from doing anything hard until they can at least read lines of C code and understand (more or less, at the least) what the lines do.
Canada #12
Jeez, what's up your ass, Gatewaysysop? I gave step by step instructions on what to do, which is far more than what I got when I was learning to code

My mistake apparently is assuming that anyone posting on a coding board knows basic coding already, so I just told him which functions to use.
Silly silly me.

Here's an example of what I was talking about. I just hacked this together in about 5 minutes, and haven't even tested if it'll compile. It's at least a nice start though, and it *should* work, unless I missed something stupid.

/*Silly little throw - Dace K*/
void do_throw( CHAR_DATA *ch, char *argument)
{
    CHAR_DATA *victim, *vch;
    EXIT_DATA *pexit;
    ROOM_INDEX_DATA *was_in_room;
    OBJ_DATA *obj;
    char arg1[MAX_INPUT_LENGTH];
    char arg2[MAX_INPUT_LENGTH];
    char temp[MAX_INPUT_LENGTH];
    char buf[MAX_STRING_LENGTH];
    sh_int dir = -1, dist = 0, color = AT_GREY;
    char *dtxt = "somewhere";
    char *stxt = "thrown item";

    argument = one_argument(argument, arg);
    argument = one_argument(argument, arg2);

    if ( arg1[0] == '\0' )
    {
	send_to_char( "Throw what where?  At who?\n\r", ch );
	return rNONE;
    }

    victim = NULL;

    if ( ms_find_obj(ch) )
	return;

	if ( ( obj = get_obj_carry( ch, arg1 ) ) == NULL )
	{
	    send_to_char( "You do not have that item.\n\r", ch );
	    return;
	}

	/*Insert checks for itemtypes you don't want throwable here*/

    /* get an exit or a victim */
    if ( (pexit = find_door(ch, arg2, TRUE)) == NULL )
    {
	if ( (victim=get_char_room(ch, arg2)) == NULL )
	{
	    send_to_char( "Throw in what direction?\n\r", ch );
	    return rNONE;
	}
	else
	{
/*	Uncomment if you don't want people throwing at fight-engaged*
		if ( who_fighting(ch) == victim )
	    {
		send_to_char( "They are too close to release that type of attack!\n\r", ch );
		return rNONE;
	    } */
	}
    }
    else
	dir = pexit->vdir;

    /* check for ranged attacks from private rooms, etc */
    if ( !victim )
    {
	if ( IS_SET(ch->in_room->room_flags, ROOM_PRIVATE)
	||   IS_SET(ch->in_room->room_flags, ROOM_SOLITARY) )
	{
	    send_to_char( "You cannot throw an object from a private room.\n\r", ch );
	    return rNONE;
	}
	if ( ch->in_room->tunnel > 0 )
	{
	    count = 0;
	    for ( vch = ch->in_room->first_person; vch; vch = vch->next_in_room )
		++count;
	    if ( count >= ch->in_room->tunnel )
	    {
		send_to_char( "This room is too cramped to perform such an attack.\n\r", ch );
		return rNONE;
	    }
	}
    }

    if ( pexit && !pexit->to_room )
    {
	send_to_char( "Are you expecting to throw through a wall!?\n\r", ch );
	return rNONE;
    }

    /* Check for obstruction */
    if ( pexit && IS_SET(pexit->exit_info, EX_CLOSED) )
    {
	if ( IS_SET(pexit->exit_info, EX_SECRET)
	||   IS_SET(pexit->exit_info, EX_DIG) )
	    send_to_char( "Are you expecting to throw through a wall!?\n\r", ch );
	else
	    send_to_char( "Are you expecting to throw through a door!?\n\r", ch );
	return rNONE;
    }

    vch = NULL;
    if ( pexit && arg1[0] != '\0' )
    {
	if ( (vch=scan_for_victim(ch, pexit, arg1)) == NULL )
	{
	    send_to_char( "You cannot see your target.\n\r", ch );
	    return rNONE;
	}

	/* don't allow attacks on mobs stuck in another area?
	if ( IS_NPC(vch) && xIS_SET(vch->act, ACT_STAY_AREA)
	&&   ch->in_room->area != vch->in_room->area) )
	{
	}
	*/
	/*don't allow attacks on mobs that are in a no-missile room --Shaddai */
	if ( IS_SET(vch->in_room->room_flags, ROOM_NOMISSILE) )
	{
	    send_to_char( "You can't get a clean shot off.\n\r", ch );
	    return rNONE;
	}
	/* Taken out cause of wait state
	if ( !IS_NPC(ch) && !IS_NPC(vch) )
	{
	    send_to_char("Pkill like a real pkiller.\n\r", ch );
	    return rNONE;
	}
	*/

	/* can't properly target someone heavily in battle */
	if ( vch->num_fighting > max_fight(vch) )
	{
	    send_to_char( "There is too much activity there for you to get a clear shot.\n\r", ch );
	    return rNONE;
	}
    }
    if ( vch ) {
    if ( !IS_NPC( vch ) && !IS_NPC( ch ) &&
	 xIS_SET(ch->act, PLR_NICE ) )
    {
	send_to_char( "Your too nice to do that!\n\r", ch );
	return rNONE;
    }
    if ( vch && is_safe(ch, vch, TRUE) )
	    return rNONE;
    }
    was_in_room = ch->in_room;

	separate_obj(obj);

	if ( pexit )
	{
		act( AT_GREY, "You throw $p $T.", ch, obj, dir_name[dir], TO_CHAR );
		act( AT_GREY, "$n throw $p $T.", ch, obj, dir_name[dir], TO_ROOM );
	}
	else
	{
		act( AT_GREY, "You throw $p at $N.", ch, obj, victim, TO_CHAR );
		act( AT_GREY, "$n throws $p at $N.", ch, obj, victim, TO_NOTVICT );
		act( AT_GREY, "$n throws $p at you!", ch, obj, victim, TO_VICT );
	}
    }


    /* victim in same room */
    if ( victim )
    {
    	check_illegal_pk( ch, victim );
	check_attacker( ch, victim );
	ranged_got_target( ch, victim, NULL, obj,
		0, TYPE_HIT, stxt, color );
    }

    /* assign scanned victim */
    victim = vch;

    /* reverse direction text from move_char */
    dtxt = rev_exit(pexit->vdir);

    while ( dist <= range )
    {
	char_from_room(ch);
	char_to_room(ch, pexit->to_room);

	if ( IS_SET(pexit->exit_info, EX_CLOSED) )
	{
	    /* whadoyahknow, the door's closed */
		sprintf(buf, "You see your %s hit a door in the distance to the %s.",
		    short_descr, dir_name[dir] );
	    act( color, buf, ch, NULL, NULL, TO_CHAR );
		sprintf(buf, "%s flies in from %s and smashes into the %sern door.",
		   obj->short_descr, dtxt, dir_name[dir] );
		buf[0] = UPPER(buf[0]);
		act( color, buf, ch, NULL, NULL, TO_ROOM );
	    break;
	}

Amended on Thu 16 Feb 2006 09:54 PM by Dace K
Canada #13



	/* no victim? pick a random one */
	if ( !victim )
	{
	    for ( vch = ch->in_room->first_person; vch; vch = vch->next_in_room )
	    {
		if ( ((IS_NPC(ch) && !IS_NPC(vch))
		||   (!IS_NPC(ch) &&  IS_NPC(vch)))
		&&    number_bits(1) == 0 )
		{
		    victim = vch;
		    break;
		}
	    }
	    if ( victim && is_safe(ch, victim, TRUE) )
	    {
	        char_from_room(ch);
	        char_to_room(ch, was_in_room);
		return;
	    }
	}

	/* In the same room as our victim? */
	if ( victim && ch->in_room == victim->in_room )
	{

		act( color, "$p flies in from $T.", ch, obj, dtxt, TO_ROOM );

	    /* get back before the action starts */
	    char_from_room(ch);
	    char_to_room(ch, was_in_room);

	    check_illegal_pk( ch, victim );
	    check_attacker( ch, victim );
	    return ranged_got_target( ch, victim, NULL, obj,
					dist, TYPE_HIT, stxt, color );

 	}

	if ( dist == range )
	{
	    if ( obj )
	    {
		act( color, "Your $t falls harmlessly to the ground to the $T.",
		    ch, myobj(obj), dir_name[dir], TO_CHAR );
		act( color, "$p flies in from $T and falls harmlessly to the ground here.",
		    ch, obj, dtxt, TO_ROOM );
		    obj_from_char(obj);
		obj_to_room(obj, ch->in_room, ch);
	    }
	    break;
	}

	if ( ( pexit = get_exit( ch->in_room, dir ) ) == NULL )
	{
	    if ( obj )
	    {
		act( color, "Your $p hits a wall and bounces harmlessly to the ground to the $T.",
		    ch, myobj(obj), dir_name[dir], TO_CHAR );
		act( color, "$p strikes the $Tsern wall and falls harmlessly to the ground.",
		    ch, obj, dir_name[dir], TO_ROOM );
		    obj_from_char(obj);
		obj_to_room(obj, ch->in_room, ch);
	    }
	    break;
	}
	    act( color, "$p flies in from $T.", ch, obj, dtxt, TO_ROOM );
	dist++;
    }

    char_from_room( ch );
    char_to_room( ch, was_in_room );

    return rNONE;
}


I'm just tired of people asking for snippets all the time. I used to release snippets, only to find people coming onto my mud and telling me my code was stock because they found it on a snippet site. Bleh.
#14
Hey Dace K, first of all, let me thank you for helping me out. I realize it'd probably get annoying with people always asking for snippets; hence, I try to keep my requests to a minimum.
I wish I knew C so I could save you the bother, but since I don't, I figure that the best way to get the more advanced things done while I'm learning the basics is to ask. :) And if nothing else, it helps me a bit to see the completed code so I can try to figure out WHY it works. I'm an inquistive fellow, you know. ;)


Godbless,
Longbow
USA #15
Quote:

Jeez, what's up your ass, Gatewaysysop?


One might ask you the same question, but it seems you already provided the answer at the end of your post. :)

Quote:

I gave step by step instructions on what to do, which is far more than what I got when I was learning to code


So, since you got so little help, others should be so lucky to get any better treatment from you when the tables are turned? That's awful big of you.

Quote:

My mistake apparently is assuming that anyone posting on a coding board knows basic coding already, so I just told him which functions to use.
Silly silly me.


If you thought that nobody would be put off by a response for a snippet request with a posting that basically said "You shouldn't need a snippet," then yes, I think that was pretty silly.

Quote:

I'm just tired of people asking for snippets all the time. I used to release snippets, only to find people coming onto my mud and telling me my code was stock because they found it on a snippet site. Bleh.


Ahh, there's the answer. It all makes sense now.

USA #16
Quote:
If you thought that nobody would be put off by a response for a snippet request with a posting that basically said "You shouldn't need a snippet," then yes, I think that was pretty silly.
It could be very constructive to tell somebody that they don't need a snippet. He did in fact give step by step instructions, even if they were very brief. If the receiver doesn't understand, then he can ask for clarification, at which point the steps become more detailed. But I think it's not very nice of you to blame somebody who's trying to teach people to fish for themselves in a simple case, instead of simply throwing fish at the problem. If somebody's really willing to learn, then they should not be put off by being told that the problem is not too hard. And the problem ISN'T too hard, given that the majority of the code is already right there.

From a teacher's perspective (I've taught this stuff) there are few things more frustrating than a student who demands immediate answers without understanding how you get there. If the student already has the tools to get there, it is a teacher's duty -- and the student's -- to use those tools as best as possible to reach the desired goal, without simply throwing code immediately at the problem.
#17
I am hesitant to post, because I do not want to stir a dying (dead?) fire. I agree with both perspectives, on the whole. Of course the ultimate goal is to help somebody learn to help themselves. Giving advice can in fact help someone. But to be honest, humiliating or belittling someone to make a point, or to 'teach them' is not productive. I AM NOT saying that was the intent in this case! Just pointing out that tone and attitude carry a lot of weight from someone who possesses skill, to someone who does not yet. Crush a student under the weight of your superior skill, and risk losing a student.
I think both of the individuals involved here were trying to help Longbow. One was trying to help him with coding. The other felt the sting they thought was implied in the tone of the post.
Nick's place is a great community, and we all benefit from everyone who posts here, one way or another.
Anyway, take care all.
Gadush
Canada #18
Ksilyan: Thanks for the backup. It's nice to see that someone understands what I'm getting at :).

Gateway: I noticed the lack of thanks for a function which you were actually the first one to request :/

Longbow: No worries. Smaug, and especially SmaugFUSS is my favorite codebase to recommend to new people learning C, because there're examples of almost everything already in the code. I learned myself by looking for functions similar to what I wanted to do, copying them, and modifying them to suit my needs. After about a year, I found that I could write most simple functions from scratch without any reference to anything else. As long as you understand a simple language like BASIC, you can learn C! (Or at least, enough C to program a mud :P).

If you have issues installing that function, lemme know in this thread and I'll help out :P
Amended on Fri 17 Feb 2006 01:02 AM by Dace K
USA #19
I also learned to program, at first, by looking at things that did something I knew, and then trying to figure out how they did it, and then copy-pasting it, tweaking it, and seeing what happened. Sometimes the results were good and sometimes they were disastrous. :P

Of course, a more formal approach is better in the long run. Never learning a language properly will only do you harm, in the long term. My recommendation is to start informally, "just for fun" as it were, and once you feel comfortable enough, to start challenging yourself to actually understanding the language. That means understanding precisely how syntax works - not just how it looks like it works - and more importantly, understanding formal data structures such as maps, trees, etc.

Having a proper grasp of data structuers is absolutely critical to programming efficiently, both in terms of speed of program execution and in terms of how long it takes you to write the program. But this is something that you can (usually) only gain through training and learning, unless of course you actually re-invent the data structures yourself. But that's a lot of wasted time. Imagine the person who spends a long time reinventing and debugging balanced binary tree algorithms, when it's something students learn relatively quickly in class...
USA #20
Quote:

Gateway: I noticed the lack of thanks for a function which you were actually the first one to request :/


You must have stopped reading after the first post then. Directly after Txzeenath posted the (now defunct) link, I posted the following in response:

Quote:

Will take a look, thanks for the help!


Sigh...
Canada #21
Then why not post the code for the guy yourself?:D
USA #22
Quote:

Then why not post the code for the guy yourself?:D


Sigh. Again, I question whether or not you've actually read the posts you yourself are responding to. I said pretty plainly before:

Quote:

For those interested, I may have a copy of the code that was posted here. I'll hunt around for it and post if I find it. In the mean time, I believe either SWR has thrown item code in it somewhere that you might look at.


USA #23
I don't think it's entirely necessary to go back and forth rehashing the argument, is it? The code is posted now, he got the help he was looking for, and from what I can see, the issue should be settled, yes?
Canada #24
Haha. I'm done, unless Longbow needs help with the code I posted.
Australia Forum Administrator #25
Please keep discussions on this forum polite.

There is nothing that annoys me more than forums which are 80% insult and counter-insult, with only 20% helpful information on them.

If you find something that someone says annoying, and can find no constructive thing so say, don't say anything please.
#26
Hey guys, interesting discussion here. I've been taking a look at the code in Smaug and its diriv Fear, and I've actually learned to read some of the code. Not a whole bunch, mind you, but a little. ;)

The thing that I really want to learn is skillbuilding, but I'm not sure how best to go about it. I'm trying to develop a throwing skill, with weapons that do differant amounts of damage depending on the weapon. That's why I asked about it here.
The C book I was studying I had to return it to the guy I was borrowing it from. So, at risk of being offtopic, does anybody know any online C tutorials, particularly those aimed toward text-based-gaming? Not to say that I should desert the basics of C by any means, of course.
And thanks for all the help so far guys. It is really appreciated. ;)

Godbless,
Longbow
USA #27
I'm not sure if there really are C tutorials meant specifically for text-based gaming. If there are, they'd be fairly rare. I imagine it'd be like asking for a violin instruction manual specially for Bach music. I'm sure such a thing exists, but playing the violin is a much more general tool than to merely play Bach.

Basically what I'm trying to say is that learning C is, at the beginner's level, fairly independent of what application you're trying to develop.

That being said, googling for "C tutorial" (with quotes) gives a number of links that look fairly decent. That being said, you're probably beyond the 'hello world' level. Do you have any more specific questions? Is there a line of code that you can't read? Or are you having trouble putting what you know together?

If you read the code for ranged weapons, do you understand what it's doing? If not, where is the problem?

Item throwing is almost identical to ranged weapons, so understanding ranged weapons is the first step in making an item throwing skill.
#28
Well, I know what void does, and I can read printf and figure out how to channel messages to the triggering PC or the room, or whatever. I have also figured out str_cmp's ability to call a particular string. Basic stuff like that, about all of which I picked up from scanning code. :) BUT! I don't know how to put it all together. Hence, I can't really write any code for myself; I spend my time editing other folk's code, and whenver I have to change variables and all that good stuff I'm stuck.

Now, as to reading the code Dace K posted, some of it I can follow, but not all of it. I'm not sure what all the char arg2[Max_Imput_Length] stuff fits in, and I don't really understand the char *dtxt and *stxt.

Is the first character argument checking for the item to be thrown, or is it checking for the target?

Is the second argument checking for a direction the heave the object in?

Where is the target determined?

Most of the rest seems to make sense, though how I'd modify it to do a skill/command of my own I'm not sure. I'll try to take a closer look at it at a later time.

Anyway, that may be more than you wanted, but there ya go!

Godbless,
Longbow
Canada #29
A char is simply a character string (word/phrase). Dtxt was directional text, and I forget what stxt is for. Whatever they're called to = in the code is what it's for though :P\

Target is determined where victim = something.

As for making it a skill of your own, add the appropriate calls in the mud.h do_funs, the table.c entries, then make a skill with the code do_throw