File: Library_Inventory.c

package info (click to toggle)
openclonk 8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 169,656 kB
  • sloc: cpp: 180,484; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 148; sh: 101; javascript: 34
file content (50 lines) | stat: -rw-r--r-- 1,257 bytes parent folder | download | duplicates (5)
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
/**
	Library_Inventory.c
	Global functions that belong to Libraries.ocd/Inventory.ocd.
	
	@author
*/

// overload function for objects with Inventory.ocd
// documented in /docs/sdk/script/fn
global func ShiftContents(bool shift_back, id target_id)
{
	if (this && (this.HandObjects > 0))
	{
		// special handling for only one hand: just move the hand to next item
		// always move hand 0
		
		// move to target ID?
		if(target_id)
		{
			for(var pos = 0; pos < this.MaxContentsCount; ++pos)
			{
				var obj = this.inventory.objects[pos];
				if(!obj) continue;
				if(obj->GetID() == target_id)
				{
					this->SetHandItemPos(0, pos);
					return true;
				}
			}
			return false;
		}
		// otherwise, move in direction
		var move_dir = 1;
		if(shift_back) move_dir = -1;
		var current_pos = this->GetHandItemPos(0);
		for(var i = this.MaxContentsCount; i > 0; --i)
		{
			current_pos += move_dir;
			if(current_pos < 0) current_pos = this.MaxContentsCount + current_pos;
			else current_pos = current_pos % this.MaxContentsCount;
			
			// is there an object at the slot?
			if(!this.inventory.objects[current_pos]) continue;
			this->SetHandItemPos(0, current_pos);
			return true;
		}
		return false;
	}
	return _inherited(shift_back, target_id, ...);
}