summaryrefslogtreecommitdiffstats
path: root/src/Items/ItemBoat.h
blob: d6e9ed7c362164e4ff91829ac54c1730297ef6e2 (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

#pragma once

#include "../Entities/Boat.h"
#include "../LineBlockTracer.h"





class cItemBoatHandler:
	public cItemHandler
{
	using Super = cItemHandler;

public:

	cItemBoatHandler(int a_ItemType):
		Super(a_ItemType)
	{
	}





	virtual bool OnItemUse(
		cWorld * a_World,
		cPlayer * a_Player,
		cBlockPluginInterface & a_PluginInterface,
		const cItem & a_HeldItem,
		const Vector3i a_ClickedBlockPos,
		eBlockFace a_ClickedBlockFace
	) override
	{
		// Only allow placing blocks on top of blocks, or when not in range of dest block:
		if ((a_ClickedBlockFace != BLOCK_FACE_YM) && (a_ClickedBlockFace != BLOCK_FACE_NONE))
		{
			return false;
		}

		// Find the actual placement position by tracing line of sight until non-air block:
		class cCallbacks:
			public cBlockTracer::cCallbacks
		{
		public:
			Vector3d m_Pos;
			bool m_HasFound;

			cCallbacks():
				m_HasFound(false)
			{
			}

			virtual bool OnNextBlock(Vector3i a_CBBlockPos, BLOCKTYPE a_CBBlockType, NIBBLETYPE a_CBBlockMeta, eBlockFace a_CBEntryFace) override
			{
				if (a_CBBlockType != E_BLOCK_AIR)
				{
					m_Pos = a_CBBlockPos;
					m_HasFound = true;
					return true;
				}
				return false;
			}
		} Callbacks;
		auto Start = a_Player->GetEyePosition() + a_Player->GetLookVector();
		auto End = a_Player->GetEyePosition() + a_Player->GetLookVector() * 5;
		cLineBlockTracer::Trace(*a_World, Callbacks, Start, End);
		if (!Callbacks.m_HasFound)
		{
			return false;
		}

		// Block above must be air to spawn a boat (prevents spawning a boat underwater)
		auto PosAbove = Callbacks.m_Pos.Floor().addedY(1);
		if (!cChunkDef::IsValidHeight(PosAbove.y))
		{
			return false;
		}
		BLOCKTYPE BlockAbove = a_World->GetBlock(PosAbove);
		if (BlockAbove != E_BLOCK_AIR)
		{
			return false;
		}

		// Spawn block at water level
		if (a_World->SpawnBoat(Callbacks.m_Pos + Vector3d(0.5, 1, 0.5), cBoat::ItemToMaterial(a_Player->GetEquippedItem())) == cEntity::INVALID_ID)
		{
			return false;
		}

		// Remove boat from players hand
		if (!a_Player->IsGameModeCreative())
		{
			a_Player->GetInventory().RemoveOneEquippedItem();
		}

		return true;
	}
} ;