blob: c0ee294e81f8f73f6b09a2ff41817c6cde27cd83 [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])
Arathorne7dbb072018-02-10 20:07:23 +0100223 && (res=check_restrictions(caster,rmap)))
224 {
225 caster->ReceiveMsg(res, MT_NOTIFICATION|MSG_BS_DONT_WRAP, MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200226 return 0;
227 }
228 return 1;
229}
230
231void
232Learn(object caster, string spell, mapping sinfo) {
233 int val,diff,attval;
234 mapping attr;
235
236 // gar nicht lernen?
237 if (sinfo[SI_NO_LEARN])
238 return;
239
240 // Attribut sollte int sein, wenn nicht anders angegeben
241 if (mappingp(sinfo[SI_LEARN_ATTRIBUTE]))
242 {
243 attr=sinfo[SI_LEARN_ATTRIBUTE];
244 // Direkt in einem durch berechnen.
245 // Ich gehe davon aus, dass wir nie nie nie
246 // ein weiteres Attribut ins MG einfuegen.
247 // Wir berechnen den Mittelwert
248 attval=((int)caster->QueryAttribute(A_INT)*attr[A_INT]+
249 (int)caster->QueryAttribute(A_DEX)*attr[A_DEX]+
250 (int)caster->QueryAttribute(A_STR)*attr[A_STR]+
251 (int)caster->QueryAttribute(A_CON)*attr[A_CON])
252 /(attr[A_CON]+attr[A_INT]+attr[A_STR]+attr[A_DEX]);
253
254
255 } else {
256 attval=(int)caster->QueryAttribute(A_INT);
257 }
258
259
260 val=((attval/2)*GetFValue(SI_SKILLLEARN,sinfo,caster)+
261 GetOffset(SI_SKILLLEARN,sinfo,caster));
262 if (!(diff=GetFValueO(SI_DIFFICULTY,sinfo,caster)))
263 diff=GetFValueO(SI_SPELLCOST,sinfo,caster);
264 caster->LearnSkill(spell,val,diff);
265}
266
267void
268Erfolg(object caster, string spell, mapping sinfo) {
269 object env;
270 if(env=environment(caster))
271 env->SpellInform(caster,spell,sinfo);
272}
273
274void
275Misserfolg(object caster, string spell, mapping sinfo) {
heull0018720c5a2018-02-08 12:29:49 +0100276 caster->ReceiveMsg(
277 "Der Zauberspruch ist missglueckt.\n"
278 "Du lernst aus Deinem Fehler.\n",
279 MT_NOTIFICATION|MSG_DONT_WRAP,
280 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200281 Learn(caster,spell,sinfo);
282}
283
284string
285SelectSpell(string spell, mapping sinfo) {
286 if (sinfo && sinfo[SI_SKILLFUNC])
287 return sinfo[SI_SKILLFUNC];
288 return spell;
289}
290
291varargs void
292prepare_spell(object caster, string spell, mapping sinfo) {
293 string txt;
294
295 if (!caster || !spell) return ;
296 if (!mappingp(sinfo) || !stringp(txt=sinfo[SI_PREPARE_MSG]))
297 txt="Du bereitest Dich auf den Spruch ``%s'' vor.\n";
298 tell_object(caster,sprintf(txt,spell));
299}
300
301varargs int
302UseSpell(object caster, string spell, mapping sinfo) {
303 mapping ski,tmp;
304 string spellbook,sname,fname,txt;
305 int res,fat,cost;
306 mixed ps;
307
308 if (!caster || !spell)
309 return 0;
310 // Spell kann in der Gilde anderen Namen haben
311 sname=SelectSpell(spell,sinfo);
312 if (!(ski=QuerySpell(sname))) // Existiert dieser Spell?
313 return 0;
314 // Gildeneigenschaften sollen Spelleigenschaften ueberschreiben koennen
315 ski=AddSkillMappings(ski,sinfo);
316 // Fuer verschiedene Rassen unterschiedliche Eigenschaften
317 ski=race_modifier(caster,ski);
318
319 // printf("Spellinfo: %O\n",ski);
320
321 if (!CanTrySpell(caster, ski))
322 return 1;
323
324 if (caster->QueryProp(P_SP) < (cost=GetFValueO(SI_SPELLCOST,ski,caster))) {
heull0018720c5a2018-02-08 12:29:49 +0100325 if(txt=ski[SI_SP_LOW_MSG])
326 caster->ReceiveMsg(
327 txt,
328 MT_NOTIFICATION,
329 MA_SPELL);
330 else
331 caster->ReceiveMsg(
332 "Du hast zu wenig Zauberpunkte fuer diesen Spruch.",
333 MT_NOTIFICATION,
334 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200335 return 1;
336 }
337 // printf("cost: %d\n",cost);
338
339 if (mappingp(ski[SI_X_SPELLFATIGUE])) {
340 // fuer jeden Key die Spellfatigue pruefen, wenn das Mapping hinterher
341 // nicht leer ist, ist man zu erschoepft.
342 tmp = filter(ski[SI_X_SPELLFATIGUE],
343 function int (string key, int val)
344 { return (int)caster->CheckSpellFatigue(key); } );
345 if (sizeof(tmp)) {
heull0018720c5a2018-02-08 12:29:49 +0100346 caster->ReceiveMsg(
347 ski[SI_TIME_MSG] ||
348 "Du bist noch zu erschoepft von Deinem letzten Spruch.",
349 MT_NOTIFICATION,
350 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200351 return 1;
352 }
353 }
354 else {
355 if (caster->CheckSpellFatigue()) {
heull0018720c5a2018-02-08 12:29:49 +0100356 caster->ReceiveMsg(
357 ski[SI_TIME_MSG] ||
358 "Du bist noch zu erschoepft von Deinem letzten Spruch.",
359 MT_NOTIFICATION,
360 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200361 return 1;
362 }
363 }
364
365 if (!(ski[SI_NO_ATTACK_BUSY]&NO_ATTACK_BUSY_QUERY) &&
366 caster->QueryProp(P_ATTACK_BUSY)) {
heull0018720c5a2018-02-08 12:29:49 +0100367 if (txt=ski[SI_ATTACK_BUSY_MSG])
368 caster->ReceiveMsg(
369 txt,
370 MT_NOTIFICATION,
371 MA_SPELL);
372 else
373 caster->ReceiveMsg(
374 "Du bist schon zu sehr beschaeftigt.",
375 MT_NOTIFICATION,
376 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200377 return 1;
378 }
379
380 // Spruchvorbereitung
381 if (pointerp(ps=(mixed)caster->QueryProp(P_PREPARED_SPELL)) // Ausstehender Spruch
382 && sizeof(ps)>=3 && intp(ps[0] && stringp(ps[1]))) {
383 if (ps[1]==spell) { // Dieser Spruch wird noch vorbereitet
384 if (time()<ps[0]) {
385 if (!stringp(txt=ski[SI_PREPARE_BUSY_MSG]))
386 txt="Du bist noch mit der Spruchvorbereitung beschaeftigt.\n";
heull0018720c5a2018-02-08 12:29:49 +0100387 caster->ReceiveMsg(
388 txt,
389 MT_NOTIFICATION,
390 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200391 return 1;
392 }
393 }
394 else { // Andere Sprueche brechen die Vorbereitung ab
395 if (!mappingp(tmp=QuerySpell(ps[1])) || // richtige Meldung holen...
396 !stringp(txt=tmp[SI_PREPARE_ABORT_MSG]))
397 txt="Du brichst die Spruchvorbereitung fuer `%s' ab.\n";
398 printf(txt,ps[1]);
399 if (fat=GetValue(SI_PREPARE_TIME,ski,caster)) {
400 // Spruch braucht vorbereitungszeit
401 caster->SetProp(P_PREPARED_SPELL,({time()+fat,spell,ski[SI_SKILLARG]}));
402 prepare_spell(caster,spell,ski);
403 return 1;
404 }
405 }
406 }
407 else {
408 if (fat=GetValue(SI_PREPARE_TIME,ski,caster)) {
409 // Spruch braucht vorbereitungszeit
410 caster->SetProp(P_PREPARED_SPELL,({time()+fat,spell,ski[SI_SKILLARG]}));
411 prepare_spell(caster,spell,ski);
412 return 1;
413 }
414 }
415 if (ps)
416 caster->SetProp(P_PREPARED_SPELL,0);
417
418 // Funktion kann anderen Namen haben als Spell
419 if (!(fname=sinfo[SI_SKILLFUNC]))
420 fname=sname;
421
422 if((ski[SI_NOMAGIC] < environment(caster)->QueryProp(P_NOMAGIC)) &&
423 random(100) < environment(caster)->QueryProp(P_NOMAGIC)) {
424 if (txt=ski[SI_NOMAGIC_MSG])
heull0018720c5a2018-02-08 12:29:49 +0100425 caster->ReceiveMsg(
426 txt,
427 MT_NOTIFICATION,
428 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200429 else
heull0018720c5a2018-02-08 12:29:49 +0100430 caster->ReceiveMsg(
431 "Dein Zauberspruch verpufft im Nichts.",
432 MT_NOTIFICATION,
433 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200434 res=ABGEWEHRT;
435 }
436 else {
437 // Spruch ausfuehren.
438 res=(int)call_other(this_object(),fname,caster,ski);
439 }
Gloinsoncd564772016-08-03 21:57:12 +0200440 if (!res || !caster)
MG Mud User88f12472016-06-24 23:31:02 +0200441 return 1;
MG Mud User88f12472016-06-24 23:31:02 +0200442
Bugfixdd8ce3f2016-08-13 19:54:02 +0200443 if(res==NICHT_ZUSTAENDIG)
444 return 0;
445
MG Mud User88f12472016-06-24 23:31:02 +0200446 if (!(ski[SI_NO_ATTACK_BUSY]&NO_ATTACK_BUSY_QUERY))
447 {
448 if (!ski[SI_ATTACK_BUSY_AMOUNT])
449 caster->SetProp(P_ATTACK_BUSY,1);
450 else
451 caster->SetProp(P_ATTACK_BUSY,ski[SI_ATTACK_BUSY_AMOUNT]);
452 }
453
454 caster->restore_spell_points(-1*cost);
455
456 if (mappingp(ski[SI_X_SPELLFATIGUE])) {
457 // fuer jeden Key die Spellfatigue setzen. Keys mit Dauer 0 loesen keine
458 // Spellfatigue aus.
459 filter(ski[SI_X_SPELLFATIGUE],
460 function int (string key, int val)
461 { return (int)caster->SetSpellFatigue(val, key); } );
462 }
463 else {
464 if ((fat=GetFValueO(SI_SPELLFATIGUE,ski,caster))<0)
465 fat=1;
466 caster->SetSpellFatigue(fat);
467 }
468
469
470 if (res==ERFOLG)
471 Erfolg(caster,spell,ski);
472 else
473 if (res==MISSERFOLG)
474 Misserfolg(caster,spell,ski);
475 return 1;
476}
477
478object *
479FindGroup(object pl, int who) {
480 object *res,ob,env,team;
481 int p1,p2;
482 closure qp;
483
484 res=({});
485 if (!pl || !(env=environment(pl))) return res;
486 p1 = query_once_interactive(pl) ? 1 : -1;
487 team=(object)pl->QueryProp(P_TEAM);
488 for (ob=first_inventory(env);ob;ob=next_inventory(ob)) {
489 if (!living(ob)) continue;
490 qp=symbol_function("QueryProp",ob);
491 if (pl->IsEnemy(ob)) // Feinde sind immer Gegner
492 p2=-1*p1;
493 else if (objectp(team) && funcall(qp,P_TEAM)==team)
494 p2=p1; // Teammitglieder sind immer auf Seite des Spielers
495 else
496 p2 = (query_once_interactive(ob)||funcall(qp,P_FRIEND)) ? 1 : -1;
497 if (p2>0 && !interactive(ob) && query_once_interactive(ob))
498 continue; // keine Netztoten.
499 if (funcall(qp,P_GHOST))
500 continue;
501 if ( who<0 && (funcall(qp,P_NO_ATTACK) || funcall(qp,P_NO_GLOBAL_ATTACK)) )
502 continue;
503 if (IS_LEARNING(ob) &&
504 (funcall(qp,P_INVIS) || (who<0 && !pl->IsEnemy(ob))))
505 continue;
506 if (p1*p2*who >=0)
507 res+=({ob});
508 }
509 return res;
510}
511
512object *
513FindGroupN(object pl, int who, int n) {
514 if (!pl) return ({});
515 n=(n*(int)pl->QuerySkillAttribute(SA_EXTENSION))/100;
516 if (n<1) n=1;
517 return FindGroup(pl,who)[0..(n-1)];
518}
519
520object *
521FindGroupP(object pl, int who, int pr) {
522 object *res,*nres;
523 int i;
524
525 nres=({});
526 if (!pl) return nres;
527 pr=(pr*(int)pl->QuerySkillAttribute(SA_EXTENSION))/100;
528 if (pr<0) return nres;
529 res=FindGroup(pl,who);
530 for (i=sizeof(res)-1;i>=0;i--)
531 if (pr>=random(100))
532 nres+=({res[i]});
533 return nres;
534}
535
536// Findet feindliche und freundliche GRUPPEN im angegebenen Bereich.
537varargs mixed
538FindDistantGroups(object pl, int dist, int dy, int dx) {
539 mapping pos;
540 object ob,enteam,myteam;
541 int p1,min,max,i;
542 mixed *b_rows,x;
543 closure is_enemy, qp;
544
545 if (!objectp(pl) || !environment(pl))
546 return ({({}),({})});
547 if (!dy) dy=100;
548 if (!dx) dx=MAX_TEAM_ROWLEN*100;
549 x=(int)pl->QuerySkillAttribute(SA_EXTENSION);
550 dx=(dx*x)/100;dy=(dy*x)/100;
551 dist=(dist*(int)pl->QuerySkillAttribute(SA_RANGE))/100;
552 min=dist-dy/2;
553 max=dist+dy/2;
554
555 pos=([]);
556 p1=query_once_interactive(pl) ? 1 : -1;
557 is_enemy=symbol_function("IsEnemy",pl); // zur Beschleunigung
558 myteam=(object)pl->QueryProp(P_TEAM);
559 for (ob=first_inventory(environment(pl));ob;ob=next_inventory(ob)) {
560 if (!living(ob)) continue;
561 qp=symbol_function("QueryProp",ob); // zur Beschleunigung
562
563 // Zuerst mal die Position feststellen:
564 if (!objectp(enteam=funcall(qp,P_TEAM)))
565 pos[ob]=1;
566 else if (!pos[ob] && mappingp(x=(mapping)ob->PresentTeamPositions()))
567 pos+=x;
568 // PresentTeamPositions wird nur einmal pro Team ausgerechnet, weil
569 // anschliessend jedes anwesende Teammitglied pos[ob]!=0 hat.
570
571 pos[ob]=(2*pos[ob])-1;
572 // Reihen sollen Abstand 2 haben, auch Reihen 1 und -1 (nach Umrechnung)
573
574 // Feindliche Reihen an Y-Achse spiegeln, also gegenueber hinstellen:
575 if (funcall(is_enemy,ob))
576 pos[ob]*=-1; // Ist auf jeden Fall Feind
577 else if (objectp(myteam) && myteam==enteam)
578 ; // Teammitglieder sind immer auf eigener Seite
579 else
580 pos[ob]*=(p1*((int)(query_once_interactive(ob)||
581 funcall(qp,P_FRIEND))?1:-1));
582
583 // Den Spieler auf keinen Fall entfernen
584 if (ob==pl)
585 continue;
586 // Netztote, Geister und unsichtbare Magier nicht beruecksichtigen,
587 // nicht (global) angreifbare Monster und nicht-feindliche Magier
588 // von feindlicher Seite entfernen.
589 if ((!interactive(ob) && query_once_interactive(ob)) // Netztote raus
590 || funcall(qp,P_GHOST)
591 || (IS_LEARNING(ob) && funcall(qp,P_INVIS)) // Bin nicht da :-)
592 || (pos[ob]<0 && (funcall(qp,P_NO_GLOBAL_ATTACK) // Nicht angreifen
593 || funcall(qp,P_NO_ATTACK)
594 || (IS_LEARNING(ob) && !funcall(is_enemy,ob)))))
595 m_delete(pos,ob);
596 }
597
598 // Reihen aufstellen
599 // Kampfreihe: | 5 4 3 2 1 || 1 2 3 4 5|
600 // Arrays: |0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19|
601 // |<----------- Freunde ------->||<--------- Feinde -------->|
602 b_rows=EMPTY_TEAMARRAY+EMPTY_TEAMARRAY+EMPTY_TEAMARRAY+EMPTY_TEAMARRAY;
603 x=m_indices(pos);
604 for (i=sizeof(x)-1;i>=0;i--) {
605 p1=2*MAX_TEAMROWS-pos[ob=x[i]];
606 pos[ob]=p1;
607 if (p1>=0 && p1<4*MAX_TEAMROWS) {
608 if (random(100)<50) // Damit gut gemischt wird...
609 b_rows[p1]=b_rows[p1]+({ob}); // zufaellig hinten anfuegen
610 else
611 b_rows[p1]=({ob})+b_rows[p1]; // oder vorne anfuegen.
612 }
613 }
614
615 // Umrechnung von Min, Max auf Reihen
616 // ... -2: -124..-75, -1:-74..-25, 0:-24..24, 1:25..74 2:75..124 ...
617 // Das muss man machen, wenn man keine vernuenftige Rundungsfunktion hat:
618 if (min<0) min=-1*((25-min)/50); else min=(25+min)/50;
619 if (max<0) max=-1*((25-max)/50); else max=(25+max)/50;
620 // Relativ zur Position des Spielers verschieben:
621 p1=pos[pl];min+=p1;max+=p1;
622 // Umrechnung von Breite auf Anzahl:
623 dx=((dx+50)/100)-1;if (dx<0) dx=0;
624
625 x=({({}),({})});
626 for (i=0;i<2*MAX_TEAMROWS;i++)
627 if (i>=min && i<=max)
628 x[1]+=b_rows[i][0..dx]; // Freunde
629 for (i=2*MAX_TEAMROWS+1;i<4*MAX_TEAMROWS;i++)
630 if (i>=min && i<=max)
631 x[0]+=b_rows[i][0..dx]; // Feinde
632
633 return x;
634}
635
636// Findet feindliche oder freundliche Gruppe (oder Summe beider) im
637// angegebenen Bereich.
638varargs object *
639FindDistantGroup(object pl, int who, int dist, int dy, int dx) {
640 mixed *x;
641
642 x=FindDistantGroups(pl,dist,dy,dx);
643 if (who<0)
644 return x[0];
645 else if (who>0)
646 return x[1];
647 return x[0]+x[1];
648}
649
650
651static varargs object
652find_victim(string wen, object pl) {
653 object victim;
654
655 if (!pl) return 0;
656 if (!sizeof(wen)) {
657 if (victim = (object)pl->SelectEnemy())
658 return victim;
659 else
660 return 0;
661 }
662 if (victim = present(wen, environment(pl)))
663 return victim;
664 else if (victim = present(wen, pl))
665 return victim;
666 else
667 return 0;
668}
669varargs object
670FindVictim(string wen, object pl, string msg) {
671 object vic;
672
673 if (!(vic=find_victim(wen,pl)) && msg)
heull0018720c5a2018-02-08 12:29:49 +0100674 pl->ReceiveMsg(
675 msg,
676 MT_NOTIFICATION,
677 MA_SPELL);
MG Mud User88f12472016-06-24 23:31:02 +0200678 return vic;
679}
680varargs object
681FindLivingVictim(string wen, object pl, string msg) {
682 object vic;
683
684 if (!(vic=FindVictim(wen,pl,msg)))
685 return 0;
686 if (!living(vic) || vic->QueryProp(P_GHOST)) {
687 printf("%s lebt doch nicht!\n", capitalize((string)vic->name()));
688 return 0;
689 }
690 return vic;
691}
692
693private varargs object
694DoFindEnemyVictim(string wen, object pl, string msg,
695 mixed func, int min, int max) {
696 object vic;
697 mixed no_attack;
698 int row;
699
700 if (!(vic=FindLivingVictim(wen,pl,msg))) {
701 if ((stringp(wen) && wen!="") || !objectp(pl))
702 return 0;
703 if (pointerp(func)) { // Soll einer DIESER Gegner genommen werden?
704 if (!(vic=(object)pl->SelectEnemy(func))) // Dann daraus auswaehlen
705 return 0;
706 } else {
707 if (!stringp(func))
708 func="SelectEnemy";
709 if (!(vic=(object)call_other(pl,func,0,min,max)))
710 return 0;
711 }
712 func=0; // kein zweites Mal pruefen.
713 }
714 if (no_attack = (mixed)vic->QueryProp(P_NO_ATTACK)) {
715 if (stringp(no_attack))
heull0018720c5a2018-02-08 12:29:49 +0100716 pl->ReceiveMsg(
717 no_attack,
718 MT_NOTIFICATION,
719 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200720 else
heull0018720c5a2018-02-08 12:29:49 +0100721 pl->ReceiveMsg(
722 vic->Name(WER,1)+" laesst sich nicht angreifen.",
723 MT_NOTIFICATION,
724 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200725 return 0;
726 }
727 if (vic==pl) {
heull0018720c5a2018-02-08 12:29:49 +0100728 pl->ReceiveMsg(
729 "Du koenntest Dir dabei wehtun.",
730 MT_NOTIFICATION,
731 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200732 return 0;
733 }
734 if (stringp(func)) {
735 switch(func) {
736 case "SelectNearEnemy":
737 if (pl->PresentPosition()>1) {
heull0018720c5a2018-02-08 12:29:49 +0100738 pl->ReceiveMsg(
739 "Du stehst nicht in der ersten Kampfreihe.",
740 MT_NOTIFICATION,
741 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200742 return 0;
743 }
744 if (vic->PresentPosition()>1) {
heull0018720c5a2018-02-08 12:29:49 +0100745 pl->ReceiveMsg(
746 vic->Name(WER,1)+" ist in einer hinteren Kampfreihe.",
747 MT_NOTIFICATION,
748 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200749 return 0;
750 }
751 break;
752 case "SelectFarEnemy":
753 if (row=(int)vic->PresentPosition())
754 row--;
755 if (row>=min && row<=max)
756 break;
757 if (row<min)
heull0018720c5a2018-02-08 12:29:49 +0100758 pl->ReceiveMsg(
759 vic->Name(WER,1)+" ist zu nahe.",
760 MT_NOTIFICATION,
761 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200762 else if (row>max)
heull0018720c5a2018-02-08 12:29:49 +0100763 pl->ReceiveMsg(
764 vic->Name(WER,1)+" ist zu weit weg.",
765 MT_NOTIFICATION,
766 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200767 else
heull0018720c5a2018-02-08 12:29:49 +0100768 pl->ReceiveMsg(
769 vic->Name(WER,1)+" ist unerreichbar.",
770 MT_NOTIFICATION,
771 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200772 return 0;
773 default:;
774 }
775 } else if (pointerp(func)) {
776 if (member(func,vic)<0) {
heull0018720c5a2018-02-08 12:29:49 +0100777 pl->ReceiveMsg(
778 vic->Name(WER,1)+" ist unerreichbar.",
779 MT_NOTIFICATION,
780 MA_FIGHT);
MG Mud User88f12472016-06-24 23:31:02 +0200781 return 0;
782 }
783 }
784
785 if (!pl->IsEnemy(vic)) // War es bisher kein Feind?
786 pl->Kill(vic); // Dann ist es jetzt einer.
787 return vic;
788}
789
790varargs object
791FindEnemyVictim(string wen, object pl, string msg) {
792 return DoFindEnemyVictim(wen,pl,msg,"SelectEnemy");
793}
794
795// Wie FindEnemyVictim, aber nur im Nahkampf erreichbare Feinde werden
796// gefunden.
797varargs object
798FindNearEnemyVictim(string wen, object pl, string msg) {
799 return DoFindEnemyVictim(wen,pl,msg,"SelectNearEnemy");
800}
801
802// Wie FindEnemyVictim, aber nur Feinde im Bereich der angegebenen Reihen
803// (min,max) werden gefunden.
804varargs object
805FindFarEnemyVictim(string wen, object pl, string msg,
806 int min, int max) {
807 return DoFindEnemyVictim(wen,pl,msg,"SelectFarEnemy",min,max);
808}
809
810// Wie FindEnemyVictim, findet aber nur Feinde in
811// FindDistantGroup(GEGNER,entfernung,abweichung)
812varargs object
813FindDistantEnemyVictim(string wen, object pl, string msg,
814 int dist, int dy) {
815 return DoFindEnemyVictim(wen,pl,msg,
816 FindDistantGroup(pl,-1,dist,dy,10000));
817}
818
819varargs int
820TryGlobalAttackSpell(object caster, mapping sinfo, int suc,
821 int damage, mixed dt, mixed is_spell,
822 int dist, int depth, int width) {
823 int i;
824 mixed x,coldam;
825 object *obs,ob;
826
827 if (!suc) suc=random(sinfo[SI_SKILLABILITY]);
828 if (!dt) dt=GetData(SI_SKILLDAMAGE_TYPE,sinfo,caster);
829 if (!is_spell) is_spell=GetData(SI_SPELL,sinfo,caster);
830 if (!dist) dist=GetRandFValueO(SI_DISTANCE,sinfo,caster);
831 if (!depth) depth=GetRandFValueO(SI_DEPTH,sinfo,caster);
832 if (!width) width=GetRandFValueO(SI_WIDTH,sinfo,caster);
833
834 if (!depth && width) depth=width;
835 if (!width && depth) width=depth;
836 if (!mappingp(is_spell)) is_spell=([]);
837 is_spell[SP_GLOBAL_ATTACK]=1;
838
839 x=FindDistantGroups(caster,dist,depth,width);
840 sinfo[SI_NUMBER_ENEMIES]=sizeof(x[0]);
841 sinfo[SI_NUMBER_FRIENDS]=sizeof(x[1]);
842
843 obs=x[0];
844 for (i=sizeof(obs)-1;i>=0;i--)
845 if (objectp(ob=obs[i]) && suc>=ob->SpellDefend(caster,sinfo))
846 TryAttackSpell(ob,(damage?random(damage):
847 GetRandFValueO(SI_SKILLDAMAGE,sinfo,caster)),
848 dt,is_spell,caster,sinfo);
849
850 if (!intp(coldam=sinfo[SI_COLLATERAL_DAMAGE]) || !coldam)
851 return 1;
852 obs=x[1];
853 for (i=sizeof(obs)-1;i>=0;i--)
854 if (objectp(ob=obs[i]) && suc>=ob->SpellDefend(caster,sinfo))
855 ob->reduce_hit_points(((damage?random(damage):
856 GetRandFValueO(SI_SKILLDAMAGE,sinfo,caster))
857 *coldam)/10);
858 // 10 statt 100 ist Absicht, weil reduce_hit_points schon um Faktor
859 // 10 staerker wirkt als Defend.
860
861 return 1;
862}