Announcement

Collapse
No announcement yet.

Design question

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Design question

    A good ways through my JavaScript eBook, which I read in bursts while my kids are napping. Just got a decent understanding of how arrays work. Will we be using those to store in-game values like terrain and unit stats? I.e., will there be a UnitsStrength array, a UnitNames array, etc.? I'm trying to visualize this. I can see how using arrays for everything would make coding for combat, in-game docs, etc. much simpler, but at the same time make the creation of games substantially more difficult than with Civ4-style markups for separate units altogether. Or can it be rigged so you just cluster the values for units separately, so that it goes (not putting this in proper JS since I don't have the knack of that yet, or the time to think it through at present):

    var UnitNames [3]="Settler"
    var UnitStrength [3]=0

    etc. That seems like much less efficient coding, though, and if the designer uses that for every value I imagine it would make one monster script file. Not sure if that matters.
    1011 1100
    Read The Curse Of Life. Or click on the link to game Google or something if you're bored. Whatever.

  • #2
    You should read a little bit further. Grouping simple variables into arrays is done when you need very fast computation, JS isn't suited for that.

    What we will use is objects. For example, there will be a unitTemplate that will have attributes like movementType, defaultMaxHealth, allowedUpgrades etc. They will be loaded from a text file and stored in a hashtable. I won't bother you with technical details, but JS has only hashtables, which means we can do things like unitTemplates["settler"] instead of unitTemplates[1].

    We will also have units. A unit is an object that has a unitTemplate as one of his attributes, so it can reuse common attributes. It will have its own attributes like experiencePoints, upgrades[], currentHealth etc. All units will be stored in another hashtable or even several hashtables, for example, each player object will have its own units hashtable, so you could do things like players[1].units.push(unit.new(unitTemplates["warrior"], players[1], selectedCity.coords))

    Actually, this won't look this way exactly, because there should be a spawnUnit function that does all that, so you can't make a bookkeeping mistake and spawn a unit for player 1 and put it into player 2 units.
    Graffiti in a public toilet
    Do not require skill or wit
    Among the **** we all are poets
    Among the poets we are ****.

    Comment


    • #3
      Forgot all about the object stuff while I was wading through this basics book. Thanks!
      1011 1100
      Read The Curse Of Life. Or click on the link to game Google or something if you're bored. Whatever.

      Comment


      • #4
        I have created a Dwarf Recognition Program:

        HTML Code:
        <HTML>
        	<HEAD>
        		<TITLE>Choose your dwarf wisely!</TITLE>
        	</HEAD>
        	<BODY>
        		<SCRIPT>
        			var yourPoison = prompt ("Who's your favorite dwarf?", "Snow White");
        			var DwarfNames = ["Doc", "Dopey", "Bashful", "Grumpy", "Sleepy", "Sneezy", "Happy"];
        			var matchFound = false;
        			if (yourPoison == "Snow White"){
        				alert ("She's not a dwarf, you tool.");
        				}
        			else for (var i=0; i<=6; i++) {
        				if (yourPoison == DwarfNames&#91;i&#93;) {
        					alert ("Oh, I know that guy!");
        					matchFound = true;
        					break;
        					}
        				}
        			if (matchFound == false){
        				alert ("Huh?  Who's that?");
        				}
        		</SCRIPT>	
        	</BODY>
        </HTML>
        It's very nearly a clone of an example from the book, but it's something. I'm on my way. (apologies for the glitch at the LT sign, but CODE brackets didn't show it at all)

        EDIT: The wife found a bug in it almost instantly just by trying it. Probably you guys can just spot it in the code. Not going to bother to fix it, though. Hooray for locally-defined variables!
        Last edited by Elok; September 4, 2014, 11:44.
        1011 1100
        Read The Curse Of Life. Or click on the link to game Google or something if you're bored. Whatever.

        Comment


        • #5
          The "Huh? Who's that?" after "Snow White" even though there's no further input?
          "tout comprendre, c'est tout pardonner"

          Comment


          • #6
            Elok, can this script be rewritten without a loop? The answer is below, but please don't look before you try.

            Also, you should always use === instead of == in JS. == is an ugly legacy of the past.

            Spoiler:
            HTML Code:
            	<BODY>
            		<SCRIPT>
            			var DwarfNames = {"Doc":true, "Dopey":true, "Bashful":true, "Grumpy":true, "Sleepy":true, "Sneezy":true, "Happy":true};
            			var yourPoison = prompt ("Who's your favorite dwarf?", "Snow White");
            			var matchFound = false;
            			if (yourPoison === "Snow White") {
            				alert ("She's not a dwarf, you tool.");
            			}
            			else if (DwarfNames[yourPoison]) {
            				alert ("Oh, I know that guy!");
            				matchFound = true;
            			}
            			else {
            				alert ("Huh?  Who's that?");
            			}
            		</SCRIPT>	
            	</BODY>
            Graffiti in a public toilet
            Do not require skill or wit
            Among the **** we all are poets
            Among the poets we are ****.

            Comment


            • #7
              Originally posted by Aeson View Post
              The "Huh? Who's that?" after "Snow White" even though there's no further input?
              Yeah. Already fixed it in my code doodad, just couldn't be arsed to fix it--that wasn't clear from what I posted, I just now realized. Simple matter of integrating it into the loop.

              Onodera, do you mean by using a crapton of if-thens, as in 'if (yourPoison == "Doc"), else if (yourPoison === "Dopey")' etc., or something more elegant? I don't know of any such method--I imagine you could store all the names as one big text string and search the string, but I haven't learned how to do that yet. Not checking the spoiler in case you want to hint more heavily.

              Will some browsers not process == properly?
              1011 1100
              Read The Curse Of Life. Or click on the link to game Google or something if you're bored. Whatever.

              Comment


              • #8
                Originally posted by Elok View Post
                Yeah. Already fixed it in my code doodad, just couldn't be arsed to fix it--that wasn't clear from what I posted, I just now realized. Simple matter of integrating it into the loop.

                Onodera, do you mean by using a crapton of if-thens, as in 'if (yourPoison == "Doc"), else if (yourPoison === "Dopey")' etc., or something more elegant? I don't know of any such method--I imagine you could store all the names as one big text string and search the string, but I haven't learned how to do that yet. Not checking the spoiler in case you want to hint more heavily.

                Will some browsers not process == properly?
                No, I mean a more elegant solution. What if your dwarves had names like 1, 2 and 3? Would you need a loop?

                As for ==, it has broken semantics: if two values are kinda equal, it says they're equal. === makes sure they're really equal. They don't often return different results for two variables, but when they do, it's a nightmare to debug your program.
                Graffiti in a public toilet
                Do not require skill or wit
                Among the **** we all are poets
                Among the poets we are ****.

                Comment


                • #9
                  Oh, I thought you meant a different way of accomplishing an identical task--with number names, you just need multiple conditions for one If.
                  If (yourPoison GT 0 & youPoison LT 8) {, I think.
                  1011 1100
                  Read The Curse Of Life. Or click on the link to game Google or something if you're bored. Whatever.

                  Comment


                  • #10
                    Originally posted by Elok View Post
                    Oh, I thought you meant a different way of accomplishing an identical task--with number names, you just need multiple conditions for one If.
                    If (yourPoison GT 0 & youPoison LT 8) {, I think.
                    What if you don't know their numbers in advance? What if they are numbered 1,2,3,13,42,666,1337?
                    Graffiti in a public toilet
                    Do not require skill or wit
                    Among the **** we all are poets
                    Among the poets we are ****.

                    Comment


                    • #11
                      Could you elaborate? I don't understand what you're asking--how can the program recognize a data set it doesn't have?
                      1011 1100
                      Read The Curse Of Life. Or click on the link to game Google or something if you're bored. Whatever.

                      Comment


                      • #12
                        You can give a program any data set you want

                        It has to do with what he was talking about earlier in the thread:

                        http://apolyton.net/showthread.php/2...=1#post6356286
                        "tout comprendre, c'est tout pardonner"

                        Comment


                        • #13
                          Sorry I haven't been very active on this project yet. CivBE is looking too interesting so far... this was my project today:

                          connect_errno) { $output = "Connect failed: " . $mysqli->connect_error; } else { $query = "SELECT * FROM civbe_virtues ORDER BY id ASC"; if ($virtues = $mysqli->query($query)) { $query = "SELECT * FROM civbe_synergies ORDER BY id ASC"; if ($synergies = $mysqli->query($query)) { $output = apDisplayVirtuePicker($virtues,$synergies); $synergies->free(); } /* free result set */ $virtues->free(); } } $mysqli->close(); //file_put_contents($file, $output); } else $output = file_get_contents($file); return $output; function apDisplayVirtuePicker($virtues, $synergies) { $width = 1006; $height = 653; $treeWidth = 230; $tierHeight = 182; $firstX = 119; $firstY = 114; $firstTopSynergyX = 115; $firstTopSynergyY = 69; $firstSideSynergyX = 44; $firstSideSynergyY = 94; $synergyX = 75; $synergyY = 9; $normalX = 60; $normalY = 62; $virtueWidth = 45; $virtueHeight = 43; $synergyWidth = 24; $synergyHeight = 24; $firstTierCounterY = 105; $firstTierCounterX = 70; $tierCounterHeight = 9; $tierCounterWidth = 16; $tierHeight = 182; $tierWidth = 16; $tierGap = 3; $firstCatCounterX = 89; $firstCatCounterY = 95; $catCounterWidth = 15; $catCounterHeight = 11; $catWidth = 225; $catHeight = 11; $catGap = 5; $output = ' '; //$virtuesEnabled = array(); $virtuesLeadTo = array(); $virtuesPrereq = array(); $virtuesLookup = array(); $virtueOutput = ''; while ($virtue = $virtues->fetch_assoc()) { //$virtuesEnabled['virtue' . $virtue['id']] = $virtue['enabled']; $virtuesLeadTo['virtue' . $virtue['id']] = $virtue['enables']; $virtuesPrereq['virtue' . $virtue['id']] = $virtue['prereqs']; $virtuesLookup[$virtue['virtue']] = 'virtue' . $virtue['id']; $category = strtolower($virtue['cat']); if($category === 'might') $tree = 0; else if($category === 'prosperity') $tree = 1; else if($category === 'knowledge') $tree = 2; else if($category === 'industry') $tree = 3; if($virtue['tier'] === '3') $tierClass = ' tier3'; else if($virtue['tier'] === '2') $tierClass = ' tier2'; else if($virtue['tier'] === '1') $tierClass = ' tier1'; else $tierClass = ''; $treeOffset = $firstX + ($tree * $treeWidth); $tierOffset = $firstY + (($virtue['tier'] - 1) * $tierHeight); $xPos = $treeOffset + (($virtue['col'] - 1) * $normalX); $yPos = $tierOffset + (($virtue['row'] - ((($virtue['tier'] - 1) * 3)) - 1) * $normalY); //echo $virtue['virtue'] . ' yPos(' . $yPos . '): toff(' . $tierOffset . ') + (row(' . $virtue['row'] . ') - (((tier(' . $tierOffset . ' - 1) * 3)) - 1) * normalY(' . $normalY . ')
                          '; if($virtue['num'] === '1') $isEnabled = ' class="glowBorderEnabled"'; else $isEnabled = ''; $virtueOutput .= '
                          ' . $virtue['virtue'] . '

                          ' . $virtue['description'] . '
                          '; } $synergyOutput = ''; while ($synergy = $synergies->fetch_assoc()) { $type = strtolower($synergy['type']); if($type === 'tier') $num = $synergy['tier']; else $num = ''; $yPos = $firstTopSynergyY; if($type === 'tier') { $xPos = $firstSideSynergyX; $yPos = $firstSideSynergyY + (($synergy['tier'] - 1) * ($tierHeight + 2)) + (($synergy['num'] - ($synergy['virtues'] / 2)) * $synergyY); } else if ($type === 'might') { $xPos = $firstTopSynergyX + (($synergy['tier'] - 1) * $synergyX); } else if ($type === 'prosperity') { $xPos = $firstTopSynergyX + (($synergy['tier'] - 1) * $synergyX) + $treeWidth; } else if ($type === 'knowledge') { $xPos = $firstTopSynergyX + (($synergy['tier'] - 1) * $synergyX) + (2 * $treeWidth); } else if ($type === 'industry') { $xPos = $firstTopSynergyX + (($synergy['tier'] - 1) * $synergyX) + (3 * $treeWidth); } if($synergy['tier'] == 3) $tierClass = ' tier3'; else $tierClass = ''; $synergyOutput .= '
                          Synergy: ' . $synergy['name'] . '

                          ' . $synergy['kicker'] . '
                          '; } $output .= '
                          Culture Icon
                          Total:
                          0
                          Next:
                          20
                          '; $output .= $virtueOutput . $synergyOutput . '
                          '; return $output; } function apConvertLeadsTo($array,$lookup) { $newArray = array(); foreach($array as $key => $val) { if($val != '') { $virtues = explode(',',$val); $newArray[$key] = ''; $comma = ''; foreach($virtues as $virtue) { $newArray[$key] .= $comma . $lookup[$virtue]; $comma = ','; } } else { $newArray[$key] = ''; } } return apArrayToJsArray($newArray); } function apArrayToJsArray($array) { $output = '{'; $comma = ''; foreach($array as $key => $val) { $output .= $comma . "'" . $key . "': '" . $val . "'"; $comma = ', '; } $output .= '};'; return $output; } function apBaseUrl() { return 'http://cache.apolyton.net/'; //return 'http://127.0.0.1/apolyton.net/'; } ?>
                          "tout comprendre, c'est tout pardonner"

                          Comment


                          • #14
                            Originally posted by Elok View Post
                            Could you elaborate? I don't understand what you're asking--how can the program recognize a data set it doesn't have?
                            I am saying that there's a way to store the data when you don't have to check every item to find the one you need. For example, you could say dwarves[666] = true and then "if (dwarves[yourPoison])" would work for dwarf 666. But arrays can be indexed only with numbers, not strings, so you need something similar, but different.
                            Graffiti in a public toilet
                            Do not require skill or wit
                            Among the **** we all are poets
                            Among the poets we are ****.

                            Comment


                            • #15
                              Oh, so you're saying I can store them as lists of objects, or something? I haven't learned about objects yet; lots of kid-wrangling on my plate lately.

                              EDIT: No, I finally bit the bullet and checked your spoilered stuff. I get it. Didn't know you could do anything like that, though.
                              1011 1100
                              Read The Curse Of Life. Or click on the link to game Google or something if you're bored. Whatever.

                              Comment

                              Working...
                              X