Advertisement
toribio

toribio

May 17th, 2009
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.85 KB | None | 0 0
  1. #include <a_samp>
  2.  
  3. new TowTruckers = 0; // BeckzyBoi removed '= 0' here. However, adding '= 0' makes it more clear to the readers. This does not affect the way this statement is compiled.
  4. new bool:IsTowTrucker[MAX_PLAYERS]; // I added the 'bool' tag to make clear that this is a boolean variable, not a random integer.
  5.  
  6. public OnFilterScriptInit()
  7. {
  8.     print("\n TowCars Filter Script v1.0 Loading...\n**********************\n");
  9.     print(" By Zeruel_Angel and !damo!spiderman\n");
  10.     print("TowCars Filter Script fully Loaded\n**********************************\n\n");
  11.     return 1; // BeckzyBoi added this, because zeruel_angel forgot.
  12. }
  13.  
  14. public OnFilterScriptExit()
  15. {
  16.     print("\n TowCars Script UnLoaded\n********************************************\n\n");
  17.     return 1;
  18.     /* BeckzyBoi changed this into 'return print(...)'. However, the pdf does not specify what print returns, so it could be anything. That means it's totally unreliable. We do not want to return anything, we specifically want to return 1. */
  19. }
  20.  
  21. public OnPlayerStateChange(playerid, newstate, oldstate)
  22. {
  23.     if(newstate == PLAYER_STATE_DRIVER) { // Based on experience, I can tell you that using the '{' on same line as the 'if' improves readability significantly.
  24.         if(GetVehicleModel(GetPlayerVehicleID(playerid)) == 525) {
  25.             IsTowTrucker[playerid] = true; // Changed to 'true' instead of '1'. This makes clear to the readers that is a boolean variable.
  26.             TowTruckers++; // Keep the variable and '++' together. Adding a space may be confusing.
  27.             SendClientMessage(playerid, 0xFFFF00AA, "You can use the ACTION KEY to Tow cars");
  28.         }
  29.         else if(TowTruckers > 0) { // Always use {}s for if-statements, loops, etc.! Not using that makes your code look very sloppy and less readable. Furthermore, it will save you some time when adding multiple statements.
  30.             SendClientMessage(playerid, 0xFFFF00AA, "If you have a problem with your car, use /TowMe to call a TowTruker");
  31.         } // For 'else if' statements it's acceptable to omit the {}s after 'else'.
  32.     }
  33.     else if(newstate == PLAYER_STATE_ONFOOT && IsTowTrucker[playerid]) { // I changed this to 'else if'. It is unnecessairy to execute another if-statement if a previous one was satisfied. BeckzyBoi removed some ()s and '==1' to make the statement more readable.
  34.         IsTowTrucker[playerid] = false; // BeckzyBoi used multiple statements on one line to make sure that the braceless if-statement works. One statement per line makes the code more readable.
  35.         TowTruckers--;
  36.     }
  37.     else if(newstate == PLAYER_STATE_PASSENGER && TowTruckers > 0) {
  38.         SendClientMessage(playerid, 0xFFFF00AA, "If you have a problem with your car, use /TowMe to call a TowTruker");
  39.     }
  40.     return 1;
  41. }
  42.  
  43. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  44. {
  45.     new playerVid = GetPlayerVehicleID(playerid); // GetPlayerVehicleID(playerid) seems to be called a lot. I saved the value in a variable now, so that we do not have to call that function all the time. This makes the code execute a little faster.
  46.  
  47.     if ((newkeys & KEY_ACTION) && IsPlayerInAnyVehicle(playerid) && GetPlayerState(playerid) == PLAYER_STATE_DRIVER && GetVehicleModel(playerVid) == 525) { // Changed to newkeys & KEY_ACTION to make it work even if other keys are pressed, and merged two if-statements
  48.         SendClientMessage(playerid,0xFFFF00AA,"trying to tow a car");
  49.         new Float:pX,Float:pY,Float:pZ; // BeckzyBoi changed this into a 6-cell array. That makes the code in the if-statement below impossible to understand. Variables have names for a reason. Using an array here doesn't make the code compile more efficiently.
  50.         GetPlayerPos(playerid,pX,pY,pZ);
  51.         new bool:Found = false;
  52.  
  53.         for(new vid = 0; vid < MAX_VEHICLES; vid++) { // Using a for-loop instead of a while loop in this case makes it very clear to the reader what it does. It also makes your code very clean. I removed the '!Found' expression. It is unnecesairy. (See why later.)
  54.             new Float:vX,Float:vY,Float:vZ; // These variables have to be defined in this loop only. There are not needed anywhere else, and that's why I moved them here. In technical terms: I limited the scope of those variables.
  55.             GetVehiclePos(vid,vX,vY,vZ);
  56.  
  57.             if(floatabs(pX - vX) < 7.0 && floatabs(pY - vY) < 7.0 && floatabs(pZ - vZ) < 7.0 && vid != playerVid) { // Using variable names, it becomes very clear what this condition will do. The ()s removed by BeckzyBoi also make it much more readable.
  58.                 Found = true;
  59.                 if(IsTrailerAttachedToVehicle(playerVid)) {
  60.                     DetachTrailerFromVehicle(playerVid);
  61.                 }
  62.                 AttachTrailerToVehicle(vid,playerVid);
  63.                 SendClientMessage(playerid,0xFFFF00AA,"Car towed!");
  64.                 break; // Break! We can break out of a loop with this statement. That's why '!Found' is unnecesairy in the loop condition.
  65.             }
  66.         }
  67.         if (!Found) { // At this part, BeckzyBoi got confused by zeruel_angel's weird indentation. It shows perfectly that zeruel_angel's will confuse the reader ;). BeckzyBoi thought this if-statement was part of the loop, but it isn't. This comes AFTER the loop.
  68.             SendClientMessage(playerid, 0xFFFF00AA, "There is no car in range.");
  69.         }
  70.     }
  71.     return 1;
  72. }
  73.  
  74. public OnPlayerDisconnect(playerid)
  75. {
  76.     if(IsTowTrucker[playerid]) {
  77.         IsTowTrucker[playerid] = false; // Changed to 'false' instead of '1', changed multiple-statement issue again.
  78.         TowTruckers--;
  79.     }
  80.     return 1;
  81. }
  82.  
  83. public OnPlayerCommandText(playerid, cmdtext[])
  84. {
  85.     if (strcmp(cmdtext, "/TowMe", true) == 0) { // BeckzyBoi changed this to '!...', which is also correct, but confusing with non-boolean return values.
  86.         if (TowTruckers == 0) { // Same thing
  87.             SendClientMessage(playerid, 0xFFFF00AA, "Sorry there isn't any TowTrucker Available at the moment"); // BeckzyBoi used his 'quick-return' thing here again. Again, there is no specification on what this function returns. However, it will probably return 0, which proves my point. We need to return 1 here, to make sure the 'Unknown command' message doesn't show up.
  88.             return 1;
  89.         }
  90.         SendClientMessage(playerid, 0xFFFF00AA, "TowTruckers has been told of your situation, just wait.");
  91.         SendClientMessage(playerid, 0xFFFF00AA, "REMEMBER: Your car can't be towed if you are sitting as the driver");
  92.         new pName[MAX_PLAYER_NAME], msg[60];
  93.         GetPlayerName(playerid, pName, sizeof(pName));
  94.         format(msg, sizeof(msg), "*** %s (id:%d) Need to be Towed ***", pName, playerid);
  95.         for (new i = 0; i < MAX_PLAYERS; i++) {
  96.             if (IsTowTrucker[i]) {
  97.                 SendClientMessage(i, 0xFFFF00AA, msg);
  98.             }
  99.         }
  100.         return 1;
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement