• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

AI/Custom control in the game

Status
Not open for further replies.
Level 3
Joined
Jan 29, 2021
Messages
34
Hello guys. Here probably will be the most important moment for me of why I started learning Jass and other stuff

So, to cut it short, I want to make an AI.

But, it is probably not that AI that most of you imagine. Or, probably I should call it the other way like controller or whatever, not an AI, I will explain more in details

How usually AI works? For example, on a standart map there are neutral units that have some logic and behaviour(when you pass by them they start to attack you, use some abilities etc)
Or, you play against computer and he harvests the resources, makes buildings, does upgrades, attacks you sometimes etc.

What I want to do is sending commands to the unit and make it to do some actions in the game!
Not sure what correct example of the map should it be but anyway

Lets assume that it is DOTA. Just for an example.
So, consider you have a map, DOTA map where you can put a computer in player slot and start the game
After the start, computer takes random hero and thats pretty it! He stays at the fountain and does nothing

============================================================
Is there a possible way for me to be able to control that computer hero while being in the game?
For example, via interface like:

-move(real x, real y)
-goToTheShop()
-buyItem(integer itemID)
-attackRoshan()

let it be computer's WitchDoctor with the ability to heal that can be activated and deactivated, then
-useHeal(real howMuchTimeInMiliseconds)

I just type all of these commands using standart ENTER input system

============================================================
Or, is there a possible way to make script, again meaning "script" not in the formal way you all used to but more like external script that will force that computer hero make some list of the actions and thats it
For example, I make a script(dont know how, anyway...), run the script(again no idea how but...), run the game and it forces that computer hero to move to the center of the map, back to the fountain and then again he does nothing

Would aprecciate any help!
 
Last edited:
Level 3
Joined
Jan 29, 2021
Messages
34
Not asking about the full solution, not asking about the DOTA map exactly, it was just an example, could be any other map/any other ganre
Just any of your thoughts about this process
 
Level 9
Joined
Mar 26, 2017
Messages
376
They way AI works is to issue different kinds of orders to the player's units.
What exact orders, at what times, to what units, under which conditions, this logic squarely depends on what you intend to do in your map.

For instance, you could have a very simple 'AI' where a player's trash mobs are periodically ordered to attackmove to a location with enemies.
If you want to properly control a Hero, you can add some stuff. Such as, ocasionally order it to go to a nearby store to purchase a specific item.
Or move to base when it's HP fall below a certain %.

An example would be a trigger such as this:
Every 60 seconds
Pick every unit of Player x and Order picked unit to attack-move to location 1

Now I dont know the dota AI very well, but I imagine it is an example of a quite complex AI. In general properly controlling Heroes with scripted orders would be difficult, if the players has alot of ways to outmanoeuver it.


Now luckily, WC3 has an internal AI that causes computer controlled units to automatically cast their spells on enemies. There are different logic for different spells. For instance, a unit with Thunder Clap will automatically be ordered to cast it when there are a few enemy units nearby.
For this you don't need to do anything. It is automatically enabled for any unit that is under the control of a Computer player.

All things other than spellcasting must be triggered.
 
Level 3
Joined
Jan 29, 2021
Messages
34
You didn't get my idea. The AI or scripts Im talking about are not that you have used to, it's more like script that allows action for some unit to be done in real time
Example
Let it be not Dota map this time, but just a simple melee map like Echo Isles or Amazonia
What I want to do is
When the game starts you have your main base and 5 workers. I want the workers start mining gold by pressing ArrowKey or by pressing Enter and typing custom command like startMining(workersNumber)

So, it's not really an AI but more like game control via external script
 
Level 3
Joined
Jan 29, 2021
Messages
34
Ok, another question related to this topic-what are possible ways to make a delay, like making command to the unit to do smth, then wait some period of time and then resume doing smth?
I need the most accurate and reliable one
And what is the minimal time delay in this game? Can it be like 0.0001 seconds?

And please, tell me if I do understand the triggers mechanics right
My thought-Jass is event-oriented language which allows to use triggers as callbacks, so whenever specific event happened trigger is able to detect that event and run specific function that is attached to it, am I right?
 
Last edited:
Level 9
Joined
Mar 26, 2017
Messages
376
The most accurate way is using a timer.
I don't think there is a minimal delay time. But if your purpose is to visually smoothly move around a unit or effect, you will want to use a ~.03 second interval.
 
Level 3
Joined
Jan 29, 2021
Messages
34
that magic number 0.03 , where does it come from? Some constraints,user experience or what?
Where to read about timers stuff in more detailed way?
 
Last edited:
Level 3
Joined
Jan 29, 2021
Messages
34
And what about threading process? For example, I want first unit doing smth and AT THE SAME TIME another unit doing smth, how this must be implemented?
 
Level 9
Joined
Mar 26, 2017
Messages
376
-The game renders 30 frames per second.

-How much knowledge you have on triggering? You might find such information in a basic guide. All actions in a function are run at the same time, unless you specifically add a wait or timer.
 
Level 3
Joined
Jan 29, 2021
Messages
34
you say that all actions in function run at the same time

Suppose, I want final script to be combination of 5 units actions, so like function SCRIPT takes nothing returns nothing
call unit1actions()
call unit2actions()
....
call unit5actions()

Lets consider now that unitactions() could be VERY logic heavy, could that be a problem?

I mean could unit5 be waiting for unit1actions() to be finished?
 
Level 9
Joined
Mar 26, 2017
Messages
376
Yes, it could cause frame drop. That's why it can be a good idea to spread out super heavy functions using timers, to make things smoother for the player. For instance creating a very large amount of units.

Another function that I know of that can cause frame drop: For some risk maps that are created in GUI, they have this trigger that adds income based on owning units/buildings. It works like this: For every unit of type x, add y gold to owning player. Now you should avoid such inefficient triggering anyway though.
 
Level 3
Joined
Jan 29, 2021
Messages
34
Wouldn't this

function SCRIPT takes nothing returns nothing
ExecuteFunc(unit1actions)
ExecuteFunc(unit2actions)
....
ExecuteFunc(unit5actions)
endfunction

solve the problem?
 

~El

Level 17
Joined
Jun 13, 2016
Messages
556
There's no multithreading in WC3. All code is executed sequentially under the hood. You sorta get the impression of asynchronicity with timers and things like trigger sleeping, but there's still a strict order of execution. So, no, it's not possible for unit A and unit B to be doing something "at the same time". Either of those actions is going to happen before the other.

In case of ExecuteFunc the handlers will still execute sequentially (but with even bigger overhead, actually).

Unless you're doing really heavy computations you shouldn't worry about it. If you turn out to actually need better performance - ditch JASS and use Lua instead.
 
Level 3
Joined
Jan 29, 2021
Messages
34
thx

But if there is no such thing as "multithreading" in WC3, then why would you need
ExecuteFunc() or StartThread() ?
 
Last edited:
Status
Not open for further replies.
Top