blob: 76c9d483f416d35dd3fea50269530f817416590f [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001// MorgenGrauen MUDlib
2//
3// spellbook.c -- Grundlegende Funktionen fuer Zaubersprueche
4//
5// $Id: spellbook.c 9142 2015-02-04 22:17:29Z Zesstra $
6#pragma strict_types
7#pragma save_types
8#pragma no_shadow
9#pragma no_clone
10#pragma pedantic
11#pragma range_check
12
13//#define NEED_PROTOTYPES
14inherit "/std/thing";
15inherit "/std/restriction_checker";
16inherit "/std/player/pklog";
17
18#include <thing/properties.h>
19#include <properties.h>
20#include <wizlevels.h>
21#include <new_skills.h>
22#include <spellbook.h>
23#define ME this_object()
24
25void create() {
26 seteuid(getuid());
27 // diese Prop sollte von aussen nicht direkt geaendert werden koennen.
28 Set(P_SB_SPELLS,([]),F_VALUE);
29 Set(P_SB_SPELLS,PROTECTED,F_MODE_AS);
30
31 ::create();
32}
33
34mapping _query_sb_spells() {
35 // Kopie liefern, da sonst versehentlich schnell eine Aenderung des
36 // originalen Mappings hier passieren kann.
37 return(deep_copy(Query(P_SB_SPELLS, F_VALUE)));
38}
39
40mapping QuerySpell(string spell) {
41 mapping spi;
42
43 //Query-Methode umgehen, damit nur ein Spellmapping kopiert werden muss und
44 //nicht das ganzen Mapping mit allen Spells.
45 if (!spell
46 || !(spi=Query(P_SB_SPELLS))
47 || !(spi=spi[spell]))
48 return 0;
49 return deep_copy(spi);
50}
51
52varargs int
53AddSpell(string verb, int kosten, mixed ski) {
54 int level;
55 mapping spells;
56
57 if (!verb || kosten<0)
58 return 0;
59
60 verb=lower_case(verb);
61 level=(intp(ski))?ski:0;
62
63 if (!mappingp(ski)) ski=([]);
64 // Zur Sicherheit eine Kopie machen, wer weiss, was da uebergeben wird, bzw.
65 // was der Setzende mit ski hinterher noch macht.
66 else ski=deep_copy(ski);
67
68 ski=AddSkillMappings(QueryProp(P_GLOBAL_SKILLPROPS),ski);
69 ski+=([SI_SPELLCOST:kosten]);
70 // SI_SPELL-Submapping hinzufuegen, wenn nicht da.
71 if (!member(ski,SI_SPELL))
72 ski+=([SI_SPELL:([]) ]);
73 // ski[SI_SPELL][SP_NAME] ergaenzen, ggf. aus ski verschieben
74 if (!stringp(ski[SI_SPELL][SP_NAME]) && stringp(ski[SP_NAME])) {
75 ski[SI_SPELL][SP_NAME] = ski[SP_NAME];
76 m_delete(ski, SP_NAME);
77 }
78 else if (!stringp(ski[SI_SPELL][SP_NAME])) {
79 ski[SI_SPELL][SP_NAME] = verb;
80 }
81 if (stringp(ski[SP_NAME])) {
82 m_delete(ski, SP_NAME);
83 }
84
85 if (level)
86 ski=AddSkillMappings(ski,([SI_SKILLRESTR_LEARN:([P_LEVEL:level])]));
87
88 // Abfrage per Query(), um das Mapping als Referenz, nicht als Kopie zu
89 // bekommen, das spart etwas Zeit.
90 if (!(spells=Query(P_SB_SPELLS,F_VALUE))) {
91 spells=([]);
92 SetProp(P_SB_SPELLS,spells);
93 }
94 // Spell setzen...
95 spells[verb]=ski;
96 // Set() nicht noetig, da Query() an spell das Originalmapping geliefert hat
97 // und dieses bereits jetzt geaendert wurde. ;-)
98 //Set(P_SB_SPELLS,spells+([verb:ski]),F_VALUE);
99
100 // hat wohl geklappt...
101 return(1);
102}
103
104int
105TryAttackSpell(object victim, int damage, mixed dtypes,
106 mixed is_spell, object caster, mapping sinfo) {
107 mixed no_attack;
108 int nomag;
109
110 if (no_attack = (mixed)victim->QueryProp(P_NO_ATTACK)) {
111 if (stringp(no_attack))
heull0018720c5a2018-02-08 12:29:49 +0100112 caster->ReceiveMsg(
113 no_attack,
114 MT_NOTIFICATION,
115 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200116 else
heull0018720c5a2018-02-08 12:29:49 +0100117 caster->ReceiveMsg(
118 victim->Name(WER,1)+" laesst sich nicht angreifen!",
119 MT_NOTIFICATION,
120 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200121 return 0;
122 }
123 if (victim->QueryProp(P_GHOST)) {
heull0018720c5a2018-02-08 12:29:49 +0100124 caster->ReceiveMsg(
125 victim->Name(WER,1)+" ist ein Geist!",
126 MT_NOTIFICATION,
127 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200128 return 0;
129 }
130
131 damage=(damage*(int)caster->QuerySkillAttribute(SA_DAMAGE))/100;
132
133 // ggf. Loggen von PK-Versuchen und ggf. ausserdem Angriff abbrechen.
134 // BTW: Aufruf von InsertEnemy() gibt 0 zurueck, wenn der Gegner nicht
135 // eingetragen wurde. Allerdings tut es das auch, wenn der Gegner schon
136 // Feind war. Daher muss noch geprueft werden, ob nach dem InsertEnemy() die
137 // beiden Spieler Feinde sind. Wenn nicht, wird der Spell auch abgebrochen
138 // und eine Meldung ausgegeben.
139 if (query_once_interactive(caster) && query_once_interactive(victim)
140 && CheckPlayerAttack(caster, victim,
141 sprintf("Spellbook: %O, Spell: %O\n",
142 object_name(this_object()),
143 ((mappingp(sinfo) && is_spell) ? sinfo[SP_NAME] : "")))
144 && !caster->IsEnemy(victim)
145 && !caster->InsertEnemy(victim)
146 )
147 {
148 tell_object(ME, "Ein goettlicher Einfluss schuetzt Deinen Gegner.\n");
149 return 0;
150 }
151
152 nomag=(int)victim->QueryProp(P_NOMAGIC);
153 if ((sinfo[SI_NOMAGIC] < nomag) &&
154 nomag*100 > random(100)*(int)caster->QuerySkillAttribute(SA_ENEMY_SAVE)) {
155 printf("%s wehrt Deinen Zauber ab.\n", capitalize((string)victim->name(WER, 1)));
156 return 0;
157 }
158 else {
159 return (int)victim->Defend(damage, dtypes, is_spell, caster);
160 }
161}
162
163varargs int
164TryDefaultAttackSpell(object victim, object caster, mapping sinfo,
165 mixed si_spell) {
166 object team;
167 int row;
168
169 if (!si_spell) si_spell=sinfo[SI_SPELL];
170 // Wenn der Spieler in einem Team ist, die Teamreihen-Boni
171 // beruecksichtigen. Wenn nicht, eben nicht.
172 if (!team=((object)caster->QueryProp(P_TEAM)))
173 return TryAttackSpell(victim,
174 GetRandFValueO(SI_SKILLDAMAGE,sinfo,caster),
175 GetData(SI_SKILLDAMAGE_TYPE,sinfo,caster),
176 si_spell,
177 caster,
178 sinfo);
179 else
180 {
181 row=(int)caster->PresentPosition();
182 return TryAttackSpell(victim,
183 GetRandFValueO(SI_SKILLDAMAGE,sinfo,caster)+
184 // Nur wenn SI_SKILLDAMAGE_BY_ROW ein mapping ist,
185 // randomisiert addieren
186 (mappingp(sinfo[SI_SKILLDAMAGE_BY_ROW])?
187 random(sinfo[SI_SKILLDAMAGE_BY_ROW][row]):0)+
188 // Nur wenn das OFFSET davon ein mapping ist,
189 // nicht randomisiert addieren.
190 (mappingp(sinfo[OFFSET(SI_SKILLDAMAGE_BY_ROW)])?
191 sinfo[OFFSET(SI_SKILLDAMAGE_BY_ROW)][row] : 0),
192 GetData(SI_SKILLDAMAGE_TYPE,sinfo,caster),
193 si_spell,caster,sinfo);
194 }
195}
196
197#define SMUL(x,y) ((x<0 && y<0)?(-1*x*y):(x*y))
198int
199SpellSuccess(object caster, mapping sinfo) {
200 int cval,abil,res;
201
202 abil=sinfo[SI_SKILLABILITY];
203 cval=(int)caster->UseSkill(SK_CASTING);
204 res=abil + (SMUL(abil,cval)) / MAX_ABILITY - random(MAX_ABILITY);
205 if (cval && res>MAX_ABILITY) // besonders gut gelungen?
206 caster->LearnSkill(SK_CASTING,1+(res-MAX_ABILITY)/2000);
207 return res;
208}
209
210int
211CanTrySpell(object caster, mapping sinfo) {
212 mapping rmap;
213 string res;
214
215 if (caster->QueryProp(P_GHOST)) {
heull0018720c5a2018-02-08 12:29:49 +0100216 caster->ReceiveMsg(
217 "Als Geist kannst Du nicht zaubern.",
218 MT_NOTIFICATION,
219 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200220 return 0;
221 }
222 if ((rmap=sinfo[SI_SKILLRESTR_USE])
223 && (res=check_restrictions(caster,rmap))) {
heull0018720c5a2018-02-08 12:29:49 +0100224 caster->ReceiveMsg(
225 res,
226 MT_NOTIFICATION,
227 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200228 return 0;
229 }
230 return 1;
231}
232
233void
234Learn(object caster, string spell, mapping sinfo) {
235 int val,diff,attval;
236 mapping attr;
237
238 // gar nicht lernen?
239 if (sinfo[SI_NO_LEARN])
240 return;
241
242 // Attribut sollte int sein, wenn nicht anders angegeben
243 if (mappingp(sinfo[SI_LEARN_ATTRIBUTE]))
244 {
245 attr=sinfo[SI_LEARN_ATTRIBUTE];
246 // Direkt in einem durch berechnen.
247 // Ich gehe davon aus, dass wir nie nie nie
248 // ein weiteres Attribut ins MG einfuegen.
249 // Wir berechnen den Mittelwert
250 attval=((int)caster->QueryAttribute(A_INT)*attr[A_INT]+
251 (int)caster->QueryAttribute(A_DEX)*attr[A_DEX]+
252 (int)caster->QueryAttribute(A_STR)*attr[A_STR]+
253 (int)caster->QueryAttribute(A_CON)*attr[A_CON])
254 /(attr[A_CON]+attr[A_INT]+attr[A_STR]+attr[A_DEX]);
255
256
257 } else {
258 attval=(int)caster->QueryAttribute(A_INT);
259 }
260
261
262 val=((attval/2)*GetFValue(SI_SKILLLEARN,sinfo,caster)+
263 GetOffset(SI_SKILLLEARN,sinfo,caster));
264 if (!(diff=GetFValueO(SI_DIFFICULTY,sinfo,caster)))
265 diff=GetFValueO(SI_SPELLCOST,sinfo,caster);
266 caster->LearnSkill(spell,val,diff);
267}
268
269void
270Erfolg(object caster, string spell, mapping sinfo) {
271 object env;
272 if(env=environment(caster))
273 env->SpellInform(caster,spell,sinfo);
274}
275
276void
277Misserfolg(object caster, string spell, mapping sinfo) {
heull0018720c5a2018-02-08 12:29:49 +0100278 caster->ReceiveMsg(
279 "Der Zauberspruch ist missglueckt.\n"
280 "Du lernst aus Deinem Fehler.\n",
281 MT_NOTIFICATION|MSG_DONT_WRAP,
282 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200283 Learn(caster,spell,sinfo);
284}
285
286string
287SelectSpell(string spell, mapping sinfo) {
288 if (sinfo && sinfo[SI_SKILLFUNC])
289 return sinfo[SI_SKILLFUNC];
290 return spell;
291}
292
293varargs void
294prepare_spell(object caster, string spell, mapping sinfo) {
295 string txt;
296
297 if (!caster || !spell) return ;
298 if (!mappingp(sinfo) || !stringp(txt=sinfo[SI_PREPARE_MSG]))
299 txt="Du bereitest Dich auf den Spruch ``%s'' vor.\n";
300 tell_object(caster,sprintf(txt,spell));
301}
302
303varargs int
304UseSpell(object caster, string spell, mapping sinfo) {
305 mapping ski,tmp;
306 string spellbook,sname,fname,txt;
307 int res,fat,cost;
308 mixed ps;
309
310 if (!caster || !spell)
311 return 0;
312 // Spell kann in der Gilde anderen Namen haben
313 sname=SelectSpell(spell,sinfo);
314 if (!(ski=QuerySpell(sname))) // Existiert dieser Spell?
315 return 0;
316 // Gildeneigenschaften sollen Spelleigenschaften ueberschreiben koennen
317 ski=AddSkillMappings(ski,sinfo);
318 // Fuer verschiedene Rassen unterschiedliche Eigenschaften
319 ski=race_modifier(caster,ski);
320
321 // printf("Spellinfo: %O\n",ski);
322
323 if (!CanTrySpell(caster, ski))
324 return 1;
325
326 if (caster->QueryProp(P_SP) < (cost=GetFValueO(SI_SPELLCOST,ski,caster))) {
heull0018720c5a2018-02-08 12:29:49 +0100327 if(txt=ski[SI_SP_LOW_MSG])
328 caster->ReceiveMsg(
329 txt,
330 MT_NOTIFICATION,
331 MA_SPELL);
332 else
333 caster->ReceiveMsg(
334 "Du hast zu wenig Zauberpunkte fuer diesen Spruch.",
335 MT_NOTIFICATION,
336 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200337 return 1;
338 }
339 // printf("cost: %d\n",cost);
340
341 if (mappingp(ski[SI_X_SPELLFATIGUE])) {
342 // fuer jeden Key die Spellfatigue pruefen, wenn das Mapping hinterher
343 // nicht leer ist, ist man zu erschoepft.
344 tmp = filter(ski[SI_X_SPELLFATIGUE],
345 function int (string key, int val)
346 { return (int)caster->CheckSpellFatigue(key); } );
347 if (sizeof(tmp)) {
heull0018720c5a2018-02-08 12:29:49 +0100348 caster->ReceiveMsg(
349 ski[SI_TIME_MSG] ||
350 "Du bist noch zu erschoepft von Deinem letzten Spruch.",
351 MT_NOTIFICATION,
352 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200353 return 1;
354 }
355 }
356 else {
357 if (caster->CheckSpellFatigue()) {
heull0018720c5a2018-02-08 12:29:49 +0100358 caster->ReceiveMsg(
359 ski[SI_TIME_MSG] ||
360 "Du bist noch zu erschoepft von Deinem letzten Spruch.",
361 MT_NOTIFICATION,
362 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200363 return 1;
364 }
365 }
366
367 if (!(ski[SI_NO_ATTACK_BUSY]&NO_ATTACK_BUSY_QUERY) &&
368 caster->QueryProp(P_ATTACK_BUSY)) {
heull0018720c5a2018-02-08 12:29:49 +0100369 if (txt=ski[SI_ATTACK_BUSY_MSG])
370 caster->ReceiveMsg(
371 txt,
372 MT_NOTIFICATION,
373 MA_SPELL);
374 else
375 caster->ReceiveMsg(
376 "Du bist schon zu sehr beschaeftigt.",
377 MT_NOTIFICATION,
378 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200379 return 1;
380 }
381
382 // Spruchvorbereitung
383 if (pointerp(ps=(mixed)caster->QueryProp(P_PREPARED_SPELL)) // Ausstehender Spruch
384 && sizeof(ps)>=3 && intp(ps[0] && stringp(ps[1]))) {
385 if (ps[1]==spell) { // Dieser Spruch wird noch vorbereitet
386 if (time()<ps[0]) {
387 if (!stringp(txt=ski[SI_PREPARE_BUSY_MSG]))
388 txt="Du bist noch mit der Spruchvorbereitung beschaeftigt.\n";
heull0018720c5a2018-02-08 12:29:49 +0100389 caster->ReceiveMsg(
390 txt,
391 MT_NOTIFICATION,
392 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200393 return 1;
394 }
395 }
396 else { // Andere Sprueche brechen die Vorbereitung ab
397 if (!mappingp(tmp=QuerySpell(ps[1])) || // richtige Meldung holen...
398 !stringp(txt=tmp[SI_PREPARE_ABORT_MSG]))
399 txt="Du brichst die Spruchvorbereitung fuer `%s' ab.\n";
400 printf(txt,ps[1]);
401 if (fat=GetValue(SI_PREPARE_TIME,ski,caster)) {
402 // Spruch braucht vorbereitungszeit
403 caster->SetProp(P_PREPARED_SPELL,({time()+fat,spell,ski[SI_SKILLARG]}));
404 prepare_spell(caster,spell,ski);
405 return 1;
406 }
407 }
408 }
409 else {
410 if (fat=GetValue(SI_PREPARE_TIME,ski,caster)) {
411 // Spruch braucht vorbereitungszeit
412 caster->SetProp(P_PREPARED_SPELL,({time()+fat,spell,ski[SI_SKILLARG]}));
413 prepare_spell(caster,spell,ski);
414 return 1;
415 }
416 }
417 if (ps)
418 caster->SetProp(P_PREPARED_SPELL,0);
419
420 // Funktion kann anderen Namen haben als Spell
421 if (!(fname=sinfo[SI_SKILLFUNC]))
422 fname=sname;
423
424 if((ski[SI_NOMAGIC] < environment(caster)->QueryProp(P_NOMAGIC)) &&
425 random(100) < environment(caster)->QueryProp(P_NOMAGIC)) {
426 if (txt=ski[SI_NOMAGIC_MSG])
heull0018720c5a2018-02-08 12:29:49 +0100427 caster->ReceiveMsg(
428 txt,
429 MT_NOTIFICATION,
430 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200431 else
heull0018720c5a2018-02-08 12:29:49 +0100432 caster->ReceiveMsg(
433 "Dein Zauberspruch verpufft im Nichts.",
434 MT_NOTIFICATION,
435 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200436 res=ABGEWEHRT;
437 }
438 else {
439 // Spruch ausfuehren.
440 res=(int)call_other(this_object(),fname,caster,ski);
441 }
Gloinsoncd564772016-08-03 21:57:12 +0200442 if (!res || !caster)
MG Mud User88f12472016-06-24 23:31:02 +0200443 return 1;
MG Mud User88f12472016-06-24 23:31:02 +0200444
Bugfixdd8ce3f2016-08-13 19:54:02 +0200445 if(res==NICHT_ZUSTAENDIG)
446 return 0;
447
MG Mud User88f12472016-06-24 23:31:02 +0200448 if (!(ski[SI_NO_ATTACK_BUSY]&NO_ATTACK_BUSY_QUERY))
449 {
450 if (!ski[SI_ATTACK_BUSY_AMOUNT])
451 caster->SetProp(P_ATTACK_BUSY,1);
452 else
453 caster->SetProp(P_ATTACK_BUSY,ski[SI_ATTACK_BUSY_AMOUNT]);
454 }
455
456 caster->restore_spell_points(-1*cost);
457
458 if (mappingp(ski[SI_X_SPELLFATIGUE])) {
459 // fuer jeden Key die Spellfatigue setzen. Keys mit Dauer 0 loesen keine
460 // Spellfatigue aus.
461 filter(ski[SI_X_SPELLFATIGUE],
462 function int (string key, int val)
463 { return (int)caster->SetSpellFatigue(val, key); } );
464 }
465 else {
466 if ((fat=GetFValueO(SI_SPELLFATIGUE,ski,caster))<0)
467 fat=1;
468 caster->SetSpellFatigue(fat);
469 }
470
471
472 if (res==ERFOLG)
473 Erfolg(caster,spell,ski);
474 else
475 if (res==MISSERFOLG)
476 Misserfolg(caster,spell,ski);
477 return 1;
478}
479
480object *
481FindGroup(object pl, int who) {
482 object *res,ob,env,team;
483 int p1,p2;
484 closure qp;
485
486 res=({});
487 if (!pl || !(env=environment(pl))) return res;
488 p1 = query_once_interactive(pl) ? 1 : -1;
489 team=(object)pl->QueryProp(P_TEAM);
490 for (ob=first_inventory(env);ob;ob=next_inventory(ob)) {
491 if (!living(ob)) continue;
492 qp=symbol_function("QueryProp",ob);
493 if (pl->IsEnemy(ob)) // Feinde sind immer Gegner
494 p2=-1*p1;
495 else if (objectp(team) && funcall(qp,P_TEAM)==team)
496 p2=p1; // Teammitglieder sind immer auf Seite des Spielers
497 else
498 p2 = (query_once_interactive(ob)||funcall(qp,P_FRIEND)) ? 1 : -1;
499 if (p2>0 && !interactive(ob) && query_once_interactive(ob))
500 continue; // keine Netztoten.
501 if (funcall(qp,P_GHOST))
502 continue;
503 if ( who<0 && (funcall(qp,P_NO_ATTACK) || funcall(qp,P_NO_GLOBAL_ATTACK)) )
504 continue;
505 if (IS_LEARNING(ob) &&
506 (funcall(qp,P_INVIS) || (who<0 && !pl->IsEnemy(ob))))
507 continue;
508 if (p1*p2*who >=0)
509 res+=({ob});
510 }
511 return res;
512}
513
514object *
515FindGroupN(object pl, int who, int n) {
516 if (!pl) return ({});
517 n=(n*(int)pl->QuerySkillAttribute(SA_EXTENSION))/100;
518 if (n<1) n=1;
519 return FindGroup(pl,who)[0..(n-1)];
520}
521
522object *
523FindGroupP(object pl, int who, int pr) {
524 object *res,*nres;
525 int i;
526
527 nres=({});
528 if (!pl) return nres;
529 pr=(pr*(int)pl->QuerySkillAttribute(SA_EXTENSION))/100;
530 if (pr<0) return nres;
531 res=FindGroup(pl,who);
532 for (i=sizeof(res)-1;i>=0;i--)
533 if (pr>=random(100))
534 nres+=({res[i]});
535 return nres;
536}
537
538// Findet feindliche und freundliche GRUPPEN im angegebenen Bereich.
539varargs mixed
540FindDistantGroups(object pl, int dist, int dy, int dx) {
541 mapping pos;
542 object ob,enteam,myteam;
543 int p1,min,max,i;
544 mixed *b_rows,x;
545 closure is_enemy, qp;
546
547 if (!objectp(pl) || !environment(pl))
548 return ({({}),({})});
549 if (!dy) dy=100;
550 if (!dx) dx=MAX_TEAM_ROWLEN*100;
551 x=(int)pl->QuerySkillAttribute(SA_EXTENSION);
552 dx=(dx*x)/100;dy=(dy*x)/100;
553 dist=(dist*(int)pl->QuerySkillAttribute(SA_RANGE))/100;
554 min=dist-dy/2;
555 max=dist+dy/2;
556
557 pos=([]);
558 p1=query_once_interactive(pl) ? 1 : -1;
559 is_enemy=symbol_function("IsEnemy",pl); // zur Beschleunigung
560 myteam=(object)pl->QueryProp(P_TEAM);
561 for (ob=first_inventory(environment(pl));ob;ob=next_inventory(ob)) {
562 if (!living(ob)) continue;
563 qp=symbol_function("QueryProp",ob); // zur Beschleunigung
564
565 // Zuerst mal die Position feststellen:
566 if (!objectp(enteam=funcall(qp,P_TEAM)))
567 pos[ob]=1;
568 else if (!pos[ob] && mappingp(x=(mapping)ob->PresentTeamPositions()))
569 pos+=x;
570 // PresentTeamPositions wird nur einmal pro Team ausgerechnet, weil
571 // anschliessend jedes anwesende Teammitglied pos[ob]!=0 hat.
572
573 pos[ob]=(2*pos[ob])-1;
574 // Reihen sollen Abstand 2 haben, auch Reihen 1 und -1 (nach Umrechnung)
575
576 // Feindliche Reihen an Y-Achse spiegeln, also gegenueber hinstellen:
577 if (funcall(is_enemy,ob))
578 pos[ob]*=-1; // Ist auf jeden Fall Feind
579 else if (objectp(myteam) && myteam==enteam)
580 ; // Teammitglieder sind immer auf eigener Seite
581 else
582 pos[ob]*=(p1*((int)(query_once_interactive(ob)||
583 funcall(qp,P_FRIEND))?1:-1));
584
585 // Den Spieler auf keinen Fall entfernen
586 if (ob==pl)
587 continue;
588 // Netztote, Geister und unsichtbare Magier nicht beruecksichtigen,
589 // nicht (global) angreifbare Monster und nicht-feindliche Magier
590 // von feindlicher Seite entfernen.
591 if ((!interactive(ob) && query_once_interactive(ob)) // Netztote raus
592 || funcall(qp,P_GHOST)
593 || (IS_LEARNING(ob) && funcall(qp,P_INVIS)) // Bin nicht da :-)
594 || (pos[ob]<0 && (funcall(qp,P_NO_GLOBAL_ATTACK) // Nicht angreifen
595 || funcall(qp,P_NO_ATTACK)
596 || (IS_LEARNING(ob) && !funcall(is_enemy,ob)))))
597 m_delete(pos,ob);
598 }
599
600 // Reihen aufstellen
601 // Kampfreihe: | 5 4 3 2 1 || 1 2 3 4 5|
602 // Arrays: |0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19|
603 // |<----------- Freunde ------->||<--------- Feinde -------->|
604 b_rows=EMPTY_TEAMARRAY+EMPTY_TEAMARRAY+EMPTY_TEAMARRAY+EMPTY_TEAMARRAY;
605 x=m_indices(pos);
606 for (i=sizeof(x)-1;i>=0;i--) {
607 p1=2*MAX_TEAMROWS-pos[ob=x[i]];
608 pos[ob]=p1;
609 if (p1>=0 && p1<4*MAX_TEAMROWS) {
610 if (random(100)<50) // Damit gut gemischt wird...
611 b_rows[p1]=b_rows[p1]+({ob}); // zufaellig hinten anfuegen
612 else
613 b_rows[p1]=({ob})+b_rows[p1]; // oder vorne anfuegen.
614 }
615 }
616
617 // Umrechnung von Min, Max auf Reihen
618 // ... -2: -124..-75, -1:-74..-25, 0:-24..24, 1:25..74 2:75..124 ...
619 // Das muss man machen, wenn man keine vernuenftige Rundungsfunktion hat:
620 if (min<0) min=-1*((25-min)/50); else min=(25+min)/50;
621 if (max<0) max=-1*((25-max)/50); else max=(25+max)/50;
622 // Relativ zur Position des Spielers verschieben:
623 p1=pos[pl];min+=p1;max+=p1;
624 // Umrechnung von Breite auf Anzahl:
625 dx=((dx+50)/100)-1;if (dx<0) dx=0;
626
627 x=({({}),({})});
628 for (i=0;i<2*MAX_TEAMROWS;i++)
629 if (i>=min && i<=max)
630 x[1]+=b_rows[i][0..dx]; // Freunde
631 for (i=2*MAX_TEAMROWS+1;i<4*MAX_TEAMROWS;i++)
632 if (i>=min && i<=max)
633 x[0]+=b_rows[i][0..dx]; // Feinde
634
635 return x;
636}
637
638// Findet feindliche oder freundliche Gruppe (oder Summe beider) im
639// angegebenen Bereich.
640varargs object *
641FindDistantGroup(object pl, int who, int dist, int dy, int dx) {
642 mixed *x;
643
644 x=FindDistantGroups(pl,dist,dy,dx);
645 if (who<0)
646 return x[0];
647 else if (who>0)
648 return x[1];
649 return x[0]+x[1];
650}
651
652
653static varargs object
654find_victim(string wen, object pl) {
655 object victim;
656
657 if (!pl) return 0;
658 if (!sizeof(wen)) {
659 if (victim = (object)pl->SelectEnemy())
660 return victim;
661 else
662 return 0;
663 }
664 if (victim = present(wen, environment(pl)))
665 return victim;
666 else if (victim = present(wen, pl))
667 return victim;
668 else
669 return 0;
670}
671varargs object
672FindVictim(string wen, object pl, string msg) {
673 object vic;
674
675 if (!(vic=find_victim(wen,pl)) && msg)
heull0018720c5a2018-02-08 12:29:49 +0100676 pl->ReceiveMsg(
677 msg,
678 MT_NOTIFICATION,
679 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200680 return vic;
681}
682varargs object
683FindLivingVictim(string wen, object pl, string msg) {
684 object vic;
685
686 if (!(vic=FindVictim(wen,pl,msg)))
687 return 0;
688 if (!living(vic) || vic->QueryProp(P_GHOST)) {
689 printf("%s lebt doch nicht!\n", capitalize((string)vic->name()));
690 return 0;
691 }
692 return vic;
693}
694
695private varargs object
696DoFindEnemyVictim(string wen, object pl, string msg,
697 mixed func, int min, int max) {
698 object vic;
699 mixed no_attack;
700 int row;
701
702 if (!(vic=FindLivingVictim(wen,pl,msg))) {
703 if ((stringp(wen) && wen!="") || !objectp(pl))
704 return 0;
705 if (pointerp(func)) { // Soll einer DIESER Gegner genommen werden?
706 if (!(vic=(object)pl->SelectEnemy(func))) // Dann daraus auswaehlen
707 return 0;
708 } else {
709 if (!stringp(func))
710 func="SelectEnemy";
711 if (!(vic=(object)call_other(pl,func,0,min,max)))
712 return 0;
713 }
714 func=0; // kein zweites Mal pruefen.
715 }
716 if (no_attack = (mixed)vic->QueryProp(P_NO_ATTACK)) {
717 if (stringp(no_attack))
heull0018720c5a2018-02-08 12:29:49 +0100718 pl->ReceiveMsg(
719 no_attack,
720 MT_NOTIFICATION,
721 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200722 else
heull0018720c5a2018-02-08 12:29:49 +0100723 pl->ReceiveMsg(
724 vic->Name(WER,1)+" laesst sich nicht angreifen.",
725 MT_NOTIFICATION,
726 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200727 return 0;
728 }
729 if (vic==pl) {
heull0018720c5a2018-02-08 12:29:49 +0100730 pl->ReceiveMsg(
731 "Du koenntest Dir dabei wehtun.",
732 MT_NOTIFICATION,
733 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200734 return 0;
735 }
736 if (stringp(func)) {
737 switch(func) {
738 case "SelectNearEnemy":
739 if (pl->PresentPosition()>1) {
heull0018720c5a2018-02-08 12:29:49 +0100740 pl->ReceiveMsg(
741 "Du stehst nicht in der ersten Kampfreihe.",
742 MT_NOTIFICATION,
743 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200744 return 0;
745 }
746 if (vic->PresentPosition()>1) {
heull0018720c5a2018-02-08 12:29:49 +0100747 pl->ReceiveMsg(
748 vic->Name(WER,1)+" ist in einer hinteren Kampfreihe.",
749 MT_NOTIFICATION,
750 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200751 return 0;
752 }
753 break;
754 case "SelectFarEnemy":
755 if (row=(int)vic->PresentPosition())
756 row--;
757 if (row>=min && row<=max)
758 break;
759 if (row<min)
heull0018720c5a2018-02-08 12:29:49 +0100760 pl->ReceiveMsg(
761 vic->Name(WER,1)+" ist zu nahe.",
762 MT_NOTIFICATION,
763 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200764 else if (row>max)
heull0018720c5a2018-02-08 12:29:49 +0100765 pl->ReceiveMsg(
766 vic->Name(WER,1)+" ist zu weit weg.",
767 MT_NOTIFICATION,
768 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200769 else
heull0018720c5a2018-02-08 12:29:49 +0100770 pl->ReceiveMsg(
771 vic->Name(WER,1)+" ist unerreichbar.",
772 MT_NOTIFICATION,
773 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200774 return 0;
775 default:;
776 }
777 } else if (pointerp(func)) {
778 if (member(func,vic)<0) {
heull0018720c5a2018-02-08 12:29:49 +0100779 pl->ReceiveMsg(
780 vic->Name(WER,1)+" ist unerreichbar.",
781 MT_NOTIFICATION,
782 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200783 return 0;
784 }
785 }
786
787 if (!pl->IsEnemy(vic)) // War es bisher kein Feind?
788 pl->Kill(vic); // Dann ist es jetzt einer.
789 return vic;
790}
791
792varargs object
793FindEnemyVictim(string wen, object pl, string msg) {
794 return DoFindEnemyVictim(wen,pl,msg,"SelectEnemy");
795}
796
797// Wie FindEnemyVictim, aber nur im Nahkampf erreichbare Feinde werden
798// gefunden.
799varargs object
800FindNearEnemyVictim(string wen, object pl, string msg) {
801 return DoFindEnemyVictim(wen,pl,msg,"SelectNearEnemy");
802}
803
804// Wie FindEnemyVictim, aber nur Feinde im Bereich der angegebenen Reihen
805// (min,max) werden gefunden.
806varargs object
807FindFarEnemyVictim(string wen, object pl, string msg,
808 int min, int max) {
809 return DoFindEnemyVictim(wen,pl,msg,"SelectFarEnemy",min,max);
810}
811
812// Wie FindEnemyVictim, findet aber nur Feinde in
813// FindDistantGroup(GEGNER,entfernung,abweichung)
814varargs object
815FindDistantEnemyVictim(string wen, object pl, string msg,
816 int dist, int dy) {
817 return DoFindEnemyVictim(wen,pl,msg,
818 FindDistantGroup(pl,-1,dist,dy,10000));
819}
820
821varargs int
822TryGlobalAttackSpell(object caster, mapping sinfo, int suc,
823 int damage, mixed dt, mixed is_spell,
824 int dist, int depth, int width) {
825 int i;
826 mixed x,coldam;
827 object *obs,ob;
828
829 if (!suc) suc=random(sinfo[SI_SKILLABILITY]);
830 if (!dt) dt=GetData(SI_SKILLDAMAGE_TYPE,sinfo,caster);
831 if (!is_spell) is_spell=GetData(SI_SPELL,sinfo,caster);
832 if (!dist) dist=GetRandFValueO(SI_DISTANCE,sinfo,caster);
833 if (!depth) depth=GetRandFValueO(SI_DEPTH,sinfo,caster);
834 if (!width) width=GetRandFValueO(SI_WIDTH,sinfo,caster);
835
836 if (!depth && width) depth=width;
837 if (!width && depth) width=depth;
838 if (!mappingp(is_spell)) is_spell=([]);
839 is_spell[SP_GLOBAL_ATTACK]=1;
840
841 x=FindDistantGroups(caster,dist,depth,width);
842 sinfo[SI_NUMBER_ENEMIES]=sizeof(x[0]);
843 sinfo[SI_NUMBER_FRIENDS]=sizeof(x[1]);
844
845 obs=x[0];
846 for (i=sizeof(obs)-1;i>=0;i--)
847 if (objectp(ob=obs[i]) && suc>=ob->SpellDefend(caster,sinfo))
848 TryAttackSpell(ob,(damage?random(damage):
849 GetRandFValueO(SI_SKILLDAMAGE,sinfo,caster)),
850 dt,is_spell,caster,sinfo);
851
852 if (!intp(coldam=sinfo[SI_COLLATERAL_DAMAGE]) || !coldam)
853 return 1;
854 obs=x[1];
855 for (i=sizeof(obs)-1;i>=0;i--)
856 if (objectp(ob=obs[i]) && suc>=ob->SpellDefend(caster,sinfo))
857 ob->reduce_hit_points(((damage?random(damage):
858 GetRandFValueO(SI_SKILLDAMAGE,sinfo,caster))
859 *coldam)/10);
860 // 10 statt 100 ist Absicht, weil reduce_hit_points schon um Faktor
861 // 10 staerker wirkt als Defend.
862
863 return 1;
864}