This repository has been archived by the owner on Aug 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
214 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright © 2014 - 2016 | Wurst-Imperium | All rights reserved. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package tk.wurst_client.ai; | ||
|
||
import net.minecraft.util.math.BlockPos; | ||
|
||
public class GoRandomAI | ||
{ | ||
private RandomPathFinder pathFinder; | ||
private PathProcessor processor; | ||
|
||
private boolean done; | ||
|
||
public GoRandomAI(BlockPos start, float range) | ||
{ | ||
pathFinder = new RandomPathFinder(start, range); | ||
pathFinder.setThinkTime(10); | ||
pathFinder.setFallingAllowed(false); | ||
} | ||
|
||
public void update() | ||
{ | ||
done = false; | ||
|
||
// find path | ||
if(!pathFinder.isDone() && !pathFinder.isFailed()) | ||
{ | ||
if(processor != null) | ||
processor.lockControls(); | ||
|
||
pathFinder.think(); | ||
|
||
if(!pathFinder.isDone() && !pathFinder.isFailed()) | ||
return; | ||
|
||
pathFinder.formatPath(); | ||
|
||
// set processor | ||
processor = pathFinder.getProcessor(); | ||
} | ||
|
||
// check path | ||
if(processor != null | ||
&& !pathFinder.isPathStillValid(processor.getIndex())) | ||
{ | ||
pathFinder = new RandomPathFinder(pathFinder); | ||
return; | ||
} | ||
|
||
// process path | ||
if(!processor.isFailed() && !processor.isDone()) | ||
processor.process(); | ||
else | ||
pathFinder = new RandomPathFinder(pathFinder); | ||
|
||
if(processor.isDone()) | ||
done = true; | ||
} | ||
|
||
public final boolean isDone() | ||
{ | ||
return done; | ||
} | ||
|
||
public void stop() | ||
{ | ||
if(processor != null) | ||
processor.stop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright © 2014 - 2016 | Wurst-Imperium | All rights reserved. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package tk.wurst_client.ai; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.Random; | ||
|
||
import net.minecraft.block.BlockLiquid; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.util.math.BlockPos; | ||
|
||
public class RandomPathFinder extends PathFinder | ||
{ | ||
private final BlockPos center; | ||
private final float rangeSq; | ||
private final int blockRange; | ||
private final Random random; | ||
|
||
public RandomPathFinder(BlockPos center, float range) | ||
{ | ||
super(new BlockPos(Minecraft.getMinecraft().player)); | ||
this.center = center; | ||
this.rangeSq = (float)Math.pow(range, 2); | ||
blockRange = (int)Math.ceil(range); | ||
random = new Random(); | ||
} | ||
|
||
public RandomPathFinder(RandomPathFinder pathFinder) | ||
{ | ||
super(new BlockPos(Minecraft.getMinecraft().player)); | ||
thinkSpeed = pathFinder.thinkSpeed; | ||
thinkTime = pathFinder.thinkTime; | ||
fallingAllowed = pathFinder.fallingAllowed; | ||
center = pathFinder.center; | ||
rangeSq = pathFinder.rangeSq; | ||
blockRange = pathFinder.blockRange; | ||
random = pathFinder.random; | ||
} | ||
|
||
@Override | ||
protected boolean isPassable(BlockPos pos) | ||
{ | ||
if(Math.abs(center.getX() - pos.getX()) > blockRange) | ||
return false; | ||
if(Math.abs(center.getY() - pos.getY()) > blockRange) | ||
return false; | ||
if(Math.abs(center.getZ() - pos.getZ()) > blockRange) | ||
return false; | ||
|
||
return super.isPassable(pos); | ||
} | ||
|
||
@Override | ||
protected boolean checkDone() | ||
{ | ||
return false; | ||
} | ||
|
||
private void setCurrentToRandomNode() | ||
{ | ||
int currentIndex = 0; | ||
int randomIndex = random.nextInt(prevPosMap.size()); | ||
|
||
for(Iterator<PathPos> itr = prevPosMap.keySet().iterator(); itr | ||
.hasNext(); currentIndex++) | ||
if(currentIndex == randomIndex) | ||
{ | ||
current = itr.next(); | ||
break; | ||
}else | ||
itr.next(); | ||
} | ||
|
||
@Override | ||
public ArrayList<PathPos> formatPath() | ||
{ | ||
done = true; | ||
failed = false; | ||
|
||
do | ||
setCurrentToRandomNode(); | ||
while(center.distanceSq(current) > rangeSq | ||
|| (!flying && !canBeSolid(current.down())) | ||
|| getBlock(current.up()) instanceof BlockLiquid); | ||
|
||
return super.formatPath(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters