Initial Comit: First commit.
[SauerEngine.git] / src / rpggame / rpgobjset.h
blob63e49754f3ca9896a772c68212dc4fccc60bc70f
1 struct rpgobjset
3 rpgclient &cl;
5 vector<rpgobj *> set, stack;
6 hashtable<char *, char *> names;
8 rpgobj *pointingat;
9 rpgobj *playerobj;
10 rpgobj *selected;
12 rpgquest *quests;
13 rpgquest *currentquest;
15 rpgobjset(rpgclient &_cl) : cl(_cl), pointingat(NULL), playerobj(NULL), selected(NULL), quests(NULL), currentquest(NULL)
17 #define N(n) CCOMMAND(r_##n, "i", (rpgobjset *self, int *val), { self->stack[0]->s_##n = *val; }); \
18 CCOMMAND(r_get_##n, "", (rpgobjset *self), { intret(self->stack[0]->s_##n); });
20 RPGNAMES
21 #undef N
22 #define N(n) CCOMMAND(r_def_##n, "ii", (rpgobjset *self, int *i1, int *i2), { stats::def_##n(*i1, *i2); }); \
23 CCOMMAND(r_eff_##n, "", (rpgobjset *self), { intret(self->stack[0]->eff_##n()); });
24 RPGSTATNAMES
25 #undef N
27 CCOMMAND(r_model, "s", (rpgobjset *self, char *s), { self->stack[0]->model = self->stringpool(s); });
28 CCOMMAND(r_spawn, "s", (rpgobjset *self, char *s), { self->spawn(self->stringpool(s)); });
29 CCOMMAND(r_contain, "s", (rpgobjset *self, char *s), { self->stack[0]->decontain(); self->stack[1]->add(self->stack[0], atoi(s)); });
30 CCOMMAND(r_pop, "", (rpgobjset *self), { self->popobj(); });
31 CCOMMAND(r_swap, "", (rpgobjset *self), { swap(self->stack[0], self->stack[1]); });
32 CCOMMAND(r_say, "s", (rpgobjset *self, char *s), { self->stack[0]->abovetext = self->stringpool(s); });
33 CCOMMAND(r_quest, "ss", (rpgobjset *self, char *s, char *a), { self->stack[0]->addaction(self->stringpool(s), self->stringpool(a), true); });
34 CCOMMAND(r_action, "ss", (rpgobjset *self, char *s, char *a), { self->stack[0]->addaction(self->stringpool(s), self->stringpool(a), false); });
35 CCOMMAND(r_action_use, "s", (rpgobjset *self, char *s), { self->stack[0]->action_use.script = self->stringpool(s); });
36 CCOMMAND(r_take, "sss", (rpgobjset *self, char *name, char *ok, char *notok), { self->takefromplayer(name, ok, notok); });
37 CCOMMAND(r_give, "s", (rpgobjset *self, char *s), { self->givetoplayer(s); });
38 CCOMMAND(r_use, "", (rpgobjset *self), { self->stack[0]->selectuse(); });
39 CCOMMAND(r_applydamage, "i", (rpgobjset *self, int *d), { self->stack[0]->takedamage(*d, *self->stack[1]); });
40 clearworld();
43 void resetstack()
45 stack.setsize(0);
46 loopi(10) stack.add(playerobj); // determines the stack depth
49 void clearworld()
51 if(playerobj) { playerobj->ent = NULL; delete playerobj; }
52 playerobj = new rpgobj("player", *this);
53 playerobj->ent = &cl.player1;
54 cl.player1.ro = playerobj;
56 pointingat = NULL;
57 set.deletecontentsp();
58 resetstack();
60 playerobj->scriptinit(); // will fail when this is called from emptymap(), which is ok
63 void removefromsystem(rpgobj *o)
65 removefromworld(o);
66 o->decontain();
67 if(pointingat==o) pointingat = NULL;
68 if(selected==o) selected = NULL;
69 resetstack();
70 DELETEP(o);
73 void update(int curtime)
75 extern vec worldpos;
76 pointingat = NULL;
77 loopv(set)
79 set[i]->update(curtime);
81 float dist = cl.player1.o.dist(set[i]->ent->o);
82 if(dist<50 && intersect(set[i]->ent, cl.player1.o, worldpos) && (!pointingat || cl.player1.o.dist(pointingat->ent->o)>dist))
84 pointingat = set[i];
89 void spawn(char *name)
91 rpgobj *o = new rpgobj(name, *this);
92 pushobj(o);
93 o->scriptinit();
96 void placeinworld(vec &pos, float yaw)
98 stack[0]->placeinworld(new rpgent(stack[0], cl, pos, yaw));
99 set.add(stack[0]);
102 void pushobj(rpgobj *o) { stack.pop(); stack.insert(0, o); } // never overflows, just removes bottom
103 void popobj() { stack.add(stack.remove(0)); } // never underflows, just puts it at the bottom
105 void removefromworld(rpgobj *worldobj)
107 set.removeobj(worldobj);
108 DELETEP(worldobj->ent);
111 void take(rpgobj *worldobj, rpgobj *newowner)
113 removefromworld(worldobj);
114 newowner->add(worldobj, false);
117 void takefromplayer(char *name, char *ok, char *notok)
119 rpgobj *o = playerobj->take(name);
120 if(o)
122 stack[0]->add(o, false);
123 conoutf("\f2you hand over a %s", o->name);
124 if(currentquest)
126 conoutf("\f2you finish a quest for %s", currentquest->npc);
127 currentquest->completed = true;
130 execute(o ? ok : notok);
133 void givetoplayer(char *name)
135 rpgobj *o = stack[0]->take(name);
136 if(o)
138 conoutf("\f2you receive a %s", o->name);
139 playerobj->add(o, false);
143 void addquest(rpgaction *a, const char *questline, const char *npc)
145 a->q = quests = new rpgquest(quests, npc, questline);
146 conoutf("\f2you have accepted a quest for %s", npc);
149 void listquests(bool completed, g3d_gui &g)
151 for(rpgquest *q = quests; q; q = q->next) if(q->completed==completed)
153 s_sprintfd(info)("%s: %s", q->npc, q->questline);
154 g.text(info, 0xAAAAAA, "info");
158 char *stringpool(char *name)
160 char **n = names.access(name);
161 if(n) return *n;
162 name = newstring(name);
163 names[name] = name;
164 return name;
167 void render() { loopv(set) set[i]->render(); }
168 void g3d_npcmenus() { loopv(set) set[i]->g3d_menu(); }