Jump to content

Recommended Posts

Posted

Hi, I am new to AI programming so I need some help. I want to make a ship that flies up to y=150 then flies back down to the ground when it spawns, then, once it is on the ground, it starts spawning my custom entity. How do I do this?

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

Posted

Is your ship an entity? Or does it have a tile entity controlling it?

 

The best way to think about AI is to think in terms of "states" and "stimulus" (conditions that can cause a change of state). For example, your ship will spawn with a state that is "Just spawned and going up". Then at some point it will get to the height (this is a stimulus) and you would change the state to "Going down after first ascent". Then at some point it will hit the ground, where you would change the state to "On the ground and haven't spawned entity yet". Then you would spawn the entity and change the state to "On the ground and already spawned entity".

 

So all you need to do is check each tick (like in the update() method) what the "state", and then check for stimuli that might cause it to change state.

 

I tend to manage the states as integer constants. I think this is preferable to enumerated type because it is easier to map into packets and NBT if needed. I find it better than strings because strings can have spelling mistakes, whereas your IDE will warn you if you try to compare with an undefined constant.

 

So you do something like:

 

private static int SPAWNED_GOING_UP = 0;

private static int SPAWNED_GOING_DOWN = 1;

private static int GROUND_NOT_YET_ENTITY = 2;

private static int GROUND_ALREADY_ENTITY = 3;

 

private int state = SPAWNED_GOING_UP;

 

Then in the update() method of your entity, you process the state then check the stimulus for changes in state. Something like

 

if (state == SPAWNED_GOING_UP)

{

    // move up

    this.posY += 0.5D;

    // check if reached maximum height

    if (this.posY > 150)

    {

        state = SPAWNED_GOING_DOWN)

    {

}

else if (state == SPAWNED_GOING_DOWN)

{

    .

    .

    .

and so on

 

 

If you organize your code this way, it is really easy to make sure it all works correctly, even when complicated things are going on.

 

   

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Thanks this really helped :)

 

However, when I use posY+=0.5D when state is spawned and going up my entity just "bounces" within a blocks space, going up and down then up and down. But when I spawn the entity at 150 it goes down without a problem. Why is this happening?

 

PS I don't have access to my computer right now so I will post code tomorrow :)

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.