Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Parallel but not really parallel. Moving game characters. Coroutines in Lua.

Virtual World game programming involves moving many characters - both player controlled and automatic - in parallel. The movements of each character depend - to a greater or lesser extent - on the prior movements of the character, and of the other charcaters and the resultant position and condition that they're left in. Can you imagine what a complex coding task that could be?

Lua uses coroutines which allows the programmer to write the movement and state logic for each character in its own separate function - with a loop in the code of the function which moves the character and then yields when the movement has been completed. You may consider that all of these functions are running in parallel (and they are certainly all active every cycle), but of course they're really time-sliced with the loop being run in each routine once in every cycle.

I've written coroutine demos before (see [here]), but I wrote a more specific virtual world example during the Lua Programming course I was presenting today.

Scenario. Jane has a 10 metre start over 10 Johns in a chase. Jane moves forward a randon amount each second - somewhere between 0 and 0.9 metres. Each of the Johns moves forward between 0 and 1 metres. Simulate the chase.

Let's start by defining the movement routine for Jane:

  function movejane()
    local covered = 10.0
    while true do
      local stepsize = math.random()*0.9
      covered = covered + stepsize
      at = covered
      target = at
      coroutine.yield()
    end
  end


and the movement routine for each John:

  function movejohn()
    local covered = 0
    while true do
      local stepsize = math.random()
      covered = covered + stepsize
      at = covered
      if at > target then
        winner = 1
      else
        winner = 0
      end
      coroutine.yield()
    end
  end



Note that there are local variables for each of the different characters, and global variables which are used to maintain the global statuses and generally visible data.

We'll then set up a table of characters:

  characters = {coroutine.create(movejane)}
  for k = 1, 10 do
    crh = coroutine.create(movejohn)
    characters[#characters+1] = crh
  end


and ... finally ... code a loop to run our simulation for 200 seconds:

  for seconds = 1, 200 do
    for person = 1,#characters do
      coroutine.resume(characters[person])
      io.write (string.format("%6.2f ",at))
      if winner == 1 then break end
    end
    if winner == 1 then
      io.write(" WONHER\n")
      break
    end
    io.write("\n")
  end


Let's see how that runs ...

munchkin:laug11 grahamellis$ lua charmoves
10.63 0.58 0.94 0.74 0.37 0.94 0.15 0.26 0.85 0.96 0.07
11.46 0.79 1.77 1.31 1.05 1.69 0.95 0.38 1.49 1.94 0.09
11.91 1.40 1.95 1.97 1.27 2.05 1.64 0.46 2.18 2.64 0.22
12.34 1.88 2.05 2.65 1.37 2.35 2.48 0.49 2.33 3.43 1.20
12.41 2.04 2.09 3.41 1.57 3.00 3.08 1.33 2.71 4.40 1.67
12.43 2.45 2.47 4.18 1.85 3.55 3.19 1.89 3.11 5.03 2.12
12.80 2.86 2.47 5.13 2.36 3.72 3.34 1.98 4.07 5.12 3.12
[etc]
31.31 24.97 23.33 28.77 24.51 22.43 26.29 23.60 27.76 25.90 27.72
31.57 25.86 23.66 29.50 25.46 22.58 26.37 23.81 28.26 26.46 28.51
31.95 26.28 24.09 30.01 25.76 22.85 26.88 24.62 29.23 26.68 29.41
32.14 26.70 24.44 30.78 26.39 23.07 27.70 25.29 30.00 27.19 30.24
32.51 27.22 25.09 31.59 27.21 23.48 28.12 26.24 30.50 27.71 30.58
32.94 27.45 25.18 32.53 27.98 23.81 28.59 26.55 31.31 28.32 31.11
33.17 28.45 25.33 33.52 WONHER
munchkin:laug11 grahamellis$


Full source code [here].

(written 2011-08-17, updated 2011-08-18)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
U114 - Lua - Threading and Coroutines.
  [1691] Co-routines in Lua - co-operative processing - (2008-06-29)
  [1699] If you are learning Lua, here are some more examples - (2008-07-06)
  [1870] What to do with a huge crop of apples - (2008-11-04)
  [2314] Passing parameters to a coroutine in Lua - (2009-08-01)
  [2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15)
  [4270] Embedding Lua to perform tailored code at an interval - (2014-05-03)


Back to
The difference between lists and strings - Tcl
Previous and next
or
Horse's mouth home
Forward to
Tables as Objects in Lua - a gentle introduction to data driven programming
Some other Articles
From fish, loaves and apples to money, plastic cards and BACS (Perl references explained)
Perl - making best use of the flexibility, but also using good coding standards
Does a for loop evaluate its end condition once, or on every iteration?
Tables as Objects in Lua - a gentle introduction to data driven programming
Parallel but not really parallel. Moving game characters. Coroutines in Lua.
The difference between lists and strings - Tcl
Rodwell Trail, Weymouth
What costs 8.20 from Melksham, or 22.30 via Chippenham?
For programmers who use Internet Explorer as their browser
Printing objects in C++
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3395_Par ... -Lua-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb