I wanted to understand how a strategy game like Clash of Clans would implement the attack. Clash of clans is just an example I am taking but I am interested in understadning logic of attack in any strategy game.

I understand that generally a game has a loop running which updates the states of all game entities every frame and renders the graphics with that state every frame.

I can think of one way to do this but do not know if it is correct.

  1. Just like any other game - run the loop for each frame and update all entities. I am more interested in understanding how to find out whether defensive entity A kills Offensive entity B or the other way round. The deciding factors are:

a. Attack speed (hitpoints per seconds)

b. total health/hitpoints

c. how many are entities attacking that entity

d. time

Here the factor that is giving me trouble understanding this is the last - time. How do we go about implementing this using real-world duration?

And if we do it this way, how to complete the attack before real world time? Because that is what happens in clash of clans when I close the app and reopen it during an attack.

Second hypothetical I am wondering is can we find the result without going through the loop? Once the army is deployed, can we do all the calculations without depending upon the real world time.

Consider for example, cannon is attacking archer. We know who will win before that attack is over because we know all their properties (hitpoint/attack speed, etc.) So ideally, we should not have to wait for real world time to pass to know the result of the attack. It gets tricky when there are muliple troops and multiple defences and we have to take into consideration all of them. So my question is I guess - how can we do that - simulate this war without depending on the real world time?

  • 1
    \$\begingroup\$ Clash of Clans uses a proprietary engine. We don't know exactly how their engine works. \$\endgroup\$
    – Kevin
    Commented Aug 31, 2022 at 1:28
  • 1
    \$\begingroup\$ It sounds like you're asking how an online game like Clash of Clans updates the game state while players are away. We have prior Q&A about this: Timed events, Resource production. The basic idea is: it doesn't. It just logs information about the last player-initiated/observed state changes. The next time a player logs in and observes the state, it updates it to account for the time passed. Does that answer your question, or can you edit your post to clarify what you need help with? \$\endgroup\$
    – DMGregory
    Commented Aug 31, 2022 at 3:09
  • \$\begingroup\$ Thanks @DMGregory for making it a better question and providing your answer. However I am looking for something else, I have rephrased the question. Hope now it is clearer. \$\endgroup\$
    – rsp
    Commented Aug 31, 2022 at 18:42
  • 1
    \$\begingroup\$ This post now seems to be asking multiple questions all at once: 1. How attack & HP stats should contribute to battle outcome resolution (game design), 2. How to implement a given resolution rule efficiently. 3. How to store/restore state when the player quits and resumes the game. I think you should edit the question to focus on one or the other, and delete all references to Clash of Clans. Tell us about the game you are making, and how your game should work, even if CoC never existed. \$\endgroup\$
    – DMGregory
    Commented Aug 31, 2022 at 18:47


You must log in to answer this question.

Browse other questions tagged .