Contents Back Forward |
8. Wonders Thread |
8.1 Wonders Thread overview In the last chapter we've seen how Civ Thread works. Here we'll analyze deeply another thread: the Wonders Thread. The wonders thread scan continuosly wonder list in memory and, continuosly it executes WonderCheck function; the CSPL designer projects his civ-related events just changing the WonderCheck function, leaving untouched the main structure of Wonders Thread. There is a big difference between Wonders Thread and other Threads we saw in previous chapters: Wonders Thread doesn't use a ReadNextWonder function; The exact source-code of Civ Thread is the following: void WonderCycle() { while(true)//Starts a continuos cycle     {     WonderCheck();//Call WonderCheck function continuosly     Sleep(1);//Wait a bit (just to avoid to freeze Tot)     } } | 8.2 Wonders functions In CSPL i've defined several functions to manage wonders:
WORD GetCityWonder(int Wonder) It scans Wonders list for wonder passed as parameter and returns the ID number of city in which wonder is located (it returns 0xFFFF if wonder has not been built yet while it returns 0xEFFF if wonder has been destroyed, i've coded two constants to help CSPL designees:WOND_NOT_BUILT and WOND_DESTROYED which maps these values) bool SetCityWonder(WORD CityID,int Wonder) This function set Wonder passed as parameter as built in city wi ID equals to CityID passed as parameter. bool DestroyWonder(int Wonder) This function is used to destroy a wonder (the wonder passed as parameter) Destroing a wonder means that the wonder cannot be built again by other civs, and it appears as lost in wonder screen. bool DeleteWonder(int Wonder) This function is used to delete a wonder (the wonder passed as parameter). Deleting a wonder means that the wonder is restored to a pre-build state: it can be built by civs with correct pre-requisites and it doesn't show in wonder screen. |
8.3 Example 7 : MovingWonder From an idea of CyberChrist. This example is the first to use different threads, so expect it to be a bit more complex than others. One of the annoyances about Wonders is that they're stucked in cities in which they're built: In some scenarios it could be interesting to move wonders as units between cities of owner civilization; imagine a Special King unit which acts as a wonder when stationed in cities (maybe as Shakespeare theatre to calm down citizen or as King Richard's crusade to boost production in the city), or an Einstein unit which acts as a moving copernicus observatory, this is exactly what we're trying to do in this example PHASE 0: CREATING A NEW PROJECTAs we've learned in the previous chapters the first step towards CSPL compilation is the project creation (usually done with CSPLCompanion);again create a new project called MovingWonder. PHASE 1: UNDERSTANDING WHAT WE NEEDThe first thing a CSPL designer should think is : "which thread i need?"In this situation, since we just want to play with wonders AND units, our choise is very easy: we need Wonders Thread and Units Thread, BOTH of them. PHASE 2: DESIGNING THE EVENTThe next thing we have to do is to design the "skeleton" of our event:From the first chapter we know that event is made of HEAD (Trigger Statement) and BODY (Action Statement): In this case HEAD is "The King Unit is in a city" while BODY is "Set wonder as built in that city." PHASE 3: FIXING UP SECONDARY PROBLEMSThe event designed in phase 2 leaves a lot of problems still open:First of all, there is only one Wonder so we need to ensure that it will be only one Special King Unit in the whole game. This can be done creating King Unit as a special unit, this means fixing prereq to no and giving a single unit to a particular civ (using for example events or giving it to one civ from start). Another problem is with wonder: we must ensure that no one will ever build that wonder (again, this can be obtained with no prereq) Another problem: what we should do while King is in transit between cities? in my opinion wonder should be destroyed and re-established when King units enters in a new city. In that way, if the King is killed while moving, the wonder effect is lost forever. Now we've cleared a couple of things about the event we should realize and we can start develping it PHASE 4: CODING THE EVENTWe should check continuosly for King unit position, then we've to check if there is a city at that position and, if the answer is yes, we should set wonder to that city, while if the answer is no, we should destroy the wonder.First of all we need to identify King unit. If the King unit is present from start we can extract the ID number and call UnitID to obtain infos about King unit; else we can use UnitType called with King type as parameter to obtain the same info (notice that obviously there must be only one unit of King type in the game). Let's say we don't need Mech. Inf. unit in our medieval scenario and let's consider mech inf type as our King type Itīs time to write a bit of code:
Then we have to detect if unit is inside a city or not:
if (StillActive) If IsThereACity is true than a city is present at King coordinates and its infos are stored in Temp variable now we've to do two things, if there isn't a city we need to destroy the wonder (BTW i chose Shakespeare's theatre), else we need to set this wonder into Temp city. This can be done by the following code:
if (IsThereACity)
As we said above the code we've written should be placed in WonderCheck:
PHASE 5: MERGING THE RESULTING SOURCE CODENow it's time to merge all source code we've written:Editing CSPLClient.h: In CSPLClient.h we need to activate the wonders thread: BYTE ACTIVITY_FLAG=ACTIVATE_WONDER; And we've finished with CSPLClient.h . Editing CSPLClient.csp: The only thing we've to do here is to edit the WonderCheck function as descrived in previous phase:
PHASE 6: COMPILING AND LINKING THE SOURCE CODEAt this point save CSPLClient.h and CSPLClient.csp files and use CSPLCompanion to compile and link MovingWonder;you should obtain a CSPLClient executable in MovingWonder directory, To test this example you should create a game with a couple of cities, and then adds a Mech. Inf. unit (remember our hypothesis of using MechInf type as King unit); start CSPLClient.exe and move Mech. Inf. unit between your cities, check frequently Wonder screen and if all goes well you should see the Shakespeare's Theatre moving with the King (Mech. Inf.) unit. Notice that this example is far from an efficient CSPL program 'cause the same info about city presence on King tile can be obtained looking directly to map structure (using the city bit, read the Map Thread chapter for more details); we still have to call CityAt to obtain City informations but ONLY if city is present, saving a lot of precious CPU time. |