1+ #region LICENSE
2+
3+ // The contents of this file are subject to the Common Public Attribution
4+ // License Version 1.0. (the "License"); you may not use this file except in
5+ // compliance with the License. You may obtain a copy of the License at
6+ // https://github.com/NiclasOlofsson/MiNET/blob/master/LICENSE.
7+ // The License is based on the Mozilla Public License Version 1.1, but Sections 14
8+ // and 15 have been added to cover use of software over a computer network and
9+ // provide for limited attribution for the Original Developer. In addition, Exhibit A has
10+ // been modified to be consistent with Exhibit B.
11+ //
12+ // Software distributed under the License is distributed on an "AS IS" basis,
13+ // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14+ // the specific language governing rights and limitations under the License.
15+ //
16+ // The Original Code is MiNET.
17+ //
18+ // The Original Developer is the Initial Developer. The Initial Developer of
19+ // the Original Code is Niclas Olofsson.
20+ //
21+ // All portions of the code written by Niclas Olofsson are Copyright (c) 2014-2018 Niclas Olofsson.
22+ // All Rights Reserved.
23+
24+ #endregion
25+
26+ using System . Numerics ;
27+ using MiNET . Blocks ;
28+ using MiNET . Entities . Passive ;
29+ using MiNET . Utils . Vectors ;
30+
31+ namespace MiNET . Entities . Behaviors
32+ {
33+ public class FindJobBlockBehaviour : BehaviorBase
34+ {
35+ private readonly Villager _entity ;
36+ private Path _currentPath ;
37+ private BlockCoordinates ? blockPosition ;
38+
39+ public FindJobBlockBehaviour ( Villager entity )
40+ {
41+ _entity = entity ;
42+ }
43+
44+ public override bool ShouldStart ( )
45+ {
46+ if ( _entity . Variant != 0 ) return false ;
47+
48+ blockPosition = FindTargetBlock ( _entity , 8 , 3 ) ;
49+
50+ if ( ! blockPosition . HasValue ) return false ;
51+
52+ var pathfinder = new Pathfinder ( ) ;
53+ _currentPath = pathfinder . FindPath ( _entity , blockPosition . Value , blockPosition . Value . DistanceTo ( ( BlockCoordinates ) _entity . KnownPosition ) + 2 ) ;
54+
55+ return _currentPath . HavePath ( ) ;
56+ }
57+
58+ private BlockCoordinates _lastPosition ;
59+ private int _stallTime = 0 ;
60+
61+ public override bool CanContinue ( )
62+ {
63+ var currPos = ( BlockCoordinates ) _entity . KnownPosition ;
64+ if ( currPos == _lastPosition )
65+ {
66+ if ( _stallTime ++ > 200 )
67+ return true ;
68+ }
69+ else
70+ {
71+ _stallTime = 0 ;
72+ _lastPosition = currPos ;
73+ }
74+
75+ return _currentPath . HavePath ( ) ;
76+ }
77+
78+ public override void OnTick ( Entity [ ] entities )
79+ {
80+ if ( _currentPath . HavePath ( ) )
81+ {
82+ if ( ! _currentPath . GetNextTile ( _entity , out var next ) )
83+ {
84+ return ;
85+ }
86+
87+ _entity . Controller . RotateTowards ( new Vector3 ( ( float ) next . X + 0.5f , _entity . KnownPosition . Y , ( float ) next . Y + 0.5f ) ) ;
88+ _entity . EntityDirection = Mob . ClampDegrees ( _entity . EntityDirection ) ;
89+ _entity . KnownPosition . HeadYaw = ( float ) _entity . EntityDirection ;
90+ _entity . KnownPosition . Yaw = ( float ) _entity . EntityDirection ;
91+ _entity . Controller . MoveForward ( 1 , entities ) ;
92+
93+ if ( _lastPosition . DistanceTo ( ( BlockCoordinates ) blockPosition ) < 3 )
94+ {
95+ var block = _entity . Level . GetBlock ( blockPosition ) ;
96+ if ( block is Composter composter && _entity . Variant == 0 )
97+ {
98+ composter . doInteract ( _entity . Level , ( BlockCoordinates ) blockPosition ) ;
99+ }
100+
101+ _entity . Variant = 1 ;
102+ _entity . BroadcastSetEntityData ( ) ;
103+ _entity . Controller . LookAt ( blockPosition ) ;
104+ }
105+ }
106+ else
107+ {
108+ return ;
109+ }
110+ }
111+
112+ public override void OnEnd ( )
113+ {
114+ _entity . Velocity = Vector3 . Zero ;
115+ _entity . KnownPosition . Pitch = 0 ;
116+ _currentPath = null ;
117+ }
118+
119+
120+ protected static BlockCoordinates ? FindTargetBlock ( Entity entity , int dxz , int dy )
121+ {
122+ BlockCoordinates coords = ( BlockCoordinates ) entity . KnownPosition ;
123+ Block currentBlock = null ;
124+ for ( int x = - dxz ; x <= dxz ; x ++ )
125+ {
126+ for ( int y = - dy ; y <= dy ; y ++ )
127+ {
128+ for ( int z = - dxz ; z <= dxz ; z ++ )
129+ {
130+ var blockCoordinates = new BlockCoordinates ( x , y , z ) + coords ;
131+ var block = entity . Level . GetBlock ( blockCoordinates ) ;
132+
133+ if ( block is Composter composter )
134+ {
135+ currentBlock = composter ;
136+
137+ return currentBlock . Coordinates ;
138+ }
139+ }
140+ }
141+ }
142+ return null ;
143+ }
144+ }
145+ }
0 commit comments