Author Topic: help with code  (Read 2742 times)

Offline stanach

  • Acquaintance
  • *
  • Posts: 19
    • View Profile
help with code
« on: July 17, 2010, 09:38:42 pm »
Changing the Trash can a little, would like it to give 2 dollars when you put an item in it you want to discard. I got the trashcan giving the 2 dollars but when you see that person put somethign int he trashcan in place of name  you see a O recieved 2 dollars from the trashcan. here is the code, any help would be appreciated.
Code: [Select]
#include <lib.h>

inherit LIB_STORAGE;

void create() {
    ::create();
    SetKeyName("bin");
    SetId(({"bin","can","dustbin","trash","rubbish"}));
    SetAdjectives(({"metal","small","blue","recycling","trash","garbage","dust"}));
    SetShort("a recycling bin");
    SetLong("This is a blue trash can, marked with "+
            "the letters \"/dev/null\".");
    SetMass(274);
    SetBaseCost("dollars",50);
    SetMaxCarry(999999);
}

int tidy_up(){
    object *inv;
    inv = all_inventory(this_object());
    foreach(object thing in inv){
        if(thing) thing->eventMove(load_object("/domains/town/room/furnace"));
    }
    return 1;
}

int eventReceiveObject(object ob){
    write("You make a deposit into the the recycling bin.\n");
    call_out((: tidy_up :),1);
    call_out ("reward", 2, this_player());
    return 1;
}
reward (who)
{
  tell_object(who, "You get 2 dollars from the trashcan for showing tidyness.\n");
  say(who->this_player() + " gets 2 dollars from the trashcan for showing tidyness.\n",who);
  who->AddCurrency("dollars",2);     
}
mixed CanGet(object ob) { return "The bucket does not budge.";}

void init(){
}

Offline stanach

  • Acquaintance
  • *
  • Posts: 19
    • View Profile
Re: help with code
« Reply #1 on: July 17, 2010, 10:56:28 pm »
Got the answer to my own question finally.  :D
reward (who)
{
  tell_object(who, "You get 2 dollars from the trashcan for showing tidyness.\n");
  say(who->GetName() + " gets 2 dollars from the trashcan for showing tidyness.\n",who);
  who->AddCurrency("dollars",2);     
}
mixed CanGet(object ob) { return "The bucket does not budge.";}

void init(){
}
 
 
 changed this_player to GetName and it works fine :)
 

Offline stanach

  • Acquaintance
  • *
  • Posts: 19
    • View Profile
Re: help with code
« Reply #2 on: July 18, 2010, 10:40:54 am »
Got most of what i want working.
What i am doing is letting people toss worthess stuff in trash can for recycling and getting a 2 dollar reward for it. It works fine but the check i have to see what the value of the item is, is not checking and letting me toss it anyway. here is my code, if someone has some pointers on why it isn't working i would appreciate it.
Code: [Select]
#include <lib.h>

inherit LIB_STORAGE;

void create() {
    ::create();
    SetKeyName("trashcan");
    SetId(({"trashcan","can","dustbin","trash","rubbish"}));
    SetAdjectives(({"metal","small","blue","recycling","trash","garbage","dust"}));
    SetShort("a recycling bin");
    SetLong("This is a blue trash can, marked with "+
            "the letters \"/dev/null\".");
    SetMass(274);
    SetBaseCost("dollars",50);
    SetMaxCarry(999999);
}
id (str) { return str=="trashcan" || str == "can"; }

init()
{
  ::init();
    add_action("toss","waste");
    add_action("toss","toss");
  }

toss (str)
{
  object ob;
  string item;
  int i,reward;
  
  if (!str)
    return write ("Usage: 'toss <what>'.\n");
  
  str = lower_case (str);
  if (!(ob = present(str,this_player())))
  {
    if(!sscanf(str,"%s in trashcan",item) || !(ob=present(item,this_player())))
      if(!sscanf(str,"%s in the trashcan",item) ||
!(ob=present(item,this_player())))
      {
write ("You don't have that.\n");
return 1;
      }
  }
  if (ob->SetValue() > 10)
    return write ("That item is too valuable to toss.\n");
  
  if (ob->drop())
    return write("That item cannot be tossed.\n");
  
  if (living(ob))
  {
    tell_object(ob, this_player()->GetName() + " tossed you away!\n");
    write("Naughty boy!\n");
  }
  ob = deep_inventory(ob) + ({ ob });
    for(i=0; i < sizeof(ob); ++i)
  destruct(ob[i]);
  write ("The trashcan starts grinding the " + str + ".\n");
  say (this_player()->GetName() + " tosses " + str + " in the trashcan.\n");
  call_out ("reward", 2, this_player());
  return 1;
}

reward (who)
{
  tell_object(who, "You get 2 dollars from the trashcan for showing tidyness.\n");
  say(who->GetName() + " gets 2 dollars from the trashcan for showing tidyness.\n",who);
  who->AddCurrency("dollars",2);
}
« Last Edit: July 18, 2010, 10:54:25 am by stanach »

Offline cratylus

  • Your favorite and best
  • Administrator
  • ***
  • Posts: 1024
  • Cratylus@Dead Souls <ds> np
    • View Profile
    • About Cratylus
Re: help with code
« Reply #3 on: July 18, 2010, 11:09:06 am »
I think the main problem here is you're trying to plug non-DS code into DS and crossing
your fingers. First thing I notice is that your functions don't have declared types. The stock
DS driver lets you get away with this, I guess, though I don't remember setting it that way
and I strongly disapprove.

Aside from that, a glance at this code shows to me that SetValue() probably doesn't
mean what you think it means.

I understand you're converting from an older lib to DS, but just shoehorning in code
like this is going to end with you starting one of these threads per day. I like activity on
this forum, but your time is better invested in looking at how DS does things, rather
than forcing DS to do things like an obsolete lib used to.

See the charity bin ( /domains/town/obj/charity.c ) for an object that calculates the
value of objects put into it.

-Crat

Offline stanach

  • Acquaintance
  • *
  • Posts: 19
    • View Profile
Re: help with code
« Reply #4 on: July 18, 2010, 11:13:25 am »
thanks cray, Didn't see that example :) will look at it.