Skip to content

Commit 6fbf4f0

Browse files
committed
villager can take farmer job
1 parent 32b0de6 commit 6fbf4f0

File tree

7 files changed

+209
-31
lines changed

7 files changed

+209
-31
lines changed

src/MiNET/MiNET/Blocks/Composter.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public override bool Interact(Level level, Player player, BlockCoordinates block
2020
{
2121
//todo: level add possibility
2222
if (!compostableIds.Contains(player.Inventory.GetItemInHand().Id)) { return true; }
23+
doInteract(level, blockCoordinates);
24+
return true;
25+
}
26+
27+
public void doInteract(Level level, BlockCoordinates blockCoordinates)
28+
{
2329
if (ComposterFillLevel < 8)
2430
{
2531
ComposterFillLevel = ComposterFillLevel + 1;
@@ -35,7 +41,6 @@ public override bool Interact(Level level, Player player, BlockCoordinates block
3541
}
3642
LegacyParticle particle = new VillagerHappy(level) { Position = new Vector3(Coordinates.X + 0.5f, Coordinates.Y + 1, Coordinates.Z + 0.5f) };
3743
particle.Spawn();
38-
return true;
3944
}
4045
}
4146
}

src/MiNET/MiNET/Entities/Behaviors/FollowOwnerBehavior.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,31 +124,6 @@ public override void OnTick(Entity[] entities)
124124
_entity.Controller.LookAt(owner);
125125
}
126126

127-
private bool GetNextTile(out Tile next)
128-
{
129-
next = null;
130-
if (_currentPath.NoPath()) return false;
131-
132-
next = _currentPath.First();
133-
134-
BlockCoordinates currPos = (BlockCoordinates) _entity.KnownPosition;
135-
if ((int) next.X == currPos.X && (int) next.Y == currPos.Z)
136-
{
137-
_currentPath.Remove(next);
138-
139-
if (!GetNextTile(out next)) return false;
140-
}
141-
142-
return true;
143-
}
144-
145-
public double Distance(Player player, Tile tile)
146-
{
147-
Vector2 pos1 = new Vector2(player.KnownPosition.X, player.KnownPosition.Z);
148-
Vector2 pos2 = new Vector2((float) tile.X, (float) tile.Y);
149-
return (pos1 - pos2).Length();
150-
}
151-
152127
public override void OnEnd()
153128
{
154129
_entity.Velocity = Vector3.Zero;

src/MiNET/MiNET/Entities/Behaviors/MobController.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ public void LookAt(Entity target)
7575
_entity.KnownPosition.Pitch = (float) pitch;
7676
}
7777

78+
public void LookAt(PlayerLocation location)
79+
{
80+
Vector3 targetPos = location + new Vector3(0, 1, 0);
81+
Vector3 entityPos = _entity.KnownPosition + new Vector3(0, (float) _entity.Height, 0) + _entity.GetHorizDir() * (float) _entity.Length / 2f;
82+
var d = Vector3.Normalize(targetPos - entityPos);
83+
84+
var dx = d.X;
85+
var dy = d.Y;
86+
var dz = d.Z;
87+
88+
double tanOutput = 90 - RadianToDegree(Math.Atan(dx / (dz)));
89+
double thetaOffset = 270d;
90+
if (dz < 0)
91+
{
92+
thetaOffset = 90;
93+
}
94+
var yaw = /*ClampDegrees*/ (thetaOffset + tanOutput);
95+
96+
double pitch = RadianToDegree(-Math.Asin(dy));
97+
98+
_entity.KnownPosition.Yaw = (float) yaw;
99+
_entity.KnownPosition.HeadYaw = (float) yaw;
100+
_entity.KnownPosition.Pitch = (float) pitch;
101+
}
102+
78103
public void RotateTowards(Vector3 targetPosition)
79104
{
80105
Vector3 entityPosition = _entity.KnownPosition;
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}

src/MiNET/MiNET/Entities/EntityType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public enum EntityType
112112
Pig = 12,
113113
Sheep = 13,
114114
Wolf = 14,
115-
Villager = 15,
115+
Villager = 115,
116116
MushroomCow = 16,
117117
Squid = 17,
118118
Rabbit = 18,
@@ -183,7 +183,7 @@ public static class EntityHelpers
183183
{ EntityType.Panda, "minecraft:panda" },
184184
{ EntityType.Salmon, "minecraft:salmon" },
185185
{ EntityType.Pig, "minecraft:pig" },
186-
{ EntityType.Villager, "minecraft:villager" },
186+
{ EntityType.Villager, "minecraft:villager_v2" },
187187
{ EntityType.Fish, "minecraft:cod" },
188188
{ EntityType.Pufferfish, "minecraft:pufferfish" },
189189
{ EntityType.Cow, "minecraft:cow" },

src/MiNET/MiNET/Entities/Passive/Villager.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,43 @@
2323

2424
#endregion
2525

26+
using MiNET.Entities.Behaviors;
27+
using MiNET.Utils.Metadata;
2628
using MiNET.Worlds;
2729

2830
namespace MiNET.Entities.Passive
2931
{
3032
public class Villager : PassiveMob
3133
{
34+
public int Variant { get; set; }
3235
public Villager(Level level) : base(EntityType.Villager, level)
3336
{
3437
Width = Length = 0.6;
35-
Height = 1.8;
38+
Height = 1.9;
39+
HealthManager.MaxHealth = 200;
40+
HealthManager.ResetHealth();
41+
Speed = 0.3;
42+
IsInLove = false;
43+
44+
Behaviors.Add(new PanicBehavior(this, 60, Speed, 1.4));
45+
46+
Behaviors.Add(new WanderBehavior(this, 1.0, 40));
47+
Behaviors.Add(new LookAtEntityBehavior(this, 4, 20));
48+
Behaviors.Add(new LookAtPlayerBehavior(this, 4, 10));
49+
Behaviors.Add(new RandomLookaroundBehavior(this, 40));
50+
Behaviors.Add(new FindJobBlockBehaviour(this));
51+
}
52+
53+
public override MetadataDictionary GetMetadata()
54+
{
55+
Scale = IsBaby ? 0.50f : 1.0;
56+
MetadataDictionary metadata = base.GetMetadata();
57+
metadata[(int) MetadataFlags.Variant] = new MetadataInt(Variant);
58+
metadata[(int) MetadataFlags.ContainerType] = new MetadataShort(247);
59+
metadata[(int) MetadataFlags.ContainerSize] = new MetadataByte(8);
60+
metadata[(int) MetadataFlags.MaxTradeTier] = new MetadataInt(4);
61+
62+
return metadata;
3663
}
3764
}
3865
}

src/MiNET/MiNET/Player.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,15 +3283,16 @@ public void SendStartGame()
32833283
levelSettings.permissionLevel = (byte) PermissionLevel;
32843284
levelSettings.gameVersion = "";
32853285
levelSettings.hasEduFeaturesEnabled = true;
3286-
3286+
levelSettings.onlySpawnV1Villagers = false;
3287+
32873288
var startGame = McpeStartGame.CreateObject();
32883289
startGame.levelSettings = levelSettings;
32893290
startGame.entityIdSelf = EntityId;
32903291
startGame.runtimeEntityId = EntityManager.EntityIdSelf;
32913292
startGame.playerGamemode = (int) GameMode;
32923293
startGame.spawn = SpawnPosition;
32933294
startGame.rotation = new Vector2(KnownPosition.HeadYaw, KnownPosition.Pitch);
3294-
3295+
32953296
startGame.levelId = "1m0AAMIFIgA=";
32963297
startGame.worldName = Level.LevelName;
32973298
startGame.premiumWorldTemplateId = "";

0 commit comments

Comments
 (0)