World coordinate system based cloud particle system done
well that didn't take long!
Caveman uses the following world coordinate system:
The game world map is divided up into 5x5 mile map squares. Each map square has its own d3d coordinate system. So a location in the world is speccified by map x and z, AND by a d3d x,y,z in that map square: IE mx,mz,x,y,z.
all i had to do was add an mx,mz to a cloud struct.
init them to the players mx,mz in init_cloud2
made draw_cloud3 use them instead of the player's mx,mz to calculate camera relative coordinates.
added a wrap_around_cloud function to wrap a cloud around to the other side of the cloud box when it moves outside the cloud box, or the cloud box moves beyond it.
added a movecloud function to move a cloud based on windspeed and direction from the weather engine
made sort_clouds (for alpha blending) use world cloud coordinates converted to camera relative coordinates for sorting
added a remove_last_cloud function to remove the last active cloud from the array of cloud structs. The array of cloud structs is statically allocated, as the max number of particles (currently 400) is known at compile time. each cloud struct has an "active" boolean (implemented as int, 0=false, 1=true). when a cloud is added, its "active" variable is set to 1. when its removed, its "active" variable is set to 0. only active clouds are rendered and updated.
added an update _clouds function that adds or removes clouds based on changes in cloud cover info provided by the weather engine, and then calls update_cloud for each active cloud.
added a wrap_around_clouds function that calls wrap _around_cloud on each active cloud. the "cloud box" is the area around the camera that active clouds are in. the cloud box is currently set to a bounding box radius of 2000 units around the camera, and altitudes from 500 to 700 units. since the cloud box is based on the camera's location, the camera must be set before making clouds outside the cloud box wrap around. this tripped me up for a moment. i was handling wrap around once after player/camera/cloud box movement and cloud movement had both been done. but when you changed map squares, it seemed to draw the clouds at their old position (IE 5 miles away) for one frame. doing wrap around after cloud box movement and after cloud movement didn't fix things. the reason is because the player had moved to the next map square, but render hadn't been called, yet, so the camera hadn't been moved to the next map square yet! the solution was to call wrap around once on all clouds just before drawing, at which point the camera has already been set to the new map square.
so now the clouds come and go as you move, and come and go with the winds, and don't change drawing order when you change map squares, and increase and decrease in number with changes in cloud cover. now i need to add grey and red cloud textures and make it use them when appropriate. The original version supported blue, grey, and red skies, and white, grey, and red clouds. Right now the new version only supports blue skies and white clouds.
I think a lot of why it was so quick and easy had to do with the following:
1. CScript. all the new code was done in CScript which allowed the quick coding of the simple routines required.
2. Existing functions in the game. things like normalize_location and camera_relative_coords. being able to copy paste edit x+=spd*cos(yr) etc from generic_move to update_cloud. i can never remember if its x+=sin or x+=cos. wait. just thought about it for a sec, its x+=sin(yr). but usually i'm lazy and just find the formula somewhere in the code like generic_move (moves the player any of 4 directions), move_animal (moves a non-player entity), or move_missile (moves a projectile), and copy and edit it - less typing!
3. the fact that i've been writing particle systems for games for 25 years now. yeah - i know, no fair!
CScript really is nice, i should have been using it all along. i can code new stuff and mod old stuff in a flash. and it only adds 2 seconds to the build time for 71,000+ lines of source code, and that includes the time to minimize visual studio 2012, launch CScript, click to quit when it finishes translating, maximize VS 2012 again, and hit F7 to build. I had it setup as a pre-build step, but since you edit the CScript .cs input file and not the .cpp output file generated by CScipt, VS 2012 thinks the build is up to date since the .cpp file hasn't changed (yet). this means you have to do a rebuild to trigger the CScript pre-build action, or make a change to the current .cpp file. so i went back to a shortcut on the desktop to launch CScript. i also tried it as a tool button, but you can't make it project specific to pass project specific source file names. There's probably some way to do it, but the desktop shortcut with the file name as a parameter works fine.