aboutsummaryrefslogtreecommitdiff
path: root/src/operate.cc
blob: a76e723726aedfdd2bea38500810b2c2bda864eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include "operate.hh"
#include "info.hh"

#include "stubs.hh"

// operate.cc
void CheckTopMoveObject(uint32 CreatureID, Object Obj, Object Ignore);
void CheckMoveObject(uint32 CreatureID, Object Obj, bool Take);
void CheckMapDestination(uint32 CreatureID, Object Obj, Object MapCon);
void CheckDepotSpace(uint32 CreatureID, Object Source, Object Destination, int Count);
void CheckWeight(uint32 CreatureID, Object Obj, int Count);
void NotifyCreature(uint32 CreatureID, Object Obj, bool Inventory);
void NotifyCreature(uint32 CreatureID, ObjectType Type, bool Inventory);
//void NotifyAllCreatures(Object Obj, int Type, Object OldCon);
void NotifyTrades(Object Obj);
void NotifyDepot(uint32 CreatureID, Object Obj, int Count);
void CloseContainer(Object Con, bool Force);

// moveuse.cc
void MovementEvent(Object Obj, Object Start, Object Dest);
void CollisionEvent(Object Obj, Object Dest);
void SeparationEvent(Object Obj, Object Start);


//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================

void AnnounceMovingCreature(uint32 CreatureID, Object Con){
	TCreature *Creature = GetCreature(CreatureID);
	if(Creature == NULL){
		error("AnnounceMovingCreature: Kreatur %d existiert nicht.\n", CreatureID);
		return;
	}

	int ConX, ConY, ConZ;
	GetObjectCoordinates(Con, &ConX, &ConY, &ConZ);

	// TODO(fusion): 17 and 15 are probably related to the client's viewport dimensions.
	int SearchWidth = 17 + std::abs(Creature->posx - ConX) / 2;
	int SearchHeight = 15 + std::abs(Creature->posy - ConY) / 2;
	int SearchCenterX = (Creature->posx + ConX) / 2;
	int SearchCenterY = (Creature->posy + ConY) / 2;
	TFindCreatures Search(SearchWidth, SearchHeight, SearchCenterX, SearchCenterY, FIND_PLAYERS);
	while(true){
		uint32 CharacterID = Search.getNext();
		if(CharacterID == 0){
			break;
		}

		TPlayer *Player = GetPlayer(CharacterID);
		if(Player != NULL && Player->Connection != NULL){
			SendMoveCreature(Player->Connection, CreatureID, ConX, ConY, ConZ);
		}
	}
}

void AnnounceChangedCreature(uint32 CreatureID, int Type){
	TCreature *Creature = GetCreature(CreatureID);
	if(Creature == NULL){
		error("AnnounceChangedCreature: Kreatur %d existiert nicht (Type=%d).\n", CreatureID, Type);
		return;
	}

	for(TKnownCreature *KnownCreature = Creature->FirstKnowingConnection;
			KnownCreature != NULL;
			KnownCreature = KnownCreature->Next){
		TConnection *KnowingConnection = KnownCreature->Connection;
		if(KnowingConnection->State != CONNECTION_GAME
				|| KnownCreature->State == KNOWNCREATURE_OUTDATED){
			continue;
		}

		if(KnowingConnection->IsVisible(Creature->posx, Creature->posy, Creature->posz)){
			switch(Type){
				case CREATURE_HEALTH_CHANGED: SendCreatureHealth(KnowingConnection, CreatureID); break;
				case CREATURE_LIGHT_CHANGED:  SendCreatureLight(KnowingConnection, CreatureID); break;
				case CREATURE_OUTFIT_CHANGED: SendCreatureOutfit(KnowingConnection, CreatureID); break;
				case CREATURE_SPEED_CHANGED:  SendCreatureSpeed(KnowingConnection, CreatureID); break;
				case CREATURE_SKULL_CHANGED:  SendCreatureSkull(KnowingConnection, CreatureID); break;
				case CREATURE_PARTY_CHANGED:  SendCreatureParty(KnowingConnection, CreatureID); break;
			}
		}else{
			KnownCreature->State = KNOWNCREATURE_OUTDATED;
		}
	}
}

void AnnounceChangedField(Object Obj, int Type){
	if(!Obj.exists()){
		error("AnnounceChangedField: Übergebenes Objekt existiert nicht.\n");
		return;
	}

	int ObjX, ObjY, ObjZ;
	GetObjectCoordinates(Obj, &ObjX, &ObjY, &ObjZ);
	TFindCreatures Search(16, 14, ObjX, ObjY, FIND_PLAYERS);
	while(true){
		uint32 CharacterID = Search.getNext();
		if(CharacterID == 0){
			break;
		}

		TPlayer *Player = GetPlayer(CharacterID);
		if(Player == NULL || Player->Connection == NULL
				|| !Player->Connection->IsVisible(ObjX, ObjY, ObjZ)){
			continue;
		}

		switch(Type){
			case OBJECT_DELETED: SendDeleteField(Player->Connection, ObjX, ObjY, ObjZ, Obj); break;
			case OBJECT_CREATED: SendAddField(Player->Connection,    ObjX, ObjY, ObjZ, Obj); break;
			case OBJECT_CHANGED: SendChangeField(Player->Connection, ObjX, ObjY, ObjZ, Obj); break;
			default:{
				error("AnnounceChangedField: Ungültiger Typ %d.\n", Type);
				return;
			}
		}
	}
}

void AnnounceChangedContainer(Object Obj, int Type){
	if(!Obj.exists()){
		error("AnnounceChangedContainer: Übergebenes Objekt existiert nicht.\n");
		return;
	}

	Object Con = Obj.getContainer();
	TFindCreatures Search(1, 1, Obj, FIND_PLAYERS);
	while(true){
		uint32 CharacterID = Search.getNext();
		if(CharacterID == 0){
			break;
		}

		TPlayer *Player = GetPlayer(CharacterID);
		if(Player == NULL || Player->Connection == NULL){
			continue;
		}

		for(int ContainerNr = 0;
				ContainerNr <= NARRAY(Player->OpenContainer);
				ContainerNr += 1){
			if(Player->GetOpenContainer(ContainerNr) != Con){
				continue;
			}

			switch(Type){
				case OBJECT_DELETED: SendDeleteInContainer(Player->Connection, ContainerNr, Obj); break;
				case OBJECT_CREATED: SendCreateInContainer(Player->Connection, ContainerNr, Obj); break;
				case OBJECT_CHANGED: SendChangeInContainer(Player->Connection, ContainerNr, Obj); break;
				default:{
					error("AnnounceChangedContainer: Ungültiger Typ %d.\n", Type);
					return;
				}
			}
		}
	}
}

void AnnounceChangedInventory(Object Obj, int Type){
	if(!Obj.exists()){
		error("AnnounceChangedInventory: Übergebenes Objekt existiert nicht.\n");
		return;
	}

	uint32 OwnerID = GetObjectCreatureID(Obj);
	TCreature *Creature = GetCreature(OwnerID);
	if(Creature == NULL){
		error("AnnounceChangedInventory: Objekt liegt in keinem Inventory.\n");
		return;
	}

	if(Creature->Type != PLAYER){
		return;
	}

	int Position = GetObjectBodyPosition(Obj);
	switch(Type){
		case OBJECT_DELETED: SendDeleteInventory(Creature->Connection, Position); break;
		case OBJECT_CREATED: SendSetInventory(Creature->Connection, Position, Obj); break;
		case OBJECT_CHANGED: SendSetInventory(Creature->Connection, Position, Obj); break;
		default:{
			error("AnnounceChangedInventory: Ungültiger Typ %d.\n", Type);
			return;
		}
	}
}

void AnnounceChangedObject(Object Obj, int Type){
	if(!Obj.exists()){
		error("AnnounceChangedObject: Übergebenes Objekt existiert nicht.\n");
		return;
	}

	Object Con = Obj.getContainer();
	ObjectType ConType = Con.getObjectType();
	if(ConType.isMapContainer()){
		AnnounceChangedField(Obj, Type);
	}else if(ConType.isBodyContainer()){
		AnnounceChangedInventory(Obj, Type);
	}else{
		AnnounceChangedContainer(Obj, Type);
	}
}

// TODO(fusion): This could have been a simple return value.
void CheckContainerDestination(Object Obj, Object Con){
	Object Help = Con;
	while(Help != NONE && !Help.getObjectType().isMapContainer()){
		if(Help == Obj){
			throw CROSSREFERENCE;
		}
		Help = Help.getContainer();
	}

	ObjectType ConType = Con.getObjectType();
	if(!ConType.getFlag(CHEST)){
		int ConObjects = CountObjectsInContainer(Con);
		int ConCapacity = (int)ConType.getAttribute(CAPACITY);
		if(ConObjects >= ConCapacity){
			throw CONTAINERFULL;
		}
	}
}

void CheckInventoryDestination(Object Obj, Object Con, bool Split){
	ObjectType ObjType = Obj.getObjectType();
	int ConPosition = GetObjectBodyPosition(Con);
	bool HandContainer = ConPosition == INVENTORY_RIGHTHAND
					|| ConPosition == INVENTORY_LEFTHAND;

	if(!HandContainer && ConPosition != INVENTORY_AMMO){
		if(!ObjType.getFlag(CLOTHES)){
			throw WRONGPOSITION;
		}

		int ObjPosition = (int)ObjType.getAttribute(BODYPOSITION);
		if(ObjPosition == 0){
			throw WRONGPOSITION2;
		}else if(ObjPosition != ConPosition){
			throw WRONGCLOTHES;
		}
	}

	uint32 CreatureID = GetObjectCreatureID(Con);
	if(ObjType.isTwoHanded() && HandContainer){
		Object RightHand = GetBodyObject(CreatureID, INVENTORY_RIGHTHAND);
		if(RightHand != NONE && RightHand != Obj){
			throw HANDSNOTFREE;
		}

		Object LeftHand = GetBodyObject(CreatureID, INVENTORY_LEFTHAND);
		if(LeftHand != NONE && LeftHand != Obj){
			throw HANDSNOTFREE;
		}

		return;
	}

	if(GetBodyObject(CreatureID, ConPosition) != NONE){
		throw NOROOM;
	}

	if(HandContainer){
		for(int Position = INVENTORY_HAND_FIRST;
				Position <= INVENTORY_HAND_LAST;
				Position += 1){
			Object Other = GetBodyObject(CreatureID, Position);
			if(Other != NONE){
				ObjectType OtherType = Other.getObjectType();
				if(OtherType.isTwoHanded()){
					throw HANDBLOCKED;
				}

				if(Split || Other != Obj){
					if(OtherType.isWeapon() && ObjType.isWeapon()){
						throw ONEWEAPONONLY;
					}
				}
			}
		}
	}
}

void Move(uint32 CreatureID, Object Obj, Object Con, int Count, bool NoMerge, Object Ignore){
	if(!Obj.exists()){
		error("Move: Übergebenes Objekt existiert nicht.\n");
		throw ERROR;
	}

	if(!Con.exists()){
		int ObjX, ObjY, ObjZ;
		ObjectType ObjType = Obj.getObjectType();
		GetObjectCoordinates(Obj, &ObjX, &ObjY, &ObjZ);
		error("Move: Zielobjekt existiert nicht.\n");
		error("# Objekt %d an [%d,%d,%d]\n", ObjType.TypeID, ObjX, ObjY, ObjZ);
		throw ERROR;
	}

	ObjectType ConType = Con.getObjectType();
	if(!ConType.getFlag(CONTAINER)){
		error("Move: Zielobjekt ist kein Container.\n");
		throw ERROR;
	}

	ObjectType ObjType = Obj.getObjectType();
	if(ObjType.getFlag(CUMULATIVE) && (Count == 0 || Count > (int)Obj.getAttribute(AMOUNT))){
		error("Move: Ungültige Anzahl %d von %d an Teilen bei kumulierbarem Objekt.\n",
				Count, (int)Obj.getAttribute(AMOUNT));
		throw ERROR;
	}

	Object OldCon = Obj.getContainer();
	if(!NoMerge && ConType.isMapContainer()
			&& ObjType.getFlag(CUMULATIVE)
			&& OldCon != Con){
		int ConX, ConY, ConZ;
		GetObjectCoordinates(Con, &ConX, &ConY, &ConZ);
		Object Top = GetTopObject(ConX, ConY, ConZ, true);
		if(Top != NONE){
			try{
				Merge(CreatureID, Obj, Top, Count, Ignore);
				return; // <-- EARLY RETURN IF `Merge` SUCCEEDS.
			}catch(RESULT r){
				if(r == DESTROYED){
					throw;
				}
			}
		}
	}

	// TODO(fusion): The only event inside `Merge` that could modify the object
	// and still throw some non `DESTROYED` error is the separation event.
	//	From what I've seen, it is used exclusively with non CUMULATIVE objects
	// like depot switches to perform "step out" events but using them with
	// CUMULATIVE objects could be problematic as the event is triggered on the
	// whole object which includes both the remainder and moved parts, meaning
	// that transforming the object then would already make the merge "invalid".

	if(!Obj.exists()){
		error("Move: Übergebenes Objekt existiert nicht mehr.\n");
	}

	int ObjCount = 1;
	if(ObjType.getFlag(CUMULATIVE)){
		ObjCount = (int)Obj.getAttribute(AMOUNT);
	}

	if(Count == -1){
		Count = ObjCount;
	}

	bool Split = Count < ObjCount;
	uint32 ObjOwnerID = GetObjectCreatureID(Obj);
	uint32 ConOwnerID = GetObjectCreatureID(Con);
	CheckTopMoveObject(CreatureID, Obj, Ignore);
	if(ConType.isMapContainer()){
		CheckMoveObject(CreatureID, Obj, false);
		CheckMapDestination(CreatureID, Obj, Con);
	}else{
		CheckMoveObject(CreatureID, Obj, true);
		if(ConType.isBodyContainer()){
			CheckInventoryDestination(Obj, Con, Split);
		}else{
			CheckContainerDestination(Obj, Con);
		}

		if(Split){
			CheckDepotSpace(CreatureID, NONE, Con, 1);
		}else{
			CheckDepotSpace(CreatureID, Obj, Con, CountObjects(Obj));
		}

		if(ConOwnerID != 0){
			CheckWeight(ConOwnerID, Obj, Count);
		}
	}

	// NOTE(fusion): `Obj` becomes the split part while `Remainder`, the remainder.
	Object Remainder = NONE;
	if(Split){
		Remainder = Obj;
		Obj = SplitObject(Obj, Count);
	}

	if(OldCon != Con){
		SeparationEvent(Obj, OldCon);
	}

	// TODO(fusion): There is surely a way to reduce these creature checks?
	// perhaps split them into two linear paths, with a few duplications but
	// easier to read.

	if(!ObjType.isCreatureContainer() && Remainder == NONE){
		AnnounceChangedObject(Obj, OBJECT_DELETED);
		NotifyTrades(Obj);
		NotifyDepot(CreatureID, Obj, CountObjects(Obj));
	}

	// TODO(fusion): This could probably be moved to the end of the function
	// along with the other `CloseContainer`.
	if(ObjType.getFlag(CONTAINER) && CreatureID == 0){
		CloseContainer(Obj, true);
	}

	if(ObjType.isCreatureContainer()){
		uint32 MovingCreatureID = Obj.getCreatureID();
		TCreature *Creature = GetCreature(MovingCreatureID);
		if(Creature != NULL){
			Creature->NotifyTurn(Con);
		}else{
			error("Move: Kreatur mit ungültiger ID %d.\n", MovingCreatureID);
		}
		AnnounceMovingCreature(MovingCreatureID, Con);
	}

	MoveObject(Obj, Con);

	if(!ObjType.isCreatureContainer()){
		AnnounceChangedObject(Obj, OBJECT_CREATED);
		NotifyTrades(Obj);
		NotifyDepot(CreatureID, Obj, -CountObjects(Obj));
	}

	if(Remainder != NONE){
		AnnounceChangedObject(Remainder, OBJECT_CHANGED);
		NotifyTrades(Remainder);
	}

	if(ObjType.isCreatureContainer()){
		uint32 MovingCreatureID = Obj.getCreatureID();
		TCreature *Creature = GetCreature(MovingCreatureID);
		if(Creature != NULL){
			Creature->NotifyGo();
		}else{
			error("Move: Kreatur mit ungültiger ID %d.\n", MovingCreatureID);
		}
	}

	NotifyCreature(ObjOwnerID, Obj, OldCon.getObjectType().isBodyContainer());
	NotifyCreature(ConOwnerID, Obj, Con.getObjectType().isBodyContainer());

	if(ObjType.getFlag(CONTAINER)){
		CloseContainer(Obj, false);
	}

	MovementEvent(Obj, OldCon, Con);
	CollisionEvent(Obj, Con);
	NotifyAllCreatures(Obj, 3, OldCon); // CREATURE_MOVE_STIMULUS_ ?
}

void Merge(uint32 CreatureID, Object Obj, Object Dest, int Count, Object Ignore){
	if(!Obj.exists()){
		error("Merge: Übergebenes Objekt existiert nicht.\n");
		throw ERROR;
	}

	if(!Dest.exists()){
		error("Merge: Zielobjekt existiert nicht.\n");
		throw ERROR;
	}

	ObjectType ObjType = Obj.getObjectType();
	if(ObjType.getFlag(CUMULATIVE) && (Count == 0 || Count > (int)Obj.getAttribute(AMOUNT))){
		error("Merge: Ungültige Anzahl %d an Teilen bei kumulierbarem Objekt.\n", Count);
		throw ERROR;
	}

	if(Obj == Dest){
		return;
	}

	if(ObjType != Dest.getObjectType()){
		throw NOMATCH;
	}

	if(!ObjType.getFlag(CUMULATIVE)){
		throw NOTCUMULABLE;
	}

	int ObjCount = Obj.getAttribute(AMOUNT);
	int DestCount = Dest.getAttribute(AMOUNT);
	if(Count == -1){
		Count = ObjCount;
	}

	if((Count + DestCount) > 100){
		throw TOOMANYPARTS;
	}

	Object DestCon = Dest.getContainer();
	uint32 DestOwnerID = GetObjectCreatureID(Dest);
	CheckTopMoveObject(CreatureID, Obj, Ignore);
	if(DestCon.getObjectType().isMapContainer()){
		CheckMoveObject(CreatureID, Obj, false);
		CheckMapDestination(CreatureID, Obj, DestCon);
	}else{
		CheckMoveObject(CreatureID, Obj, true);
		CheckDepotSpace(CreatureID, Obj, DestCon, 0);
		if(DestOwnerID != 0){
			CheckWeight(DestOwnerID, Obj, Count);
		}
	}

	Object ObjCon = Obj.getContainer();
	uint32 ObjOwnerID = GetObjectCreatureID(Obj);

	// TODO(fusion): What is even going on here?
	if(ObjType.getFlag(MOVEMENTEVENT)){
		error("Merge: Movement-Event für kumulierbares Objekt %d wird nicht ausgelöst.\n", ObjType.TypeID);
	}

	if(ObjCon != DestCon){
		SeparationEvent(Obj, ObjCon);
	}

	if(Count < ObjCount){
		ChangeObject(Obj, AMOUNT, ObjCount - Count);
		AnnounceChangedObject(Obj, OBJECT_CHANGED);
		NotifyTrades(Obj);
		ChangeObject(Dest, AMOUNT, DestCount + Count);
	}else{
		AnnounceChangedObject(Obj, OBJECT_DELETED);
		NotifyTrades(Obj);
		NotifyDepot(CreatureID, Obj, 1);
		MergeObjects(Obj, Dest);
	}

	AnnounceChangedObject(Dest, OBJECT_CHANGED);
	NotifyTrades(Dest);
	NotifyCreature(ObjOwnerID, Dest, ObjCon.getObjectType().isBodyContainer());
	NotifyCreature(DestOwnerID, Dest, DestCon.getObjectType().isBodyContainer());
	CollisionEvent(Dest, DestCon);
	NotifyAllCreatures(Dest, 2, NONE); // CREATURE_MOVE_STIMULUS_ ?
}