case spell hickup - whats wrong ?

  • JTP
  • JTP's Avatar Topic Author
  • Offline
  • Platinum Boarder
  • Platinum Boarder
More
7 months 5 days ago - 7 months 5 days ago #6997 by JTP
JTP created the topic: case spell hickup - whats wrong ?
I made this spell, the code compiles, spell works when there is an ITEM_TREE in the room.

BUT

When there is no tree, i some times only get an Okay. when cast. Other times i get the correct send_to_char.

So whats up, would be good to get the send_to_char every time there is no tree.
/* Every spell that does an affect comes through here.  This determines the
 * effect, whether it is added or replacement, whether it is legal or not, etc.
 * affect_join(vict, aff, add_dur, avg_dur, add_mod, avg_mod) */
#define MAX_SPELL_AFFECTS 5     /* change if more needed */
void mag_affects(int level, struct char_data *ch, struct char_data *victim,
                      int spellnum, int savetype)
{
  struct affected_type af[MAX_SPELL_AFFECTS];
  bool accum_affect = FALSE, accum_duration = FALSE;
  const char *to_vict = NULL, *to_room = NULL;
  int i, j;
  if (victim == NULL || ch == NULL)
    return;
  for (i = 0; i < MAX_SPELL_AFFECTS; i++) {
    new_affect(&(af[i]));
    af[i].spell = spellnum;
  }

  switch (spellnum) {

  case SPELL_TREE_TRAVEL:
{
struct obj_data *tree;
for (tree = world[ch->in_room].contents; tree; tree = tree->next_content) {
  if (GET_OBJ_TYPE(tree) != ITEM_TREE) {
    send_to_char(ch, "But there is no tree around to travel from.\r\n");
    return;
    }
    af[0].duration = 24;
    SET_BIT_AR(af[0].bitvector, AFF_TREE_TRAVEL);
    accum_duration = FALSE;
    to_vict = "You feel one with the trees!";
 }
}
    break;
} /* end of cases */
Last Edit: 7 months 5 days ago by JTP.

Please Log in or Create an account to join the conversation.

  • zusuk
  • zusuk's Avatar
  • Offline
  • Gold Boarder
  • Gold Boarder
  • LuminariMUD Developer
More
7 months 5 days ago #6999 by zusuk
zusuk replied the topic: case spell hickup - whats wrong ?
Hey Jan!

Sorry I have not been able to help you figure this out... I am not quite sure how to make it an affection spell... would need to know -exactly- what you want your spell to do, then check all the appropriate places in the code to really figure out what is going on.

For fun, and maybe it can help out, I threw our version of 'transport via plants' spells on the snippets though...

tbamud.com/forum/8-snippets/4259-transpo...vel-druid-spell-idea

Good luck,
Zusuk

Website
www.luminariMUD.com

Main Game Port
luminariMUD.com:4100

Please Log in or Create an account to join the conversation.

  • zusuk
  • zusuk's Avatar
  • Offline
  • Gold Boarder
  • Gold Boarder
  • LuminariMUD Developer
More
7 months 5 days ago #7000 by zusuk
zusuk replied the topic: case spell hickup - whats wrong ?
I am just guessing, but based on what you posted, I would adjust it this way:
  switch (spellnum) {

    case SPELL_TREE_TRAVEL:
      struct obj_data *tree = NULL;
      bool found_tree = FALSE;
      
      for (tree = world[ch->in_room].contents; tree; tree = tree->next_content) {
        if (GET_OBJ_TYPE(tree) == ITEM_TREE) {
          /* found a tree! */
          found_tree = TRUE;
          af[0].duration = 24;
          SET_BIT_AR(af[0].bitvector, AFF_TREE_TRAVEL);
          accum_duration = FALSE;
          to_vict = "You feel one with the trees!";
          break; /* we are "breaking" out of the loop */
        }
      } /* end tree search */
      
      if (found_tree == FALSE) {
        /* did not find any trees! */
        send_to_char(ch, "But there is no tree around to travel from.\r\n");
      }
      
      /* finish case */
      break;
    }
  } /* end of cases */

Website
www.luminariMUD.com

Main Game Port
luminariMUD.com:4100

Please Log in or Create an account to join the conversation.

  • zusuk
  • zusuk's Avatar
  • Offline
  • Gold Boarder
  • Gold Boarder
  • LuminariMUD Developer
More
7 months 5 days ago #7002 by zusuk
zusuk replied the topic: case spell hickup - whats wrong ?
if (GET_OBJ_TYPE(tree) != ITEM_TREE) {

should be

if (GET_OBJ_TYPE(tree) == ITEM_TREE) {

Website
www.luminariMUD.com

Main Game Port
luminariMUD.com:4100

Please Log in or Create an account to join the conversation.

  • JTP
  • JTP's Avatar Topic Author
  • Offline
  • Platinum Boarder
  • Platinum Boarder
More
7 months 5 days ago #7003 by JTP
JTP replied the topic: case spell hickup - whats wrong ?
Seems to make it work better.
BUT

Now when the spell is already in affect, i get this when trying to cast again:
Okay.
But there is no tree around to travel from.
Nothing seems to happen.

The right way should be:
Okay.
Nothing seems to happen.

Please Log in or Create an account to join the conversation.

  • zusuk
  • zusuk's Avatar
  • Offline
  • Gold Boarder
  • Gold Boarder
  • LuminariMUD Developer
More
7 months 5 days ago #7004 by zusuk
zusuk replied the topic: case spell hickup - whats wrong ?
with the code i posted, the only way that is possible is if the tree disappeared between casting (i.e. there is no tree now)

Website
www.luminariMUD.com

Main Game Port
luminariMUD.com:4100

Please Log in or Create an account to join the conversation.

  • JTP
  • JTP's Avatar Topic Author
  • Offline
  • Platinum Boarder
  • Platinum Boarder
More
7 months 5 days ago #7005 by JTP
JTP replied the topic: case spell hickup - whats wrong ?
What you posted, works when there is a tree.

It also gives the right message if there is no tree.


But if i cast the spell 2 times after eachother, it also checks for if there is a tree.
It didnt before.

Standard TBAmud message if spell is already in effect is:
Okay.
Nothing seems to happen.

But now it suddently says:
Okay.
But there is no tree around to travel from.
Nothing seems to happen.


With all other spells its still just:
Okay.
Nothing seems to happen.

So something is alittle off in the tree_travel spell still

Please Log in or Create an account to join the conversation.

  • zusuk
  • zusuk's Avatar
  • Offline
  • Gold Boarder
  • Gold Boarder
  • LuminariMUD Developer
More
7 months 5 days ago #7006 by zusuk
zusuk replied the topic: case spell hickup - whats wrong ?
so just add an affection check in the message display... there is no error in the code that i can see... unless i am simply not understanding again

i.e. this
if (found_tree == FALSE) {
/* did not find any trees! */
send_to_char(ch, "But there is no tree around to travel from.\r\n");
}

turns into this:
if (found_tree == FALSE && !affected_by_spell(vict, SPELL_TREE_TRAVEL)) {
/* did not find any trees! */
send_to_char(ch, "But there is no tree around to travel from.\r\n");
}

Website
www.luminariMUD.com

Main Game Port
luminariMUD.com:4100
The following user(s) said Thank You: JTP

Please Log in or Create an account to join the conversation.

More
7 months 5 days ago #7007 by thomas
thomas replied the topic: case spell hickup - whats wrong ?
the call to new_affect() allocates memory.
You will want to move your validation code up above that call:
/* Every spell that does an affect comes through here.  This determines the
 * effect, whether it is added or replacement, whether it is legal or not, etc.
 * affect_join(vict, aff, add_dur, avg_dur, add_mod, avg_mod) */
#define MAX_SPELL_AFFECTS 5     /* change if more needed */
void mag_affects(int level, struct char_data *ch, struct char_data *victim,
                      int spellnum, int savetype)
{
  struct affected_type af[MAX_SPELL_AFFECTS];
  bool accum_affect = FALSE, accum_duration = FALSE;
  const char *to_vict = NULL, *to_room = NULL;
  int i, j;
  if (victim == NULL || ch == NULL)
    return;

  if (spellnum == SPELL_TREE_TRAVEL) {
    struct obj_data *tree;
    for (tree = world[ch->in_room].contents; tree; tree = tree->next_content) 
      if (GET_OBJ_TYPE(tree) == ITEM_TREE) 
        break;
      
    if (tree == NULL) {
      send_to_char(ch, "But there is no tree around to travel from.\r\n");
      return;
	}
  }
	
  for (i = 0; i < MAX_SPELL_AFFECTS; i++) {
    new_affect(&(af[i]));
    af[i].spell = spellnum;
  }

  switch (spellnum) {

  case SPELL_TREE_TRAVEL:
    af[0].duration = 24;
    SET_BIT_AR(af[0].bitvector, AFF_TREE_TRAVEL);
    accum_duration = FALSE;
    to_vict = "You feel one with the trees!";
    break;
	
} /* end of cases */

Let us know if that fixes the message, too :)

Please Log in or Create an account to join the conversation.

  • JTP
  • JTP's Avatar Topic Author
  • Offline
  • Platinum Boarder
  • Platinum Boarder
More
7 months 5 days ago #7008 by JTP
JTP replied the topic: case spell hickup - whats wrong ?
Both ways seems to work now, but your Way is better because ?

Please Log in or Create an account to join the conversation.

  • zusuk
  • zusuk's Avatar
  • Offline
  • Gold Boarder
  • Gold Boarder
  • LuminariMUD Developer
More
7 months 5 days ago #7009 by zusuk
zusuk replied the topic: case spell hickup - whats wrong ?
Hope Thomas does not mind me jumping in here :)

It is more efficient! Less memory, which probably is not an issue, but still proper practice.

The way I did it is just trying to kinda throw a bandage on your issue instead of thinking it out from top-to-bottom for optimal performance.

-Zusuk

Website
www.luminariMUD.com

Main Game Port
luminariMUD.com:4100

Please Log in or Create an account to join the conversation.

  • JTP
  • JTP's Avatar Topic Author
  • Offline
  • Platinum Boarder
  • Platinum Boarder
More
7 months 5 days ago #7010 by JTP
JTP replied the topic: case spell hickup - whats wrong ?
Well still Big Thanks to both of you !

Please Log in or Create an account to join the conversation.

More
7 months 4 days ago #7012 by thomas
thomas replied the topic: case spell hickup - whats wrong ?
You're welcome.

And the reason my way was better is because doing this is a memory leak:
int foo() {
  void* data = allocate_data();
  if (bar)
    return;

  use_data(data);
}
After you return in the condition, there no longer exists any way to get a hold of the allocated memory. Thus, it can never be free'd.
In the case of the affect array in your code, this is later added to the character, so it is freed when the character is extracted. But if you skip out before the assignment, but after the allocation, the memory is lost.

Please Log in or Create an account to join the conversation.

Time to create page: 1.455 seconds